func (c *Controller) handleEvents() { // read all events for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch et := event.(type) { case *sdl.QuitEvent: log.Println("Want to quit, but how do we apply this... ?") /* c.Destroy() return // NOTE: caller keeps calling Iterate because it doesn't know we're done... how to fix? */ case *sdl.KeyDownEvent: c.sendKeyEvents(sdl.GetKeyName(et.Keysym.Sym), true) case *sdl.KeyUpEvent: c.sendKeyEvents(sdl.GetKeyName(et.Keysym.Sym), false) // these are events we know but currently don't use FIXME implement case *sdl.WindowEvent: case *sdl.TextInputEvent: case *sdl.TextEditingEvent: case *sdl.MouseButtonEvent: case *sdl.MouseMotionEvent: case *sdl.MouseWheelEvent: default: log.Printf("Unknown event: %T\n", et) } } }
// CheckForUpdates polls sdl events and updates the map accordingly func CheckForUpdates() bool { changeDetected := false // poll any current events var event sdl.Event for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: inputMap["Quit"] = true case *sdl.KeyDownEvent: if t.Keysym.Sym == sdl.K_ESCAPE { inputMap["Esc"] = true } else { // fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", // t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } default: // fmt.Printf("Some event\n") } changeDetected = true } return changeDetected }
func main() { start() defer window.Destroy() defer renderer.Destroy() text := loadText("Press enter to reset", sdl.Color{0, 0, 0, 255}) startTime := sdl.GetTicks() quit := false for !quit { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch t := e.(type) { case *sdl.QuitEvent: quit = true case *sdl.KeyDownEvent: if t.Keysym.Sym == sdl.K_RETURN { startTime = sdl.GetTicks() } } } renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF) renderer.Clear() timeText := loadText(fmt.Sprintf("time: %d", sdl.GetTicks()-startTime), sdl.Color{0, 0, 0, 255}) renderer.Copy(text, nil, &sdl.Rect{0, 0, 400, 100}) renderer.Copy(timeText, nil, &sdl.Rect{400, 0, 400, 100}) renderer.Present() } }
func main() { rom, err := LoadROM(os.Args[1]) if err != nil { panic(err) } frameDelay := 1000 / frameRate window := sdl.CreateWindow( "Go NES", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 256, 240, sdl.WINDOW_SHOWN, ) if window == nil { panic(sdl.GetError()) } defer window.Destroy() controller := NewController() ppu := NewPPU(window, NewVRAM(rom.Mapper())) cpu := NewCPU(NewMMU(rom.Mapper(), ppu, controller), ppu) ticksPerFrame := uint32(341 * 262) for { expectedTicks := sdl.GetTicks() + frameDelay for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event := event.(type) { case *sdl.KeyDownEvent: controller.SetKeyState(event.Keysym.Sym, true) case *sdl.KeyUpEvent: controller.SetKeyState(event.Keysym.Sym, false) case *sdl.QuitEvent: return } } ppuTicks := uint32(0) for ppuTicks < ticksPerFrame { ppuTicks += ppu.Tick(cpu.Tick()) } actualTicks := sdl.GetTicks() if actualTicks < expectedTicks { sdl.Delay(expectedTicks - actualTicks) } } }
func main() { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if window == nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError()) os.Exit(1) } renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if renderer == nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError()) os.Exit(2) } var peepArray []sdl.Event = make([]sdl.Event, 5) running = true for running { sdl.PumpEvents() numEventsRetrieved := sdl.PeepEvents(peepArray, sdl.PEEKEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT) if numEventsRetrieved < 0 { fmt.Printf("PeepEvents error: %s\n", sdl.GetError()) } else { for i := 0; i < numEventsRetrieved; i++ { fmt.Printf("Event Peeked Value: %v\n", peepArray[i]) // primitive printing of event } } for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } } sdl.Delay(1000 / 30) } renderer.Destroy() window.Destroy() }
func main() { start() defer window.Destroy() defer renderer.Destroy() loadAudio() screens := loadScreens() quit := false cur := screens["main"] for !quit { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch t := e.(type) { case *sdl.QuitEvent: quit = true case *sdl.KeyDownEvent: switch t.Keysym.Sym { case sdl.K_1: high.PlayChannel(-1, 0) case sdl.K_2: medium.PlayChannel(-1, 0) case sdl.K_3: low.PlayChannel(-1, 0) case sdl.K_4: scratch.PlayChannel(-1, 0) case sdl.K_9: if !mix.MusicPlaying() { if !music.Play(1) { fmt.Printf("music did not play: %s", sdl.GetError()) } } else { if !mix.PausedMusic() { mix.PauseMusic() } else { mix.ResumeMusic() } } } } } renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF) renderer.Clear() renderer.Copy(cur, nil, nil) renderer.Present() } }
func catchEvents() bool { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: return false case *sdl.KeyDownEvent: handleKeyEvent(t.Keysym.Sym) } } return true }
func (g *Engine) eventLoop() { g.Event = nil for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: g.Running = false case *sdl.KeyDownEvent: g.Event = &t.Keysym.Sym } } }
//wait for an event and returns the value of the event in key_mapping func (c *Chip8) waitEvent() byte { for { for c.event = sdl.PollEvent(); c.event != nil; c.event = sdl.PollEvent() { switch event := c.event.(type) { case *sdl.KeyDownEvent: if code, found := c.key_mapping[event.Keysym.Sym]; found { return code } } } } }
func main() { var winTitle string = "Go-SDL2 + Go-GL" var winWidth, winHeight int = 800, 600 var window *sdl.Window var context sdl.GLContext var event sdl.Event var running bool var err error if err = sdl.Init(sdl.INIT_EVERYTHING); err != nil { panic(err) } defer sdl.Quit() if err = gl.Init(); err != nil { panic(err) } window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_OPENGL) if err != nil { panic(err) } defer window.Destroy() context, err = sdl.GL_CreateContext(window) if err != nil { panic(err) } defer sdl.GL_DeleteContext(context) gl.Enable(gl.DEPTH_TEST) gl.ClearColor(0.2, 0.2, 0.3, 1.0) gl.ClearDepth(1) gl.DepthFunc(gl.LEQUAL) gl.Viewport(0, 0, int32(winWidth), int32(winHeight)) running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Which, t.X, t.Y, t.XRel, t.YRel) } } drawgl() sdl.GL_SwapWindow(window) } }
func main() { window, renderer = tools.Start("planets", int(SCREEN_W), int(SCREEN_H)) wideView := tools.ViewBox{tools.Vector{-SCREEN_SCALE / 2 / float64(SCREEN_H) * float64(SCREEN_W), -SCREEN_SCALE / 2}, SCREEN_SCALE / float64(SCREEN_H) * float64(SCREEN_W), SCREEN_SCALE} defer window.Destroy() defer renderer.Destroy() sprite = tools.LoadTextureTransparent(renderer, "resources/sprites.png", 0, 0xFF, 0xFF) bodies := []*tools.Body{&Sun, &Earth, &Mars, &Ecc} quit := false curView := &wideView for !quit { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch t := e.(type) { case *sdl.QuitEvent: quit = true case *sdl.KeyDownEvent: switch t.Keysym.Sym { case sdl.K_UP: auto := tools.GetAutoView( []tools.Vector{ bodies[0].Position, bodies[1].Position}, SCREEN_RATIO) curView = &auto case sdl.K_DOWN: curView = &wideView case sdl.K_q: quit = true } // fmt.Println("View:", curView) } } // Draw background renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF) renderer.Clear() renderer.SetDrawColor(0, 0, 0, 0xFF) tools.UpdateBodiesSeconds(bodies, 60*60*24*7) DrawBodies(bodies, *curView) renderer.Present() //fmt.Println(earth.Position, sun.Position) } }
func (i *InputHandler) HandleEvents() { i.pressed_keys_this_frame = make([]sdl.Keycode, 10) i.released_keys_this_frame = make([]sdl.Keycode, 10) for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.KeyDownEvent: i.pressed_keys_this_frame = append(i.pressed_keys_this_frame, t.Keysym.Sym) i.pressed_keys[t.Keysym.Sym] = true case *sdl.KeyUpEvent: i.released_keys_this_frame = append(i.released_keys_this_frame, t.Keysym.Sym) i.pressed_keys[t.Keysym.Sym] = false } } }
func main() { window, err := sdl.CreateWindow("Monster Path", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, 640, 480, sdl.WINDOW_SHOWN) if err != nil { os.Exit(1) } defer window.Destroy() renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { os.Exit(2) } defer renderer.Destroy() var event sdl.Event running := true myGrid := grid.New(17, 12) setupMap(myGrid) gunner.location, _ = myGrid.RoomAt(15, 7) alien.location, _ = myGrid.RoomAt(1, 1) graph := graph.NewFromGrid(myGrid) path := graph.Path(gunner.location, alien.location) for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event.(type) { case *sdl.QuitEvent: running = false } } renderer.SetDrawColor(205, 201, 201, 255) renderer.Clear() renderer.SetDrawColor(10, 10, 10, 255) Draw(renderer, myGrid) DrawGraph(renderer, graph) renderer.SetDrawColor(200, 0, 0, 255) DrawPath(renderer, path) renderer.SetDrawColor(0, 100, 0, 255) alien.Draw(renderer) gunner.Draw(renderer) renderer.Present() } }
func (p *PlayerFrontend) processInput() { for ev := sdl.PollEvent(); ev != nil; ev = sdl.PollEvent() { input := Input{} for _, v := range p.inputs { earlyExit, in := v.ProcessEvent(ev, p) if in != nil { input.Combine(in) } if earlyExit { break } } input.Normalize() p.sendInput(input) } }
func Poll() Event { e := sdl.PollEvent() if e == nil { return nil } return wrapEvent(e) }
// GetKey returns the key or button that has been pressed by the user. // Possible return values are the constants: // // KeyNone - indicating that no key, or an unrecognised key as been pressed // KeyUp - indicating that the up cursor key has been pressed // KeyDown - indicating that the down cursor key has been pressed // KeyPause - indicating that the pause key has been presses // ButtonClose - indicating that the windows close button has been pressed // // GetKey will panic if: // // 1. The toolbox has not been initialised. // // 2. If an internal check fails. In this case the panic message is "KeyDownEvent type assertion failed!" // This is highly unlikely to occur and indcates a problem with the underlying // graphics llibrary. func GetKey() Key { if !initialised { // this stops execution here, so ne need for an else after the if panic(notInitialisedMessage) } var event sdl.Event event = sdl.PollEvent() if event != nil { if isQuitEvent(event) { return ButtonClose } if isKeyDownEvent(event) { if isKeyUp(event) { return KeyUp } if isKeyDown(event) { return KeyDown } // We must always respond to the paused key being pressed - if the // game is not over. // If the game is running the pause key pauses the game. // But if the game is paused, we must still respond to the paused key. // This is the only way to unpause the game. if isKeyPause(event) { return KeyPause } } } return KeyNone }
func (mc *MissionControl) Start() { var event sdl.Event var running bool = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) // Quit on escape if t.Keysym.Sym == sdl.K_ESCAPE { running = false } } } // Update each widget for _, d := range mc.drawables { d.Update() } // This will update the entire screen to this color SetDrawColor(mc.renderer, mc.background_color) mc.renderer.Clear() // Update the ui for _, d := range mc.drawables { d.Draw(mc.renderer) } mc.renderer.Present() // We need to sleep the remainder of the frame here } }
func get_event() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.WindowEvent: if e.Event == sdl.WINDOWEVENT_FOCUS_LOST && game.state == PLAY { game.state = PAUSE } case *sdl.QuitEvent: window.Destroy() os.Exit(0) case *sdl.KeyDownEvent: control.UpdateKey(int(e.Keysym.Sym), true) case *sdl.KeyUpEvent: control.UpdateKey(int(e.Keysym.Sym), false) } } }
func run() int { var window *sdl.Window var renderer *sdl.Renderer var event sdl.Event var running bool var err error window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, winWidth, winHeight, sdl.WINDOW_SHOWN) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err) return 1 } defer window.Destroy() renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED) if err != nil { fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err) return 2 } defer renderer.Destroy() running = true for running { for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: running = false case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat) } } } return 0 }
func (i *Input) Handle() { var event sdl.Event copy(i.oldKeyStates, i.KeyStates) for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.QuitEvent: G.Quit = true case *sdl.MouseButtonEvent: i.handleMouseButtonEvent(e) case *sdl.KeyDownEvent: i.handleKeyDownEvent(e) case *sdl.KeyUpEvent: i.handleKeyUpEvent(e) } } }
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() { window, renderer = tools.Start("flip", int(W), int(H)) defer window.Destroy() defer renderer.Destroy() arrow := tools.LoadTexture(renderer, "resources/arrow.png") back := tools.LoadTexture(renderer, "resources/background.png") angle := 0.0 var flip = sdl.RendererFlip(sdl.FLIP_NONE) quit := false for !quit { for e := sdl.PollEvent(); e != nil; e = sdl.PollEvent() { switch t := e.(type) { case *sdl.QuitEvent: quit = true case *sdl.KeyDownEvent: switch t.Keysym.Sym { case sdl.K_LEFT: angle += 30.0 case sdl.K_RIGHT: angle -= 30.0 case sdl.K_q: flip ^= sdl.FLIP_HORIZONTAL case sdl.K_w: flip ^= sdl.FLIP_VERTICAL } } } renderer.SetDrawColor(0xFF, 0xFF, 0xFF, 0xFF) renderer.Clear() renderer.Copy(back, nil, nil) renderer.CopyEx(arrow, nil, &sdl.Rect{300, 400, 100, 100}, angle, &sdl.Point{50, 50}, flip) renderer.Present() } }
func (dmg *DMG) exec() { run := true for run { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.QuitEvent: run = false case *sdl.KeyUpEvent: dmg.keypad.keyup(uint8(e.Keysym.Sym)) case *sdl.KeyDownEvent: dmg.keypad.keydown(uint8(e.Keysym.Sym)) } } dmg.cpu.exec() } dmg.lcdController.shutdown() }
func processInput(quit chan bool) { var event sdl.Event for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: close(quit) case *sdl.MouseMotionEvent: fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel) case *sdl.MouseButtonEvent: fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State) case *sdl.MouseWheelEvent: fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n", t.Timestamp, t.Type, t.Which, t.X, t.Y) case *sdl.KeyDownEvent: if t.Keysym.Sym == 1073741904 { // left arrow cam.Orientation[0] -= oneDegree } if t.Keysym.Sym == 1073741903 { // right arrow cam.Orientation[0] += oneDegree } if cam.Orientation[0] < 0 { cam.Orientation[0] = 2 * math.Pi } if cam.Orientation[0] > 2*math.Pi { cam.Orientation[0] = 0 } fmt.Printf("cam.Orientation[0] = %f\n", cam.Orientation[0]) pt := vec3.T{10, 20, 3} sp := perspProj(&pt, cam) fmt.Printf("sp: %v\n", sp) case *sdl.KeyUpEvent: fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tcode:%d\tmodifiers:%d\n", t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Sym, t.Keysym.Mod) if t.Keysym.Sym == 27 || t.Keysym.Sym == 1073741881 /* Caps Lock */ { close(quit) } } } }
// CheckQuit checks if the user has clicked the window's close button. // If the user has then the quit variable is set it true. CheckQuit returns // the value of the quit variable. func checkQuit() bool { var event sdl.Event event = sdl.PollEvent() if event != nil { switch event.(type) { case *sdl.QuitEvent: quit = true } } return quit }
// GetNextEvent returns the next sdl.Event depending on the eventMode func (g *GameManager) GetNextEvent() sdl.Event { switch g.eventMode { case GameManagerEventDriven: return sdl.WaitEvent() case GameManagerEventTimeoutDriven: return sdl.WaitEventTimeout(g.EventTimeout) case GameManagerPollDriven: return sdl.PollEvent() } panic(fmt.Sprintf("GetNextEvent: unknown event mode: %d", g.eventMode)) }
func (events *Events) GetEvents() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch t := event.(type) { case *sdl.QuitEvent: os.Exit(0) case *sdl.KeyDownEvent: if t.Repeat != 0 { continue } switch t.Keysym.Scancode { case sdl.SCANCODE_UP: events.up = true case sdl.SCANCODE_DOWN: events.down = true case sdl.SCANCODE_LEFT: events.left = true case sdl.SCANCODE_RIGHT: events.right = true case sdl.SCANCODE_ESCAPE: os.Exit(0) } case *sdl.KeyUpEvent: switch t.Keysym.Scancode { case sdl.SCANCODE_UP: events.up = false case sdl.SCANCODE_DOWN: events.down = false case sdl.SCANCODE_LEFT: events.left = false case sdl.SCANCODE_RIGHT: events.right = false } } } }
func (controlManager *ControlManager) ReadInput() Update { Running = true allSym, allKeys, allFKeys := controlManager.getDirectPointers() return func(state int) int { if Running { controlManager.Player1.ResetFrameKey() controlManager.Player2.ResetFrameKey() event := sdl.PollEvent() for event != nil { switch t := event.(type) { case *sdl.QuitEvent: Running = false case *sdl.KeyUpEvent: for i := 0; i < 16; i++ { if t.Keysym.Sym == allSym[i] { *allKeys[i] = false } } case *sdl.KeyDownEvent: for i := 0; i < 16; i++ { if t.Keysym.Sym == allSym[i] { if t.Repeat == 0 { *allKeys[i] = true *allFKeys[i] = true } } } } event = sdl.PollEvent() } } else { controlManager.Running = false return -1 } return 0 } }
// Poll is used by the game loop to gather events and delegate to each pacakge. // Generally you should not have to use this method however if you are doing your // own game loop this should be called at the end. func Poll() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch e := event.(type) { case *sdl.WindowEvent: delegateWindowEvent(event, e) case *sdl.KeyDownEvent, *sdl.KeyUpEvent, *sdl.TextEditingEvent, *sdl.TextInputEvent: keyboard.Delegate(e) case *sdl.MouseMotionEvent, *sdl.MouseButtonEvent, *sdl.MouseWheelEvent: mouse.Delegate(e) case *sdl.JoyAxisEvent, *sdl.JoyBallEvent, *sdl.JoyHatEvent, *sdl.JoyButtonEvent, *sdl.JoyDeviceEvent, *sdl.ControllerAxisEvent, *sdl.ControllerButtonEvent, *sdl.ControllerDeviceEvent: joystick.Delegate(e) case *sdl.TouchFingerEvent: touch.Delegate(e) case *sdl.QuitEvent: window.Close(true) case *sdl.DropEvent, *sdl.RenderEvent, *sdl.UserEvent, *sdl.ClipboardEvent, *sdl.OSEvent, *sdl.CommonEvent: //discard not used in amore yet } } }
// HandleEvents is used for receiving and handling SDL Events such as // Update the mouse location, Check if the user is requesting to close the game, etc. func HandleEvents() { event := sdl.PollEvent() switch event.(type) { case *sdl.QuitEvent: log.Println("Game // Got Quit Event. Stopping Game ...") running = false break default: input.HandleEvents(event) currentScene.HandleEvents(event) break } }