Ejemplo n.º 1
0
func MouseRepeat() {
	var x, y int
	mouseState := sdl.GetMouseState(&x, &y)

	if mouseState&MOUSE_BTN_LEFT == MOUSE_BTN_LEFT {
		HandleMouseButton(&sdl.MouseButtonEvent{Type: sdl.MOUSEBUTTONDOWN, Button: sdl.BUTTON_LEFT, State: 1, X: uint16(x), Y: uint16(y)})
	}
	if mouseState&MOUSE_BTN_RIGHT == MOUSE_BTN_RIGHT {
		HandleMouseButton(&sdl.MouseButtonEvent{Type: sdl.MOUSEBUTTONDOWN, Button: sdl.BUTTON_RIGHT, State: 1, X: uint16(x), Y: uint16(y)})

	}

}
Ejemplo n.º 2
0
func (win *window) eventLoop() {
	if win.ec == nil {
		win.ec = make(chan interface{})
	}

eloop:
	for win.events {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.KeyboardEvent:
				switch e.Type {
				case sdl.KEYUP:
					win.ec <- gui.KeyEvent{int(-e.Keysym.Sym)}
				case sdl.KEYDOWN:
					win.ec <- gui.KeyEvent{int(e.Keysym.Sym)}
				}
			case *sdl.MouseMotionEvent:
				win.ec <- gui.MouseEvent{
					Buttons: int(e.State),
					Loc:     image.Pt(int(e.X), int(e.Y)),
					Nsec:    time.Nanoseconds(),
				}
			case *sdl.MouseButtonEvent:
				win.ec <- gui.MouseEvent{
					Buttons: int(sdl.GetMouseState(nil, nil)),
					Loc:     image.Pt(int(e.X), int(e.Y)),
					Nsec:    time.Nanoseconds(),
				}
			case *sdl.ResizeEvent:
				win.ec <- gui.ConfigEvent{image.Config{
					win.Screen().ColorModel(),
					int(e.W),
					int(e.H),
				}}
			case *sdl.QuitEvent:
				break eloop
			}
		}
	}

	close(win.ec)
}
Ejemplo n.º 3
0
func main() {
	runtime.LockOSThread()
	flag.Parse()
	buildPalette()
	sdl.Init(sdl.INIT_VIDEO)
	defer sdl.Quit()

	sdl.GL_SetAttribute(sdl.GL_SWAP_CONTROL, 1)

	if sdl.SetVideoMode(512, 512, 32, sdl.OPENGL) == nil {
		panic("sdl error")
	}

	if gl.Init() != 0 {
		panic("gl error")
	}

	sdl.WM_SetCaption("Gomandel", "Gomandel")

	gl.Enable(gl.TEXTURE_2D)
	gl.Viewport(0, 0, 512, 512)
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(0, 512, 512, 0, -1, 1)

	gl.ClearColor(0, 0, 0, 0)

	//-----------------------------------------------------------------------------
	var dndDragging bool = false
	var dndStart Point
	var dndEnd Point
	var tex gl.Texture
	var tc TexCoords
	var lastProgress int
	initialRect := Rect{-1.5, -1.5, 3, 3}
	rect := initialRect

	rc := new(MandelbrotRequest)
	rc.MakeRequest(512, 512, rect)
	rc.WaitFor(Small, &tex, &tc)

	running := true
	for running {
		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.QuitEvent:
				running = false
			case *sdl.MouseButtonEvent:
				mbe := e.(*sdl.MouseButtonEvent)
				if mbe.Type == sdl.MOUSEBUTTONDOWN {
					dndDragging = true
					sdl.GetMouseState(&dndStart.X, &dndStart.Y)
					dndEnd = dndStart
				} else {
					dndDragging = false
					sdl.GetMouseState(&dndEnd.X, &dndEnd.Y)
					if mbe.Which == 3 {
						rect = initialRect
					} else {
						rect = rectFromSelection(dndStart, dndEnd, 512, 512, rect)
						tc = texCoordsFromSelection(dndStart, dndEnd, 512, 512, tc)
					}

					// make request
					rc.MakeRequest(512, 512, rect)
				}
			case *sdl.MouseMotionEvent:
				if dndDragging {
					sdl.GetMouseState(&dndEnd.X, &dndEnd.Y)
				}
			}
		}

		// if we're waiting for a result, check if it's ready
		p := rc.Update(&tex, &tc)
		if p != -1 {
			lastProgress = p
		}

		gl.Clear(gl.COLOR_BUFFER_BIT)
		tex.Bind(gl.TEXTURE_2D)
		drawQuad(0, 0, 512, 512, tc.TX, tc.TY, tc.TX2, tc.TY2)
		tex.Unbind(gl.TEXTURE_2D)
		if dndDragging {
			drawSelection(dndStart, dndEnd)
		}
		drawProgress(512, 512, lastProgress, rc.Pending)
		sdl.GL_SwapBuffers()
	}
}
Ejemplo n.º 4
0
func (self *Inventory) Draw(t int64) {
	gl.MatrixMode(gl.MODELVIEW)
	gl.PushMatrix()
	gl.LoadIdentity()

	gl.Disable(gl.DEPTH_TEST)
	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.LIGHT0)
	gl.Disable(gl.LIGHT1)

	gl.Color4ub(0, 0, 0, 208)

	gl.Begin(gl.QUADS)
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.tplane))
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.tplane))
	gl.End()

	picker.DrawItemHighlight(t, 3)
	picker.DrawItemHighlight(t, 4)
	picker.DrawItemHighlight(t, 5)
	picker.DrawItemHighlight(t, 6)
	picker.DrawItemHighlight(t, 7)
	picker.DrawPlayerItems(t)

	const blocksize = float64(0.3)
	const COLSIZE = 12

	diam := blocksize * 2.4

	offset := diam + float64(4)*PIXEL_SCALE

	for i := range self.inventoryRects {
		x := float64(viewport.lplane) + float64(10)*PIXEL_SCALE + float64(i/COLSIZE)*offset
		y := float64(viewport.tplane) - float64(10)*PIXEL_SCALE - float64(i%COLSIZE)*offset
		self.inventoryRects[i] = Rect{x, y - diam, diam, diam}
		self.DrawItemSlot(t, self.inventoryRects[i])
	}

	slot := 0
	for i := 1; i < len(ThePlayer.inventory); i++ {
		if ThePlayer.inventory[i] > 0 {
			self.inventorySlots[slot] = uint16(i)
			self.DrawItem(t, ThePlayer.inventory[i], uint16(i), self.inventoryRects[slot])
			slot++
		}
	}

	for i := range self.componentSlots {
		x := float64(viewport.lplane) + offset*float64(2+len(self.inventoryRects)/COLSIZE) + float64(i)*offset
		y := float64(viewport.tplane) - (10.0 * PIXEL_SCALE)
		self.componentRects[i] = Rect{x, y - diam, diam, diam}

		self.DrawItemSlot(t, self.componentRects[i])
	}

	for i, cs := range self.componentSlots {
		if cs != 0 {
			self.DrawItem(t, ThePlayer.inventory[cs], cs, self.componentRects[i])
		}
	}

	for i := range self.productSlots {
		x := float64(viewport.lplane) + offset*float64(2+len(self.inventoryRects)/COLSIZE) + offset*float64(i%len(self.componentRects))
		y := float64(viewport.tplane) - (10.0 * PIXEL_SCALE) - offset*float64(2+float64(i/len(self.componentRects)))
		self.productRects[i] = Rect{x, y - diam, diam, diam}

		self.DrawItemSlot(t, self.productRects[i])
	}

	for i, ps := range self.productSlots {
		if ps != nil {
			self.DrawItem(t, ps.product.quantity, ps.product.item, self.productRects[i])
		}
	}

	if self.currentContainer != nil {

		for i := range self.containerRects {
			x := float64(viewport.lplane) + offset*float64(2+len(self.inventoryRects)/COLSIZE) + float64(i)*offset
			y := float64(viewport.tplane) - (10.0 * PIXEL_SCALE) - offset*float64(2+float64(len(self.productRects)/len(self.componentRects))) - offset*float64(2+float64(i/len(self.componentRects)))
			self.containerRects[i] = Rect{x, y - diam, diam, diam}

			self.DrawItemSlot(t, self.containerRects[i])
		}

		for i := uint16(0); i < self.currentContainer.Slots(); i++ {
			item := self.currentContainer.Item(i)
			if item != nil {
				self.DrawItem(t, item.quantity, item.item, self.containerRects[i])
			}
		}

		gl.PushMatrix()
		gl.LoadIdentity()
		gl.Translated(self.containerRects[0].x, self.containerRects[0].y+diam, 0)
		inventoryItemFont.Print(self.currentContainer.Label())
		gl.PopMatrix()

	}

	var mousex, mousey int
	mousestate := sdl.GetMouseState(&mousex, &mousey)
	self.HandleMouse(mousex, mousey, mousestate)

	gl.PopMatrix()
}
Ejemplo n.º 5
0
func main() {

	if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
		panic(sdl.GetError())
	}

	if ttf.Init() != 0 {
		panic(sdl.GetError())
	}

	if mixer.OpenAudio(mixer.DEFAULT_FREQUENCY, mixer.DEFAULT_FORMAT,
		mixer.DEFAULT_CHANNELS, 4096) != 0 {
		panic(sdl.GetError())
	}

	var screen = sdl.SetVideoMode(640, 480, 32, sdl.RESIZABLE)

	if screen == nil {
		panic(sdl.GetError())
	}

	var video_info = sdl.GetVideoInfo()

	println("HW_available = ", video_info.HW_available)
	println("WM_available = ", video_info.WM_available)
	println("Video_mem = ", video_info.Video_mem, "kb")

	sdl.EnableUNICODE(1)

	sdl.WM_SetCaption("Go-SDL SDL Test", "")

	image := sdl.Load("test.png")

	if image == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetIcon(image, nil)

	running := true

	font := ttf.OpenFont("Fontin Sans.otf", 72)

	if font == nil {
		panic(sdl.GetError())
	}

	font.SetStyle(ttf.STYLE_UNDERLINE)
	white := sdl.Color{255, 255, 255, 0}
	text := ttf.RenderText_Blended(font, "Test (with music)", white)
	music := mixer.LoadMUS("test.ogg")
	sound := mixer.LoadWAV("sound.ogg")

	if music == nil || sound == nil {
		panic(sdl.GetError())
	}

	music.PlayMusic(-1)

	if sdl.GetKeyName(270) != "[+]" {
		panic("GetKeyName broken")
	}

	worm_in := make(chan Point)
	draw := make(chan Point, 64)

	var out chan Point
	var in chan Point

	out = worm_in

	in = out
	out = make(chan Point)
	go worm(in, out, draw)

	for running {
		for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() {
			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyboardEvent:
				println("")
				println(e.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(e.Keysym.Sym)))

				if e.Keysym.Sym == 27 {
					running = false
				}

				fmt.Printf("%04x ", e.Type)

				for i := 0; i < len(e.Pad0); i++ {
					fmt.Printf("%02x ", e.Pad0[i])
				}
				println()

				fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", e.Type, e.Which, e.State, e.Pad0[0])
				fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", e.Keysym.Scancode, e.Keysym.Sym, e.Keysym.Mod, e.Keysym.Unicode)
			case *sdl.MouseButtonEvent:
				if e.Type == sdl.MOUSEBUTTONDOWN {
					println("Click:", e.X, e.Y)
					in = out
					out = make(chan Point)
					go worm(in, out, draw)
					sound.PlayChannel(-1, 0)
				}
			case *sdl.ResizeEvent:
				println("resize screen ", e.W, e.H)

				screen = sdl.SetVideoMode(int(e.W), int(e.H), 32, sdl.RESIZABLE)

				if screen == nil {
					panic(sdl.GetError())
				}
			}
		}

		screen.FillRect(nil, 0x302019)
		screen.Blit(&sdl.Rect{0, 0, 0, 0}, text, nil)

		loop := true

		for loop {

			select {
			case p := <-draw:
				screen.Blit(&sdl.Rect{int16(p.x), int16(p.y), 0, 0}, image, nil)
			case <-out:
			default:
				loop = false
			}

		}

		var p Point
		sdl.GetMouseState(&p.x, &p.y)
		worm_in <- p

		screen.Flip()
		sdl.Delay(25)
	}

	image.Free()
	music.Free()
	font.Close()

	ttf.Quit()
	sdl.Quit()
}
Ejemplo n.º 6
0
func (self *Viewport) SelectedBlockFace() *BlockFace {
	var newmousex, newmousey int
	_ = sdl.GetMouseState(&newmousex, &newmousey)

	self.selectedBlockFace = nil
	self.mousex = newmousex
	self.mousey = newmousey

	x := (float64(self.mousex) - float64(self.screenWidth)/2) / (float64(self.screenWidth) / 2)
	z := (float64(self.screenHeight)/2 - float64(self.mousey)) / (float64(self.screenHeight) / 2)

	origin, norm := self.ProjectPoint(&Vectorf{x, z, -1})

	if origin != nil {
		pos := IntPosition(ThePlayer.position)
		ray := Ray{origin, norm}

		// See http://www.dyn-lab.com/articles/pick-selection.html
		var box *Box = nil
		distance := float64(1e9)
		face := uint8(0)
		for dy := int16(PLAYER_REACH); dy > -(PLAYER_REACH + 1); dy-- {
			for dz := -int16(PLAYER_REACH); dz < PLAYER_REACH+1; dz++ {
				for dx := -int16(PLAYER_REACH); dx < PLAYER_REACH+1; dx++ {
					if dy*dy+dz*dz+dx*dx <= PLAYER_REACH*PLAYER_REACH {
						blockDirection := Vectorf{float64(dx), float64(dy), float64(dz)}

						if /* ThePlayer.Facing(blockDirection) && */ blockDirection.Magnitude() <= float64(PLAYER_REACH) {

							posTest := pos.Translate(dx, dy, dz)
							trialDistance := math.Sqrt(math.Pow(float64(posTest[XAXIS])-origin[0], 2) + math.Pow(float64(posTest[YAXIS])-origin[1], 2) + math.Pow(float64(posTest[ZAXIS])-origin[2], 2))
							if trialDistance < distance {

								if TheWorld.Atv(posTest) != BLOCK_AIR {
									trialBox := &Box{
										&Vectorf{float64(posTest[XAXIS]) - 0.5, float64(posTest[YAXIS]) - 0.5, float64(posTest[ZAXIS]) - 0.5},
										&Vectorf{float64(posTest[XAXIS]) + 0.5, float64(posTest[YAXIS]) + 0.5, float64(posTest[ZAXIS]) + 0.5}}

									hit, trialFace := ray.HitsBox(trialBox)
									if hit {
										distance = trialDistance
										box = trialBox
										face = trialFace
									}

								}
							}
						}
					}
				}
			}
		}

		if box != nil {
			self.selectedBlockFace = &BlockFace{pos: Vectori{uint16(box.min[XAXIS] + 0.5), uint16(box.min[YAXIS] + 0.5), uint16(box.min[ZAXIS] + 0.5)}, face: face}
			self.selectionDirty = false
		}

	}
	// }

	return self.selectedBlockFace

}
Ejemplo n.º 7
0
func (self *Inventory) Draw(t int64) {
	gl.MatrixMode(gl.MODELVIEW)
	gl.PushMatrix()
	gl.LoadIdentity()

	gl.Disable(gl.DEPTH_TEST)
	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.LIGHT0)
	gl.Disable(gl.LIGHT1)

	gl.Color4ub(0, 0, 0, 240)

	gl.Begin(gl.QUADS)
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.tplane))
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.tplane))
	gl.End()

	picker.DrawItemHighlight(t, 3)
	picker.DrawItemHighlight(t, 4)
	picker.DrawItemHighlight(t, 5)
	picker.DrawItemHighlight(t, 6)
	picker.DrawItemHighlight(t, 7)
	picker.DrawPlayerItems(t)

	const blocksize = float64(0.3)
	const COLSIZE = 12

	diam := blocksize * 2.4

	offset := diam + float64(4)*PIXEL_SCALE

	for i := 0; i < len(self.inventoryRects); i++ {
		x := float64(viewport.lplane) + float64(10)*PIXEL_SCALE + float64(i/COLSIZE)*offset
		y := float64(viewport.tplane) - float64(10)*PIXEL_SCALE - float64(i%COLSIZE)*offset
		self.inventoryRects[i] = Rect{x, y - diam, diam, diam}
		self.DrawItemSlot(t, self.inventoryRects[i])
	}

	slot := 0
	for i := 1; i < len(ThePlayer.inventory); i++ {
		if ThePlayer.inventory[i] > 0 {
			self.inventorySlots[slot] = uint16(i)
			self.DrawItem(t, ThePlayer.inventory[i], uint16(i), self.inventoryRects[slot])
			slot++
		}
	}

	for i := 0; i < len(self.componentSlots); i++ {
		x := float64(viewport.lplane) + offset*float64(2+len(self.inventoryRects)/COLSIZE) + float64(i)*offset
		y := float64(viewport.tplane) - (float64(10) * PIXEL_SCALE)
		self.componentRects[i] = Rect{x, y - diam, diam, diam}

		self.DrawItemSlot(t, self.componentRects[i])
	}

	for i := 0; i < len(self.componentSlots); i++ {
		if self.componentSlots[i] != 0 {
			self.DrawItem(t, ThePlayer.inventory[self.componentSlots[i]], self.componentSlots[i], self.componentRects[i])
		}
	}

	for i := 0; i < len(self.productSlots); i++ {
		x := float64(viewport.lplane) + offset*float64(2+len(self.inventoryRects)/COLSIZE) + offset*float64(i%len(self.componentRects))
		y := float64(viewport.tplane) - (float64(10) * PIXEL_SCALE) - offset*float64(2+float64(i/len(self.componentRects)))
		self.productRects[i] = Rect{x, y - diam, diam, diam}

		self.DrawItemSlot(t, self.productRects[i])
	}

	for i := 0; i < len(self.productSlots); i++ {
		if self.productSlots[i] != nil {
			self.DrawItem(t, self.productSlots[i].product.quantity, self.productSlots[i].product.item, self.productRects[i])
		}
	}

	var mousex, mousey int
	mousestate := sdl.GetMouseState(&mousex, &mousey)
	self.HandleMouse(mousex, mousey, mousestate)

	gl.PopMatrix()
}
Ejemplo n.º 8
0
func gameLoop() {
	var startTime int64 = time.Now().UnixNano()
	var currentTime, accumulator int64 = 0, 0
	var t, dt int64 = 0, 1e9 / 40
	var drawFrame, computeFrame int64 = 0, 0

	update500ms := new(Timer)
	update500ms.interval = 500 * 1e6
	update500ms.Start()

	update150ms := new(Timer)
	update150ms.interval = 50 * 1e6
	update150ms.Start()

	update2000ms := new(Timer)
	update2000ms.interval = 2000 * 1e6
	update2000ms.Start()

	var interactingBlock *InteractingBlockFace

	done := false
	for !done {

		for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() {
			switch e.(type) {
			case *sdl.QuitEvent:
				done = true
			case *sdl.ResizeEvent:
				re := e.(*sdl.ResizeEvent)
				screen := sdl.SetVideoMode(int(re.W), int(re.H), 16,
					sdl.OPENGL|sdl.RESIZABLE)
				if screen != nil {
					viewport.Reshape(int(screen.W), int(screen.H))
				} else {
					panic("Could not set video mode")
				}
				break

			case *sdl.MouseButtonEvent:
				re := e.(*sdl.MouseButtonEvent)
				if inventory.visible {
					inventory.HandleMouseButton(re)
				} else {
					picker.HandleMouseButton(re)

					if re.Button == 1 && re.State == 1 { // LEFT, DOWN
						if ThePlayer.CanInteract() {
							selectedBlockFace := viewport.SelectedBlockFace()
							if selectedBlockFace != nil {
								if interactingBlock == nil || interactingBlock.blockFace.pos != selectedBlockFace.pos {
									interactingBlock = new(InteractingBlockFace)
									interactingBlock.blockFace = selectedBlockFace
									interactingBlock.hitCount = 0
								}
								ThePlayer.Interact(interactingBlock)
							}
							// println("Click:", re.X, re.Y, re.State, re.Button, re.Which)
						}
					}
				}

			case *sdl.KeyboardEvent:
				re := e.(*sdl.KeyboardEvent)

				if re.Keysym.Sym == sdl.K_ESCAPE {
					if re.Type == sdl.KEYDOWN {
						inventory.visible = false
						if pause.visible {
							pause.visible = false
							update2000ms.Unpause()
							update500ms.Unpause()
							update150ms.Unpause()
						} else {
							pause.visible = true
							update2000ms.Pause()
							update500ms.Pause()
							update150ms.Pause()
						}
					}
				}

				if re.Keysym.Sym == sdl.K_F3 {
					if re.Type == sdl.KEYDOWN {
						if console.visible == true {
							console.visible = false
						} else {
							console.visible = true
						}
					}
				}

				if !pause.visible {
					if re.Keysym.Sym == sdl.K_i {
						if re.Type == sdl.KEYDOWN {
							if inventory.visible == true {
								inventory.visible = false
							} else {
								inventory.visible = true
							}
						}
					}

					if inventory.visible {
						inventory.HandleKeyboard(re)
					}
				}
			}
		}

		keys := sdl.GetKeyState()

		if console.visible {
			console.HandleKeys(keys)
		}

		if pause.visible {
			pause.HandleKeys(keys)
		} else if inventory.visible {
			inventory.HandleKeys(keys)
		} else {
			viewport.HandleKeys(keys)
			ThePlayer.HandleKeys(keys)
			picker.HandleKeys(keys)
		}

		if update150ms.PassedInterval() {

			if !inventory.visible {
				// If player is breaking a block then allow them to hold mouse down to continue action
				if interactingBlock != nil && ThePlayer.currentAction == ACTION_BREAK {
					mouseState := sdl.GetMouseState(nil, nil)
					if mouseState == 1 {
						if ThePlayer.CanInteract() {
							selectedBlockFace := viewport.SelectedBlockFace()
							if selectedBlockFace != nil {
								if interactingBlock == nil || !interactingBlock.blockFace.pos.Equals(&selectedBlockFace.pos) {
									interactingBlock = new(InteractingBlockFace)
									interactingBlock.blockFace = selectedBlockFace
									interactingBlock.hitCount = 0
								}
								ThePlayer.Interact(interactingBlock)
							}
							// println("Click:", re.X, re.Y, re.State, re.Button, re.Which)
						}
					}
				}
			}

			PreloadChunks(50)
			update150ms.Start()
		}

		if update500ms.PassedInterval() {
			console.fps = float64(drawFrame) / (float64(update500ms.GetTicks()) / float64(1e9))
			console.Update()

			UpdateTimeOfDay()
			UpdateCampfires()
			PreloadChunks(220)

			drawFrame, computeFrame = 0, 0
			update500ms.Start()

		}

		if update2000ms.PassedInterval() {
			CullChunks()
			UpdatePlayerStats()
			update2000ms.Start()
		}

		if !pause.visible {
			newTime := time.Now().UnixNano()
			deltaTime := newTime - currentTime
			currentTime = newTime
			if deltaTime > 1e9/4 {
				deltaTime = 1e9 / 4
			}

			accumulator += deltaTime

			for accumulator > dt {
				accumulator -= dt

				TheWorld.ApplyForces(ThePlayer, float64(dt)/1e9)

				ThePlayer.Update(float64(dt) / 1e9)
				TheWorld.Simulate(float64(dt) / 1e9)

				computeFrame++
				t += dt
			}
		}
		//interpolate(previous, current, accumulator/dt)

		Draw(currentTime - startTime)
		drawFrame++
	}

}
Ejemplo n.º 9
0
func (self *Inventory) Draw(t int64) {
	gl.MatrixMode(gl.MODELVIEW)
	gl.PushMatrix()
	gl.LoadIdentity()

	gl.Disable(gl.DEPTH_TEST)
	gl.Disable(gl.LIGHTING)
	gl.Disable(gl.LIGHT0)
	gl.Disable(gl.LIGHT1)

	gl.Color4ub(0, 0, 0, 208)

	gl.Begin(gl.QUADS)
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.bplane))
	gl.Vertex2f(float32(viewport.rplane), float32(viewport.tplane))
	gl.Vertex2f(float32(viewport.lplane), float32(viewport.tplane))
	gl.End()

	picker.DrawItemHighlight(t, 3)
	picker.DrawItemHighlight(t, 4)
	picker.DrawItemHighlight(t, 5)
	picker.DrawItemHighlight(t, 6)
	picker.DrawItemHighlight(t, 7)
	picker.DrawPlayerItems(t, false)

	const blocksize = float64(0.3)
	const COLSIZE = 12

	slotsize := blocksize * 2.4
	slotstep := slotsize + float64(4)*PIXEL_SCALE
	slotsInRow := len(self.componentRects)

	xtools := float64(viewport.lplane) + 10.0*PIXEL_SCALE
	ytools := float64(viewport.tplane) - 10.0*PIXEL_SCALE - slotstep

	gl.PushMatrix()
	gl.LoadIdentity()
	gl.Translated(xtools, ytools+4*PIXEL_SCALE, 0)
	inventoryItemFont.Print("Inventory")
	gl.PopMatrix()
	for i := range self.inventoryRects {
		x := xtools + float64(i%slotsInRow)*slotstep
		y := ytools - float64(i/slotsInRow)*slotstep
		self.inventoryRects[i] = Rect{x, y - slotsize, slotsize, slotsize}
		self.DrawItemSlot(t, self.inventoryRects[i])
	}

	for i := range self.inventorySlots {
		if self.inventorySlots[i].item != 0 && self.inventorySlots[i].quantity > 0 {
			self.DrawItemInSlot(t, self.inventorySlots[i].quantity, self.inventorySlots[i].item, self.inventoryRects[i])
		}
	}

	xtools += slotstep * (1.0 + float64(slotsInRow))

	gl.PushMatrix()
	gl.LoadIdentity()
	gl.Translated(xtools, ytools+4*PIXEL_SCALE, 0)

	if self.currentCrafting == nil {
		inventoryItemFont.Print("Handcrafting")
	} else {
		inventoryItemFont.Print(self.currentCrafting.Label())
	}
	gl.PopMatrix()

	for i := range self.componentSlots {
		x := xtools + float64(i)*slotstep
		y := ytools
		self.componentRects[i] = Rect{x, y - slotsize, slotsize, slotsize}

		self.DrawItemSlot(t, self.componentRects[i])
	}

	for i, cs := range self.componentSlots {
		if cs.item != 0 {
			self.DrawItemInSlot(t, cs.quantity, cs.item, self.componentRects[i])
		}
	}

	ytools -= slotstep * 2
	for i := range self.productSlots {
		x := xtools + slotstep*float64(i%slotsInRow)
		y := ytools - slotstep*float64(i/slotsInRow)
		self.productRects[i] = Rect{x, y - slotsize, slotsize, slotsize}

		self.DrawItemSlot(t, self.productRects[i])
	}

	for i, ps := range self.productSlots {
		if ps != nil {
			self.DrawItemInSlot(t, ps.product.quantity, ps.product.item, self.productRects[i])
		}
	}

	ytools -= slotstep * float64(1+len(self.productRects)/slotsInRow)
	if self.currentContainer != nil {

		gl.PushMatrix()
		gl.LoadIdentity()
		gl.Translated(xtools, ytools+4*PIXEL_SCALE, 0)
		inventoryItemFont.Print(self.currentContainer.Label())
		gl.PopMatrix()

		for i := range self.containerRects {
			x := xtools + slotstep*float64(i%slotsInRow)
			y := ytools - slotstep*float64(i/slotsInRow)
			self.containerRects[i] = Rect{x, y - slotsize, slotsize, slotsize}

			self.DrawItemSlot(t, self.containerRects[i])
		}

		for i := 0; i < self.currentContainer.Slots(); i++ {
			if self.currentContainer.Item(i).item != 0 {
				self.DrawItemInSlot(t, self.currentContainer.Item(i).quantity, self.currentContainer.Item(i).item, self.containerRects[i])
			}
		}

	}

	var mousex, mousey int
	mousestate := sdl.GetMouseState(&mousex, &mousey)

	if self.selectedItem != nil {
		x, y := viewport.ScreenCoordsToWorld2D(uint16(mousex), uint16(mousey))
		self.DrawItem(t, self.selectedItem.quantity, self.selectedItem.item, x, y)
	}

	self.HandleMouse(mousex, mousey, mousestate)

	gl.PopMatrix()
}