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-11-25 10:50:16 +00:00
|
|
|
const allocator = std.heap.page_allocator;
|
2024-11-08 15:00:57 +00:00
|
|
|
|
2024-11-15 12:36:32 +00:00
|
|
|
var prng = std.rand.DefaultPrng.init(124346556);
|
|
|
|
|
|
|
|
|
|
|
|
const rand = prng.random();
|
|
|
|
|
|
|
|
fn randMinMaxFloat(min:f32,max:f32) f32 {
|
|
|
|
var r1 = rand.float(f32);
|
|
|
|
r1 = 2*(r1 - 0.5);//-0.5..0.5
|
|
|
|
return (@abs(min)+@abs(max))/2 * r1;
|
|
|
|
}
|
|
|
|
|
|
|
|
test "random" {
|
|
|
|
var seed:u64 = undefined;
|
|
|
|
var r1 :f32 = undefined;
|
|
|
|
try std.posix.getrandom(std.mem.asBytes(&seed));
|
|
|
|
|
|
|
|
prng = std.rand.DefaultPrng.init(seed);
|
|
|
|
while(true) {
|
|
|
|
r1 = randMinMaxFloat(-10,10);
|
|
|
|
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
|
|
|
|
if (r1 > 8 or r1 < -8 ) {
|
|
|
|
std.debug.print("Greater:{}\n",.{r1});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 15:14:46 +00:00
|
|
|
const screenWidth = 800;
|
|
|
|
const screenHeight = 450;
|
2024-11-15 12:36:32 +00:00
|
|
|
|
|
|
|
const Particle = struct {
|
2024-11-08 15:00:57 +00:00
|
|
|
const Self = @This();
|
|
|
|
position : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
|
|
|
velocity : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
|
|
|
acceleration : pr.Vec = .{.a = .{.x=0,.y=0,.z=0}},
|
2024-11-15 12:36:32 +00:00
|
|
|
lifespan : u8 = 0,
|
2024-11-26 10:40:50 +00:00
|
|
|
show : u8 = 0,
|
2024-11-15 12:36:32 +00:00
|
|
|
pub fn new(self:Self) void {
|
2024-11-08 15:00:57 +00:00
|
|
|
_ = self;
|
|
|
|
}
|
2024-11-15 12:36:32 +00:00
|
|
|
pub fn update(self:*Self) void {
|
|
|
|
self.velocity.add(self.acceleration);
|
|
|
|
self.position.add(self.velocity);
|
2024-11-25 14:17:35 +00:00
|
|
|
if (self.position.a.y > screenHeight)
|
|
|
|
{
|
|
|
|
self.velocity.a.y *= -0.9;
|
|
|
|
// self.acceleration.a.y *= -0.5;
|
|
|
|
}
|
2024-11-15 12:36:32 +00:00
|
|
|
|
|
|
|
if (self.lifespan > 0) {
|
|
|
|
self.lifespan -= 1;
|
2024-11-26 10:40:50 +00:00
|
|
|
}
|
|
|
|
else
|
2024-11-25 14:17:35 +00:00
|
|
|
{
|
2024-11-26 10:40:50 +00:00
|
|
|
self.show = 0;
|
2024-11-15 12:36:32 +00:00
|
|
|
}
|
2024-11-15 15:14:46 +00:00
|
|
|
}
|
2024-11-26 10:40:50 +00:00
|
|
|
pub fn spawn(self:*Self) void
|
|
|
|
{
|
|
|
|
const xrnr = randMinMaxFloat(-1,1);
|
|
|
|
const yrnr = randMinMaxFloat(-0.5,0.5);
|
|
|
|
|
|
|
|
self.lifespan = rand.intRangeAtMost(u8,75,255);
|
|
|
|
self.acceleration.a.x = xrnr;
|
|
|
|
self.acceleration.a.y = yrnr;
|
|
|
|
self.position.a.x = @floatFromInt(rl.getMouseX()-screenWidth/2);
|
|
|
|
self.position.a.y = @floatFromInt(rl.getMouseY()-screenHeight/2);
|
|
|
|
self.velocity.a.x = 0;
|
|
|
|
self.velocity.a.y = -20;
|
|
|
|
self.position.a.color.r = rand.intRangeAtMost(u8,0,30);
|
|
|
|
self.position.a.color.g = rand.intRangeAtMost(u8,0,255);
|
|
|
|
self.position.a.color.b = rand.intRangeAtMost(u8,0,255);
|
|
|
|
self.show = 1;
|
|
|
|
}
|
2024-11-15 15:14:46 +00:00
|
|
|
pub fn applyGravity(self : *Self,val : f32) void {
|
|
|
|
self.velocity.a.y += val;
|
2024-11-08 15:00:57 +00:00
|
|
|
}
|
|
|
|
};
|
2024-11-26 10:40:50 +00:00
|
|
|
|
|
|
|
const Emitter = struct
|
|
|
|
{
|
|
|
|
|
|
|
|
particleCount : u32 = 0,
|
|
|
|
particles : []Particle = undefined,
|
|
|
|
|
|
|
|
pub fn init(self:*Emitter) !void
|
|
|
|
{
|
|
|
|
var xrnr = randMinMaxFloat(-1,1);
|
|
|
|
var yrnr = randMinMaxFloat(-0.5,0.5);
|
|
|
|
self.particles = try allocator.alloc(Particle,500000);
|
|
|
|
for (self.particles) |*p| {
|
|
|
|
xrnr = randMinMaxFloat(0,2);
|
|
|
|
yrnr = randMinMaxFloat(0,2);
|
|
|
|
p.* = Particle{.velocity = .{.a = .{.x = 1,.y = -3}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn emit(self:*Emitter,count : u32) void
|
|
|
|
{
|
|
|
|
var i : u32 = 0;
|
|
|
|
for(self.particles) |*p|
|
|
|
|
{
|
|
|
|
if(p.show == 0)
|
|
|
|
{
|
|
|
|
i+=1;
|
|
|
|
p.spawn();
|
|
|
|
}
|
|
|
|
if(i >= count)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn update(self:*Emitter) void
|
|
|
|
{
|
|
|
|
var posx : i32 = 0;
|
|
|
|
var posy : i32 = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (self.particles) |*p| {
|
|
|
|
if(p.show == 1)
|
|
|
|
{
|
|
|
|
p.update();
|
|
|
|
p.applyGravity(2);
|
|
|
|
if(p.position.a.x >= -(screenWidth/2)){
|
|
|
|
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
|
|
|
|
} else {
|
|
|
|
posx = 0;
|
|
|
|
}
|
|
|
|
if(p.position.a.y >= -(screenHeight/2)){
|
|
|
|
posy = @intFromFloat(screenHeight / 2 + p.position.a.y);
|
|
|
|
} else {
|
|
|
|
posy = 0;
|
|
|
|
}
|
|
|
|
rl.drawRectangle(posx,posy,2,2,rl.Color{.r=p.position.a.color.r,.g=p.position.a.color.g,.b=p.position.a.color.b,.a=255});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-11-25 14:17:35 +00:00
|
|
|
fn ItoF(x:i32) f32
|
|
|
|
{
|
|
|
|
return @as(f32,@floatFromInt(x));
|
|
|
|
}
|
2024-11-08 09:34:08 +00:00
|
|
|
|
|
|
|
pub fn main() anyerror!void {
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2024-11-15 15:14:46 +00:00
|
|
|
|
2024-11-08 09:34:08 +00:00
|
|
|
const rect2 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(10)), .width = @as(f32, @floatFromInt(300)), .height = @as(f32, @floatFromInt(100)) };
|
2024-11-26 10:40:50 +00:00
|
|
|
const rect3 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(150)), .width = @as(f32, @floatFromInt(600)), .height = @as(f32, @floatFromInt(10)) };
|
2024-11-08 09:34:08 +00:00
|
|
|
var msg_res : i32=-1;
|
|
|
|
var state : i32=-1;
|
2024-11-15 12:36:32 +00:00
|
|
|
|
|
|
|
var seed:u64 = undefined;
|
|
|
|
try std.posix.getrandom(std.mem.asBytes(&seed));
|
|
|
|
|
|
|
|
prng = std.rand.DefaultPrng.init(seed);
|
2024-11-08 15:00:57 +00:00
|
|
|
|
2024-11-25 14:17:35 +00:00
|
|
|
var value : f32 = 0;
|
2024-11-15 12:36:32 +00:00
|
|
|
|
2024-11-26 10:40:50 +00:00
|
|
|
var emitter = Emitter{};
|
|
|
|
try emitter.init();
|
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-15 15:14:46 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
|
|
|
|
|
|
|
|
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-08 15:00:57 +00:00
|
|
|
rl.clearBackground(rl.Color{.r=181,
|
|
|
|
.g =177,
|
|
|
|
.b =154,
|
|
|
|
.a = 255,
|
|
|
|
});
|
2024-11-08 09:34:08 +00:00
|
|
|
|
|
|
|
if(state != 0){
|
2024-11-08 15:00:57 +00:00
|
|
|
msg_res = rg.guiMessageBox(rect2, "Title", "question?", "Nice;Cool");
|
2024-11-08 09:34:08 +00:00
|
|
|
if(msg_res > -1){
|
|
|
|
state = msg_res;
|
|
|
|
}
|
|
|
|
rl.clearBackground(rl.Color.white);
|
|
|
|
switch(state){
|
|
|
|
1 => {
|
|
|
|
rl.drawText("1", 190, 200, 20, rl.Color.red);
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
rl.drawText("2", 190, 200, 20, rl.Color.red);
|
|
|
|
},
|
|
|
|
else =>{},
|
|
|
|
}
|
|
|
|
}
|
2024-11-26 10:40:50 +00:00
|
|
|
_ = rg.guiSlider(rect3,"0","5000",&value,ItoF(0),ItoF(5000));
|
|
|
|
|
|
|
|
if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left))
|
|
|
|
{
|
|
|
|
emitter.emit(@as(u32,@intFromFloat(value)));
|
2024-11-15 12:36:32 +00:00
|
|
|
}
|
2024-11-08 09:34:08 +00:00
|
|
|
|
2024-11-26 10:40:50 +00:00
|
|
|
emitter.update();
|
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());
|
|
|
|
}
|