zig-raylib/src/main.zig
2024-12-10 15:41:50 +01:00

101 lines
3.5 KiB
Zig

const std = @import("std");
const rl = @import("raylib");
const rg = @import("raygui");
const pr = @import("primitives.zig");
const particles = @import("particles.zig");
const plot = @import("plot.zig");
const allocator = std.heap.page_allocator;
const screenWidth = 1200;
const screenHeight = 800;
fn ItoF(x: i32) f32 {
return @as(f32, @floatFromInt(x));
}
fn rectangle(x: u16, y: u16, width: u16, height: u16) rl.Rectangle {
return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) };
}
pub fn main() anyerror!void {
var value: f32 = 0;
var value2: f32 = 0;
var value3: f32 = 0;
var color: rl.Color = undefined;
var emitter = particles.Emitter{};
try emitter.init(allocator, screenWidth, screenHeight);
//const img : rl.Texture = rl.loadTexture("img.png");
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
defer rl.closeWindow(); // Close window and OpenGL context
var texture: rl.Texture2D = undefined;
const image: rl.Image = rl.loadImage("resources/blut.png");
if (image.width > 0) {
texture = rl.loadTextureFromImage(image);
} else {
std.debug.print("error opening resource", .{});
std.posix.exit(1);
}
rl.unloadImage(image);
rl.setTargetFPS(60);
const file = try plot.open("plot.dat");
var plotStr: []u8 = undefined;
var plotStrBuf: [128]u8 = undefined;
try plot.log(file, "acceleration, position, velocity, show\n");
var att: [10]particles.Attractor = undefined;
var active: i32 = 0;
var editMode: bool = false;
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(rl.Color{
.r = 0,
.g = 0,
.b = 0,
.a = 255,
});
if (rg.guiDropdownBox(rectangle(10, 10, 100, 20), "Settings;Particles", &active, editMode) != 0) {
editMode = !editMode;
std.debug.print("dropdownbox: {}", .{active});
}
if (active == 1) {
if (rg.guiWindowBox(rectangle(10, 10, 800, 600), "Window Box") == 1) {
active = 0;
editMode = false;
}
_ = rg.guiSlider(rectangle(10, 150, 600, 10), "0", "500", &value, ItoF(0), ItoF(500));
_ = rg.guiSlider(rectangle(10, 170, 600, 10), "-3", "3", &value2, ItoF(-100), ItoF(100));
_ = rg.guiSlider(rectangle(10, 190, 600, 10), "-3", "3", &value3, -100, 100);
_ = rg.guiColorPicker(rectangle(10, 200, 300, 100), "Choose Color", &color);
}
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
}
plotStr = try std.fmt.bufPrint(&plotStrBuf, "{d}, {d}, {d}, {d}\n", .{ emitter.particles[0].acceleration.a.x, emitter.particles[0].position.a.x, emitter.particles[0].velocity.a.x, emitter.particles[0].show });
try plot.log(file, plotStr);
rl.beginBlendMode(rl.BlendMode.blend_additive);
emitter.update(&att, texture, color);
rl.endBlendMode();
}
plot.close(file);
}
fn updateGUI() !void {}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}