Harbor

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

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

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

	config := particles.default_config()
	config.max_particles = 500
	config.emission_rate = 100
	config.start_color = gpu.ORANGE
	config.end_color = {1, 0, 0, 0}
	config.gravity = {0, 200}
	config.speed = {80, 180}
	config.spread = 45

	ps, ps_ok := particles.init(config)
	defer particles.destroy(&ps)

	if !ps_ok {
		fmt.println("Failed to create particle system")
		return
	}

	// Second system for click bursts
	burst_config := particles.default_config()
	burst_config.max_particles = 200
	burst_config.emission_rate = 0 // burst-only
	burst_config.start_color = gpu.CYAN
	burst_config.end_color = {0, 0.5, 1, 0}
	burst_config.speed = {100, 250}
	burst_config.spread = 180 // omnidirectional
	burst_config.gravity = {0, 100}
	burst_config.start_size = 5
	burst_config.end_size = 1

	burst, burst_ok := particles.init(burst_config)
	defer particles.destroy(&burst)
	if !burst_ok {
		fmt.println("Failed to create burst particle system")
		return
	}

	for !app.window_should_close(&state) {
		mouse := app.get_mouse_position(&state)

		// Continuous emission follows mouse
		particles.update(&ps, mouse, app.get_frame_time(&state))

		// Burst on click
		particles.update(&burst, mouse, app.get_frame_time(&state))
		if app.is_mouse_button_pressed(&state, .Left) {
			particles.emit(&burst, mouse, 50)
		}

		frame := app.begin_frame(&state, {0.05, 0.05, 0.08, 1.0})
		_ = gpu.push_layer(&frame, {name = "particles", sort_base = 0, order = .Sortable})
		particles.draw_2d(&frame, &ps, .Sortable, 0)
		particles.draw_2d(&frame, &burst, .Sortable, 10_000)
		_ = gpu.pop_layer(&frame)

		_ = gpu.push_layer(&frame, {name = "overlay", sort_base = 100_000, order = .Strict})
		_ = gpu.draw_rect(&frame, {x = 5, y = 5, width = 80, height = 20, color = {0, 0, 0, 0.5}, order = .Strict})
		gpu.draw_text_frame(&frame, {
			text = fmt.tprintf("FPS: %d", app.get_fps(&state)),
			x = 10,
			y = 9,
			color = gpu.WHITE,
			scale = 2,
			order = .Strict,
			sort_key = 1,
		})
		_ = gpu.pop_layer(&frame)
		_ = app.submit_frame(&state, &frame)

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