From 68ef5dcac56fb6c8f945e956f0a0bf11934144cb Mon Sep 17 00:00:00 2001 From: squishy Date: Sun, 24 Mar 2024 00:14:26 +0000 Subject: [PATCH] reorganised Run --- cursor-big.png => Run/cursor-big.png | Bin cursor.png => Run/cursor.png | Bin run.go => Run/run.go | 22 ++++++----------- gui/gui.go | 16 +++++++++++++ image.go | 34 +++++++++++++++++++-------- runsupport.go | 15 ++++++++++++ test/gui/main.go | 22 +++++++++++++++++ 7 files changed, 84 insertions(+), 25 deletions(-) rename cursor-big.png => Run/cursor-big.png (100%) rename cursor.png => Run/cursor.png (100%) rename run.go => Run/run.go (92%) create mode 100644 gui/gui.go create mode 100644 runsupport.go create mode 100644 test/gui/main.go diff --git a/cursor-big.png b/Run/cursor-big.png similarity index 100% rename from cursor-big.png rename to Run/cursor-big.png diff --git a/cursor.png b/Run/cursor.png similarity index 100% rename from cursor.png rename to Run/cursor.png diff --git a/run.go b/Run/run.go similarity index 92% rename from run.go rename to Run/run.go index 2735034..d3a4d43 100644 --- a/run.go +++ b/Run/run.go @@ -1,8 +1,10 @@ -package bloob +package run import ( _ "embed" "fmt" + . "git.danitheskunk.com/squishy/blooblib" + "git.danitheskunk.com/squishy/blooblib/gui" "github.com/veandco/go-sdl2/sdl" "time" ) @@ -13,20 +15,6 @@ type Runnable interface { 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" var defaultCursorBytes []byte @@ -51,6 +39,8 @@ func Run(game Runnable) { game.Init(&bloob, &settings) + gui.InitGui(settings.WindowSize) + window, _ := sdl.CreateWindow( settings.Title, sdl.WINDOWPOS_CENTERED, @@ -59,6 +49,7 @@ func Run(game Runnable) { int32(settings.WindowSize.Y*settings.Scale), sdl.WINDOW_SHOWN, ) + window.SetBordered(false) surface, _ := window.GetSurface() windowImage := NewImageFromPointer(surface.Data(), MulScalar(settings.WindowSize, settings.Scale)) screen := NewImage(settings.WindowSize) @@ -99,6 +90,7 @@ func Run(game Runnable) { if delta2 >= frameDuration { game.Update(&bloob) game.Render(&bloob, screen) + gui.DrawGui(screen) if bloob.Cursor != nil { screen.Draw(bloob.Cursor, bloob.MousePos) } diff --git a/gui/gui.go b/gui/gui.go new file mode 100644 index 0000000..dd5faf2 --- /dev/null +++ b/gui/gui.go @@ -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) { + +} diff --git a/image.go b/image.go index 03cdcac..6824b65 100644 --- a/image.go +++ b/image.go @@ -13,20 +13,22 @@ import ( ) type Image struct { - Data []uint32 + Data []Color Size Vec2i Alpha bool } +type Color = uint32 + var wg sync.WaitGroup var threadProfile = pprof.Lookup("threadcreate") 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 { - 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 { @@ -56,7 +58,7 @@ func LoadImageBytes(data []byte) *Image { 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 { image.Data[i] = color } @@ -89,6 +91,18 @@ func CropToArea(sizeDst Vec2i, rectSrc Recti, posDst Vec2i) (start, end, dst Vec 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) { 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) } -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) for y := start.Y; y < end.Y; y += 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 for _, ch := range str { 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 for _, ch := range str { 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 for _, ch := range str { 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 for _, ch := range str { c := font.chars[ch] diff --git a/runsupport.go b/runsupport.go new file mode 100644 index 0000000..fcce8c5 --- /dev/null +++ b/runsupport.go @@ -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 diff --git a/test/gui/main.go b/test/gui/main.go new file mode 100644 index 0000000..72fc0e7 --- /dev/null +++ b/test/gui/main.go @@ -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{}) +}