reorganised Run

This commit is contained in:
Squishy Bloob 2024-03-24 00:14:26 +00:00
parent 6531d191e2
commit 68ef5dcac5
7 changed files with 84 additions and 25 deletions

View File

Before

Width:  |  Height:  |  Size: 128 B

After

Width:  |  Height:  |  Size: 128 B

View File

Before

Width:  |  Height:  |  Size: 128 B

After

Width:  |  Height:  |  Size: 128 B

View File

@ -1,8 +1,10 @@
package bloob package run
import ( import (
_ "embed" _ "embed"
"fmt" "fmt"
. "git.danitheskunk.com/squishy/blooblib"
"git.danitheskunk.com/squishy/blooblib/gui"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
"time" "time"
) )
@ -13,20 +15,6 @@ type Runnable interface {
Update(bloob *Bloob) Update(bloob *Bloob)
} }
type Settings struct {
WindowSize Vec2i
Scale int
TargetFps int
Title string
}
type Bloob struct {
MousePos Vec2i
Cursor *Image
}
var DefaultCursor *Image
//go:embed "cursor.png" //go:embed "cursor.png"
var defaultCursorBytes []byte var defaultCursorBytes []byte
@ -51,6 +39,8 @@ func Run(game Runnable) {
game.Init(&bloob, &settings) game.Init(&bloob, &settings)
gui.InitGui(settings.WindowSize)
window, _ := sdl.CreateWindow( window, _ := sdl.CreateWindow(
settings.Title, settings.Title,
sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
@ -59,6 +49,7 @@ func Run(game Runnable) {
int32(settings.WindowSize.Y*settings.Scale), int32(settings.WindowSize.Y*settings.Scale),
sdl.WINDOW_SHOWN, sdl.WINDOW_SHOWN,
) )
window.SetBordered(false)
surface, _ := window.GetSurface() surface, _ := window.GetSurface()
windowImage := NewImageFromPointer(surface.Data(), MulScalar(settings.WindowSize, settings.Scale)) windowImage := NewImageFromPointer(surface.Data(), MulScalar(settings.WindowSize, settings.Scale))
screen := NewImage(settings.WindowSize) screen := NewImage(settings.WindowSize)
@ -99,6 +90,7 @@ func Run(game Runnable) {
if delta2 >= frameDuration { if delta2 >= frameDuration {
game.Update(&bloob) game.Update(&bloob)
game.Render(&bloob, screen) game.Render(&bloob, screen)
gui.DrawGui(screen)
if bloob.Cursor != nil { if bloob.Cursor != nil {
screen.Draw(bloob.Cursor, bloob.MousePos) screen.Draw(bloob.Cursor, bloob.MousePos)
} }

16
gui/gui.go Normal file
View File

@ -0,0 +1,16 @@
package gui
import . "git.danitheskunk.com/squishy/blooblib"
type gui struct {
}
var Gui gui
func InitGui(size Vec2i) {
Gui = gui{}
}
func DrawGui(target *Image) {
}

View File

@ -13,20 +13,22 @@ import (
) )
type Image struct { type Image struct {
Data []uint32 Data []Color
Size Vec2i Size Vec2i
Alpha bool Alpha bool
} }
type Color = uint32
var wg sync.WaitGroup var wg sync.WaitGroup
var threadProfile = pprof.Lookup("threadcreate") var threadProfile = pprof.Lookup("threadcreate")
func NewImage(size Vec2i) *Image { func NewImage(size Vec2i) *Image {
return &Image{Data: make([]uint32, size.Size()), Size: Vec2i{X: size.X, Y: size.Y}, Alpha: true} return &Image{Data: make([]Color, size.Size()), Size: Vec2i{X: size.X, Y: size.Y}, Alpha: true}
} }
func NewImageFromPointer(pointer unsafe.Pointer, size Vec2i) *Image { func NewImageFromPointer(pointer unsafe.Pointer, size Vec2i) *Image {
return &Image{Data: unsafe.Slice((*uint32)(pointer), size.Size()), Size: Vec2i{X: size.X, Y: size.Y}, Alpha: true} return &Image{Data: unsafe.Slice((*Color)(pointer), size.Size()), Size: Vec2i{X: size.X, Y: size.Y}, Alpha: true}
} }
func LoadImage(path string) *Image { func LoadImage(path string) *Image {
@ -56,7 +58,7 @@ func LoadImageBytes(data []byte) *Image {
return img return img
} }
func (image *Image) Clear(color uint32) { func (image *Image) Clear(color Color) {
for i := 0; i < image.Size.X*image.Size.Y; i += 1 { for i := 0; i < image.Size.X*image.Size.Y; i += 1 {
image.Data[i] = color image.Data[i] = color
} }
@ -89,6 +91,18 @@ func CropToArea(sizeDst Vec2i, rectSrc Recti, posDst Vec2i) (start, end, dst Vec
return return
} }
func (image *Image) DrawHLine(pos Vec2i, length int, color Color) {
for i := 0; i < length; i += 1 {
image.Data[pos.X+i+pos.Y*image.Size.X] = color
}
}
func (image *Image) DrawVLine(pos Vec2i, length int, color Color) {
for i := 0; i < length; i += 1 {
image.Data[pos.X+(pos.Y+i)*image.Size.X] = color
}
}
func (image *Image) Draw(other *Image, dst Vec2i) { func (image *Image) Draw(other *Image, dst Vec2i) {
image.DrawSub(other, dst, Recti{Size: other.Size}) image.DrawSub(other, dst, Recti{Size: other.Size})
} }
@ -116,11 +130,11 @@ func (image *Image) DrawSub(other *Image, pos Vec2i, rect Recti) {
} }
} }
func (image *Image) DrawColor(other *Image, dst Vec2i, color uint32) { func (image *Image) DrawColor(other *Image, dst Vec2i, color Color) {
image.DrawSubColor(other, dst, Recti{Size: other.Size}, color) image.DrawSubColor(other, dst, Recti{Size: other.Size}, color)
} }
func (image *Image) DrawSubColor(other *Image, pos Vec2i, rect Recti, color uint32) { func (image *Image) DrawSubColor(other *Image, pos Vec2i, rect Recti, color Color) {
start, end, dst := CropToArea(image.Size, rect, pos) start, end, dst := CropToArea(image.Size, rect, pos)
for y := start.Y; y < end.Y; y += 1 { for y := start.Y; y < end.Y; y += 1 {
for x := start.X; x < end.X; x += 1 { for x := start.X; x < end.X; x += 1 {
@ -250,7 +264,7 @@ func (image *Image) DrawUpscale(other *Image) {
} }
} }
func (image *Image) DrawText(str string, font *Font, pos Vec2i, color uint32) { func (image *Image) DrawText(str string, font *Font, pos Vec2i, color Color) {
origX := pos.X origX := pos.X
for _, ch := range str { for _, ch := range str {
c := font.chars[ch] c := font.chars[ch]
@ -264,7 +278,7 @@ func (image *Image) DrawText(str string, font *Font, pos Vec2i, color uint32) {
} }
} }
func (image *Image) DrawTextSin(str string, font *Font, pos Vec2i, color uint32, freq, amplitude, phase float64) { func (image *Image) DrawTextSin(str string, font *Font, pos Vec2i, color Color, freq, amplitude, phase float64) {
origX := pos.X origX := pos.X
for _, ch := range str { for _, ch := range str {
c := font.chars[ch] c := font.chars[ch]
@ -278,7 +292,7 @@ func (image *Image) DrawTextSin(str string, font *Font, pos Vec2i, color uint32,
} }
} }
func (image *Image) DrawTextOutline(str string, font *Font, pos Vec2i, color, outlineColor uint32) { func (image *Image) DrawTextOutline(str string, font *Font, pos Vec2i, color, outlineColor Color) {
origX := pos.X origX := pos.X
for _, ch := range str { for _, ch := range str {
c := font.chars[ch] c := font.chars[ch]
@ -305,7 +319,7 @@ func (image *Image) DrawTextOutline(str string, font *Font, pos Vec2i, color, ou
} }
} }
func (image *Image) DrawTextOutlineSin(str string, font *Font, pos Vec2i, color, outlineColor uint32, freq, amplitude, phase float64) { func (image *Image) DrawTextOutlineSin(str string, font *Font, pos Vec2i, color, outlineColor Color, freq, amplitude, phase float64) {
origX := pos.X origX := pos.X
for _, ch := range str { for _, ch := range str {
c := font.chars[ch] c := font.chars[ch]

15
runsupport.go Normal file
View File

@ -0,0 +1,15 @@
package bloob
type Settings struct {
WindowSize Vec2i
Scale int
TargetFps int
Title string
}
type Bloob struct {
MousePos Vec2i
Cursor *Image
}
var DefaultCursor *Image

22
test/gui/main.go Normal file
View File

@ -0,0 +1,22 @@
package main
import (
bloob "git.danitheskunk.com/squishy/blooblib"
run "git.danitheskunk.com/squishy/blooblib/Run"
)
type Game struct {
}
func (g Game) Init(bloob *bloob.Bloob, settings *bloob.Settings) {
}
func (g Game) Render(bloob *bloob.Bloob, screen *bloob.Image) {
}
func (g Game) Update(bloob *bloob.Bloob) {
}
func main() {
run.Run(&Game{})
}