Exemple #1
0
// An example of using closures
func main() {
	func() {
		const (
			speed    = 90.0
			friction = 0.98
		)

		var (
			spr sprite.Sprite
			fnt *font.Font
			par *particle.ParticleSystem

			snd *Effect

			x  = 100.0
			y  = 100.0
			dx = 0.0
			dy = 0.0
		)

		hge := New()

		hge.SetState(LOGFILE, "tutorial03.log")
		hge.SetState(TITLE, "HGE Tutorial 03 - Using helper classes")
		hge.SetState(FPS, 100)
		hge.SetState(WINDOWED, true)
		hge.SetState(SCREENWIDTH, 800)
		hge.SetState(SCREENHEIGHT, 600)
		hge.SetState(SCREENBPP, 32)
		hge.SetState(FRAMEFUNC, func() int {
			dt := float64(Delta())

			boom := func() {
				pan := int((x - 400) / 4)
				pitch := (dx*dx+dy*dy)*0.0005 + 0.2
				snd.PlayEx(100, pan, pitch)
			}

			// Process keys
			if NewKey(K_ESCAPE).State() {
				return 1
			}
			if NewKey(K_LEFT).State() {
				dx -= speed * dt
			}
			if NewKey(K_RIGHT).State() {
				dx += speed * dt
			}
			if NewKey(K_UP).State() {
				dy -= speed * dt
			}
			if NewKey(K_DOWN).State() {
				dy += speed * dt
			}

			// Do some movement calculations and collision detection
			dx *= friction
			dy *= friction
			x += dx
			y += dy
			if x > 784 {
				x = 784 - (x - 784)
				dx = -dx
				boom()
			}
			if x < 16 {
				x = 16 + 16 - x
				dx = -dx
				boom()
			}
			if y > 584 {
				y = 584 - (y - 584)
				dy = -dy
				boom()
			}
			if y < 16 {
				y = 16 + 16 - y
				dy = -dy
				boom()
			}

			// Update particle system
			par.Info.Emission = (int)(dx*dx+dy*dy) * 2
			par.MoveTo(x, y)
			par.Update(dt)

			return 0
		})

		hge.SetState(RENDERFUNC, func() int {
			BeginScene()
			Clear(0)
			par.Render()
			spr.Render(x, y)
			fnt.Printf(5, 5, font.TEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", Delta(), GetFPS())
			EndScene()

			return 0
		})

		if err := hge.Initiate(); err == nil {
			defer hge.Shutdown()

			snd = NewEffect("menu.ogg")
			tex := LoadTexture("particles.png")
			if snd == nil || tex == nil {
				fmt.Printf("Error: Can't load one of the following files:\nmenu.ogg, particles.png, font1.fnt, font1.png, trail.psi\n")
				return
			}

			spr = sprite.New(tex, 96, 64, 32, 32)
			spr.SetColor(0xFFFFA000)
			spr.SetHotSpot(16, 16)

			if fnt = font.New("font1.fnt"); fnt == nil {
				fmt.Println("Error loading font1.fnt")
				return
			}

			spt := sprite.New(tex, 32, 32, 32, 32)
			spt.SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE)
			spt.SetHotSpot(16, 16)

			par = particle.New("trail.psi", spt)
			if par == nil {
				fmt.Println("Error loading trail.psi")
				return
			}
			par.Fire()

			hge.Start()
		}
	}()

	for i := 0; i < 10; i++ {
		fmt.Println("Sleeping")
		time.Sleep(1 * time.Second)
		runtime.GC()
	}
}
Exemple #2
0
func main() {
	h := hge.New()

	h.SetState(hge.LOGFILE, "tutorial04.log")
	h.SetState(hge.FRAMEFUNC, FrameFunc)
	h.SetState(hge.RENDERFUNC, RenderFunc)
	h.SetState(hge.GFXRESTOREFUNC, RestoreFunc)
	h.SetState(hge.TITLE, "HGE Tutorial 04 - Using render targets")
	h.SetState(hge.FPS, 100)
	h.SetState(hge.WINDOWED, true)
	h.SetState(hge.SCREENWIDTH, 800)
	h.SetState(hge.SCREENHEIGHT, 600)
	h.SetState(hge.SCREENBPP, 32)

	if err := h.Initiate(); err == nil {
		defer h.Shutdown()
		snd = NewEffect("menu.ogg")
		tex = LoadTexture("particles.png")
		if snd == nil || tex == nil {
			// If one of the data files is not found, display
			// an error message and shutdown.
			fmt.Printf("Error: Can't load one of the following files:\nmenu.ogg, particles.png, font1.fnt, font1.png, trail.psi\n")
			return
		}

		// Delete created objects and free loaded resources
		defer snd.Free()
		defer tex.Free()

		spr = sprite.New(tex, 96, 64, 32, 32)
		spr.SetColor(0xFFFFA000)
		spr.SetHotSpot(16, 16)

		fnt = font.New("font1.fnt")

		if fnt == nil {
			fmt.Println("Error: Can't load font1.fnt or font1.png")
			return
		}

		spt = sprite.New(tex, 32, 32, 32, 32)
		spt.SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE)
		spt.SetHotSpot(16, 16)
		par = particle.New("trail.psi", spt)

		if par == nil {
			fmt.Println("Error: Cannot load trail.psi")
			return
		}
		par.Fire()

		// Create a render target and a sprite for it
		target = NewTarget(512, 512, false)
		defer target.Free()
		tar = sprite.New(target.Texture(), 0, 0, 512, 512)
		tar.SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE)

		// Let's rock now!
		h.Start()
	}
}