Merge branch 'master' of 5ccppi:ccppi/zig-raylib
This commit is contained in:
commit
ea8a74e323
@ -25,7 +25,7 @@
|
||||
.dependencies = .{
|
||||
.@"raylib-zig" = .{
|
||||
.url = "https://github.com/Not-Nik/raylib-zig/archive/devel.tar.gz",
|
||||
.hash = "12202f8c415153088be8df39a51e0a4c9d402afd403422a0dcc9afdd417e437a6fdb",
|
||||
.hash = "1220c61380facb4480c01109fda97cf1a37f45308c522a84fdea142ca625593ddc2a",
|
||||
},
|
||||
},
|
||||
.paths = .{
|
||||
|
208
src/main.zig
208
src/main.zig
@ -6,83 +6,78 @@ const allocator = std.heap.page_allocator;
|
||||
|
||||
var prng = std.rand.DefaultPrng.init(124346556);
|
||||
|
||||
|
||||
const rand = prng.random();
|
||||
|
||||
fn randMinMaxFloat(min:f32,max:f32) f32 {
|
||||
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;
|
||||
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;
|
||||
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);
|
||||
while (true) {
|
||||
r1 = randMinMaxFloat(-10, 10);
|
||||
//std.debug.print("randMinMaxFloat:{}\n",.{r1});
|
||||
if (r1 > 8 or r1 < -8 ) {
|
||||
std.debug.print("Greater:{}\n",.{r1});
|
||||
if (r1 > 8 or r1 < -8) {
|
||||
std.debug.print("Greater:{}\n", .{r1});
|
||||
}
|
||||
}
|
||||
}
|
||||
const screenWidth = 1200;
|
||||
const screenHeight = 800;
|
||||
const screenWidth = 1200;
|
||||
const screenHeight = 800;
|
||||
|
||||
const Particle = struct {
|
||||
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}},
|
||||
lifespan : u8 = 0,
|
||||
show : u8 = 0,
|
||||
pub fn new(self:Self) void {
|
||||
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 } },
|
||||
lifespan: u8 = 0,
|
||||
show: u8 = 0,
|
||||
pub fn new(self: Self) void {
|
||||
_ = self;
|
||||
}
|
||||
pub fn update(self:*Self) void {
|
||||
pub fn update(self: *Self) void {
|
||||
self.velocity.add(self.acceleration);
|
||||
self.position.add(self.velocity);
|
||||
if (self.position.a.y > screenHeight)
|
||||
{
|
||||
if (self.position.a.y > screenHeight) {
|
||||
self.velocity.a.y *= -0.9;
|
||||
// self.acceleration.a.y *= -0.5;
|
||||
}
|
||||
|
||||
if (self.lifespan > 0) {
|
||||
self.lifespan -= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
self.show = 0;
|
||||
}
|
||||
self.acceleration.a.x = 0;
|
||||
self.acceleration.a.y = 0;
|
||||
self.acceleration.a.z = 0;
|
||||
}
|
||||
pub fn spawn(self:*Self,xaccel : f32,yaccel : f32) void
|
||||
{
|
||||
const xrnr = randMinMaxFloat(-xaccel,xaccel);
|
||||
const yrnr = randMinMaxFloat(-yaccel,yaccel);
|
||||
pub fn spawn(self: *Self, xaccel: f32, yaccel: f32) void {
|
||||
const xrnr = randMinMaxFloat(-xaccel, xaccel);
|
||||
const yrnr = randMinMaxFloat(-yaccel, yaccel);
|
||||
|
||||
self.lifespan = rand.intRangeAtMost(u8,75,255);
|
||||
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.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 = 0;
|
||||
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.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;
|
||||
}
|
||||
pub fn applyGravity(self : *Self,val : f32) void {
|
||||
pub fn applyGravity(self: *Self, val: f32) void {
|
||||
self.velocity.a.y += val;
|
||||
}
|
||||
pub fn applyForce(self : *Self,vec : pr.Vec) void {
|
||||
pub fn applyForce(self: *Self, vec: pr.Vec) void {
|
||||
self.acceleration.a.x = vec.a.x * 0.8;
|
||||
self.acceleration.a.y = vec.a.y * 0.8;
|
||||
self.acceleration.a.z = vec.a.z;
|
||||
@ -91,52 +86,42 @@ const Particle = struct {
|
||||
}
|
||||
};
|
||||
|
||||
const Emitter = struct
|
||||
{
|
||||
const Emitter = struct {
|
||||
particleCount: u32 = 0,
|
||||
particles: []Particle = undefined,
|
||||
|
||||
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);
|
||||
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}}};
|
||||
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,xaccel : f32,yaccel : f32) void
|
||||
{
|
||||
var i : u32 = 0;
|
||||
for(self.particles) |*p|
|
||||
{
|
||||
if(p.show == 0)
|
||||
{
|
||||
i+=1;
|
||||
p.spawn(xaccel,yaccel);
|
||||
pub fn emit(self: *Emitter, count: u32, xaccel: f32, yaccel: f32) void {
|
||||
var i: u32 = 0;
|
||||
for (self.particles) |*p| {
|
||||
if (p.show == 0) {
|
||||
i += 1;
|
||||
p.spawn(xaccel, yaccel);
|
||||
}
|
||||
if(i >= count)
|
||||
{
|
||||
if (i >= count) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn update(self:*Emitter,texture : rl.Texture2D) void
|
||||
{
|
||||
var posx : i32 = 0;
|
||||
var posy : i32 = 0;
|
||||
const att : Attractor = .{.vec = .{.a=.{.x=10,.y=10,.z=10}}};
|
||||
const att2 : Attractor = .{.vec = .{.a=.{.x=-200,.y=-200,.z=0}}};
|
||||
pub fn update(self: *Emitter, texture: rl.Texture2D) void {
|
||||
var posx: i32 = 0;
|
||||
var posy: i32 = 0;
|
||||
const att: Attractor = .{ .vec = .{ .a = .{ .x = 10, .y = 10, .z = 10 } } };
|
||||
const att2: Attractor = .{ .vec = .{ .a = .{ .x = -200, .y = -200, .z = 0 } } };
|
||||
|
||||
att.draw();
|
||||
att2.draw();
|
||||
for (self.particles) |*p| {
|
||||
if(p.show == 1)
|
||||
{
|
||||
|
||||
if (p.show == 1) {
|
||||
p.applyGravity(0);
|
||||
p.applyForce(att.attract(p.*));
|
||||
p.applyForce(att2.attract(p.*));
|
||||
@ -145,57 +130,49 @@ const Emitter = struct
|
||||
posx = @intFromFloat(screenWidth / 2 + p.position.a.x);
|
||||
posy = @intFromFloat(screenHeight / 2 + p.position.a.y);
|
||||
//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});
|
||||
rl.drawTexture(texture,posx,posy,rl.Color.white);
|
||||
rl.drawTexture(texture, posx, posy, rl.Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
const Attractor = struct
|
||||
{
|
||||
vec : pr.Vec,
|
||||
pub fn attract(self:Attractor,particle : Particle) pr.Vec
|
||||
{
|
||||
|
||||
const Attractor = struct {
|
||||
vec: pr.Vec,
|
||||
pub fn attract(self: Attractor, particle: Particle) pr.Vec {
|
||||
const distance = self.vec.sub(particle.position);
|
||||
var force : pr.Vec = undefined;
|
||||
if( distance.a.x != 0 and distance.a.y != 0)
|
||||
{
|
||||
force.a.x = distance.a.x/200;
|
||||
force.a.y = distance.a.y/200;
|
||||
var force: pr.Vec = undefined;
|
||||
if (distance.a.x != 0 and distance.a.y != 0) {
|
||||
force.a.x = distance.a.x / 200;
|
||||
force.a.y = distance.a.y / 200;
|
||||
force.a.z = 0;
|
||||
}
|
||||
return force;
|
||||
}
|
||||
pub fn draw(self:Attractor) void
|
||||
{
|
||||
const posx : i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);
|
||||
const posy : i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y);
|
||||
rl.drawRectangle(posx,posy,10,10,rl.Color{.r=10,.g=10,.b=120,.a=255});
|
||||
pub fn draw(self: Attractor) void {
|
||||
const posx: i32 = @intFromFloat(screenWidth / 2 + self.vec.a.x);
|
||||
const posy: i32 = @intFromFloat(screenHeight / 2 + self.vec.a.y);
|
||||
rl.drawRectangle(posx, posy, 10, 10, rl.Color{ .r = 10, .g = 10, .b = 120, .a = 255 });
|
||||
}
|
||||
};
|
||||
fn ItoF(x:i32) f32
|
||||
{
|
||||
return @as(f32,@floatFromInt(x));
|
||||
fn ItoF(x: i32) f32 {
|
||||
return @as(f32, @floatFromInt(x));
|
||||
}
|
||||
fn rectangle(x : u16,y : u16,width : u16,height : u16) rl.Rectangle
|
||||
{
|
||||
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 {
|
||||
const rect3 = rectangle(10, 150, 600, 10);
|
||||
const rect4 = rectangle(10, 170, 600, 10);
|
||||
const rect5 = rectangle(10, 190, 600, 10);
|
||||
|
||||
const rect3 = rectangle(10,150,600,10);
|
||||
const rect4 = rectangle(10,170,600,10);
|
||||
const rect5 = rectangle(10,190,600,10);
|
||||
|
||||
var seed:u64 = undefined;
|
||||
var seed: u64 = undefined;
|
||||
try std.posix.getrandom(std.mem.asBytes(&seed));
|
||||
|
||||
prng = std.rand.DefaultPrng.init(seed);
|
||||
|
||||
var value : f32 = 0;
|
||||
var value2 : f32 = 0;
|
||||
var value3 : f32 = 0;
|
||||
var value: f32 = 0;
|
||||
var value2: f32 = 0;
|
||||
var value3: f32 = 0;
|
||||
|
||||
var emitter = Emitter{};
|
||||
try emitter.init();
|
||||
@ -204,40 +181,35 @@ pub fn main() anyerror!void {
|
||||
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)
|
||||
{
|
||||
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",.{});
|
||||
} else {
|
||||
std.debug.print("error opening resource", .{});
|
||||
std.posix.exit(1);
|
||||
}
|
||||
rl.unloadImage(image);
|
||||
|
||||
|
||||
|
||||
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
|
||||
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
|
||||
rl.beginDrawing();
|
||||
defer rl.endDrawing();
|
||||
|
||||
rl.clearBackground(rl.Color{.r=181,
|
||||
.g =177,
|
||||
.b =154,
|
||||
rl.clearBackground(rl.Color{
|
||||
.r = 0,
|
||||
.g = 0,
|
||||
.b = 0,
|
||||
.a = 255,
|
||||
});
|
||||
|
||||
_ = rg.guiSlider(rect3,"0","500",&value,ItoF(0),ItoF(500));
|
||||
_ = rg.guiSlider(rect4,"-3","3",&value2,ItoF(-10),ItoF(10));
|
||||
_ = rg.guiSlider(rect5,"-3","3",&value3,-10,10);
|
||||
_ = rg.guiSlider(rect3, "0", "500", &value, ItoF(0), ItoF(500));
|
||||
_ = rg.guiSlider(rect4, "-3", "3", &value2, ItoF(-10), ItoF(10));
|
||||
_ = rg.guiSlider(rect5, "-3", "3", &value3, -10, 10);
|
||||
|
||||
if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left))
|
||||
{
|
||||
emitter.emit(@as(u32,@intFromFloat(value)),value2,value3);
|
||||
if (rl.isMouseButtonDown(rl.MouseButton.mouse_button_left)) {
|
||||
emitter.emit(@as(u32, @intFromFloat(value)), value2, value3);
|
||||
}
|
||||
|
||||
rl.beginBlendMode(rl.BlendMode.blend_additive);
|
||||
|
Loading…
Reference in New Issue
Block a user