Esempio n. 1
0
func MainLoop(gs *gamestate.GameState, renderer *rendering.WorldRenderer) {
	var frames int
	time := float32(sdl.GetTicks()) / 1000
	window := gs.Window
	running := true

	for running {
		currentTime := float32(sdl.GetTicks()) / 1000
		if currentTime > time+1 {
			gs.Fps = float32(frames)
			frames = 0
			time = currentTime
		}
		frames += 1
		running = Input(gs, renderer)

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		gl.Disable(gl.BLEND)

		simulation.Simulate(gs, renderer.ParticleSystem)
		renderer.View = gs.Camera.View()
		renderer.Render(gs.World, &gs.Options, window)

		tw.Draw()
		sdl.GL_SwapWindow(window)

		helpers.UpdateManagers()
	}
}
Esempio n. 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
}