Files
raylib-speedruns/languages/go/main.go

67 lines
1.5 KiB
Go

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 <raylib.h>
*/
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()
}