package main

import "core:time"

foreign import raylib {
	"../_raylib-5.5_linux_amd64/lib/libraylib.a",
	"system:m",
}

Image :: struct {
	data:    rawptr,
	width:   i32,
	height:  i32,
	mipmaps: i32,
	format:  i32,
}

Texture2D :: struct {
	id:      u32,
	width:   i32,
	height:  i32,
	mipmaps: i32,
	format:  i32,
}

Vector2 :: struct {
	x: f32,
	y: f32,
}

Color :: struct {
	r: u8,
	g: u8,
	b: u8,
	a: u8,
}

@(default_calling_convention = "c")
foreign raylib {
	InitWindow           :: proc(width: i32, height: i32, title: cstring) ---
	CloseWindow          :: proc() ---
	SetTargetFPS         :: proc(fps: i32) ---
	SetRandomSeed        :: proc(seed: u32) ---
	GetRandomValue       :: proc(min: i32, max: i32) -> i32 ---
	LoadImage            :: proc(file_name: cstring) -> Image ---
	ImageResize          :: proc(image: ^Image, new_width: i32, new_height: i32) ---
	LoadTextureFromImage :: proc(image: Image) -> Texture2D ---
	UnloadImage          :: proc(image: Image) ---
	UnloadTexture        :: proc(texture: Texture2D) ---
	WindowShouldClose    :: proc() -> bool ---
	GetFrameTime         :: proc() -> f32 ---
	BeginDrawing         :: proc() ---
	ClearBackground      :: proc(color: Color) ---
	DrawTextureV         :: proc(texture: Texture2D, position: Vector2, tint: Color) ---
	EndDrawing           :: proc() ---
}

WINDOW_WIDTH  :: 800
WINDOW_HEIGHT :: 600
LOGO_HEIGHT   :: 64
LOGO_SPEED    :: 300

BLACK :: Color{0, 0, 0, 255}
WHITE :: Color{255, 255, 255, 255}

main :: proc() {
	InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Raylib in Odin")
	SetTargetFPS(60)
	SetRandomSeed(cast(u32)time.now()._nsec)

	logo_image := LoadImage("./odin_logo.png")
	ImageResize(&logo_image, logo_image.width * LOGO_HEIGHT / logo_image.height, LOGO_HEIGHT)
	logo_texture := LoadTextureFromImage(logo_image)
	UnloadImage(logo_image)

	x := f32(GetRandomValue(0, WINDOW_WIDTH - logo_texture.width))
	y := f32(GetRandomValue(0, WINDOW_HEIGHT - logo_texture.height))

	dx: f32 = LOGO_SPEED
	if GetRandomValue(0, 1) == 1 do dx = -dx
	dy: f32 = LOGO_SPEED
	if GetRandomValue(0, 1) == 1 do dy = -dy

	for !WindowShouldClose() {
		delta_time := GetFrameTime()

		x += dx * delta_time
		y += dy * delta_time

		if x < 0 || x + f32(logo_texture.width) >= WINDOW_WIDTH - 1 {
			dx *= -1
			x += dx * delta_time
		}
		if y < 0 || y + f32(logo_texture.height) >= WINDOW_HEIGHT - 1 {
			dy *= -1
			y += dy * delta_time
		}

		BeginDrawing()
		ClearBackground(BLACK)
		DrawTextureV(logo_texture, Vector2{x, y}, WHITE)
		EndDrawing()
	}

	UnloadTexture(logo_texture)
	CloseWindow()
}
