From 42671d0195cd7be5439436954a2b3acd484bbc91 Mon Sep 17 00:00:00 2001 From: Not-Nik Date: Sun, 9 Jul 2023 18:45:15 +0200 Subject: [PATCH] Some work on the examples --- README.md | 4 +- build.zig | 5 ++ examples/ReadMe.md | 32 -------- examples/core/2d_camera.zig | 95 +++++++++++------------- examples/core/3d_camera_first_person.zig | 83 +++++++++++++++++++++ examples/core/basic_window.zig | 7 +- examples/core/input_keys.zig | 39 +++++----- examples/core/input_mouse.zig | 38 +++++----- examples/core/input_mouse_wheel.zig | 28 +++---- examples/core/input_multitouch.zig | 80 +++++++++++--------- examples/shaders/texture_outline.zig | 42 +++++------ examples/textures/sprite_anim.zig | 67 +++++++++-------- lib/raylib-zig-types.zig | 58 ++++++++------- lib/raylib-zig.zig | 58 ++++++++------- 14 files changed, 349 insertions(+), 287 deletions(-) delete mode 100755 examples/ReadMe.md create mode 100644 examples/core/3d_camera_first_person.zig diff --git a/README.md b/README.md index a875a43..6b25ba3 100755 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ # raylib-zig -Manually tweaked, auto generated [raylib](https://github.com/raysan5/raylib) bindings for zig. +Manually tweaked, auto-generated [raylib](https://github.com/raysan5/raylib) bindings for zig. Bindings tested on raylib version 4.5.0-dev and Zig 0.10.1 -Thanks to @jessrud, @mbcrocci, @franciscod, @AlxHnr, @Gertkeno, @Ivan-Velickovic, @alanoliveira, @rcorre and @sacredbirdman for their contributions to this binding. +Thanks to all the [contributors](https://github.com/Not-Nik/raylib-zig/graphs/contributors) for their help with this binding. The binding currently only supports a subset of raylib. For more information read [here](#technical-restrictions). diff --git a/build.zig b/build.zig index e3f5408..4678b71 100755 --- a/build.zig +++ b/build.zig @@ -50,6 +50,11 @@ pub fn build(b: *Builder) void { .path = "examples/core/2d_camera.zig", .desc = "Shows the functionality of a 2D camera", }, + .{ + .name = "3d_camera_first_person", + .path = "examples/core/3d_camera_first_person.zig", + .desc = "Simple first person demo", + }, .{ .name = "sprite_anim", .path = "examples/textures/sprite_anim.zig", diff --git a/examples/ReadMe.md b/examples/ReadMe.md deleted file mode 100755 index 37cfa1a..0000000 --- a/examples/ReadMe.md +++ /dev/null @@ -1,32 +0,0 @@ -# Examples - -Making raylib bindings in zig is pretty straight forward since zig has a built-in c parser, so I am now on a quest to port all the examples. - -### category: core - -Examples using raylib core platform functionality like window creation, inputs, drawing modes and system functionality. - -| ## | example | developer | -|----|----------|:----------:| -| 01 | [core_basic_window](core/basic_window.zig) | ray -| 02 | [core_input_keys](core/input_keys.zig) | ray -| 04 | [core_input_mouse_wheel](core/input_mouse_wheel.zig) | ray -| 06 | [core_input_multitouch](core/input_multitouch.zig) | [Berni](https://github.com/Berni8k) -| 08 | [core_2d_camera](core/2d_camera.zig) | ray - -### category: models - -Examples using raylib models functionality, including models loading/generation and drawing, provided by raylib models module. - -| ## | example | developer | -|----|----------|:----------:| -| 74 | [models_loading](models/models_loading.zig) | ray - -### category: shaders - -Examples using raylib shaders functionality, including shaders loading, parameters configuration and drawing using them (model shaders and postprocessing shaders). This -functionality is directly provided by raylib rlgl module. - -| ## | example | developer | -|----|----------|:----------:| -| 74 | [shaders_basic_lighting](shaders/shaders_basic_lighting.zig) | [Chris Camacho](https://github.com/codifies) diff --git a/examples/core/2d_camera.zig b/examples/core/2d_camera.zig index 799ba5c..4197521 100755 --- a/examples/core/2d_camera.zig +++ b/examples/core/2d_camera.zig @@ -1,17 +1,11 @@ -// -// 2d_camera -// Zig version: -// Author: Nikolas Wipper -// Date: 2020-02-16 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); const rlm = @import("raylib-math"); const MAX_BUILDINGS = 100; -pub fn main() anyerror!void -{ +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -19,14 +13,13 @@ pub fn main() anyerror!void rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera"); - var player = rl.Rectangle { .x = 400, .y = 280, .width = 40, .height = 40 }; + var player = rl.Rectangle{ .x = 400, .y = 280, .width = 40, .height = 40 }; var buildings: [MAX_BUILDINGS]rl.Rectangle = undefined; var buildColors: [MAX_BUILDINGS]rl.Color = undefined; var spacing: i32 = 0; - for (buildings) |_, i| - { + for (buildings) |_, i| { buildings[i].width = @intToFloat(f32, rl.GetRandomValue(50, 200)); buildings[i].height = @intToFloat(f32, rl.GetRandomValue(100, 800)); buildings[i].y = screenHeight - 130 - buildings[i].height; @@ -34,36 +27,40 @@ pub fn main() anyerror!void spacing += @floatToInt(i32, buildings[i].width); - buildColors[i] = rl.Color { .r = @intCast(u8, rl.GetRandomValue(200, 240)), .g = @intCast(u8, rl.GetRandomValue(200, 240)), - .b = @intCast(u8, rl.GetRandomValue(200, 250)), .a = 255 }; + buildColors[i] = rl.Color{ .r = @intCast(u8, rl.GetRandomValue(200, 240)), .g = @intCast(u8, rl.GetRandomValue(200, 240)), .b = @intCast(u8, rl.GetRandomValue(200, 250)), .a = 255 }; } - var camera = rl.Camera2D { - .target = rl.Vector2 { .x = player.x + 20, .y = player.y + 20 }, - .offset = rl.Vector2 { .x = screenWidth/2, .y = screenHeight/2 }, + var camera = rl.Camera2D{ + .target = rl.Vector2{ .x = player.x + 20, .y = player.y + 20 }, + .offset = rl.Vector2{ .x = screenWidth / 2, .y = screenHeight / 2 }, .rotation = 0, .zoom = 1, }; - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- // Player movement - if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { player.x += 2; } - else if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { player.x -= 2; } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { + player.x += 2; + } else if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { + player.x -= 2; + } // Camera target follows player - camera.target = rl.Vector2 { .x = player.x + 20, .y = player.y + 20 }; + camera.target = rl.Vector2{ .x = player.x + 20, .y = player.y + 20 }; // Camera rotation controls - if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) { camera.rotation -= 1; } - else if (rl.IsKeyDown(rl.KeyboardKey.KEY_S)) { camera.rotation += 1; } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_A)) { + camera.rotation -= 1; + } else if (rl.IsKeyDown(rl.KeyboardKey.KEY_S)) { + camera.rotation += 1; + } // Limit camera rotation to 80 degrees (-40 to 40) camera.rotation = rlm.Clamp(camera.rotation, -40, 40); @@ -74,8 +71,7 @@ pub fn main() anyerror!void camera.zoom = rlm.Clamp(camera.zoom, 0.1, 3.0); // Camera reset (zoom and rotation) - if (rl.IsKeyPressed(rl.KeyboardKey.KEY_R)) - { + if (rl.IsKeyPressed(rl.KeyboardKey.KEY_R)) { camera.zoom = 1.0; camera.rotation = 0.0; } @@ -85,39 +81,38 @@ pub fn main() anyerror!void //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - camera.Begin(); + camera.begin(); - rl.DrawRectangle(-6000, 320, 13000, 8000, rl.DARKGRAY); + rl.DrawRectangle(-6000, 320, 13000, 8000, rl.DARKGRAY); - for (buildings) |building, i| - { - rl.DrawRectangleRec(building, buildColors[i]); - } + for (buildings) |building, i| { + rl.DrawRectangleRec(building, buildColors[i]); + } - rl.DrawRectangleRec(player, rl.RED); + rl.DrawRectangleRec(player, rl.RED); - rl.DrawLine(@floatToInt(c_int, camera.target.x), -screenHeight*10, @floatToInt(c_int, camera.target.x), screenHeight*10, rl.GREEN); - rl.DrawLine(-screenWidth*10, @floatToInt(c_int, camera.target.y), screenWidth*10, @floatToInt(c_int, camera.target.y), rl.GREEN); + rl.DrawLine(@floatToInt(c_int, camera.target.x), -screenHeight * 10, @floatToInt(c_int, camera.target.x), screenHeight * 10, rl.GREEN); + rl.DrawLine(-screenWidth * 10, @floatToInt(c_int, camera.target.y), screenWidth * 10, @floatToInt(c_int, camera.target.y), rl.GREEN); - camera.End(); + camera.end(); - rl.DrawText("SCREEN AREA", 640, 10, 20, rl.RED); + rl.DrawText("SCREEN AREA", 640, 10, 20, rl.RED); - rl.DrawRectangle(0, 0, screenWidth, 5, rl.RED); - rl.DrawRectangle(0, 5, 5, screenHeight - 10, rl.RED); - rl.DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, rl.RED); - rl.DrawRectangle(0, screenHeight - 5, screenWidth, 5, rl.RED); + rl.DrawRectangle(0, 0, screenWidth, 5, rl.RED); + rl.DrawRectangle(0, 5, 5, screenHeight - 10, rl.RED); + rl.DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, rl.RED); + rl.DrawRectangle(0, screenHeight - 5, screenWidth, 5, rl.RED); - rl.DrawRectangle( 10, 10, 250, 113, rl.Fade(rl.SKYBLUE, 0.5)); - rl.DrawRectangleLines( 10, 10, 250, 113, rl.BLUE); + rl.DrawRectangle(10, 10, 250, 113, rl.Fade(rl.SKYBLUE, 0.5)); + rl.DrawRectangleLines(10, 10, 250, 113, rl.BLUE); - rl.DrawText("Free 2d camera controls:", 20, 20, 10, rl.BLACK); - rl.DrawText("- Right/Left to move Offset", 40, 40, 10, rl.DARKGRAY); - rl.DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rl.DARKGRAY); - rl.DrawText("- A / S to Rotate", 40, 80, 10, rl.DARKGRAY); - rl.DrawText("- R to reset Zoom and Rotation", 40, 100, 10, rl.DARKGRAY); + rl.DrawText("Free 2d camera controls:", 20, 20, 10, rl.BLACK); + rl.DrawText("- Right/Left to move Offset", 40, 40, 10, rl.DARKGRAY); + rl.DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, rl.DARKGRAY); + rl.DrawText("- A / S to Rotate", 40, 80, 10, rl.DARKGRAY); + rl.DrawText("- R to reset Zoom and Rotation", 40, 100, 10, rl.DARKGRAY); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -125,6 +120,6 @@ pub fn main() anyerror!void // De-Initialization //-------------------------------------------------------------------------------------- - rl.CloseWindow(); // Close window and OpenGL context + 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 new file mode 100644 index 0000000..f016a55 --- /dev/null +++ b/examples/core/3d_camera_first_person.zig @@ -0,0 +1,83 @@ +// raylib-zig (c) Nikolas Wipper 2023 + +const rl = @import("raylib"); +const rlm = @import("raylib-math"); + +const MAX_COLUMNS = 20; + +pub fn main() anyerror!void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - 3d camera first person"); + + var camera = rl.Camera3D{ + .position = rl.Vector3{ .x = 4, .y = 2, .z = 4 }, + .target = rl.Vector3{ .x = 0, .y = 1.8, .z = 0 }, + .up = rl.Vector3{ .x = 0, .y = 1, .z = 0 }, + .fovy = 60, + .projection = rl.CameraProjection.CAMERA_PERSPECTIVE, + }; + + var heights: [MAX_COLUMNS]f32 = undefined; + var positions: [MAX_COLUMNS]rl.Vector3 = undefined; + var colors: [MAX_COLUMNS]rl.Color = undefined; + + for (heights) |_, i| { + heights[i] = @intToFloat(f32, rl.GetRandomValue(1, 12)); + positions[i] = rl.Vector3{ .x = @intToFloat(f32, rl.GetRandomValue(-15, 15)), .y = heights[i] / 2.0, .z = @intToFloat(f32, rl.GetRandomValue(-15, 15)) }; + colors[i] = rl.Color{ .r = @intCast(u8, rl.GetRandomValue(20, 255)), .g = @intCast(u8, rl.GetRandomValue(10, 55)), .b = 30, .a = 255 }; + } + + camera.setMode(rl.CameraMode.CAMERA_FIRST_PERSON); + + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key + // Update + //---------------------------------------------------------------------------------- + camera.update(); + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.BeginDrawing(); + + rl.ClearBackground(rl.RAYWHITE); + + camera.begin(); + + // Draw ground + rl.DrawPlane(rl.Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }, rl.Vector2{ .x = 32.0, .y = 32.0 }, rl.LIGHTGRAY); + rl.DrawCube(rl.Vector3{ .x = -16.0, .y = 2.5, .z = 0.0 }, 1.0, 5.0, 32.0, rl.BLUE); // Draw a blue wall + rl.DrawCube(rl.Vector3{ .x = 16.0, .y = 2.5, .z = 0.0 }, 1.0, 5.0, 32.0, rl.LIME); // Draw a green wall + rl.DrawCube(rl.Vector3{ .x = 0.0, .y = 2.5, .z = 16.0 }, 32.0, 5.0, 1.0, rl.GOLD); // Draw a yellow wall + + // Draw some cubes around + for (heights) |height, i| { + rl.DrawCube(positions[i], 2.0, height, 2.0, colors[i]); + rl.DrawCubeWires(positions[i], 2.0, height, 2.0, rl.MAROON); + } + + camera.end(); + + rl.DrawRectangle(10, 10, 220, 70, rl.Fade(rl.SKYBLUE, 0.5)); + rl.DrawRectangleLines(10, 10, 220, 70, rl.BLUE); + + rl.DrawText("First person camera default controls:", 20, 20, 10, rl.BLACK); + rl.DrawText("- Move with keys: W, A, S, D", 40, 40, 10, rl.DARKGRAY); + rl.DrawText("- Mouse move to look around", 40, 60, 10, rl.DARKGRAY); + + 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 2451ece..f6c657e 100755 --- a/examples/core/basic_window.zig +++ b/examples/core/basic_window.zig @@ -1,9 +1,4 @@ -// -// basic_window -// Zig version: 0.6.0 -// Author: Nikolas Wipper -// Date: 2020-02-15 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); diff --git a/examples/core/input_keys.zig b/examples/core/input_keys.zig index 75c5b74..f02356c 100755 --- a/examples/core/input_keys.zig +++ b/examples/core/input_keys.zig @@ -1,9 +1,4 @@ -// -// input_keys -// Zig version: -// Author: Nikolas Wipper -// Date: 2020-02-16 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); @@ -15,31 +10,39 @@ pub fn main() anyerror!void { rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - keyboard input"); - var ballPosition = rl.Vector2 { .x = screenWidth/2, .y = screenHeight/2 }; + var ballPosition = rl.Vector2{ .x = screenWidth / 2, .y = screenHeight / 2 }; - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key + // Update //---------------------------------------------------------------------------------- - if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { ballPosition.x += 2.0; } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { ballPosition.x -= 2.0; } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_UP)) { ballPosition.y -= 2.0; } - if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { ballPosition.y += 2.0; } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_RIGHT)) { + ballPosition.x += 2.0; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_LEFT)) { + ballPosition.x -= 2.0; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_UP)) { + ballPosition.y -= 2.0; + } + if (rl.IsKeyDown(rl.KeyboardKey.KEY_DOWN)) { + ballPosition.y += 2.0; + } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - rl.DrawText("move the ball with arrow keys", 10, 10, 20, rl.DARKGRAY); + rl.DrawText("move the ball with arrow keys", 10, 10, 20, rl.DARKGRAY); - rl.DrawCircleV(ballPosition, 50, rl.MAROON); + rl.DrawCircleV(ballPosition, 50, rl.MAROON); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -47,6 +50,6 @@ pub fn main() anyerror!void { // De-Initialization //-------------------------------------------------------------------------------------- - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- } diff --git a/examples/core/input_mouse.zig b/examples/core/input_mouse.zig index 1590f72..b5ecf81 100755 --- a/examples/core/input_mouse.zig +++ b/examples/core/input_mouse.zig @@ -1,14 +1,8 @@ -// -// input_mouse -// Zig version: -// Author: Nikolas Wipper -// Date: 2020-02-16 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); -pub fn main() anyerror!void -{ +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -16,36 +10,39 @@ pub fn main() anyerror!void rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - mouse input"); - var ballPosition = rl.Vector2 { .x = -100.0, .y = -100.0 }; + var ballPosition = rl.Vector2{ .x = -100.0, .y = -100.0 }; var ballColor = rl.DARKBLUE; - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- ballPosition = rl.GetMousePosition(); ballPosition.x = @intToFloat(f32, rl.GetMouseX()); ballPosition.y = @intToFloat(f32, rl.GetMouseY()); - if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_LEFT)) { ballColor = rl.MAROON; } - else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { ballColor = rl.LIME; } - else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { ballColor = rl.DARKBLUE; } + if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_LEFT)) { + ballColor = rl.MAROON; + } else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { + ballColor = rl.LIME; + } else if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { + ballColor = rl.DARKBLUE; + } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - rl.DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, ballColor); - //DrawCircleV(ballPosition, 40, ballColor); + rl.DrawCircle(@floatToInt(c_int, ballPosition.x), @floatToInt(c_int, ballPosition.y), 50, ballColor); + //DrawCircleV(ballPosition, 40, ballColor); - rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY); + rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -53,7 +50,6 @@ pub fn main() anyerror!void // De-Initialization //-------------------------------------------------------------------------------------- - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- } - diff --git a/examples/core/input_mouse_wheel.zig b/examples/core/input_mouse_wheel.zig index 3b15063..67c3725 100755 --- a/examples/core/input_mouse_wheel.zig +++ b/examples/core/input_mouse_wheel.zig @@ -1,14 +1,8 @@ -// -// input_mouse_wheel -// Zig version: -// Author: Nikolas Wipper -// Date: 2020-02-16 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); -pub fn main() anyerror!void -{ +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -17,14 +11,13 @@ pub fn main() anyerror!void rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); var boxPositionY: f32 = screenHeight / 2 - 40; - var scrollSpeed: f32 = 4; // Scrolling speed in pixels + var scrollSpeed: f32 = 4; // Scrolling speed in pixels - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- boxPositionY -= (rl.GetMouseWheelMove() * scrollSpeed); @@ -34,12 +27,12 @@ pub fn main() anyerror!void //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.WHITE); + rl.ClearBackground(rl.WHITE); - rl.DrawRectangle(screenWidth/2 - 40, @floatToInt(c_int, boxPositionY), 80, 80, rl.MAROON); + rl.DrawRectangle(screenWidth / 2 - 40, @floatToInt(c_int, boxPositionY), 80, 80, rl.MAROON); - rl.DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.GRAY); - rl.DrawText(rl.TextFormat("Box position Y: %03i", @floatToInt(c_int, boxPositionY)), 10, 40, 20, rl.LIGHTGRAY); + rl.DrawText("Use mouse wheel to move the cube up and down!", 10, 10, 20, rl.GRAY); + rl.DrawText(rl.TextFormat("Box position Y: %03i", @floatToInt(c_int, boxPositionY)), 10, 40, 20, rl.LIGHTGRAY); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -47,7 +40,6 @@ pub fn main() anyerror!void // De-Initialization //-------------------------------------------------------------------------------------- - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- } - diff --git a/examples/core/input_multitouch.zig b/examples/core/input_multitouch.zig index fae9815..ec6c412 100755 --- a/examples/core/input_multitouch.zig +++ b/examples/core/input_multitouch.zig @@ -1,14 +1,8 @@ -// -// input_multitouch -// Zig version: -// Author: Nikolas Wipper -// Date: 2020-02-16 -// +// raylib-zig (c) Nikolas Wipper 2023 const rl = @import("raylib"); -pub fn main() anyerror!void -{ +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -16,59 +10,72 @@ pub fn main() anyerror!void rl.InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window"); - var ballPosition = rl.Vector2 { .x = -100.0, .y = -100.0 }; + var ballPosition = rl.Vector2{ .x = -100.0, .y = -100.0 }; var ballColor = rl.BEIGE; var touchCounter: f32 = 0; - var touchPosition = rl.Vector2 { .x = 0.0, .y = 0.0 }; + var touchPosition = rl.Vector2{ .x = 0.0, .y = 0.0 }; - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- ballPosition = rl.GetMousePosition(); ballColor = rl.BEIGE; - if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_LEFT)) { ballColor = rl.MAROON; } - if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { ballColor = rl.LIME; } - if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { ballColor = rl.DARKBLUE; } + if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_LEFT)) { + ballColor = rl.MAROON; + } + if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { + ballColor = rl.LIME; + } + if (rl.IsMouseButtonDown(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { + ballColor = rl.DARKBLUE; + } - if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_LEFT)) { touchCounter = 10; } - if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { touchCounter = 10; } - if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { touchCounter = 10; } + if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_LEFT)) { + touchCounter = 10; + } + if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_MIDDLE)) { + touchCounter = 10; + } + if (rl.IsMouseButtonPressed(rl.MouseButton.MOUSE_BUTTON_RIGHT)) { + touchCounter = 10; + } - if (touchCounter > 0) { touchCounter -= 1; } + if (touchCounter > 0) { + touchCounter -= 1; + } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - const nums = [_]i32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - for (nums) |i| - { - touchPosition = rl.GetTouchPosition(i); // Get the touch point + const nums = [_]i32{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + for (nums) |i| { + touchPosition = rl.GetTouchPosition(i); // Get the touch point - if ((touchPosition.x >= 0) and (touchPosition.y >= 0)) // Make sure point is not (-1,-1) as this means there is no touch for it - { - // Draw circle and touch index number - rl.DrawCircleV(touchPosition, 34, rl.ORANGE); - rl.DrawText(rl.TextFormat("%d", i), @floatToInt(c_int, touchPosition.x) - 10, @floatToInt(c_int, touchPosition.y) - 70, 40, rl.BLACK); - } + // Make sure point is not (-1,-1) as this means there is no touch for it + if ((touchPosition.x >= 0) and (touchPosition.y >= 0)) { + + // Draw circle and touch index number + rl.DrawCircleV(touchPosition, 34, rl.ORANGE); + rl.DrawText(rl.TextFormat("%d", i), @floatToInt(c_int, touchPosition.x) - 10, @floatToInt(c_int, touchPosition.y) - 70, 40, rl.BLACK); } + } - // Draw the normal mouse location - rl.DrawCircleV(ballPosition, 30 + (touchCounter*3), ballColor); + // Draw the normal mouse location + rl.DrawCircleV(ballPosition, 30 + (touchCounter * 3), ballColor); - rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY); - rl.DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, rl.DARKGRAY); + rl.DrawText("move ball with mouse and click mouse button to change color", 10, 10, 20, rl.DARKGRAY); + rl.DrawText("touch the screen at multiple locations to get multiple balls", 10, 30, 20, rl.DARKGRAY); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -76,7 +83,6 @@ pub fn main() anyerror!void // De-Initialization //-------------------------------------------------------------------------------------- - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- } - diff --git a/examples/shaders/texture_outline.zig b/examples/shaders/texture_outline.zig index 26ece52..0a04f7b 100644 --- a/examples/shaders/texture_outline.zig +++ b/examples/shaders/texture_outline.zig @@ -1,6 +1,5 @@ // A raylib port of https://github.com/raysan5/raylib/blob/master/examples/shaders/shaders_texture_outline.c - const rl = @import("raylib"); const std = @import("std"); @@ -16,34 +15,33 @@ pub fn main() anyerror!void { rl.InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Apply an outline to a texture"); const texture: rl.Texture2D = rl.LoadTexture("resources/textures/fudesumi.png"); - + const shdrOutline: rl.Shader = rl.LoadShader(0, rl.TextFormat("resources/shaders/glsl330/outline.fs", @intCast(c_int, 330))); var outlineSize: f32 = 2.0; - const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color - const textureSize = rl.Vector2{ .x=@intToFloat(f32, texture.width), .y=@intToFloat(f32,texture.height) }; - + const outlineColor = [4]f32{ 1.0, 0.0, 0.0, 1.0 }; // Normalized RED color + const textureSize = rl.Vector2{ .x = @intToFloat(f32, texture.width), .y = @intToFloat(f32, texture.height) }; + // Get shader locations const outlineSizeLoc = rl.GetShaderLocation(shdrOutline, "outlineSize"); const outlineColorLoc = rl.GetShaderLocation(shdrOutline, "outlineColor"); const textureSizeLoc = rl.GetShaderLocation(shdrOutline, "textureSize"); - + // Set shader values (they can be changed later) rl.SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, @enumToInt(rl.ShaderUniformDataType.SHADER_UNIFORM_FLOAT)); rl.SetShaderValue(shdrOutline, outlineColorLoc, &outlineColor, @enumToInt(rl.ShaderUniformDataType.SHADER_UNIFORM_VEC4)); rl.SetShaderValue(shdrOutline, textureSizeLoc, &textureSize, @enumToInt(rl.ShaderUniformDataType.SHADER_UNIFORM_VEC2)); - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- outlineSize += rl.GetMouseWheelMove(); if (outlineSize < 1.0) outlineSize = 1.0; - + rl.SetShaderValue(shdrOutline, outlineSizeLoc, &outlineSize, @enumToInt(rl.ShaderUniformDataType.SHADER_UNIFORM_FLOAT)); //---------------------------------------------------------------------------------- @@ -51,19 +49,19 @@ pub fn main() anyerror!void { //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - rl.BeginShaderMode(shdrOutline); - - rl.DrawTexture(texture, @divFloor(rl.GetScreenWidth(),2) - @divFloor(texture.width,2), -30, rl.WHITE); - - rl.EndShaderMode(); + rl.BeginShaderMode(shdrOutline); - rl.DrawText("Shader-based\ntexture\noutline", 10, 10, 20, rl.GRAY); - - rl.DrawText(rl.TextFormat("Outline size: %i px", @floatToInt(i32, outlineSize)), 10, 120, 20, rl.MAROON); + rl.DrawTexture(texture, @divFloor(rl.GetScreenWidth(), 2) - @divFloor(texture.width, 2), -30, rl.WHITE); - rl.DrawFPS(710, 10); + rl.EndShaderMode(); + + rl.DrawText("Shader-based\ntexture\noutline", 10, 10, 20, rl.GRAY); + + rl.DrawText(rl.TextFormat("Outline size: %i px", @floatToInt(i32, outlineSize)), 10, 120, 20, rl.MAROON); + + rl.DrawFPS(710, 10); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -74,7 +72,7 @@ pub fn main() anyerror!void { rl.UnloadTexture(texture); rl.UnloadShader(shdrOutline); - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- -} \ No newline at end of file +} diff --git a/examples/textures/sprite_anim.zig b/examples/textures/sprite_anim.zig index a4e391e..0ceb3a0 100644 --- a/examples/textures/sprite_anim.zig +++ b/examples/textures/sprite_anim.zig @@ -12,46 +12,49 @@ pub fn main() anyerror!void { const screenWidth = 800; const screenHeight = 450; - rl.InitAudioDevice(); // Initialize audio device + rl.InitAudioDevice(); // Initialize audio device rl.InitWindow(screenWidth, screenHeight, "raylib [texture] example - sprite anim"); // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) - const scarfy: rl.Texture2D = rl.LoadTexture("resources/textures/scarfy.png"); // Texture loading + const scarfy: rl.Texture2D = rl.LoadTexture("resources/textures/scarfy.png"); // Texture loading const position = rl.Vector2{ .x = 350.0, .y = 280.0 }; - var frameRec = rl.Rectangle{ .x = 0.0, .y = 0.0, .width = @intToFloat(f32, @divFloor(scarfy.width,6)), .height = @intToFloat(f32, scarfy.height) }; - var currentFrame:u8 = 0; + var frameRec = rl.Rectangle{ .x = 0.0, .y = 0.0, .width = @intToFloat(f32, @divFloor(scarfy.width, 6)), .height = @intToFloat(f32, scarfy.height) }; + var currentFrame: u8 = 0; - var framesCounter:u8 = 0; - var framesSpeed:u8 = 8; // Number of spritesheet frames shown by second + var framesCounter: u8 = 0; + var framesSpeed: u8 = 8; // Number of spritesheet frames shown by second - rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second + rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!rl.WindowShouldClose()) // Detect window close button or ESC key - { + while (!rl.WindowShouldClose()) { // Detect window close button or ESC key // Update //---------------------------------------------------------------------------------- framesCounter += 1; - if (framesCounter >= (60/framesSpeed)) - { + if (framesCounter >= (60 / framesSpeed)) { framesCounter = 0; currentFrame += 1; if (currentFrame > 5) currentFrame = 0; - frameRec.x = @intToFloat(f32,currentFrame)*@intToFloat(f32,@divFloor(scarfy.width,6)); + frameRec.x = @intToFloat(f32, currentFrame) * @intToFloat(f32, @divFloor(scarfy.width, 6)); } // Control frames speed - if (rl.IsKeyPressed(rl.KeyboardKey.KEY_RIGHT)) {framesSpeed+=1;} - else if (rl.IsKeyPressed(rl.KeyboardKey.KEY_LEFT)) {framesSpeed-=1;} - - if (framesSpeed > MAX_FRAME_SPEED) {framesSpeed = MAX_FRAME_SPEED;} - else if (framesSpeed < MIN_FRAME_SPEED) {framesSpeed = MIN_FRAME_SPEED;} + if (rl.IsKeyPressed(rl.KeyboardKey.KEY_RIGHT)) { + framesSpeed += 1; + } else if (rl.IsKeyPressed(rl.KeyboardKey.KEY_LEFT)) { + framesSpeed -= 1; + } + if (framesSpeed > MAX_FRAME_SPEED) { + framesSpeed = MAX_FRAME_SPEED; + } else if (framesSpeed < MIN_FRAME_SPEED) { + framesSpeed = MIN_FRAME_SPEED; + } //---------------------------------------------------------------------------------- @@ -59,24 +62,26 @@ pub fn main() anyerror!void { //---------------------------------------------------------------------------------- rl.BeginDrawing(); - rl.ClearBackground(rl.RAYWHITE); + rl.ClearBackground(rl.RAYWHITE); - rl.DrawTexture(scarfy, 15, 40, rl.WHITE); - rl.DrawRectangleLines(15, 40, scarfy.width, scarfy.height, rl.LIME); - rl.DrawRectangleLines(15 + @floatToInt(i32,frameRec.x), 40 + @floatToInt(i32, frameRec.y), @floatToInt(i32,frameRec.width), @floatToInt(i32,frameRec.height), rl.RED); + rl.DrawTexture(scarfy, 15, 40, rl.WHITE); + rl.DrawRectangleLines(15, 40, scarfy.width, scarfy.height, rl.LIME); + rl.DrawRectangleLines(15 + @floatToInt(i32, frameRec.x), 40 + @floatToInt(i32, frameRec.y), @floatToInt(i32, frameRec.width), @floatToInt(i32, frameRec.height), rl.RED); - rl.DrawText("FRAME SPEED: ", 165, 210, 10, rl.DARKGRAY); - rl.DrawText(rl.TextFormat("%02i FPS", framesSpeed), 575, 210, 10, rl.DARKGRAY); - rl.DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, rl.DARKGRAY); + rl.DrawText("FRAME SPEED: ", 165, 210, 10, rl.DARKGRAY); + rl.DrawText(rl.TextFormat("%02i FPS", framesSpeed), 575, 210, 10, rl.DARKGRAY); + rl.DrawText("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, rl.DARKGRAY); - for ([_]u32{0} ** MAX_FRAME_SPEED) |_, i| { - if (i < framesSpeed) {rl.DrawRectangle(250 + 21*@intCast(c_int, i), 205, 20, 20, rl.RED);} - rl.DrawRectangleLines(250 + 21*@intCast(c_int, i), 205, 20, 20, rl.MAROON); + for ([_]u32{0} ** MAX_FRAME_SPEED) |_, i| { + if (i < framesSpeed) { + rl.DrawRectangle(250 + 21 * @intCast(c_int, i), 205, 20, 20, rl.RED); } + rl.DrawRectangleLines(250 + 21 * @intCast(c_int, i), 205, 20, 20, rl.MAROON); + } - rl.DrawTextureRec(scarfy, frameRec, position, rl.WHITE); // Draw part of the texture + rl.DrawTextureRec(scarfy, frameRec, position, rl.WHITE); // Draw part of the texture - rl.DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, rl.GRAY); + rl.DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, rl.GRAY); rl.EndDrawing(); //---------------------------------------------------------------------------------- @@ -84,8 +89,8 @@ pub fn main() anyerror!void { // De-Initialization //-------------------------------------------------------------------------------------- - rl.UnloadTexture(scarfy); // Texture unloading + rl.UnloadTexture(scarfy); // Texture unloading - rl.CloseWindow(); // Close window and OpenGL context + rl.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- } diff --git a/lib/raylib-zig-types.zig b/lib/raylib-zig-types.zig index 0a51702..f29f30f 100755 --- a/lib/raylib-zig-types.zig +++ b/lib/raylib-zig-types.zig @@ -93,16 +93,16 @@ pub const Image = extern struct { return rl.LoadImage(fileName); } - pub fn initRaw(fileName: [*c]const u8, width: c_int, height: c_int, format: PixelFormat, headerSize: c_int) Image { - return rl.LoadImageRaw(fileName, width, height, format, headerSize); + pub fn initRaw(fileName: *const []u8, width: c_int, height: c_int, format: PixelFormat, headerSize: c_int) Image { + return rl.LoadImageRaw(@as([*c]const u8, fileName), width, height, format, headerSize); } - pub fn initText(text: [*c]const u8, fontSize: c_int, color: Color) Image { - return rl.ImageText(text, fontSize, color); + pub fn initText(text: *const []u8, fontSize: c_int, color: Color) Image { + return rl.ImageText(@as([*c]const u8, text), fontSize, color); } - pub fn initTextEx(font: Font, text: [*c]const u8, fontSize: f32, spacing: f32, tint: Color) Image { - return rl.ImageTextEx(font, text, fontSize, spacing, tint); + pub fn initTextEx(font: Font, text: *const []u8, fontSize: f32, spacing: f32, tint: Color) Image { + return rl.ImageTextEx(font, @as([*c]const u8, text), fontSize, spacing, tint); } pub fn copy(image: Image) Image { @@ -113,35 +113,35 @@ pub const Image = extern struct { return rl.ImageFromImage(image, rec); } - pub fn GenColor(width: c_int, height: c_int, color: Color) Image { + pub fn genColor(width: c_int, height: c_int, color: Color) Image { return rl.GenImageColor(width, height, color); } - pub fn GenGradientV(width: c_int, height: c_int, top: Color, bottom: Color) Image { + pub fn genGradientV(width: c_int, height: c_int, top: Color, bottom: Color) Image { return rl.GenImageGradientV(width, height, top, bottom); } - pub fn GenGradientH(width: c_int, height: c_int, left: Color, right: Color) Image { + pub fn genGradientH(width: c_int, height: c_int, left: Color, right: Color) Image { return rl.GenImageGradientH(width, height, left, right); } - pub fn GenGradientRadial(width: c_int, height: c_int, density: f32, inner: Color, outer: Color) Image { + pub fn genGradientRadial(width: c_int, height: c_int, density: f32, inner: Color, outer: Color) Image { return rl.GenImageGradientRadial(width, height, density, inner, outer); } - pub fn GenChecked(width: c_int, height: c_int, checksX: c_int, checksY: c_int, col1: Color, col2: Color) Image { + pub fn genChecked(width: c_int, height: c_int, checksX: c_int, checksY: c_int, col1: Color, col2: Color) Image { return rl.GenImageChecked(width, height, checksX, checksY, col1, col2); } - pub fn GenWhiteNoise(width: c_int, height: c_int, factor: f32) Image { + pub fn genWhiteNoise(width: c_int, height: c_int, factor: f32) Image { return rl.GenImageWhiteNoise(width, height, factor); } - pub fn GenCellular(width: c_int, height: c_int, tileSize: c_int) Image { + pub fn genCellular(width: c_int, height: c_int, tileSize: c_int) Image { return rl.GenImageCellular(width, height, tileSize); } - pub fn UseAsWindowIcon(self: Image) void { + pub fn useAsWindowIcon(self: Image) void { rl.SetWindowIcon(self); } }; @@ -161,11 +161,11 @@ pub const RenderTexture = extern struct { texture: Texture, depth: Texture, - pub fn Begin(self: RenderTexture2D) void { + pub fn begin(self: RenderTexture2D) void { rl.BeginTextureMode(self); } - pub fn End(_: RenderTexture2D) void { + pub fn end(_: RenderTexture2D) void { rl.EndTextureMode(); } }; @@ -204,23 +204,23 @@ pub const Camera3D = extern struct { fovy: f32, projection: CameraProjection, - pub fn Begin(self: Camera3D) void { + pub fn begin(self: Camera3D) void { rl.BeginMode3D(self); } - pub fn Update(self: *Camera3D) void { - rl.UpdateCamera(self); + pub fn update(self: *Camera3D) void { + rl.UpdateCamera(@as([*c]Camera3D, self)); } - pub fn GetMatrix(self: Camera3D) Matrix { + pub fn getMatrix(self: Camera3D) Matrix { return rl.GetCameraMatrix(self); } - pub fn SetMode(self: Camera3D, mode: CameraMode) void { + pub fn setMode(self: Camera3D, mode: CameraMode) void { rl.SetCameraMode(self, mode); } - pub fn End(_: Camera3D) void { + pub fn end(_: Camera3D) void { rl.EndMode3D(); } }; @@ -232,15 +232,15 @@ pub const Camera2D = extern struct { rotation: f32, zoom: f32, - pub fn Begin(self: Camera2D) void { + pub fn begin(self: Camera2D) void { rl.BeginMode2D(self); } - pub fn GetMatrix(self: Camera2D) Matrix { + pub fn getMatrix(self: Camera2D) Matrix { return rl.GetCameraMatrix2D(self); } - pub fn End(_: Camera2D) void { + pub fn end(_: Camera2D) void { rl.EndMode2D(); } }; @@ -261,6 +261,14 @@ pub const Mesh = extern struct { boneWeights: [*c]f32, vaoId: c_uint, vboId: [*c]c_uint, + + pub fn draw(self: Mesh, material: Material, transform: Matrix) void { + rl.DrawMesh(self, material, transform); + } + + pub fn drawInstanced(self: Mesh, material: Material, transforms: []const Matrix) void { + rl.DrawMeshInstanced(self, material, @as([*c]const Matrix, transforms), transforms.len); + } }; pub const Shader = extern struct { diff --git a/lib/raylib-zig.zig b/lib/raylib-zig.zig index f0d1bad..2f78b15 100644 --- a/lib/raylib-zig.zig +++ b/lib/raylib-zig.zig @@ -93,16 +93,16 @@ pub const Image = extern struct { return rl.LoadImage(fileName); } - pub fn initRaw(fileName: [*c]const u8, width: c_int, height: c_int, format: PixelFormat, headerSize: c_int) Image { - return rl.LoadImageRaw(fileName, width, height, format, headerSize); + pub fn initRaw(fileName: *const []u8, width: c_int, height: c_int, format: PixelFormat, headerSize: c_int) Image { + return rl.LoadImageRaw(@as([*c]const u8, fileName), width, height, format, headerSize); } - pub fn initText(text: [*c]const u8, fontSize: c_int, color: Color) Image { - return rl.ImageText(text, fontSize, color); + pub fn initText(text: *const []u8, fontSize: c_int, color: Color) Image { + return rl.ImageText(@as([*c]const u8, text), fontSize, color); } - pub fn initTextEx(font: Font, text: [*c]const u8, fontSize: f32, spacing: f32, tint: Color) Image { - return rl.ImageTextEx(font, text, fontSize, spacing, tint); + pub fn initTextEx(font: Font, text: *const []u8, fontSize: f32, spacing: f32, tint: Color) Image { + return rl.ImageTextEx(font, @as([*c]const u8, text), fontSize, spacing, tint); } pub fn copy(image: Image) Image { @@ -113,35 +113,35 @@ pub const Image = extern struct { return rl.ImageFromImage(image, rec); } - pub fn GenColor(width: c_int, height: c_int, color: Color) Image { + pub fn genColor(width: c_int, height: c_int, color: Color) Image { return rl.GenImageColor(width, height, color); } - pub fn GenGradientV(width: c_int, height: c_int, top: Color, bottom: Color) Image { + pub fn genGradientV(width: c_int, height: c_int, top: Color, bottom: Color) Image { return rl.GenImageGradientV(width, height, top, bottom); } - pub fn GenGradientH(width: c_int, height: c_int, left: Color, right: Color) Image { + pub fn genGradientH(width: c_int, height: c_int, left: Color, right: Color) Image { return rl.GenImageGradientH(width, height, left, right); } - pub fn GenGradientRadial(width: c_int, height: c_int, density: f32, inner: Color, outer: Color) Image { + pub fn genGradientRadial(width: c_int, height: c_int, density: f32, inner: Color, outer: Color) Image { return rl.GenImageGradientRadial(width, height, density, inner, outer); } - pub fn GenChecked(width: c_int, height: c_int, checksX: c_int, checksY: c_int, col1: Color, col2: Color) Image { + pub fn genChecked(width: c_int, height: c_int, checksX: c_int, checksY: c_int, col1: Color, col2: Color) Image { return rl.GenImageChecked(width, height, checksX, checksY, col1, col2); } - pub fn GenWhiteNoise(width: c_int, height: c_int, factor: f32) Image { + pub fn genWhiteNoise(width: c_int, height: c_int, factor: f32) Image { return rl.GenImageWhiteNoise(width, height, factor); } - pub fn GenCellular(width: c_int, height: c_int, tileSize: c_int) Image { + pub fn genCellular(width: c_int, height: c_int, tileSize: c_int) Image { return rl.GenImageCellular(width, height, tileSize); } - pub fn UseAsWindowIcon(self: Image) void { + pub fn useAsWindowIcon(self: Image) void { rl.SetWindowIcon(self); } }; @@ -161,11 +161,11 @@ pub const RenderTexture = extern struct { texture: Texture, depth: Texture, - pub fn Begin(self: RenderTexture2D) void { + pub fn begin(self: RenderTexture2D) void { rl.BeginTextureMode(self); } - pub fn End(_: RenderTexture2D) void { + pub fn end(_: RenderTexture2D) void { rl.EndTextureMode(); } }; @@ -204,23 +204,23 @@ pub const Camera3D = extern struct { fovy: f32, projection: CameraProjection, - pub fn Begin(self: Camera3D) void { + pub fn begin(self: Camera3D) void { rl.BeginMode3D(self); } - pub fn Update(self: *Camera3D) void { - rl.UpdateCamera(self); + pub fn update(self: *Camera3D) void { + rl.UpdateCamera(@as([*c]Camera3D, self)); } - pub fn GetMatrix(self: Camera3D) Matrix { + pub fn getMatrix(self: Camera3D) Matrix { return rl.GetCameraMatrix(self); } - pub fn SetMode(self: Camera3D, mode: CameraMode) void { + pub fn setMode(self: Camera3D, mode: CameraMode) void { rl.SetCameraMode(self, mode); } - pub fn End(_: Camera3D) void { + pub fn end(_: Camera3D) void { rl.EndMode3D(); } }; @@ -232,15 +232,15 @@ pub const Camera2D = extern struct { rotation: f32, zoom: f32, - pub fn Begin(self: Camera2D) void { + pub fn begin(self: Camera2D) void { rl.BeginMode2D(self); } - pub fn GetMatrix(self: Camera2D) Matrix { + pub fn getMatrix(self: Camera2D) Matrix { return rl.GetCameraMatrix2D(self); } - pub fn End(_: Camera2D) void { + pub fn end(_: Camera2D) void { rl.EndMode2D(); } }; @@ -261,6 +261,14 @@ pub const Mesh = extern struct { boneWeights: [*c]f32, vaoId: c_uint, vboId: [*c]c_uint, + + pub fn draw(self: Mesh, material: Material, transform: Matrix) void { + rl.DrawMesh(self, material, transform); + } + + pub fn drawInstanced(self: Mesh, material: Material, transforms: []const Matrix) void { + rl.DrawMeshInstanced(self, material, @as([*c]const Matrix, transforms), transforms.len); + } }; pub const Shader = extern struct {