From d01ce8210675a01b11d3d33686bbfb98f2079d2e Mon Sep 17 00:00:00 2001 From: squishy Date: Sat, 23 Mar 2024 06:26:56 +0000 Subject: [PATCH] removed test code, and restructured to use as library --- build.bat | 5 - {src/color => color}/color.go | 0 src/font-thin.png => font-thin.png | Bin src/font.go => font.go | 0 src/font.png => font.png | Bin go.aseprite | Bin 1327 -> 0 bytes go.png | Bin 986 -> 0 bytes src/image.go => image.go | 0 main.go | 156 ----------------------------- src/math.go => math.go | 0 src/rectangle.go => rectangle.go | 0 run.go | 85 ++++++++++++++++ src/tilemap.go => tilemap.go | 0 src/vec2.go => vec2.go | 0 14 files changed, 85 insertions(+), 161 deletions(-) delete mode 100644 build.bat rename {src/color => color}/color.go (100%) rename src/font-thin.png => font-thin.png (100%) rename src/font.go => font.go (100%) rename src/font.png => font.png (100%) delete mode 100644 go.aseprite delete mode 100644 go.png rename src/image.go => image.go (100%) delete mode 100644 main.go rename src/math.go => math.go (100%) rename src/rectangle.go => rectangle.go (100%) create mode 100644 run.go rename src/tilemap.go => tilemap.go (100%) rename src/vec2.go => vec2.go (100%) diff --git a/build.bat b/build.bat deleted file mode 100644 index 4cf5237..0000000 --- a/build.bat +++ /dev/null @@ -1,5 +0,0 @@ -set CGO_ENABLED=1 -set CC=x86_64-w64-mingw32-gcc -set GOOS=windows -set GOARCH=amd64 -go build -tags static -ldflags "-s -w" \ No newline at end of file diff --git a/src/color/color.go b/color/color.go similarity index 100% rename from src/color/color.go rename to color/color.go diff --git a/src/font-thin.png b/font-thin.png similarity index 100% rename from src/font-thin.png rename to font-thin.png diff --git a/src/font.go b/font.go similarity index 100% rename from src/font.go rename to font.go diff --git a/src/font.png b/font.png similarity index 100% rename from src/font.png rename to font.png diff --git a/go.aseprite b/go.aseprite deleted file mode 100644 index 57c616e0ce2fa00428938bb012429753db7d4a6c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1327 zcmdr~ZA@Eb6u#ZqYWQKN;xMSJ8C^n#wK7o0NNE{MP&%0@>l`rY7$8Ce=t>nv+Dqal z##x&1VfTS+6X=*TO--QI!r*SfS&n1O&kY2^+34i0q6NmA+xQ z$?oSEPKa;E-M;;aq#e)XChrj^Gl%JW?RjZONaR>8)3j)Gw!; z$z$+ypQh83va8ci{J>Bx(JiK{Q5%F1tlh*d-><8cu6pr}qRXuZ+PPUJjNRG0Ur5=P zk^W(R?x>{DHc%Bb_buF888ZHIzFXHIkry33m6el!kdalMxxbUoACc6%_5F?q(1;Od zFE1aLj~7bMrPUViJ6rv(PTsXRG9CVN*?isf?RoX5r&?u9^w@djYZ-0<@==QTN z;}+)|DGWwp*_oYBO?K7HdjiGpp%pV(=Cp**yQgchA#`}kVI}6O&L#h$|A_GUDvpsd zkqMG(><1I0h9jtD5djH7Zi?^1Xs2r3GSy7pW;FVvRno!sWzh;W1{`q zgv@tFuwmoHJ9?~!c0q*J(XM$7p%yYcW+pXnIWwEVn$hS|$3E$PJl>!~C(lw)Xj?x|Blxhf(b{P!*s0}OgIJ0~lw{n&TbSPO+{O@qMgovz(&?VVkGC~9+pY(Kj zun_oIPM|-{V!*8g>qoGl4#hPnImaSBD%&w4643iGi_kk`<(%-%DVqdW98i5N031#M zvkCP^%}&+0TL^|2Ah$Dj9q0eb$T-d#;x{2T7Z BEY<)3 diff --git a/go.png b/go.png deleted file mode 100644 index 0808bf57fbfac0a8948c70ff5a9c28182f3c036e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 986 zcmeAS@N?(olHy`uVBq!ia0vp^4Is?H1|$#LC7uRSjKx9jP7LeL$-HD>V4mXX;uumf z=k4A7*}{o3$3H$-5wWtmve0?L59Wp0|C{EizHvatd%=KAv%<=nLj}KhA!4^=y zl=Z{Q*WYK(KeO9B|A}4nop(9sa_r~vXxrX4xwzG6o=NVHZMT;%<-K2K-Nh~EVDsea zRMXA7HchLnpH^{V`FsDh-<5k7FsiiM>q@$Wv7CHZzI~VR_v3*Ajur=*Jf!~}eY*4W zN5ktMdJV(#;$2vzcdLj0%Uu*?$1GuN{ipkMakjw5H4pV~eYr4O%JD-!V`ZC6(7b~! zQgOFF|M`^|&(5(ie|{;?bwi!MRt>L@YVH!4@@HSc9TQ~he)>6Lb; zzi>bQ9UKvUkV(S7E%4sLGN2>6tv>60(n$)`kgE!A&-Z!2-&m>SAzfGWd(Uspf(j48 zKhX`kOt(KwzWw`7|IaC(6hG9nv|j&od~)KEtqn&t-bOdP-yl(D$9~&A?zT1ahT7g& z{BF-#tvnMLKcNSh9g^R^v(4iiL2)PE{N?f&t1PR^qEtu;lKJBf=*MfKYrb}9jZ>S z!GcNPI?%SMat-hQ^_MfrcC2`Da`W}L>!0`Ut&(~b!S!?R&0@P{ci*MldtUwL^D>gTe~DWM4fD%Ydr diff --git a/src/image.go b/image.go similarity index 100% rename from src/image.go rename to image.go diff --git a/main.go b/main.go deleted file mode 100644 index 23744be..0000000 --- a/main.go +++ /dev/null @@ -1,156 +0,0 @@ -package main - -import ( - _ "embed" - "fmt" - . "git.danitheskunk.com/squishy/blooblib/src" - "git.danitheskunk.com/squishy/blooblib/src/color" - "github.com/veandco/go-sdl2/sdl" - "time" -) - -const Width = 640 -const Height = 360 - -//test - -func main() { - _ = sdl.Init(sdl.INIT_EVERYTHING) - defer sdl.Quit() - - scale := 3 - - window, _ := sdl.CreateWindow( - "test", - sdl.WINDOWPOS_CENTERED, - sdl.WINDOWPOS_CENTERED, - int32(Width*scale), - int32(Height*scale), - sdl.WINDOW_SHOWN, - ) - surface, _ := window.GetSurface() - //data := unsafe.Slice((*uint32)(surface.Data()), Width*Height) - windowImage := NewImageFromPointer(surface.Data(), Vec2i{X: Width * scale, Y: Height * scale}) - screen := NewImage(Vec2i{X: Width, Y: Height}) - screen.Alpha = false - //sprite := NewImage(Vec2i{X: 32, Y: 32}) - //sprite.Clear(0xabcdef) - fmt.Printf("%d\n", surface.Format.BitsPerPixel) - println("\n"[0]) - - tiles := LoadImage("go.png").MakeTileset(Vec2i{X: 16, Y: 16}) - tilemap := NewTilemap(Vec2i{X: 40, Y: 23}, tiles) - tilemap.Clear(16) - for y := 0; y < 19; y += 1 { - for x := 0; x < 19; x += 1 { - tilemap.Set(Vec2i{X: 10 + x, Y: 1 + y}, 3) - } - //tilemap.Set(Vec2i{X: 9, Y: 1 + y}, 17) - tilemap.Set(Vec2i{X: 10, Y: 1 + y}, 11) - tilemap.Set(Vec2i{X: 28, Y: 1 + y}, 12) - } - - for x := 0; x < 19; x += 1 { - //tilemap.Set(Vec2i{X: 10 + x, Y: 0}, 18) - tilemap.Set(Vec2i{X: 10 + x, Y: 1}, 9) - tilemap.Set(Vec2i{X: 10 + x, Y: 19}, 10) - tilemap.Set(Vec2i{X: 10 + x, Y: 20}, 17) - tilemap.Set(Vec2i{X: 10 + x, Y: 21}, 18) - } - - for y := 0; y < 21; y += 1 { - tilemap.Set(Vec2i{X: 29, Y: 1 + y}, 19) - } - tilemap.Set(Vec2i{X: 10, Y: 21}, 20) - tilemap.Set(Vec2i{X: 29, Y: 1}, 21) - - tilemap.Set(Vec2i{X: 10, Y: 1}, 5) - tilemap.Set(Vec2i{X: 28, Y: 1}, 6) - tilemap.Set(Vec2i{X: 10, Y: 19}, 7) - tilemap.Set(Vec2i{X: 28, Y: 19}, 8) - - tilemap.Set(Vec2i{X: 13, Y: 4}, 4) - tilemap.Set(Vec2i{X: 25, Y: 4}, 4) - tilemap.Set(Vec2i{X: 13, Y: 16}, 4) - tilemap.Set(Vec2i{X: 25, Y: 16}, 4) - tilemap.Set(Vec2i{X: 13, Y: 10}, 4) - tilemap.Set(Vec2i{X: 13, Y: 16}, 4) - tilemap.Set(Vec2i{X: 25, Y: 10}, 4) - tilemap.Set(Vec2i{X: 25, Y: 16}, 4) - tilemap.Set(Vec2i{X: 19, Y: 4}, 4) - tilemap.Set(Vec2i{X: 19, Y: 10}, 4) - tilemap.Set(Vec2i{X: 19, Y: 16}, 4) - - _ = window.UpdateSurface() - - lastSecond := time.Now() - framesSinceLastSecond := 0 - - targetFPS := 60 - frameDuration := time.Second / time.Duration(targetFPS) - lastFrame := lastSecond - - running := true - tick := 0 - 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) - //println(delta.Seconds()) - //println(now.Second()) - 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 { - - //t := (float64(tick)) / 1000 - screen.Clear(0x123456) - screen.DrawTextOutline( - // "Hello World, this is a test,\nand now for another line :)", - // "Hello World, this is a test", - // "Vigilance, deathtouch, haste\n\nQuesting Beast can't be blocked by creatures\nwith power 2 or less.\n\nCombat damage that would be dealt by\ncreatures you control can't be prevented.\n\nWhenever Questing Beast deals combat damage\nto an opponent, it deals that much damage to\ntarget planeswalker that player controls.", - "Lorem ipsum dolor sit amet,\n"+ - "consectetur adipiscing elit,\n"+ - "sed do eiusmod tempor incididunt\n"+ - "ut labore et dolore magna aliqua.\n"+ - "Ut enim ad minim veniam, quis\n"+ - "nostrud exercitation ullamco\n"+ - "laboris nisi ut aliquip ex ea\n"+ - "commodo consequat. Duis aute\n"+ - "irure dolor in reprehenderit in\n"+ - "voluptate velit esse cillum dolore\n"+ - "eu fugiat nulla pariatur.\n"+ - "Excepteur sint occaecat cupidatat\n"+ - "non proident, sunt in culpa qui\n"+ - "officia deserunt mollit anim id\n"+ - "est laborum.", - DefaultFont, - Vec2i{X: 10, Y: 10}, - 0xffdddd, - color.Black, - // 50, - // 3.0, - // float64(tick) / 60.0, - ) - screen.DrawTilemap(tilemap, Vec2i{}) - screen.Draw(tilemap.Tileset[2], Vec2i{X: 16 * 25, Y: 16 * 4}) - screen.Draw(tilemap.Tileset[1], Vec2i{X: 16 * 13, Y: 16 * 16}) - windowImage.DrawUpscale(screen) - //windowImage.Draw(screen, Vec2i{}) - tick += 1 - framesSinceLastSecond += 1 - lastFrame = lastFrame.Add(frameDuration) - } - _ = window.UpdateSurface() - } -} diff --git a/src/math.go b/math.go similarity index 100% rename from src/math.go rename to math.go diff --git a/src/rectangle.go b/rectangle.go similarity index 100% rename from src/rectangle.go rename to rectangle.go diff --git a/run.go b/run.go new file mode 100644 index 0000000..632c46f --- /dev/null +++ b/run.go @@ -0,0 +1,85 @@ +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() + } + +} diff --git a/src/tilemap.go b/tilemap.go similarity index 100% rename from src/tilemap.go rename to tilemap.go diff --git a/src/vec2.go b/vec2.go similarity index 100% rename from src/vec2.go rename to vec2.go