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 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 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 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() }