Harbor

branch main
showing the latest snapshot on main
main.odin 1.7 KB · Plain text
examples/textured_quad/main.odin 0644 Raw
package main

import "core:fmt"
import gpu "../.."
import app "../../app"

// Generate a 64x64 checkerboard texture (magenta/white)
gen_checkerboard :: proc() -> gpu.Texture {
	W :: 64
	pixels: [W * W * 4]u8
	for y in 0..<W {
		for x in 0..<W {
			i := (y * W + x) * 4
			dark := ((x / 8) + (y / 8)) % 2 == 0
			if dark {
				pixels[i + 0] = 255 // R
				pixels[i + 1] = 0   // G
				pixels[i + 2] = 255 // B
				pixels[i + 3] = 255 // A
			} else {
				pixels[i + 0] = 255
				pixels[i + 1] = 255
				pixels[i + 2] = 255
				pixels[i + 3] = 255
			}
		}
	}
	return gpu.load_texture_from_rgba(pixels[:], W, W)
}

main :: proc() {
	state, ok := app.init_window(800, 600, "GPU - Textured Quad")
	if !ok {
		return
	}
	defer app.shutdown(&state)

	// Try loading from file, fall back to generated checkerboard
	tex := gpu.load_texture("tests/assets/test.png")
	if tex.id == 0 {
		fmt.println("PNG load failed, using generated checkerboard")
		tex = gen_checkerboard()
	} else {
		fmt.printfln("Loaded tests/assets/test.png: %dx%d, id=%d", tex.width, tex.height, tex.id)
	}
	defer gpu.unload_texture(tex)

	fmt.printfln("Texture: id=%d, %dx%d", tex.id, tex.width, tex.height)

	for !app.window_should_close(&state) {
		frame := app.begin_frame(&state, gpu.CORNFLOWER_BLUE)

		// Draw textured quad
		if tex.id != 0 {
			_ = gpu.draw_texture_quad(&frame, {
				texture = tex,
				x = 100,
				y = 100,
				color = gpu.WHITE,
			})
		}

		// Draw colored rectangle alongside (proves unified pipeline)
		_ = gpu.draw_rect(&frame, {x = 500, y = 200, width = 150, height = 100, color = gpu.RED})
		_ = gpu.draw_circle_frame(&frame, {cx = 600, cy = 400, radius = 50, color = gpu.GREEN})

		_ = app.submit_frame(&state, &frame)

		if app.is_key_pressed(&state, .Escape) {
			break
		}
	}
}