2024-03-23 07:26:56 +01:00
|
|
|
package bloob
|
|
|
|
|
|
|
|
import (
|
2024-03-23 10:12:55 +01:00
|
|
|
_ "embed"
|
2024-03-23 07:26:56 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Runnable interface {
|
|
|
|
Init(settings *Settings)
|
|
|
|
Render(screen *Image)
|
2024-03-23 09:41:02 +01:00
|
|
|
Update(bloob *Bloob)
|
2024-03-23 07:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Settings struct {
|
|
|
|
WindowSize Vec2i
|
|
|
|
Scale int
|
|
|
|
TargetFps int
|
|
|
|
Title string
|
|
|
|
}
|
|
|
|
|
2024-03-23 09:41:02 +01:00
|
|
|
type Bloob struct {
|
|
|
|
MousePos Vec2i
|
2024-03-23 10:12:55 +01:00
|
|
|
Cursor *Image
|
2024-03-23 09:41:02 +01:00
|
|
|
}
|
|
|
|
|
2024-03-23 10:12:55 +01:00
|
|
|
var DefaultCursor *Image
|
|
|
|
|
|
|
|
//go:embed "cursor.png"
|
|
|
|
var defaultCursorBytes []byte
|
|
|
|
|
2024-03-23 07:26:56 +01:00
|
|
|
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",
|
|
|
|
}
|
|
|
|
|
2024-03-23 10:12:55 +01:00
|
|
|
if DefaultCursor == nil {
|
|
|
|
DefaultCursor = LoadImageBytes(defaultCursorBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
bloob := Bloob{
|
|
|
|
Cursor: DefaultCursor,
|
|
|
|
}
|
2024-03-23 09:41:02 +01:00
|
|
|
|
2024-03-23 07:26:56 +01:00
|
|
|
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
|
|
|
|
|
2024-03-23 10:12:55 +01:00
|
|
|
sdl.ShowCursor(sdl.DISABLE)
|
|
|
|
|
2024-03-23 07:26:56 +01:00
|
|
|
_ = 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() {
|
2024-03-23 09:41:02 +01:00
|
|
|
switch event := event.(type) {
|
|
|
|
case sdl.QuitEvent:
|
2024-03-23 07:26:56 +01:00
|
|
|
running = false
|
|
|
|
break
|
2024-03-23 09:41:02 +01:00
|
|
|
case sdl.MouseMotionEvent:
|
|
|
|
bloob.MousePos.X = int(event.X) / settings.Scale
|
|
|
|
bloob.MousePos.Y = int(event.Y) / settings.Scale
|
|
|
|
break
|
2024-03-23 07:26:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
2024-03-23 09:41:02 +01:00
|
|
|
game.Update(&bloob)
|
2024-03-23 07:26:56 +01:00
|
|
|
game.Render(screen)
|
2024-03-23 10:12:55 +01:00
|
|
|
if bloob.Cursor != nil {
|
|
|
|
screen.Draw(bloob.Cursor, bloob.MousePos)
|
|
|
|
}
|
2024-03-23 07:26:56 +01:00
|
|
|
windowImage.DrawUpscale(screen)
|
|
|
|
framesSinceLastSecond += 1
|
|
|
|
lastFrame = lastFrame.Add(frameDuration)
|
|
|
|
}
|
|
|
|
_ = window.UpdateSurface()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|