86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
|
package bloob
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/veandco/go-sdl2/sdl"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Runnable interface {
|
||
|
Init(settings *Settings)
|
||
|
Render(screen *Image)
|
||
|
Update()
|
||
|
}
|
||
|
|
||
|
type Settings struct {
|
||
|
WindowSize Vec2i
|
||
|
Scale int
|
||
|
TargetFps int
|
||
|
Title string
|
||
|
}
|
||
|
|
||
|
func Run(game Runnable) {
|
||
|
_ = sdl.Init(sdl.INIT_EVERYTHING)
|
||
|
defer sdl.Quit()
|
||
|
|
||
|
settings := Settings{
|
||
|
WindowSize: Vec2i{X: 640, Y: 360},
|
||
|
Scale: 3,
|
||
|
TargetFps: 60,
|
||
|
Title: "Blooblib",
|
||
|
}
|
||
|
|
||
|
game.Init(&settings)
|
||
|
|
||
|
window, _ := sdl.CreateWindow(
|
||
|
settings.Title,
|
||
|
sdl.WINDOWPOS_CENTERED,
|
||
|
sdl.WINDOWPOS_CENTERED,
|
||
|
int32(settings.WindowSize.X*settings.Scale),
|
||
|
int32(settings.WindowSize.Y*settings.Scale),
|
||
|
sdl.WINDOW_SHOWN,
|
||
|
)
|
||
|
surface, _ := window.GetSurface()
|
||
|
windowImage := NewImageFromPointer(surface.Data(), MulScalar(settings.WindowSize, settings.Scale))
|
||
|
screen := NewImage(settings.WindowSize)
|
||
|
screen.Alpha = false
|
||
|
|
||
|
_ = window.UpdateSurface()
|
||
|
|
||
|
lastSecond := time.Now()
|
||
|
framesSinceLastSecond := 0
|
||
|
|
||
|
targetFPS := 60
|
||
|
frameDuration := time.Second / time.Duration(targetFPS)
|
||
|
lastFrame := lastSecond
|
||
|
|
||
|
running := true
|
||
|
for running {
|
||
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||
|
switch event.(type) {
|
||
|
case *sdl.QuitEvent:
|
||
|
running = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
now := time.Now()
|
||
|
delta := now.Sub(lastSecond)
|
||
|
if delta.Seconds() >= 1.0 {
|
||
|
fmt.Printf("FPS: %5d | mspf: %f\n", framesSinceLastSecond, 1000.0/float32(framesSinceLastSecond))
|
||
|
framesSinceLastSecond = 0
|
||
|
lastSecond = lastSecond.Add(time.Second)
|
||
|
}
|
||
|
|
||
|
delta2 := now.Sub(lastFrame)
|
||
|
if delta2 >= frameDuration {
|
||
|
game.Update()
|
||
|
game.Render(screen)
|
||
|
windowImage.DrawUpscale(screen)
|
||
|
framesSinceLastSecond += 1
|
||
|
lastFrame = lastFrame.Add(frameDuration)
|
||
|
}
|
||
|
_ = window.UpdateSurface()
|
||
|
}
|
||
|
|
||
|
}
|