func moveCamera(camera *trace.FreeFlightCamera, dtf float32) { state := sdl.GetKeyboardState() switch { case state[sdl.GetScancodeFromKey(sdl.K_UP)] != 0: camera.YRot += dtf * cameraSpeed case state[sdl.GetScancodeFromKey(sdl.K_DOWN)] != 0: camera.YRot -= dtf * cameraSpeed case state[sdl.GetScancodeFromKey(sdl.K_LEFT)] != 0: camera.XRot += dtf * cameraSpeed case state[sdl.GetScancodeFromKey(sdl.K_RIGHT)] != 0: camera.XRot -= dtf * cameraSpeed case state[sdl.GetScancodeFromKey(sdl.K_w)] != 0: camera.Move(dtf * cameraSpeed) case state[sdl.GetScancodeFromKey(sdl.K_s)] != 0: camera.Move(dtf * -cameraSpeed) case state[sdl.GetScancodeFromKey(sdl.K_a)] != 0: camera.Strafe(dtf * cameraSpeed) case state[sdl.GetScancodeFromKey(sdl.K_d)] != 0: camera.Strafe(dtf * -cameraSpeed) case state[sdl.GetScancodeFromKey(sdl.K_e)] != 0: camera.Lift(dtf * cameraSpeed) case state[sdl.GetScancodeFromKey(sdl.K_q)] != 0: camera.Lift(dtf * -cameraSpeed) } }
// Key returns the state of a keyboard button func Key(key sdl.Scancode) bool { keyState := sdl.GetKeyboardState() if keyState[key] == 1 { return true } return false }
func IsKeyPressed(b byte) bool { keyboardState := sdl.GetKeyboardState() code := KeyMap[b] pressed := keyboardState[code] if pressed == 1 { return true } else { return false } }
func NewInput() *Input { i := &Input{ KeyDownHandlers: make(InputEventHandlers), KeyUpHandlers: make(InputEventHandlers), MouseDownHandlers: make(InputEventHandlers), MouseUpHandlers: make(InputEventHandlers), KeyStates: sdl.GetKeyboardState(), } i.oldKeyStates = make([]uint8, len(i.KeyStates)) return i }
func main() { start() defer window.Destroy() defer renderer.Destroy() screens := loadScreens() quit := false cur := screens["main"] for !quit { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch e.(type) { case *sdl.QuitEvent: quit = true } } curKeyState := sdl.GetKeyboardState() switch { case curKeyState[sdl.SCANCODE_UP] != 0: cur = screens["up"] case curKeyState[sdl.SCANCODE_DOWN] != 0: cur = screens["down"] case curKeyState[sdl.SCANCODE_LEFT] != 0: cur = screens["left"] case curKeyState[sdl.SCANCODE_RIGHT] != 0: cur = screens["right"] default: cur = screens["main"] } renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF) renderer.Clear() renderer.Copy(cur, nil, nil) renderer.Present() } }
func main() { fmt.Print() sdl.SetHint(sdl.HINT_RENDER_VSYNC, "1") check(sdl.Init(sdl.INIT_EVERYTHING)) defer sdl.Quit() window, r, err := sdl.CreateWindowAndRenderer( 640, 480, sdl.WINDOW_RESIZABLE, ) check(err) renderer = r defer renderer.Destroy() defer window.Destroy() window.SetTitle("Gophette's Adventures - Level Editor") window.SetPosition(50, 50) window.SetSize(1800, 900) window.SetFullscreen(sdl.WINDOW_FULLSCREEN_DESKTOP) renderer.SetDrawBlendMode(sdl.BLENDMODE_BLEND) if len(LevelImages) == 0 { for i, id := range []string{ "grass left", "grass right", "grass center 1", "grass center 2", "grass center 3", "small tree", } { images = append(images, image{id, loadImage(id), 0, i * 50}) } } else { for i := range LevelImages { id := LevelImages[i].ID x, y := LevelImages[i].X, LevelImages[i].Y img := loadImage(id) images = append(images, image{id, img, x, y}) } } leftDown := false middleDown := false rightDown := false selectedImage := -1 selectedObject := -1 var lastX, lastY int moveImage := func(dx, dy int) { if selectedImage != -1 { images[selectedImage].x += dx images[selectedImage].y += dy } } stretchObject := func(dx, dy int) { if selectedObject != -1 { if sdl.GetKeyboardState()[sdl.SCANCODE_LCTRL] != 0 { dx *= 20 dy *= 20 } obj := &LevelObjects[selectedObject] obj.W += dx obj.H += dy } } running := true for running { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch event := e.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseButtonEvent: if event.Button == sdl.BUTTON_LEFT { leftDown = event.State == sdl.PRESSED if !leftDown { draggingImage = false draggingObject = false } else { selectedObject = -1 selectedImage = -1 for i := range images { if images[i].contains( int(event.X)-cameraX, int(event.Y)-cameraY, ) { draggingImage = true selectedImage = i } } if selectedImage == -1 { for i := range LevelObjects { if contains(LevelObjects[i], int(event.X)-cameraX, int(event.Y)-cameraY, ) { draggingObject = true selectedObject = i } } } } } if event.Button == sdl.BUTTON_MIDDLE { middleDown = event.State == sdl.PRESSED } if event.Button == sdl.BUTTON_RIGHT { rightDown = event.State == sdl.PRESSED LevelObjects = append(LevelObjects, LevelObject{ int(event.X) - cameraX, int(event.Y) - cameraY, 0, 0, true, }) selectedObject = -1 } case *sdl.MouseMotionEvent: dx, dy := int(event.X)-lastX, int(event.Y)-lastY if selectedImage != -1 && draggingImage { img := &images[selectedImage] img.x += dx img.y += dy } if selectedObject != -1 && draggingObject { obj := &LevelObjects[selectedObject] obj.X += dx obj.Y += dy } lastX, lastY = int(event.X), int(event.Y) if middleDown { cameraX += dx cameraY += dy } if rightDown { last := &LevelObjects[len(LevelObjects)-1] last.W += dx last.H += dy } case *sdl.KeyDownEvent: switch event.Keysym.Sym { case sdl.K_ESCAPE: running = false case sdl.K_LEFT: cameraX += 100 case sdl.K_RIGHT: cameraX -= 100 case sdl.K_UP: cameraY += 100 case sdl.K_DOWN: cameraY -= 100 case sdl.K_a: moveImage(-1, 0) case sdl.K_d: moveImage(1, 0) case sdl.K_w: moveImage(0, -1) case sdl.K_s: moveImage(0, 1) case sdl.K_j: stretchObject(-1, 0) case sdl.K_l: stretchObject(1, 0) case sdl.K_i: stretchObject(0, -1) case sdl.K_k: stretchObject(0, 1) case sdl.K_MINUS: if selectedImage != -1 && selectedImage != 0 { images = append(append( []image{images[selectedImage]}, images[0:selectedImage]...), images[selectedImage+1:]..., ) selectedImage = 0 } case sdl.K_PLUS: last := len(images) - 1 fmt.Println("last", last, len(images)) fmt.Println("selectedImage", selectedImage) if selectedImage != -1 && selectedImage != last { images = append(append( images[0:selectedImage], images[selectedImage+1:]...), images[selectedImage], ) selectedImage = last fmt.Println("selectedImage after", selectedImage) } case sdl.K_SPACE: if selectedObject != -1 { obj := &LevelObjects[selectedObject] obj.Solid = !obj.Solid } case sdl.K_c: if selectedImage != -1 { copy := images[selectedImage] copy.x += 10 copy.y += 10 images = append(images, copy) } case sdl.K_DELETE: if selectedImage != -1 { images = append(images[:selectedImage], images[selectedImage+1:]...) selectedImage = -1 } else if selectedObject != -1 { LevelObjects = append( LevelObjects[:selectedObject], LevelObjects[selectedObject+1:]..., ) selectedObject = -1 } case sdl.K_F3: saveLevel() } } } renderer.SetDrawColor(backColor[0], backColor[1], backColor[2], 255) renderer.Clear() for i, img := range images { img.render(i == selectedImage) } for i, obj := range LevelObjects { var g uint8 = 0 var a uint8 = 100 if i == selectedObject { g = 255 } renderer.SetDrawColor(0, g, 0, a) if obj.Solid { renderer.SetDrawColor(0, g, 255, a) } obj.X += cameraX obj.Y += cameraY r := sdl.Rect{int32(obj.X), int32(obj.Y), int32(obj.W), int32(obj.H)} renderer.FillRect(&r) } renderer.Present() } }
// IsScancodeDown checks whether a certain scancode is down. func IsScancodeDown(scancode Scancode) bool { state := sdl.GetKeyboardState() return state[int(scancode)] == 1 }
func (m *machine) Key(code int) bool { scan := sdl.GetScancodeFromName(keymap[code]) state := sdl.GetKeyboardState() return state[scan] != 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 }