From 2578dceb420d056405cab9fcae0766b7b1b6b173 Mon Sep 17 00:00:00 2001 From: Not-Nik Date: Tue, 11 Jul 2023 15:39:11 +0200 Subject: [PATCH] Texture member functions --- examples/shaders/texture_outline.zig | 2 +- examples/textures/sprite_anim.zig | 2 +- lib/preludes/raylib-zig-prelude.zig | 44 ++++++++++++++++++++++++++++ lib/raylib-zig.zig | 44 ++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/examples/shaders/texture_outline.zig b/examples/shaders/texture_outline.zig index 212b148..aab46fb 100644 --- a/examples/shaders/texture_outline.zig +++ b/examples/shaders/texture_outline.zig @@ -53,7 +53,7 @@ pub fn main() anyerror!void { rl.beginShaderMode(shdrOutline); - rl.drawTexture(texture, @divFloor(rl.getScreenWidth(), 2) - @divFloor(texture.width, 2), -30, rl.Color.white); + texture.draw(@divFloor(rl.getScreenWidth(), 2) - @divFloor(texture.width, 2), -30, rl.Color.white); rl.endShaderMode(); diff --git a/examples/textures/sprite_anim.zig b/examples/textures/sprite_anim.zig index 53cea67..44d0586 100644 --- a/examples/textures/sprite_anim.zig +++ b/examples/textures/sprite_anim.zig @@ -79,7 +79,7 @@ pub fn main() anyerror!void { rl.drawRectangleLines(250 + 21 * @intCast(c_int, i), 205, 20, 20, rl.Color.maroon); } - rl.drawTextureRec(scarfy, frameRec, position, rl.Color.white); // Draw part of the texture + scarfy.drawRec(frameRec, position, rl.Color.white); // Draw part of the texture rl.drawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, rl.Color.gray); diff --git a/lib/preludes/raylib-zig-prelude.zig b/lib/preludes/raylib-zig-prelude.zig index d0984b7..ce10056 100755 --- a/lib/preludes/raylib-zig-prelude.zig +++ b/lib/preludes/raylib-zig-prelude.zig @@ -96,6 +96,10 @@ pub const Rectangle = extern struct { y: f32, width: f32, height: f32, + + pub fn init(x: f32, y: f32, width: f32, height: f32) Rectangle { + return Rectangle{ .x = x, .y = y, .width = width, .height = height }; + } }; pub const Image = extern struct { @@ -168,6 +172,42 @@ pub const Texture = extern struct { height: c_int, mipmaps: c_int, format: c_int, + + pub fn init(fileName: [*c]const u8) Texture { + return rl.loadTexture(fileName); + } + + pub fn fromImage(image: Image) Texture { + return rl.loadTextureFromImage(image); + } + + pub fn fromCubemap(image: Image, layout: i32) Texture { + return rl.loadTextureCubemap(image, layout); + } + + pub fn draw(self: Texture, posX: i32, posY: i32, tint: Color) void { + rl.drawTexture(self, posX, posY, tint); + } + + pub fn drawV(self: Texture, position: Vector2, tint: Color) void { + rl.drawTextureV(self, position, tint); + } + + pub fn drawEx(self: Texture, position: Vector2, rotation: f32, scale: f32, tint: Color) void { + rl.drawTextureEx(self, position, rotation, scale, tint); + } + + pub fn drawRec(self: Texture, source: Rectangle, position: Vector2, tint: Color) void { + rl.drawTextureRec(self, source, position, tint); + } + + pub fn drawPro(self: Texture, source: Rectangle, dest: Rectangle, origin: Vector2, rotation: f32, tint: Color) void { + rl.drawTexturePro(self, source, dest, origin, rotation, tint); + } + + pub fn drawNPatch(self: Texture, nPatchInfo: NPatchInfo, dest: Rectangle, origin: Vector2, rotation: f32, tint: Color) void { + rl.drawTextureNPatch(self, nPatchInfo, dest, origin, rotation, tint); + } }; pub const Texture2D = Texture; pub const TextureCubemap = Texture; @@ -177,6 +217,10 @@ pub const RenderTexture = extern struct { texture: Texture, depth: Texture, + pub fn init(width: i32, height: i32) RenderTexture { + return rl.loadRenderTexture(width, height); + } + pub fn begin(self: RenderTexture2D) void { rl.beginTextureMode(self); } diff --git a/lib/raylib-zig.zig b/lib/raylib-zig.zig index 08b5725..8bad5bc 100644 --- a/lib/raylib-zig.zig +++ b/lib/raylib-zig.zig @@ -96,6 +96,10 @@ pub const Rectangle = extern struct { y: f32, width: f32, height: f32, + + pub fn init(x: f32, y: f32, width: f32, height: f32) Rectangle { + return Rectangle{ .x = x, .y = y, .width = width, .height = height }; + } }; pub const Image = extern struct { @@ -168,6 +172,42 @@ pub const Texture = extern struct { height: c_int, mipmaps: c_int, format: c_int, + + pub fn init(fileName: [*c]const u8) Texture { + return rl.loadTexture(fileName); + } + + pub fn fromImage(image: Image) Texture { + return rl.loadTextureFromImage(image); + } + + pub fn fromCubemap(image: Image, layout: i32) Texture { + return rl.loadTextureCubemap(image, layout); + } + + pub fn draw(self: Texture, posX: i32, posY: i32, tint: Color) void { + rl.drawTexture(self, posX, posY, tint); + } + + pub fn drawV(self: Texture, position: Vector2, tint: Color) void { + rl.drawTextureV(self, position, tint); + } + + pub fn drawEx(self: Texture, position: Vector2, rotation: f32, scale: f32, tint: Color) void { + rl.drawTextureEx(self, position, rotation, scale, tint); + } + + pub fn drawRec(self: Texture, source: Rectangle, position: Vector2, tint: Color) void { + rl.drawTextureRec(self, source, position, tint); + } + + pub fn drawPro(self: Texture, source: Rectangle, dest: Rectangle, origin: Vector2, rotation: f32, tint: Color) void { + rl.drawTexturePro(self, source, dest, origin, rotation, tint); + } + + pub fn drawNPatch(self: Texture, nPatchInfo: NPatchInfo, dest: Rectangle, origin: Vector2, rotation: f32, tint: Color) void { + rl.drawTextureNPatch(self, nPatchInfo, dest, origin, rotation, tint); + } }; pub const Texture2D = Texture; pub const TextureCubemap = Texture; @@ -177,6 +217,10 @@ pub const RenderTexture = extern struct { texture: Texture, depth: Texture, + pub fn init(width: i32, height: i32) RenderTexture { + return rl.loadRenderTexture(width, height); + } + pub fn begin(self: RenderTexture2D) void { rl.beginTextureMode(self); }