summaryrefslogtreecommitdiffhomepage
path: root/examples/src/core/core_2d_camera.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/src/core/core_2d_camera.c')
-rw-r--r--examples/src/core/core_2d_camera.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/examples/src/core/core_2d_camera.c b/examples/src/core/core_2d_camera.c
index 6a0b11a..fe8e584 100644
--- a/examples/src/core/core_2d_camera.c
+++ b/examples/src/core/core_2d_camera.c
@@ -13,21 +13,21 @@
#define MAX_BUILDINGS 100
-int main()
+int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
- int screenWidth = 800;
- int screenHeight = 450;
+ const int screenWidth = 800;
+ const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
-
+
Rectangle player = { 400, 280, 40, 40 };
Rectangle buildings[MAX_BUILDINGS];
Color buildColors[MAX_BUILDINGS];
-
+
int spacing = 0;
-
+
for (int i = 0; i < MAX_BUILDINGS; i++)
{
buildings[i].width = GetRandomValue(50, 200);
@@ -36,22 +36,22 @@ int main()
buildings[i].x = -6000 + spacing;
spacing += buildings[i].width;
-
+
buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
}
-
- Camera2D camera;
-
+
+ Camera2D camera = { 0 };
+
camera.target = (Vector2){ player.x + 20, player.y + 20 };
camera.offset = (Vector2){ 0, 0 };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
-
- SetTargetFPS(60);
+
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
- while (!WindowShouldClose()) // Detect window close button or ESC key
+ while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
@@ -65,26 +65,26 @@ int main()
player.x -= 2; // Player movement
camera.offset.x += 2; // Camera displacement with player movement
}
-
+
// Camera target follows player
camera.target = (Vector2){ player.x + 20, player.y + 20 };
-
+
// Camera rotation controls
if (IsKeyDown(KEY_A)) camera.rotation--;
else if (IsKeyDown(KEY_S)) camera.rotation++;
-
+
// Limit camera rotation to 80 degrees (-40 to 40)
- if (camera.rotation > 40) camera.rotation = 40;
+ if (camera.rotation > 40) camera.rotation = 40;
else if (camera.rotation < -40) camera.rotation = -40;
-
+
// Camera zoom controls
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
-
+
if (camera.zoom > 3.0f) camera.zoom = 3.0f;
else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
-
+
// Camera reset (zoom and rotation)
- if (IsKeyPressed(KEY_R))
+ if (IsKeyPressed(KEY_R))
{
camera.zoom = 1.0f;
camera.rotation = 0.0f;
@@ -94,32 +94,32 @@ int main()
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
-
+
ClearBackground(RAYWHITE);
-
+
BeginMode2D(camera);
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
-
+
for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
-
+
DrawRectangleRec(player, RED);
-
+
DrawLine(camera.target.x, -screenHeight*10, camera.target.x, screenHeight*10, GREEN);
DrawLine(-screenWidth*10, camera.target.y, screenWidth*10, camera.target.y, GREEN);
-
+
EndMode2D();
-
+
DrawText("SCREEN AREA", 640, 10, 20, RED);
-
+
DrawRectangle(0, 0, screenWidth, 5, RED);
DrawRectangle(0, 5, 5, screenHeight - 10, RED);
DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
-
+
DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
DrawRectangleLines( 10, 10, 250, 113, BLUE);
-
+
DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
@@ -131,7 +131,7 @@ int main()
}
// De-Initialization
- //--------------------------------------------------------------------------------------
+ //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------