@@ -31,6 +31,8 @@ int main ()
31
31
Camera2D camera = { 0 };
32
32
camera .zoom = 1.0f ;
33
33
34
+ int zoomMode = 0 ; // 0-Mouse Wheel, 1-Mouse Move
35
+
34
36
SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
35
37
//--------------------------------------------------------------------------------------
36
38
@@ -39,41 +41,69 @@ int main ()
39
41
{
40
42
// Update
41
43
//----------------------------------------------------------------------------------
44
+ if (IsKeyPressed (KEY_ONE )) zoomMode = 0 ;
45
+ else if (IsKeyPressed (KEY_TWO )) zoomMode = 1 ;
46
+
42
47
// Translate based on mouse right click
43
48
if (IsMouseButtonDown (MOUSE_BUTTON_RIGHT ))
44
49
{
45
50
Vector2 delta = GetMouseDelta ();
46
51
delta = Vector2Scale (delta , -1.0f /camera .zoom );
47
-
48
52
camera .target = Vector2Add (camera .target , delta );
49
53
}
50
54
51
- // Zoom based on mouse wheel
52
- float wheel = GetMouseWheelMove ();
53
- if (wheel != 0 )
55
+ if (zoomMode == 0 )
54
56
{
55
- // Get the world point that is under the mouse
56
- Vector2 mouseWorldPos = GetScreenToWorld2D (GetMousePosition (), camera );
57
-
58
- // Set the offset to where the mouse is
59
- camera .offset = GetMousePosition ();
60
-
61
- // Set the target to match, so that the camera maps the world space point
62
- // under the cursor to the screen space point under the cursor at any zoom
63
- camera .target = mouseWorldPos ;
64
-
65
- // Zoom increment
66
- float scaleFactor = 1.0f + (0.25f * fabsf (wheel ));
67
- if (wheel < 0 ) scaleFactor = 1.0f / scaleFactor ;
68
- camera .zoom = Clamp (camera .zoom * scaleFactor , 0.125 , 64 );
57
+ // Zoom based on mouse wheel
58
+ float wheel = GetMouseWheelMove ();
59
+ if (wheel != 0 )
60
+ {
61
+ // Get the world point that is under the mouse
62
+ Vector2 mouseWorldPos = GetScreenToWorld2D (GetMousePosition (), camera );
63
+
64
+ // Set the offset to where the mouse is
65
+ camera .offset = GetMousePosition ();
66
+
67
+ // Set the target to match, so that the camera maps the world space point
68
+ // under the cursor to the screen space point under the cursor at any zoom
69
+ camera .target = mouseWorldPos ;
70
+
71
+ // Zoom increment
72
+ float scaleFactor = 1.0f + (0.25f * fabsf (wheel ));
73
+ if (wheel < 0 ) scaleFactor = 1.0f /scaleFactor ;
74
+ camera .zoom = Clamp (camera .zoom * scaleFactor , 0.125f , 64.0f );
75
+ }
76
+ }
77
+ else
78
+ {
79
+ // Zoom based on mouse left click
80
+ if (IsMouseButtonPressed (MOUSE_BUTTON_LEFT ))
81
+ {
82
+ // Get the world point that is under the mouse
83
+ Vector2 mouseWorldPos = GetScreenToWorld2D (GetMousePosition (), camera );
84
+
85
+ // Set the offset to where the mouse is
86
+ camera .offset = GetMousePosition ();
87
+
88
+ // Set the target to match, so that the camera maps the world space point
89
+ // under the cursor to the screen space point under the cursor at any zoom
90
+ camera .target = mouseWorldPos ;
91
+ }
92
+ if (IsMouseButtonDown (MOUSE_BUTTON_LEFT ))
93
+ {
94
+ // Zoom increment
95
+ float deltaX = GetMouseDelta ().x ;
96
+ float scaleFactor = 1.0f + (0.01f * fabsf (deltaX ));
97
+ if (deltaX < 0 ) scaleFactor = 1.0f /scaleFactor ;
98
+ camera .zoom = Clamp (camera .zoom * scaleFactor , 0.125f , 64.0f );
99
+ }
69
100
}
70
-
71
101
//----------------------------------------------------------------------------------
72
102
73
103
// Draw
74
104
//----------------------------------------------------------------------------------
75
105
BeginDrawing ();
76
- ClearBackground (BLACK );
106
+ ClearBackground (RAYWHITE );
77
107
78
108
BeginMode2D (camera );
79
109
@@ -86,11 +116,13 @@ int main ()
86
116
rlPopMatrix ();
87
117
88
118
// Draw a reference circle
89
- DrawCircle (100 , 100 , 50 , YELLOW );
119
+ DrawCircle (GetScreenWidth ()/ 2 , GetScreenHeight ()/ 2 , 50 , MAROON );
90
120
91
121
EndMode2D ();
92
122
93
- DrawText ("Mouse right button drag to move, mouse wheel to zoom" , 10 , 10 , 20 , WHITE );
123
+ DrawText ("[1][2] Select mouse zoom mode (Wheel or Move)" , 20 , 20 , 20 , DARKGRAY );
124
+ if (zoomMode == 0 ) DrawText ("Mouse right button drag to move, mouse wheel to zoom" , 20 , 50 , 20 , DARKGRAY );
125
+ else DrawText ("Mouse right button drag to move, mouse press and move to zoom" , 20 , 50 , 20 , DARKGRAY );
94
126
95
127
EndDrawing ();
96
128
//----------------------------------------------------------------------------------
0 commit comments