From 39ead974a4f7615c5160649b3d48503e3a0e7936 Mon Sep 17 00:00:00 2001 From: Jeffery Myers Date: Tue, 19 Jul 2022 12:55:10 -0700 Subject: [PATCH] add mouse zoom example (#2583) --- examples/Makefile | 1 + examples/core/core_2d_camera_mouse_zoom.c | 101 +++++ .../core_2d_camera_mouse_zoom.vcxproj | 390 ++++++++++++++++++ projects/VS2022/raylib.sln | 19 + 4 files changed, 511 insertions(+) create mode 100644 examples/core/core_2d_camera_mouse_zoom.c create mode 100644 projects/VS2022/examples/core_2d_camera_mouse_zoom.vcxproj diff --git a/examples/Makefile b/examples/Makefile index 17ddafd05..063ad52c1 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -375,6 +375,7 @@ CORE = \ core/core_input_gestures \ core/core_2d_camera \ core/core_2d_camera_platformer \ + core/core_2d_camera_mouse_zoom \ core/core_3d_camera_mode \ core/core_3d_camera_free \ core/core_3d_camera_first_person \ diff --git a/examples/core/core_2d_camera_mouse_zoom.c b/examples/core/core_2d_camera_mouse_zoom.c new file mode 100644 index 000000000..112d6152f --- /dev/null +++ b/examples/core/core_2d_camera_mouse_zoom.c @@ -0,0 +1,101 @@ +/******************************************************************************************* +* +* raylib [core] example - 2d camera mouse zoom +* +* This example has been created using raylib 1.5 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2022 Jeffery Myers +* +********************************************************************************************/ + + +#include "raylib.h" +#include "rlgl.h" +#include "raymath.h"; + + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main () +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera mouse zoom"); + + Camera2D camera = { 0 }; + camera.zoom = 1; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) + { + // translate based on right click + if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) + { + Vector2 delta = GetMouseDelta(); + delta = Vector2Scale(delta, -1.0f / camera.zoom); + + camera.target = Vector2Add(camera.target, delta); + } + + // zoom based on wheel + float wheel = GetMouseWheelMove(); + if (wheel != 0) + { + // get the world point that is under the mouse + Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); + + // set the offset to where the mouse is + 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 + camera.target = mouseWorldPos; + + // zoom + const float zoomIncrement = 0.125f; + + camera.zoom += wheel * zoomIncrement; + if (camera.zoom < zoomIncrement) + camera.zoom = zoomIncrement; + } + + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + BeginDrawing(); + ClearBackground(BLACK); + + BeginMode2D(camera); + + // draw the 3d grid, rotated 90 degrees and centered around 0,0 just so we have something in the XY plane + rlPushMatrix(); + rlTranslatef(0, 25 * 50, 0); + rlRotatef(90, 1, 0, 0); + DrawGrid(100, 50); + rlPopMatrix(); + + // draw a thing + DrawCircle(100, 100, 50, YELLOW); + EndMode2D(); + + DrawText("Right drag to move, mouse wheel to zoom", 2, 2, 20, WHITE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + return 0; +} \ No newline at end of file diff --git a/projects/VS2022/examples/core_2d_camera_mouse_zoom.vcxproj b/projects/VS2022/examples/core_2d_camera_mouse_zoom.vcxproj new file mode 100644 index 000000000..2552fa98c --- /dev/null +++ b/projects/VS2022/examples/core_2d_camera_mouse_zoom.vcxproj @@ -0,0 +1,390 @@ + + + + + Debug.DLL + Win32 + + + Debug.DLL + x64 + + + Debug + Win32 + + + Debug + x64 + + + Release.DLL + Win32 + + + Release.DLL + x64 + + + Release + Win32 + + + Release + x64 + + + + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359} + Win32Proj + core_2d_camera_mouse_zoom + 10.0 + core_2d_camera_mouse_zoom + + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + true + $(DefaultPlatformToolset) + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + Application + false + $(DefaultPlatformToolset) + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + true + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + false + $(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)\ + $(SolutionDir)\build\$(ProjectName)\obj\$(Platform)\$(Configuration)\ + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + $(SolutionDir)..\..\examples\core + WindowsLocalDebugger + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + /FS %(AdditionalOptions) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + Copy Debug DLL to output directory + + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;PLATFORM_DESKTOP;%(PreprocessorDefinitions) + CompileAsC + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + + + Console + true + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + Copy Debug DLL to output directory + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + + + Copy Release DLL to output directory + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);PLATFORM_DESKTOP + $(SolutionDir)..\..\src;%(AdditionalIncludeDirectories) + CompileAsC + true + + + Console + true + true + true + raylib.lib;opengl32.lib;kernel32.lib;user32.lib;gdi32.lib;winmm.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + $(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\ + + + xcopy /y /d "$(SolutionDir)\build\raylib\bin\$(Platform)\$(Configuration)\raylib.dll" "$(SolutionDir)\build\$(ProjectName)\bin\$(Platform)\$(Configuration)" + + + Copy Release DLL to output directory + + + + + + + + + + + {e89d61ac-55de-4482-afd4-df7242ebc859} + + + + + + \ No newline at end of file diff --git a/projects/VS2022/raylib.sln b/projects/VS2022/raylib.sln index 24c73e6cb..056e303c5 100644 --- a/projects/VS2022/raylib.sln +++ b/projects/VS2022/raylib.sln @@ -251,6 +251,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "textures_fog_of_war", "exam EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "textures_gif_player", "examples\textures_gif_player.vcxproj", "{191A5289-BA65-4638-A215-C521F0187313}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core_2d_camera_mouse_zoom", "examples\core_2d_camera_mouse_zoom.vcxproj", "{3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug.DLL|x64 = Debug.DLL|x64 @@ -2099,6 +2101,22 @@ Global {191A5289-BA65-4638-A215-C521F0187313}.Release|x64.Build.0 = Release|x64 {191A5289-BA65-4638-A215-C521F0187313}.Release|x86.ActiveCfg = Release|Win32 {191A5289-BA65-4638-A215-C521F0187313}.Release|x86.Build.0 = Release|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug.DLL|x64.ActiveCfg = Debug.DLL|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug.DLL|x64.Build.0 = Debug.DLL|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug.DLL|x86.ActiveCfg = Debug.DLL|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug.DLL|x86.Build.0 = Debug.DLL|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug|x64.ActiveCfg = Debug|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug|x64.Build.0 = Debug|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug|x86.ActiveCfg = Debug|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Debug|x86.Build.0 = Debug|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release.DLL|x64.ActiveCfg = Release.DLL|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release.DLL|x64.Build.0 = Release.DLL|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release.DLL|x86.ActiveCfg = Release.DLL|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release.DLL|x86.Build.0 = Release.DLL|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release|x64.ActiveCfg = Release|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release|x64.Build.0 = Release|x64 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release|x86.ActiveCfg = Release|Win32 + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2226,6 +2244,7 @@ Global {3FE7E9B6-49AC-4246-A789-28DB4644567B} = {6C82BAAE-BDDF-457D-8FA8-7E2490B07035} {EBBBF4A0-2DA2-4DE6-B4FE-C6654A2417A0} = {DA049009-21FF-4AC0-84E4-830DD1BCD0CE} {191A5289-BA65-4638-A215-C521F0187313} = {DA049009-21FF-4AC0-84E4-830DD1BCD0CE} + {3CFF7AB8-32CB-4D6D-9FED-53DBEF277359} = {6C82BAAE-BDDF-457D-8FA8-7E2490B07035} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E926C768-6307-4423-A1EC-57E95B1FAB29}