diff --git a/go/Makefile b/go/Makefile new file mode 100644 index 0000000..542d42e --- /dev/null +++ b/go/Makefile @@ -0,0 +1,2 @@ +raylib_go: main.go + go build -o raylib_go main.go diff --git a/go/README.md b/go/README.md new file mode 100644 index 0000000..8c6a5ad --- /dev/null +++ b/go/README.md @@ -0,0 +1,8 @@ +# Raylib in Go + +## Quick start + +```sh +$ make +$ ./raylib_go +``` diff --git a/go/go_logo.png b/go/go_logo.png new file mode 100644 index 0000000..b4fa424 Binary files /dev/null and b/go/go_logo.png differ diff --git a/go/main.go b/go/main.go new file mode 100644 index 0000000..e43b17e --- /dev/null +++ b/go/main.go @@ -0,0 +1,66 @@ +package main + +/* +#cgo CFLAGS: -I../_raylib-5.5_linux_amd64/include +#cgo LDFLAGS: -L../_raylib-5.5_linux_amd64/lib -l:libraylib.a -lm +#include +*/ +import "C" +import "time" + +const ( + windowWidth = 800 + windowHeight = 600 + logoHeight = 64 + logoSpeed = 300.0 +) + +func main() { + C.InitWindow(windowWidth, windowHeight, C.CString("Raylib in Go")) + C.SetTargetFPS(60) + C.SetRandomSeed(C.uint(time.Now().Unix())) + + logoImage := C.LoadImage(C.CString("./go_logo.png")) + C.ImageResize(&logoImage, logoImage.width*logoHeight/logoImage.height, logoHeight) + logoTexture := C.LoadTextureFromImage(logoImage) + C.UnloadImage(logoImage) + + x := float32(C.GetRandomValue(0, windowWidth-C.int(logoTexture.width))) + y := float32(C.GetRandomValue(0, windowHeight-C.int(logoTexture.height))) + + dx := float32(logoSpeed) + if C.GetRandomValue(0, 1) == 1 { + dx = -dx + } + dy := float32(logoSpeed) + if C.GetRandomValue(0, 1) == 1 { + dy = -dy + } + + black := C.Color{0, 0, 0, 255} + white := C.Color{255, 255, 255, 255} + + for !bool(C.WindowShouldClose()) { + deltaTime := float32(C.GetFrameTime()) + + x += dx * deltaTime + y += dy * deltaTime + + if x < 0 || x+float32(logoTexture.width) >= windowWidth-1 { + dx *= -1 + x += dx * deltaTime + } + if y < 0 || y+float32(logoTexture.height) >= windowHeight-1 { + dy *= -1 + y += dy * deltaTime + } + + C.BeginDrawing() + C.ClearBackground(black) + C.DrawTextureV(logoTexture, C.Vector2{x: C.float(x), y: C.float(y)}, white) + C.EndDrawing() + } + + C.UnloadTexture(logoTexture) + C.CloseWindow() +}