More defer

This commit is contained in:
Not-Nik 2023-07-20 16:23:41 +02:00
parent 183cc437ac
commit f50189fdd0
No known key found for this signature in database
GPG Key ID: 08BB71E672DB3BFD
12 changed files with 21 additions and 64 deletions

View File

@ -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
//--------------------------------------------------------------------------------------
}
```

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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
//--------------------------------------------------------------------------------------
}

View File

@ -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 {

View File

@ -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);
}