zig-raylib/src/main.zig

101 lines
3.5 KiB
Zig
Raw Normal View History

2024-11-08 09:34:08 +00:00
const std = @import("std");
const rl = @import("raylib");
const rg = @import("raygui");
2024-11-08 15:00:57 +00:00
const pr = @import("primitives.zig");
2024-12-10 14:41:50 +00:00
const particles = @import("particles.zig");
2024-11-28 09:40:05 +00:00
const plot = @import("plot.zig");
2024-11-15 12:36:32 +00:00
2024-11-28 09:40:05 +00:00
const allocator = std.heap.page_allocator;
2024-11-15 12:36:32 +00:00
2024-11-28 09:40:05 +00:00
const screenWidth = 1200;
const screenHeight = 800;
2024-11-27 12:57:56 +00:00
fn ItoF(x: i32) f32 {
return @as(f32, @floatFromInt(x));
2024-11-25 14:17:35 +00:00
}
2024-11-27 12:57:56 +00:00
fn rectangle(x: u16, y: u16, width: u16, height: u16) rl.Rectangle {
2024-11-26 13:58:48 +00:00
return rl.Rectangle{ .x = @as(f32, @floatFromInt(x)), .y = @as(f32, @floatFromInt(y)), .width = @as(f32, @floatFromInt(width)), .height = @as(f32, @floatFromInt(height)) };
}
2024-11-08 09:34:08 +00:00
pub fn main() anyerror!void {
2024-11-27 12:57:56 +00:00
var value: f32 = 0;
var value2: f32 = 0;
var value3: f32 = 0;
2024-12-03 11:36:43 +00:00
var color: rl.Color = undefined;
2024-11-15 12:36:32 +00:00
2024-12-10 14:41:50 +00:00
var emitter = particles.Emitter{};
try emitter.init(allocator, screenWidth, screenHeight);
2024-11-08 15:00:57 +00:00
2024-11-25 10:17:00 +00:00
//const img : rl.Texture = rl.loadTexture("img.png");
2024-11-08 09:34:08 +00:00
rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
defer rl.closeWindow(); // Close window and OpenGL context
2024-11-27 12:57:56 +00:00
var texture: rl.Texture2D = undefined;
const image: rl.Image = rl.loadImage("resources/blut.png");
if (image.width > 0) {
2024-11-26 13:58:48 +00:00
texture = rl.loadTextureFromImage(image);
2024-11-27 12:57:56 +00:00
} else {
std.debug.print("error opening resource", .{});
2024-11-26 13:58:48 +00:00
std.posix.exit(1);
}
rl.unloadImage(image);
2024-12-10 14:41:50 +00:00
rl.setTargetFPS(60);
2024-11-08 09:34:08 +00:00
2024-11-28 09:40:05 +00:00
const file = try plot.open("plot.dat");
2024-12-10 14:41:50 +00:00
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;
2024-11-28 09:40:05 +00:00
2024-11-08 09:34:08 +00:00
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
2024-11-08 15:00:57 +00:00
rl.beginDrawing();
defer rl.endDrawing();
2024-11-08 09:34:08 +00:00
2024-11-27 12:57:56 +00:00
rl.clearBackground(rl.Color{
.r = 0,
.g = 0,
.b = 0,
.a = 255,
2024-11-08 15:00:57 +00:00
});
2024-11-08 09:34:08 +00:00
2024-12-10 14:41:50 +00:00
if (rg.guiDropdownBox(rectangle(10, 10, 100, 20), "Settings;Particles", &active, editMode) != 0) {
2024-12-03 11:36:43 +00:00
editMode = !editMode;
2024-12-10 14:41:50 +00:00
std.debug.print("dropdownbox: {}", .{active});
2024-12-03 11:36:43 +00:00
}
2024-12-10 14:41:50 +00:00
if (active == 1) {
if (rg.guiWindowBox(rectangle(10, 10, 800, 600), "Window Box") == 1) {
2024-12-03 11:36:43 +00:00
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);
2024-12-10 14:41:50 +00:00
_ = rg.guiColorPicker(rectangle(10, 200, 300, 100), "Choose Color", &color);
2024-11-15 12:36:32 +00:00
}
2024-12-10 14:41:50 +00:00
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
2024-12-03 11:36:43 +00:00
}
2024-12-10 14:41:50 +00:00
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();
2024-11-08 09:34:08 +00:00
}
2024-11-28 09:40:05 +00:00
plot.close(file);
2024-11-08 09:34:08 +00:00
}
2024-12-10 14:41:50 +00:00
fn updateGUI() !void {}
2024-11-08 09:34:08 +00:00
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());
}