-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2d-camera.c
136 lines (108 loc) · 4.67 KB
/
2d-camera.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/********************************************************************************
* raylib [core] example - 2d camera
*******************************************************************************/
// Librerias
#include <raylib.h>
// Defines
#define MAX_BUILDINGS 100
int main()
{
// Initialization
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "2d camera");
// Jugador y edificios
Rectangle player = {400, 280, 40, 40};
Rectangle buildings[MAX_BUILDINGS] = {0};
Color buildColors[MAX_BUILDINGS] = {0};
int spacing = 0;
// Esta función crea un agrello de 100 edificios con valores y colores
// aleatorios entre cada uno; y los posiciona uno al par del otro.
for(int i = 0; i < MAX_BUILDINGS; i++) {
buildings[i].width = (float)GetRandomValue(50,200);
buildings[i].height = (float)GetRandomValue(100,800);
buildings[i].y = screenHeight - 130.0f - buildings[i].height;
buildings[i].x = -6000.0f + spacing;
spacing += (int)buildings[i].width;
buildColors[i] = (Color){
GetRandomValue(200,240),
GetRandomValue(200, 250),
GetRandomValue(200, 250),
255
};
}
// Camara configuración
Camera2D camera = {0};
camera.target = (Vector2){ player.x + 20.0f, player.y + 20.0f };
camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(60);
while(!WindowShouldClose()) // Detecta el cerrar la ventana
{
// Update
//------------------------------------------------------------
// Movimiento del pesonaje con joystick
if(IsGamepadAvailable(0))
{
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_RIGHT)) player.x += 2;
else if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_FACE_LEFT)) player.x -= 2;
// Control de rotación de la camara
if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_LEFT_TRIGGER_1)) camera.rotation--;
else if(IsGamepadButtonDown(0, GAMEPAD_BUTTON_RIGHT_TRIGGER_1)) camera.rotation++;
// Control de zoom de la camara
camera.zoom += ((float)GetGamepadAxisMovement(0, GAMEPAD_AXIS_RIGHT_Y) * -0.05f);
if(GetGamepadButtonPressed() == 17) {
camera.zoom = 0.1f;
camera.rotation = 0.0f;
}
}
// Movimiento del pesonaje con teclado
if(IsKeyDown(KEY_RIGHT)) player.x += 2;
else if(IsKeyDown(KEY_LEFT)) player.x -= 2;
// control de rotación de la camara
if(IsKeyDown(KEY_A)) camera.rotation--;
else if(IsKeyDown(KEY_S)) camera.rotation++;
// Camara sigue al player
camera.target = (Vector2){ player.x + 20, player.y + 20 };
// Limites de la rotación de camara 80 grados (-40 a 40)
if(camera.rotation > 40) camera.rotation = 40;
else if(camera.rotation < -40) camera.rotation = -40;
// Control de zoom de la camara
camera.zoom += ((float)GetMouseWheelMove() * 0.05f);
// Limites del zoom de la camara
if(camera.zoom > 3.0f) camera.zoom = 3.0f;
else if(camera.zoom < 0.1f) camera.zoom = 0.1f;
// Resetear camara
if(IsKeyPressed(KEY_R)) {
camera.zoom = 0.1f;
camera.rotation = 0.0f;
}
//------------------------------------------------------------
// Draw
//------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
// Dibuja el suelo
DrawRectangle(-6000, 320, 13000, 8000, DARKGRAY);
// Dibuja los edificios
for(int i = 0; i < MAX_BUILDINGS; i++)
DrawRectangleRec(buildings[i], buildColors[i]);
DrawRectangleRec(player, MAROON);
EndMode2D();
DrawText("SCREEN AREA", 640, 10, 20, RED);
DrawRectangle(10, 10, 250, 113, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(10, 10, 250, 113, BLUE);
DrawText("Free 2d camera control", 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);
DrawText("- A / S to Rotate", 40, 80, 10, DARKGRAY);
DrawText("- R to reset Zoom and Rotation", 40, 100, 10, DARKGRAY);
EndDrawing();
}
// De-Initialization
CloseWindow();
// Finaliza el programa
return 0;
}