예제 #1
0
func NewGameState(window *sdl.Window, world *World) (gamestate *GameState) {
	gl.ClearColor(0., 0., 0.4, 0.0)

	gl.Enable(gl.DEPTH_TEST)

	bar := tw.NewBar("TweakBar")

	gamestate = &GameState{
		Window:  window,
		Camera:  nil,
		Bar:     bar,
		World:   world,
		Fps:     0,
		Options: settings.BoolOptions{},
	}

	opt := &gamestate.Options
	opt.Load()
	gamestate.Camera = gamestate.World.Player.GetCamera()

	tw.Define(" GLOBAL help='This example shows how to integrate AntTweakBar with SDL2 and OpenGL.' ")
	bar.AddVarRO("fps", tw.TYPE_FLOAT, unsafe.Pointer(&gamestate.Fps), "")
	opt.CreateGui(bar)

	for i, portal := range gamestate.World.KdTree {
		ptr := &(portal.(*Portal).Orientation)
		bar.AddVarRW(fmt.Sprintf("Rotation %d", i), tw.TYPE_QUAT4F, unsafe.Pointer(ptr), "")
	}

	//window.GetSize(w, h)
	w, h := window.GetSize()
	tw.WindowSize(w, h)

	return
}
예제 #2
0
// returns false if the player wants to quit
func Input(gs *gamestate.GameState, worldRenderer *rendering.WorldRenderer) bool {
	running := true
	window := gs.Window
	inp := gamestate.PlayerInput{}

	for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
		consumeEvent := true
		if !sdl.GetRelativeMouseMode() {
			consumeEvent = tw.EventSDL(event, 2, 0)
		}
		if consumeEvent {
			switch e := event.(type) {
			case *sdl.WindowEvent:
				switch e.Event {
				case sdl.WINDOWEVENT_CLOSE:
					running = false
				case sdl.WINDOWEVENT_RESIZED:
					width, height := int(e.Data1), int(e.Data2)
					worldRenderer.Resize(width, height)
					tw.WindowSize(width, height)
				}
			case *sdl.MouseButtonEvent:
				button := e.Button

				if e.State == sdl.PRESSED && drag == 255 {
					drag = e.Button
				}
				if e.State == sdl.RELEASED && drag == button {
					drag = 255
				}

				if e.State == sdl.PRESSED && button == sdl.BUTTON_RIGHT {
					GrabCursor()
				}

				// ray cast testing
				if e.State == sdl.PRESSED && button == sdl.BUTTON_LEFT {
					dir_cs := GetMouseDirection(window, int(e.X), int(e.Y))
					out, hit := RayCastInCameraSpace(gs, dir_cs)
					if hit {
						n := helpers.Vector(gs.World.HeightMap.Normal2f(out[0], out[1]))
						debug.Color(mgl.Vec4{0, 1, 0, 1})
						debug.Line(out, out.Add(n))
					}
				}
			case *sdl.KeyDownEvent:
				switch e.Keysym.Scancode {
				case sdl.SCANCODE_RETURN:
					GrabCursor()
				case sdl.SCANCODE_SPACE:
				case sdl.SCANCODE_ESCAPE:
					running = false
				}
			case *sdl.KeyUpEvent:
			}
		}
	}

	if sdl.GetRelativeMouseMode() {
		x, y, _ := sdl.GetRelativeMouseState()
		inp.Rotate[0] = -float32(y) / 500
		inp.Rotate[1] = -float32(x) / 500
	}

	keyState := sdl.GetKeyboardState()

	if keyState[sdl.SCANCODE_E] == 1 {
		inp.Move[2] -= 1
	}
	if keyState[sdl.SCANCODE_D] == 1 {
		inp.Move[2] += 1
	}
	if keyState[sdl.SCANCODE_S] == 1 {
		inp.Move[0] -= 1
	}
	if keyState[sdl.SCANCODE_F] == 1 {
		inp.Move[0] += 1
	}
	if keyState[sdl.SCANCODE_R] == 1 {
		inp.Rotate[2] -= 0.01
	}
	if keyState[sdl.SCANCODE_W] == 1 {
		inp.Rotate[2] += 0.01
	}

	x, y, button_state := sdl.GetMouseState()
	if button_state&sdl.BUTTON_LEFT != 0 {
		dir_cs := GetMouseDirection(window, x, y)
		out, hit := RayCastInCameraSpace(gs, dir_cs)
		if hit {
			heightMap := gs.World.HeightMap
			heightMap.Bump(mgl.Vec2{out[0], out[1]}, 3)
		}
	}

	gs.World.Player.Input = inp

	return running
}