1/*******************************************************************************************
2*
3* raylib [core] example - 2d camera
4*
5* This example has been created using raylib 1.5 (www.raylib.com)
6* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
7*
8* Copyright (c) 2016 Ramon Santamaria (@raysan5)
9*
10********************************************************************************************/
11
12#include "raylib.h"
13
14#define MAX_BUILDINGS 100
15
16int main(void)
17{
18 // Initialization
19 //--------------------------------------------------------------------------------------
20 const int screenWidth = 800;
21 const int screenHeight = 450;
22
23 InitWindow(screenWidth, screenHeight, "raylib [core] example - 2d camera");
24
25 Rectangle player = { 400, 280, 40, 40 };
26 Rectangle buildings[MAX_BUILDINGS] = { 0 };
27 Color buildColors[MAX_BUILDINGS] = { 0 };
28
29 int spacing = 0;
30
31 for (int i = 0; i < MAX_BUILDINGS; i++)
32 {
33 buildings[i].width = GetRandomValue(50, 200);
34 buildings[i].height = GetRandomValue(100, 800);
35 buildings[i].y = screenHeight - 130 - buildings[i].height;
36 buildings[i].x = -6000 + spacing;
37
38 spacing += buildings[i].width;
39
40 buildColors[i] = (Color){ GetRandomValue(200, 240), GetRandomValue(200, 240), GetRandomValue(200, 250), 255 };
41 }
42
43 Camera2D camera = { 0 };
44 camera.target = (Vector2){ player.x + 20, player.y + 20 };
45 camera.offset = (Vector2){ screenWidth/2, screenHeight/2 };
46 camera.rotation = 0.0f;
47 camera.zoom = 1.0f;
48
49 SetTargetFPS(60); // Set our game to run at 60 frames-per-second
50 //--------------------------------------------------------------------------------------
51
52 // Main game loop
53 while (!WindowShouldClose()) // Detect window close button or ESC key
54 {
55 // Update
56 //----------------------------------------------------------------------------------
57
58 // Player movement
59 if (IsKeyDown(KEY_RIGHT)) player.x += 2;
60 else if (IsKeyDown(KEY_LEFT)) player.x -= 2;
61
62 // Camera target follows player
63 camera.target = (Vector2){ player.x + 20, player.y + 20 };
64
65 // Camera rotation controls
66 if (IsKeyDown(KEY_A)) camera.rotation--;
67 else if (IsKeyDown(KEY_S)) camera.rotation++;
68
69 // Limit camera rotation to 80 degrees (-40 to 40)
70 if (camera.rotation > 40) camera.rotation = 40;
71 else if (camera.rotation < -40) camera.rotation = -40;
72
73 // Camera zoom controls
74 camera.zoom += ((float)GetMouseWheelMove()*0.05f);
75
76 if (camera.zoom > 3.0f) camera.zoom = 3.0f;
77 else if (camera.zoom < 0.1f) camera.zoom = 0.1f;
78
79 // Camera reset (zoom and rotation)
80 if (IsKeyPressed(KEY_R))
81 {
82 camera.zoom = 1.0f;
83 camera.rotation = 0.0f;
84 }
85 //----------------------------------------------------------------------------------
86
87 // Draw
88 //----------------------------------------------------------------------------------
89 BeginDrawing();
90
91 ClearBackground(RAYWHITE);
92
93 BeginMode2D(camera);
94
95 DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
96
97 for (int i = 0; i < MAX_BUILDINGS; i++) DrawRectangleRec(buildings[i], buildColors[i]);
98
99 DrawRectangleRec(player, RED);
100
101 DrawLine(camera.target.x, -screenHeight*10, camera.target.x, screenHeight*10, GREEN);
102 DrawLine(-screenWidth*10, camera.target.y, screenWidth*10, camera.target.y, GREEN);
103
104 EndMode2D();
105
106 DrawText("SCREEN AREA", 640, 10, 20, RED);
107
108 DrawRectangle(0, 0, screenWidth, 5, RED);
109 DrawRectangle(0, 5, 5, screenHeight - 10, RED);
110 DrawRectangle(screenWidth - 5, 5, 5, screenHeight - 10, RED);
111 DrawRectangle(0, screenHeight - 5, screenWidth, 5, RED);
112
113 DrawRectangle( 10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
114 DrawRectangleLines( 10, 10, 250, 113, BLUE);
115
116 DrawText("Free 2d camera controls:", 20, 20, 10, BLACK);
117 DrawText("- Right/Left to move Offset", 40, 40, 10, DARKGRAY);
118 DrawText("- Mouse Wheel to Zoom in-out", 40, 60, 10, DARKGRAY);
119 DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
120 DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
121
122 EndDrawing();
123 //----------------------------------------------------------------------------------
124 }
125
126 // De-Initialization
127 //--------------------------------------------------------------------------------------
128 CloseWindow(); // Close window and OpenGL context
129 //--------------------------------------------------------------------------------------
130
131 return 0;
132}
133