From f50189fdd06016ea60a325af08add7eecd672188 Mon Sep 17 00:00:00 2001 From: Not-Nik Date: Thu, 20 Jul 2023 16:23:41 +0200 Subject: [PATCH] More defer --- README.md | 8 +------- examples/core/2d_camera.zig | 6 +----- examples/core/3d_camera_first_person.zig | 6 +----- examples/core/basic_window.zig | 6 +----- examples/core/input_keys.zig | 6 +----- examples/core/input_mouse.zig | 6 +----- examples/core/input_mouse_wheel.zig | 6 +----- examples/core/input_multitouch.zig | 6 +----- examples/shaders/texture_outline.zig | 12 +++--------- examples/textures/sprite_anim.zig | 9 ++------- lib/preludes/raylib-zig-prelude.zig | 12 +++++++----- lib/raylib-zig.zig | 2 +- 12 files changed, 21 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 17af3bf..d30d182 100755 --- a/README.md +++ b/README.md @@ -12,8 +12,6 @@ The binding currently only supports a subset of raylib. For more information rea ## Example -We can copy the default example with some minor changes: - ```zig const rl = @import("raylib"); @@ -24,6 +22,7 @@ pub fn main() anyerror!void { const screenHeight = 450; 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 //-------------------------------------------------------------------------------------- @@ -46,11 +45,6 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } ``` diff --git a/examples/core/2d_camera.zig b/examples/core/2d_camera.zig index 33e4a2e..6da95d5 100755 --- a/examples/core/2d_camera.zig +++ b/examples/core/2d_camera.zig @@ -12,6 +12,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera"); + defer rl.closeWindow(); // Close window and OpenGL context var player = rl.Rectangle{ .x = 400, .y = 280, .width = 40, .height = 40 }; var buildings: [MAX_BUILDINGS]rl.Rectangle = undefined; @@ -117,9 +118,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/3d_camera_first_person.zig b/examples/core/3d_camera_first_person.zig index ca6f778..5d732fc 100644 --- a/examples/core/3d_camera_first_person.zig +++ b/examples/core/3d_camera_first_person.zig @@ -11,6 +11,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - 3d camera first person"); + defer rl.closeWindow(); // Close window and OpenGL context var camera = rl.Camera3D{ .position = rl.Vector3.init(4, 2, 4), @@ -74,9 +75,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/basic_window.zig b/examples/core/basic_window.zig index 0bdfaaa..b5f64b4 100755 --- a/examples/core/basic_window.zig +++ b/examples/core/basic_window.zig @@ -9,6 +9,7 @@ pub fn main() anyerror!void { const screenHeight = 450; 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 //-------------------------------------------------------------------------------------- @@ -31,9 +32,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/input_keys.zig b/examples/core/input_keys.zig index 0bb3b11..82dd17f 100755 --- a/examples/core/input_keys.zig +++ b/examples/core/input_keys.zig @@ -9,6 +9,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input"); + defer rl.closeWindow(); // Close window and OpenGL context var ballPosition = rl.Vector2.init(screenWidth / 2, screenHeight / 2); @@ -47,9 +48,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/input_mouse.zig b/examples/core/input_mouse.zig index 1c74be3..aead21b 100755 --- a/examples/core/input_mouse.zig +++ b/examples/core/input_mouse.zig @@ -9,6 +9,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input"); + defer rl.closeWindow(); // Close window and OpenGL context var ballPosition = rl.Vector2.init(-100, -100); var ballColor = rl.Color.dark_blue; @@ -46,9 +47,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/input_mouse_wheel.zig b/examples/core/input_mouse_wheel.zig index 06228df..6613221 100755 --- a/examples/core/input_mouse_wheel.zig +++ b/examples/core/input_mouse_wheel.zig @@ -10,6 +10,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); + defer rl.closeWindow(); // Close window and OpenGL context var boxPositionY: f32 = screenHeight / 2 - 40; var scrollSpeed: f32 = 4; // Scrolling speed in pixels @@ -39,9 +40,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/core/input_multitouch.zig b/examples/core/input_multitouch.zig index d6a3c6f..0feb30f 100755 --- a/examples/core/input_multitouch.zig +++ b/examples/core/input_multitouch.zig @@ -9,6 +9,7 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); + defer rl.closeWindow(); // Close window and OpenGL context var ballPosition = rl.Vector2.init(-100, -100); var ballColor = rl.Color.beige; @@ -80,9 +81,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/examples/shaders/texture_outline.zig b/examples/shaders/texture_outline.zig index 0a01c5a..413eeb6 100644 --- a/examples/shaders/texture_outline.zig +++ b/examples/shaders/texture_outline.zig @@ -13,10 +13,13 @@ pub fn main() anyerror!void { const screenHeight = 450; rl.initWindow(screenWidth, screenHeight, "raylib [shaders] example - Apply an outline to a texture"); + defer rl.closeWindow(); // Close window and OpenGL context const texture: rl.Texture = rl.Texture.init("resources/textures/fudesumi.png"); + defer rl.unloadTexture(texture); const shdrOutline: rl.Shader = rl.loadShader(null, "resources/shaders/glsl330/outline.fs"); + defer rl.unloadShader(shdrOutline); var outlineSize: f32 = 2.0; const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color @@ -66,13 +69,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.unloadTexture(texture); - rl.unloadShader(shdrOutline); - - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- - } diff --git a/examples/textures/sprite_anim.zig b/examples/textures/sprite_anim.zig index 9623359..29a4744 100644 --- a/examples/textures/sprite_anim.zig +++ b/examples/textures/sprite_anim.zig @@ -14,9 +14,11 @@ pub fn main() anyerror!void { rl.initAudioDevice(); // Initialize audio device rl.initWindow(screenWidth, screenHeight, "raylib [texture] example - sprite anim"); + defer rl.closeWindow(); // Close window and OpenGL context // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) const scarfy: rl.Texture = rl.Texture.init("resources/textures/scarfy.png"); // Texture loading + defer rl.unloadTexture(scarfy); // Texture unloading const position = rl.Vector2.init(350.0, 280.0); var frameRec = rl.Rectangle.init(0, 0, @intToFloat(f32, @divFloor(scarfy.width, 6)), @intToFloat(f32, scarfy.height)); @@ -86,11 +88,4 @@ pub fn main() anyerror!void { rl.endDrawing(); //---------------------------------------------------------------------------------- } - - // De-Initialization - //-------------------------------------------------------------------------------------- - rl.unloadTexture(scarfy); // Texture unloading - - rl.closeWindow(); // Close window and OpenGL context - //-------------------------------------------------------------------------------------- } diff --git a/lib/preludes/raylib-zig-prelude.zig b/lib/preludes/raylib-zig-prelude.zig index 45c927f..5e97b1e 100755 --- a/lib/preludes/raylib-zig-prelude.zig +++ b/lib/preludes/raylib-zig-prelude.zig @@ -154,7 +154,7 @@ pub const Image = extern struct { mipmaps: c_int, format: PixelFormat, - pub fn init(fileName: []const u8) Image { + pub fn init(fileName: [:0]const u8) Image { return rl.loadImage(fileName); } @@ -222,7 +222,7 @@ pub const Texture = extern struct { mipmaps: c_int, format: c_int, - pub fn init(fileName: []const u8) Texture { + pub fn init(fileName: [:0]const u8) Texture { return rl.loadTexture(fileName); } @@ -995,11 +995,11 @@ pub fn saveFileData(fileName: [:0]const u8, data: []anyopaque) bool { return cdef.SaveFileData(@ptrCast([*c]const u8, fileName), @ptrCast(*anyopaque, data.ptr), @intCast(c_uint, data.len)); } -pub fn exportDataAsCode(data: []const u8, fileName: []const u8) bool { +pub fn exportDataAsCode(data: [:0]const u8, fileName: [:0]const u8) bool { return cdef.ExportDataAsCode(@ptrCast([*c]const u8, data), @as(c_uint, data.len), @ptrCast([*c]const u8, fileName)); } -pub fn loadImageFromMemory(fileType: [:0]const u8, fileData: []const u8) Image { +pub fn loadImageFromMemory(fileType: [:0]const u8, fileData: [:0]const u8) Image { return cdef.LoadImageFromMemory(@ptrCast([*c]const u8, fileType), @ptrCast([*c]const u8, fileData), @intCast(c_int, fileData.len)); } @@ -1020,10 +1020,12 @@ pub fn loadImagePalette(image: Image, maxPaletteSize: i32) []Color { pub fn loadFontFromMemory(fileType: [:0]const u8, fileData: ?[]const u8, fontSize: i32, fontChars: []i32) Font { var fileDataFinal = @as([*c]const u8, 0); + var fileDataLen = 0; if (fileData) |fileDataSure| { fileDataFinal = @ptrCast([*c]const u8, fileDataSure); + fileDataLen = fileDataSure.len; } - return cdef.LoadFontFromMemory(@ptrCast([*c]const u8, fileType), @ptrCast([*c]const u8, fileDataFinal), @intCast(c_int, fileData.len), @as(c_int, fontSize), @ptrCast([*c]c_int, fontChars), @intCast(c_int, fontChars.len)); + return cdef.LoadFontFromMemory(@ptrCast([*c]const u8, fileType), @ptrCast([*c]const u8, fileDataFinal), @intCast(c_int, fileDataLen), @as(c_int, fontSize), @ptrCast([*c]c_int, fontChars), @intCast(c_int, fontChars.len)); } pub fn loadFontData(fileData: []const u8, fontSize: i32, fontChars: []i32, ty: i32) []GlyphInfo { diff --git a/lib/raylib-zig.zig b/lib/raylib-zig.zig index 1239589..8cf4335 100644 --- a/lib/raylib-zig.zig +++ b/lib/raylib-zig.zig @@ -222,7 +222,7 @@ pub const Texture = extern struct { mipmaps: c_int, format: c_int, - pub fn init(fileName: []const u8) Texture { + pub fn init(fileName: [:0]const u8) Texture { return rl.loadTexture(fileName); }