add image texture loading

This commit is contained in:
jonathan santis 2024-11-26 14:58:48 +01:00
parent bd824f8a10
commit 1afcdadcfa
2 changed files with 39 additions and 12 deletions

View File

@ -59,10 +59,10 @@ const Particle = struct {
self.show = 0;
}
}
pub fn spawn(self:*Self) void
pub fn spawn(self:*Self,xaccel : f32,yaccel : f32) void
{
const xrnr = randMinMaxFloat(-1,1);
const yrnr = randMinMaxFloat(-0.5,0.5);
const xrnr = randMinMaxFloat(-1-xaccel,1+xaccel);
const yrnr = randMinMaxFloat(-0.5-yaccel,0.5+yaccel);
self.lifespan = rand.intRangeAtMost(u8,75,255);
self.acceleration.a.x = xrnr;
@ -98,7 +98,7 @@ const Emitter = struct
p.* = Particle{.velocity = .{.a = .{.x = 1,.y = -3}},.acceleration = .{.a = .{.x=xrnr,.y=yrnr}}};
}
}
pub fn emit(self:*Emitter,count : u32) void
pub fn emit(self:*Emitter,count : u32,xaccel : f32,yaccel : f32) void
{
var i : u32 = 0;
for(self.particles) |*p|
@ -106,7 +106,7 @@ const Emitter = struct
if(p.show == 0)
{
i+=1;
p.spawn();
p.spawn(xaccel,yaccel);
}
if(i >= count)
{
@ -114,12 +114,11 @@ const Emitter = struct
}
}
}
pub fn update(self:*Emitter) void
pub fn update(self:*Emitter,texture : rl.Texture2D) void
{
var posx : i32 = 0;
var posy : i32 = 0;
for (self.particles) |*p| {
if(p.show == 1)
{
@ -135,7 +134,9 @@ const Emitter = struct
} 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});
//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);
}
}
}
@ -144,6 +145,10 @@ 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 {
// Initialization
@ -151,6 +156,8 @@ pub fn main() anyerror!void {
const rect2 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(10)), .width = @as(f32, @floatFromInt(300)), .height = @as(f32, @floatFromInt(100)) };
const rect3 = rl.Rectangle{ .x = @as(f32, @floatFromInt(10)), .y = @as(f32, @floatFromInt(150)), .width = @as(f32, @floatFromInt(600)), .height = @as(f32, @floatFromInt(10)) };
const rect4 = rectangle(10,170,600,10);
const rect5 = rectangle(10,190,600,10);
var msg_res : i32=-1;
var state : i32=-1;
@ -160,15 +167,31 @@ pub fn main() anyerror!void {
prng = std.rand.DefaultPrng.init(seed);
var value : f32 = 0;
var value2 : f32 = 0;
var value3 : f32 = 0;
var emitter = Emitter{};
try emitter.init();
//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/test_64.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); // Set our game to run at 60 frames-per-second
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
@ -181,6 +204,8 @@ pub fn main() anyerror!void {
.a = 255,
});
if(state != 0){
msg_res = rg.guiMessageBox(rect2, "Title", "question?", "Nice;Cool");
if(msg_res > -1){
@ -197,14 +222,16 @@ pub fn main() anyerror!void {
else =>{},
}
}
_ = rg.guiSlider(rect3,"0","5000",&value,ItoF(0),ItoF(5000));
_ = rg.guiSlider(rect3,"0","5000",&value,ItoF(0),ItoF(500));
_ = rg.guiSlider(rect4,"0","10",&value2,ItoF(0),ItoF(3));
_ = rg.guiSlider(rect5,"0","10",&value3,ItoF(0),ItoF(3));
if(rl.isMouseButtonDown(rl.MouseButton.mouse_button_left))
{
emitter.emit(@as(u32,@intFromFloat(value)));
emitter.emit(@as(u32,@intFromFloat(value)),value2,value3);
}
emitter.update();
emitter.update(texture);
}
}

Binary file not shown.