From 6d52ac4e13503810ec591c711b0e2ccbc64e0ab0 Mon Sep 17 00:00:00 2001 From: YetAnotherCoder00 <65019724+YetAnotherCoder00@users.noreply.github.com> Date: Sat, 17 Aug 2024 14:58:15 +0000 Subject: [PATCH] added 2d camera mouse zoom example (#129) * added 2d camera mouse zoom example * correctly formatted build.zig * removed "gl" const --- build.zig | 6 ++ examples/core/2d_camera_mouse_zoom.zig | 118 +++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 examples/core/2d_camera_mouse_zoom.zig diff --git a/build.zig b/build.zig index b63d869..8a84dbf 100755 --- a/build.zig +++ b/build.zig @@ -220,7 +220,13 @@ pub fn build(b: *std.Build) !void { .name = "3d_camera_first_person", .path = "examples/core/3d_camera_first_person.zig", .desc = "Simple first person demo", + }, + .{ + .name = "2d_camera_mouse_zoom", + .path = "examples/core/2d_camera_mouse_zoom.zig", + .desc = "Shows mouse zoom demo", }, + .{ .name = "window_flags", .path = "examples/core/window_flags.zig", diff --git a/examples/core/2d_camera_mouse_zoom.zig b/examples/core/2d_camera_mouse_zoom.zig new file mode 100644 index 0000000..3ac68a4 --- /dev/null +++ b/examples/core/2d_camera_mouse_zoom.zig @@ -0,0 +1,118 @@ +// port of raylib example https://github.com/raysan5/raylib/blob/master/examples/core/core_2d_camera_mouse_zoom.c + +const rl = @import("raylib"); + +pub fn main() anyerror!void { + // Initialization + //-------------------------------------------------------------------------------------- + const screenWidth = 800; + const screenHeight = 450; + + rl.initWindow(screenWidth, screenHeight, "raylib-zig [core] example - 2d camera mouse zoom"); + defer rl.closeWindow(); // Close window and OpenGL context + + var camera = rl.Camera2D{ + .target = .{ .x = 0, .y = 0 }, + .offset = .{ .x = 0, .y = 0 }, + .zoom = 1.0, + .rotation = 0, + }; + + var zoomMode: i32 = 0; // 0-Mouse Wheel, 1-Mouse Move + + 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 + //---------------------------------------------------------------------------------- + if (rl.isKeyPressed(.key_one)) { + zoomMode = 0; + } else if (rl.isKeyPressed(.key_two)) { + zoomMode = 1; + } + + // Translate based on mouse right click + if (rl.isMouseButtonDown(.mouse_button_right)) { + var delta = rl.getMouseDelta(); + delta = rl.math.vector2Scale(delta, -1.0 / camera.zoom); + camera.target = rl.math.vector2Add(camera.target, delta); + } + + if (zoomMode == 0) { + // Zoom based on mouse wheel + const wheel = rl.getMouseWheelMove(); + if (wheel != 0) { + // Get the world point that is under the mouse + const mouseWorldPos = rl.getScreenToWorld2D(rl.getMousePosition(), camera); + + // Set the offset to where the mouse is + camera.offset = rl.getMousePosition(); + + // Set the target to match, so that the camera maps the world space point + // under the cursor to the screen space point under the cursor at any zoom + camera.target = mouseWorldPos; + + // Zoom increment + var scaleFactor = 1.0 + (0.25 * @abs(wheel)); + if (wheel < 0) { + scaleFactor = 1.0 / scaleFactor; + } + camera.zoom = rl.math.clamp(camera.zoom * scaleFactor, 0.125, 64.0); + } + } else { + // Zoom based on left click + if (rl.isMouseButtonPressed(.mouse_button_left)) { + // Get the world point that is under the mouse + const mouseWorldPos = rl.getScreenToWorld2D(rl.getMousePosition(), camera); + + // Set the offset to where the mouse is + camera.offset = rl.getMousePosition(); + + // Set the target to match, so that the camera maps the world space point + // under the cursor to the screen space point under the cursor at any zoom + camera.target = mouseWorldPos; + } + if (rl.isMouseButtonDown(.mouse_button_left)) { + // Zoom increment + const deltaX = rl.getMouseDelta().x; + var scaleFactor = 1.0 + (0.01 * @abs(deltaX)); + if (deltaX < 0) { + scaleFactor = 1.0 / scaleFactor; + } + camera.zoom = rl.math.clamp(camera.zoom * scaleFactor, 0.125, 64.0); + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + rl.beginDrawing(); + defer rl.endDrawing(); + + rl.clearBackground(rl.Color.ray_white); + + { + camera.begin(); + defer camera.end(); + + rl.gl.rlPushMatrix(); + rl.gl.rlTranslatef(0, 25 * 50, 0); + rl.gl.rlRotatef(90, 1, 0, 0); + rl.drawGrid(100, 50); + rl.gl.rlPopMatrix(); + + rl.drawCircle(screenWidth / 2, screenHeight / 2, 50, rl.Color.maroon); + } + + rl.drawText("[1][2] Select mouse zoom mode (Wheel or Move)", 20, 20, 20, rl.Color.dark_gray); + if (zoomMode == 0) { + rl.drawText("Mouse right button drag to move, mouse wheel to zoom", 20, 50, 20, rl.Color.dark_gray); + } else { + rl.drawText("Mouse right button drag to move, mouse press and move to zoom", 20, 50, 20, rl.Color.dark_gray); + } + + //---------------------------------------------------------------------------------- + } +}