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() }
// Loop is the main loop for the Base System, it continues until SetQuit is called with 'true' func (b *Base) Loop() { for b.quit == false { if b.input.Process() == true { b.SetQuit(true) } b.Process() sdl.Delay(16) } }
func main() { var window *sdl.Window var renderer *sdl.Renderer var points []sdl.Point var rect sdl.Rect var rects []sdl.Rect 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) } renderer.SetDrawColor(255, 255, 255, 255) renderer.DrawPoint(150, 300) renderer.SetDrawColor(0, 0, 255, 255) renderer.DrawLine(0, 0, 200, 200) points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}} renderer.SetDrawColor(255, 255, 0, 255) renderer.DrawLines(&points, 4) rect = sdl.Rect{300, 0, 200, 200} renderer.SetDrawColor(255, 0, 0, 255) renderer.DrawRect(&rect) rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}} renderer.SetDrawColor(0, 255, 255, 255) renderer.DrawRects(&rects, 2) rect = sdl.Rect{250, 250, 200, 200} renderer.SetDrawColor(0, 255, 0, 255) renderer.FillRect(&rect) rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}} renderer.SetDrawColor(255, 0, 255, 255) renderer.FillRects(&rects, 2) renderer.Present() sdl.Delay(2000) renderer.Destroy() window.Destroy() }
func main() { var window *sdl.Window var renderer *sdl.Renderer var image *sdl.Surface var texture *sdl.Texture var src, dst sdl.Rect 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) } image = sdl.LoadBMP(imageName) if image == nil { fmt.Fprintf(os.Stderr, "Failed to load BMP: %s", sdl.GetError()) os.Exit(3) } texture = renderer.CreateTextureFromSurface(image) if texture == nil { fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", sdl.GetError()) os.Exit(4) } src = sdl.Rect{0, 0, 512, 512} dst = sdl.Rect{100, 50, 512, 512} renderer.Clear() renderer.Copy(texture, &src, &dst) renderer.Present() sdl.Delay(2000) image.Free() texture.Destroy() renderer.Destroy() window.Destroy() }
func MainLoop(renderer *sdl.Renderer, font *ttf.Font) { lastTick := sdl.GetTicks() lastSecond := lastTick frames := 0 fpsText := gfx.CreateText(renderer, font, "FPS: ... "+StatusCallback(), sdl.Color{255, 255, 255, 255}) framesPerSecond := 0 for running { tick := sdl.GetTicks() if tick < lastTick+10 { sdl.Delay(1) continue } delta := float64(tick-lastTick) / 1000.0 lastTick = tick fpsText.Destroy() fpsText = gfx.CreateText(renderer, font, fmt.Sprintf("FPS: %3d ", framesPerSecond)+StatusCallback(), sdl.Color{255, 255, 255, 255}) if tick > (lastSecond + 1000) { lastSecond = tick framesPerSecond = frames frames = 0 } else { frames++ } handleEvents() UpdateCallback(delta) renderer.SetDrawColor(0, 0, 0, 0) renderer.Clear() RenderCallback(renderer) gfx.RenderTexture(renderer, fpsText, 0, 0) renderer.Present() } fpsText.Destroy() }
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) } running = true lastPushTime := sdl.GetTicks() for running { // Push a UserEvent every second if lastPushTime+pushTime < sdl.GetTicks() { lastPushTime = sdl.GetTicks() pEvent := &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 1331, nil, nil} retVal := sdl.PushEvent(pEvent) // Here's where the event is actually pushed switch retVal { case 1: fmt.Println("PushEvent returned success") case 0: fmt.Println("PushEvent returned filtered") case -1: fmt.Printf("PushEvent returned error: %s\n", sdl.GetError) } } 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) case *sdl.UserEvent: fmt.Printf("[%d ms] UserEvent\tcode:%d\n", t.Timestamp, t.Code) } } sdl.Delay(1000 / 30) } renderer.Destroy() window.Destroy() }
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, 2) peepArray[0] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 1331, nil, nil} peepArray[1] = &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), window.GetID(), 10101, nil, nil} running = true lastPushTime := sdl.GetTicks() for running { if lastPushTime+pushTime < sdl.GetTicks() { lastPushTime = sdl.GetTicks() sdl.PumpEvents() numEventsHandled := sdl.PeepEvents(peepArray, sdl.ADDEVENT, sdl.FIRSTEVENT, sdl.LASTEVENT) if numEventsHandled < 0 { fmt.Printf("PeepEvents error: %s\n", sdl.GetError()) } else { fmt.Printf("Successful push of %d events\n", numEventsHandled) } } 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) case *sdl.UserEvent: fmt.Printf("[%d ms] UserEvent\tcode:%d\n", t.Timestamp, t.Code) } } sdl.Delay(1000 / 30) } renderer.Destroy() window.Destroy() }
// DeltaSleep tells SDL to wait func (b *Base) DeltaSleep() { sdl.Delay(16) }