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