Reviewed example formating

This commit is contained in:
Ray 2022-07-19 22:06:18 +02:00
parent a3a6b3f24a
commit 4fced50fd1
3 changed files with 85 additions and 82 deletions

View file

@ -2,18 +2,18 @@
* *
* raylib [core] example - 2d camera mouse zoom * raylib [core] example - 2d camera mouse zoom
* *
* This example has been created using raylib 1.5 (www.raylib.com) * This example was originally created with raylib 4.2, last time updated for raylib 4.2
*
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
* *
* Copyright (c) 2022 Jeffery Myers * Copyright (c) 2022 Jeffery Myers
* *
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "rlgl.h"
#include "raymath.h";
#include "rlgl.h"
#include "raymath.h"
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -28,42 +28,44 @@ int main ()
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera mouse zoom"); InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera mouse zoom");
Camera2D camera = { 0 }; Camera2D camera = { 0 };
camera.zoom = 1; camera.zoom = 1.0f;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
while (!WindowShouldClose()) while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// translate based on right click // Update
//----------------------------------------------------------------------------------
// Translate based on mouse right click
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{ {
Vector2 delta = GetMouseDelta(); Vector2 delta = GetMouseDelta();
delta = Vector2Scale(delta, -1.0f / camera.zoom); delta = Vector2Scale(delta, -1.0f/camera.zoom);
camera.target = Vector2Add(camera.target, delta); camera.target = Vector2Add(camera.target, delta);
} }
// zoom based on wheel // Zoom based on mouse wheel
float wheel = GetMouseWheelMove(); float wheel = GetMouseWheelMove();
if (wheel != 0) if (wheel != 0)
{ {
// get the world point that is under the mouse // Get the world point that is under the mouse
Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera);
// set the offset to where the mouse is // Set the offset to where the mouse is
camera.offset = GetMousePosition(); camera.offset = 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 // 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; camera.target = mouseWorldPos;
// zoom // Zoom increment
const float zoomIncrement = 0.125f; const float zoomIncrement = 0.125f;
camera.zoom += wheel * zoomIncrement; camera.zoom += (wheel*zoomIncrement);
if (camera.zoom < zoomIncrement) if (camera.zoom < zoomIncrement) camera.zoom = zoomIncrement;
camera.zoom = zoomIncrement;
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -71,23 +73,24 @@ int main ()
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode2D(camera); BeginMode2D(camera);
// draw the 3d grid, rotated 90 degrees and centered around 0,0 just so we have something in the XY plane // Draw the 3d grid, rotated 90 degrees and centered around 0,0
// just so we have something in the XY plane
rlPushMatrix(); rlPushMatrix();
rlTranslatef(0, 25 * 50, 0); rlTranslatef(0, 25*50, 0);
rlRotatef(90, 1, 0, 0); rlRotatef(90, 1, 0, 0);
DrawGrid(100, 50); DrawGrid(100, 50);
rlPopMatrix(); rlPopMatrix();
// draw a thing // Draw a reference circle
DrawCircle(100, 100, 50, YELLOW); DrawCircle(100, 100, 50, YELLOW);
EndMode2D(); EndMode2D();
DrawText("Right drag to move, mouse wheel to zoom", 2, 2, 20, WHITE); DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, WHITE);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB