func drawObstacles(r *sdl.Renderer) { r.SetDrawColor(255, 128, 100, 255) for _, o := range obstacles { rect := sdl.Rect{int32(o.X - o.Size), int32(o.Y - o.Size), int32(o.Size * 2), int32(o.Size * 2)} r.DrawRect(&rect) } }
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 CreateText(renderer *sdl.Renderer, font *ttf.Font, message string, color sdl.Color) *sdl.Texture { surf := font.RenderText_Blended(message, color) if surf == nil { log.Println("Error when rendering text") return nil } defer surf.Free() return renderer.CreateTextureFromSurface(surf) }
func drawShip(r *sdl.Renderer) { ship := make([]sdl.Point, 4) for i := 0; i < 4; i++ { p := shipPoints[i%3] ship[i] = sdl.Point{p.X + int32(x), p.Y + int32(y)} } r.SetDrawColor(255, 255, 255, 255) err := r.DrawLines(ship) if err != 0 { error("Draw error") } }
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 for running { event = sdl.WaitEventTimeout(1000) // wait here until an event is in the event queue if event == nil { fmt.Println("WaitEventTimeout timed out") continue } 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) } } 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 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 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 drawMissiles(r *sdl.Renderer) { for _, p := range missiles { r.DrawPoint(int(p.X), int(p.Y)) } }
func RenderTexture(renderer *sdl.Renderer, texture *sdl.Texture, x, y int) { var w, h int sdl.QueryTexture(texture, nil, nil, &w, &h) dst := sdl.Rect{int32(x), int32(y), int32(w), int32(h)} renderer.Copy(texture, nil, &dst) }
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() }