示例#1
0
func main() {
	h = hge.Create(VERSION)
	defer h.Release()

	// Set desired system states and initialize HGE
	h.System_SetState(LOGFILE, "tutorial07.log")
	h.System_SetState(FRAMEFUNC, frame)
	h.System_SetState(RENDERFUNC, render)
	h.System_SetState(TITLE, "HGE Tutorial 07 - Thousand of Hares")
	h.System_SetState(USESOUND, false)
	h.System_SetState(WINDOWED, true)
	h.System_SetState(SCREENWIDTH, SCREEN_WIDTH)
	h.System_SetState(SCREENHEIGHT, SCREEN_HEIGHT)
	h.System_SetState(SCREENBPP, 32)

	if h.System_Initiate() {
		defer h.System_Shutdown()
		// Load textures
		bgtex = h.Texture_Load("bg2.png")
		tex = h.Texture_Load("zazaka.png")
		if bgtex == nil || tex == nil {
			fmt.Println("Error: Can't load bg2.png or zazaka.png\n")
			return
		}
		// Delete created objects and free loaded resources
		defer h.Texture_Free(tex)
		defer h.Texture_Free(bgtex)

		// Load font, create sprites
		fnt = font.New("font1.fnt")
		spr = sprite.New(tex, 0, 0, 64, 64)
		spr.SetHotSpot(32, 32)

		bgspr = sprite.New(bgtex, 0, 0, 800, 600)
		bgspr.SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE)
		bgspr.SetColor(0xFF000000, 0)
		bgspr.SetColor(0xFF000000, 1)
		bgspr.SetColor(0xFF000040, 2)
		bgspr.SetColor(0xFF000040, 3)

		// Initialize objects list
		objects = 1000

		for i := 0; i < MAX_OBJECTS; i++ {
			objs[i].x = h.Random_Float(0, SCREEN_WIDTH)
			objs[i].y = h.Random_Float(0, SCREEN_HEIGHT)
			objs[i].dx = h.Random_Float(-200, 200)
			objs[i].dy = h.Random_Float(-200, 200)
			objs[i].scale = h.Random_Float(0.5, 2.0)
			objs[i].dscale = h.Random_Float(-1.0, 1.0)
			objs[i].rot = h.Random_Float(0, Pi*2)
			objs[i].drot = h.Random_Float(-1.0, 1.0)
		}

		setBlend(0)

		// Let's rock now!
		h.System_Start()
	}
}
示例#2
0
func NewGUIButton(id int, x, y, w, h float64, tex *gfx.Texture, tx, ty float64) *GUIButton {
	b := new(GUIButton)

	b.GUIObject.Initialize()

	b.GUIObject.Id = id
	b.GUIObject.Visible = true
	b.GUIObject.Enabled = true
	b.GUIObject.Rect.Set(x, y, x+w, y+h)

	b.up = sprite.New(tex, tx, ty, w, h)
	b.down = sprite.New(tex, tx+w, ty, w, h)

	b.GUIObject.Render = func() {
		if b.pressed {
			b.down.Render(b.GUIObject.Rect.X1, b.GUIObject.Rect.Y1)
		} else {
			b.up.Render(b.GUIObject.Rect.X1, b.GUIObject.Rect.Y1)
		}
	}

	b.GUIObject.MouseLButton = func(down bool) bool {
		if down {
			b.oldState = b.pressed
			b.pressed = true
			return false
		}

		if b.trigger {
			b.pressed = !b.oldState
		} else {
			b.pressed = false
		}

		return true
	}

	return b
}
示例#3
0
func New(tex *gfx.Texture, frames int, fps, x, y, w, h float64) Animation {
	var a Animation

	a.Sprite = sprite.New(tex, x, y, w, h)

	a.origWidth = tex.Width(true)

	a.sinceLastFrame = -1.0
	a.speed = 1.0 / fps
	a.frames = frames

	a.mode = FWD | LOOP
	a.delta = 1
	a.SetFrame(0)

	return a
}
示例#4
0
func InitSimulation() bool {
	// Load texture
	texObjects = LoadTexture("objects.png")
	if texObjects == nil {
		return false
	}

	rand = New(0)

	// Create sprites
	sky = sprite.New(nil, 0, 0, SCREEN_WIDTH, SKY_HEIGHT)
	sea = dist.New(SEA_SUBDIVISION, SEA_SUBDIVISION)
	sea.SetTextureRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-SKY_HEIGHT)

	sun = sprite.New(texObjects, 81, 0, 114, 114)
	sun.SetHotSpot(57, 57)
	moon = sprite.New(texObjects, 0, 0, 81, 81)
	moon.SetHotSpot(40, 40)
	star = sprite.New(texObjects, 72, 81, 9, 9)
	star.SetHotSpot(5, 5)

	glow = sprite.New(texObjects, 128, 128, 128, 128)
	glow.SetHotSpot(64, 64)
	glow.SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE)
	seaglow = sprite.New(texObjects, 128, 224, 128, 32)
	seaglow.SetHotSpot(64, 0)
	seaglow.SetBlendMode(BLEND_COLORADD | BLEND_ALPHAADD | BLEND_NOZWRITE)

	// Initialize simulation state
	colWhite.SetHWColor(0xFFFFFFFF)
	timet = GetTime()
	speed = 0.0

	for i := 0; i < NUM_STARS; i++ { // star positions
		starX[i] = rand.Float64(0, SCREEN_WIDTH)
		starY[i] = rand.Float64(0, STARS_HEIGHT)
		starS[i] = rand.Float64(0.1, 0.7)
	}

	for i := 0; i < SEA_SUBDIVISION; i++ { // sea waves phase shifts
		seaP[i] = float64(i) + rand.Float64(-15.0, 15.0)
	}

	// Systems are ready now!
	return true
}
示例#5
0
func main() {
	h := hge.New()
	h.SetState(hge.LOGFILE, "tutorial06.log")
	h.SetState(hge.FRAMEFUNC, frame)
	h.SetState(hge.RENDERFUNC, render)
	h.SetState(hge.TITLE, "HGE Tutorial 06 - Creating menus")
	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 {
		fmt.Println("Error: ", h.GetErrorMessage())
	} else {
		defer h.Shutdown()

		quad.Texture = LoadTexture("bg.png")

		if quad.Texture == nil {
			fmt.Println("Error loading bg.png")
			return
		}

		snd := NewEffect("menu.ogg")

		if snd == nil {
			fmt.Println("Error loading menu.ogg")
			return
		}

		cursorTex := LoadTexture("cursor.png")

		if cursorTex == nil {
			fmt.Println("Error loading cursor.png")
			return
		}

		// Set up the quad we will use for background animation
		quad.Blend = BLEND_ALPHABLEND | BLEND_COLORMUL | BLEND_NOZWRITE

		for i := 0; i < 4; i++ {
			// Set up z-coordinate of vertices
			quad.V[i].Z = 0.5
			// Set up color. The format of DWORD col is 0xAARRGGBB
			quad.V[i].Color = 0xFFFFFFFF
		}

		quad.V[0].X, quad.V[0].Y = 0, 0
		quad.V[1].X, quad.V[1].Y = 800, 0
		quad.V[2].X, quad.V[2].Y = 800, 600
		quad.V[3].X, quad.V[3].Y = 0, 600

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

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

		cursor := sprite.New(cursorTex, 0, 0, 32, 32)
		GUI = gui.New()

		GUI.AddCtrl(NewGUIMenuItem(1, fnt, snd, 400, 200, 0.0, "Play"))
		GUI.AddCtrl(NewGUIMenuItem(2, fnt, snd, 400, 240, 0.1, "Options"))
		GUI.AddCtrl(NewGUIMenuItem(3, fnt, snd, 400, 280, 0.2, "Instructions"))
		GUI.AddCtrl(NewGUIMenuItem(4, fnt, snd, 400, 320, 0.3, "Credits"))
		GUI.AddCtrl(NewGUIMenuItem(5, fnt, snd, 400, 360, 0.4, "Exit"))

		GUI.SetNavMode(gui.GUI_UPDOWN | gui.GUI_CYCLED)
		GUI.SetCursor(&cursor)
		GUI.SetFocus(1)
		GUI.Enter()

		h.Start()
	}

}
示例#6
0
func New(filename string, arg ...interface{}) *Font {
	mipmap := false

	if len(arg) == 1 {
		if m, ok := arg[0].(bool); ok {
			mipmap = m
		}
	}

	f := new(Font)

	h := hge.New()

	f.scale, f.proportion = 1.0, 1.0
	f.spacing = 1.0

	f.z = 0.5
	f.blend = gfx.BLEND_COLORMUL | gfx.BLEND_ALPHABLEND | gfx.BLEND_NOZWRITE
	f.color = 0xFFFFFFFF

	desc := resource.LoadString(filename)

	if desc == nil {
		h.Log("Font %s seems to be empty.", filename)
		return nil
	}

	lines := getLines(*desc)

	if len(lines) == 0 || lines[0] != fntHEADERTAG {
		h.Log("Font %s has incorrect format.", filename)
		return nil
	}

	// parse the font description
	for _, line := range lines {
		if line == fntHEADERTAG {
			continue
		}

		option, value, err := tokenizeLine(line)

		if err != nil || len(line) == 0 || len(option) == 0 || len(value) == 0 {
			h.Log("Unreadable line (%s) in font file: %s", line, filename)
			continue
		}

		if option == fntBITMAPTAG {
			f.texture = gfx.LoadTexture(value, 0, mipmap)
		} else if option == fntCHARTAG {
			chr, x, y, w, h, a, c := tokenizeChar(value)

			sprt := sprite.New(f.texture, x, y, w, h)

			f.letters[chr] = &sprt
			f.pre[chr] = a
			f.post[chr] = c
			f.height = h
		}
	}

	return f
}
示例#7
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()
	}
}
示例#8
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()
	}
}
示例#9
0
func NewGUIListBox(id int, x, y, w, h float64, fnt *font.Font, color, highlightColor, spriteHighlightColor hge.Dword) *GUIListBox {
	l := new(GUIListBox)

	l.GUIObject.Initialize()

	l.GUIObject.Id = id
	l.GUIObject.Visible = true
	l.GUIObject.Enabled = true
	l.GUIObject.Rect.Set(x, y, x+w, y+h)
	l.Font = fnt
	l.highlight = sprite.New(nil, 0, 0, w, fnt.GetHeight())
	l.highlight.SetColor(highlightColor)
	l.color = color
	l.highlightColor = highlightColor
	l.List = list.New()

	l.GUIObject.Render = func() {
		item := l.List.Front()

		for i := 0; i < l.topItem; i++ {
			if item == nil {
				return
			}

			item = item.Next()
		}

		for i := 0; i < l.NumRows(); i++ {
			if i >= l.items {
				return
			}
			if l.topItem+i == l.selectedItem {
				l.highlight.Render(l.GUIObject.Rect.X1, l.GUIObject.Rect.Y1+float64(i)*l.Font.GetHeight())
				l.Font.SetColor(l.highlightColor)
			} else {
				l.Font.SetColor(l.color)
			}

			l.Font.Render(l.GUIObject.Rect.X1+3, l.GUIObject.Rect.Y1+float64(i)*l.Font.GetHeight(), font.TEXT_LEFT, item.Value.(*guiListboxItem).text)

			item = item.Next()
		}
	}

	l.GUIObject.MouseMove = func(x, y float64) bool {
		l.mx, l.my = x, y
		return false
	}

	l.GUIObject.MouseLButton = func(down bool) bool {
		if down {
			item := l.topItem + int(l.my)/int(l.Font.GetHeight())
			if item < l.Len() {
				l.selectedItem = item
				return true
			}
		}
		return false
	}

	l.GUIObject.MouseWheel = func(notches int) bool {
		l.topItem -= notches
		if l.topItem < 0 {
			l.topItem = 0
		}
		if l.topItem > l.Len()-l.NumRows() {
			l.topItem = l.Len() - l.NumRows()
		}

		return true
	}

	l.GUIObject.KeyClick = func(key input.Key, chr int) bool {
		switch key {
		case input.K_DOWN:
			if l.selectedItem < l.Len()-1 {
				l.selectedItem++
				if l.selectedItem > l.topItem+l.NumRows()-1 {
					l.topItem = l.selectedItem - l.NumRows() + 1
				}
				return true
			}

		case input.K_UP:
			if l.selectedItem > 0 {
				l.selectedItem--
				if l.selectedItem < l.topItem {
					l.topItem = l.selectedItem
				}
				return true
			}

		}
		return false
	}

	return l
}
示例#10
0
func NewGUISlider(id int, x, y, w, h float64, tex *gfx.Texture, tx, ty, sw, sh float64, a ...interface{}) *GUISlider {
	s := new(GUISlider)

	if len(a) == 1 {
		if b, ok := a[0].(bool); ok {
			s.vertical = b
		}
	}

	s.GUIObject.Initialize()

	s.GUIObject.Id = id
	s.GUIObject.Visible = true
	s.GUIObject.Enabled = true
	s.GUIObject.Rect.Set(x, y, x+w, y+h)

	s.mode = SLIDER_BAR
	s.max, s.val = 100, 50
	s.sl_w, s.sl_h = sw, sh

	s.Sprite = sprite.New(tex, tx, ty, sw, sh)

	s.GUIObject.Render = func() {
		var x1, y1, x2, y2 float64

		xx := s.GUIObject.Rect.X1 + (s.GUIObject.Rect.X2-s.GUIObject.Rect.X1)*(s.val-s.min)/(s.max-s.min)
		yy := s.GUIObject.Rect.Y1 + (s.GUIObject.Rect.Y2-s.GUIObject.Rect.Y1)*(s.val-s.min)/(s.max-s.min)

		if s.vertical {
			switch s.mode {
			case SLIDER_BAR:
				x1 = s.GUIObject.Rect.X1
				y1 = s.GUIObject.Rect.Y1
				x2 = s.GUIObject.Rect.X2
				y2 = y

			case SLIDER_BARRELATIVE:
				x1 = s.GUIObject.Rect.X1
				y1 = (s.GUIObject.Rect.Y1 + s.GUIObject.Rect.Y2) / 2
				x2 = s.GUIObject.Rect.X2
				y2 = y

			case SLIDER_SLIDER:
				x1 = (s.GUIObject.Rect.X1 + s.GUIObject.Rect.X2 - s.sl_w) / 2
				y1 = yy - s.sl_h/2
				x2 = (s.GUIObject.Rect.X1 + s.GUIObject.Rect.X2 + s.sl_w) / 2
				y2 = yy + s.sl_h/2

			}
		} else {
			switch s.mode {
			case SLIDER_BAR:
				x1 = s.GUIObject.Rect.X1
				y1 = s.GUIObject.Rect.Y1
				x2 = xx
				y2 = s.GUIObject.Rect.Y2

			case SLIDER_BARRELATIVE:
				x1 = (s.GUIObject.Rect.X1 + s.GUIObject.Rect.X2) / 2
				y1 = s.GUIObject.Rect.Y1
				x2 = xx
				y2 = s.GUIObject.Rect.Y2

			case SLIDER_SLIDER:
				x1 = xx - s.sl_w/2
				y1 = (s.GUIObject.Rect.Y1 + s.GUIObject.Rect.Y2 - s.sl_h) / 2
				x2 = xx + s.sl_w/2
				y2 = (s.GUIObject.Rect.Y1 + s.GUIObject.Rect.Y2 + s.sl_h) / 2
			}
		}

		s.Sprite.RenderStretch(x1, y1, x2, y2)
	}

	s.GUIObject.MouseMove = func(x, y float64) bool {
		if s.pressed {
			if s.vertical {
				if y > s.GUIObject.Rect.Y2-s.GUIObject.Rect.Y1 {
					y = s.GUIObject.Rect.Y2 - s.GUIObject.Rect.Y1
				}
				if y < 0 {
					y = 0
				}
				s.val = s.min + (s.max-s.min)*y/(s.GUIObject.Rect.Y2-s.GUIObject.Rect.Y1)
			} else {
				if x > s.GUIObject.Rect.X2-s.GUIObject.Rect.X1 {
					x = s.GUIObject.Rect.X2 - s.GUIObject.Rect.X1
				}
				if x < 0 {
					x = 0
				}
				s.val = s.min + (s.max-s.min)*x/(s.GUIObject.Rect.X2-s.GUIObject.Rect.X1)
			}
			return true
		}

		return false
	}

	s.GUIObject.MouseLButton = func(down bool) bool {
		s.pressed = down
		return false
	}

	return s
}