-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame2.cpp
More file actions
296 lines (246 loc) · 12.9 KB
/
Copy pathGame2.cpp
File metadata and controls
296 lines (246 loc) · 12.9 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "raylib.h"
#include <cmath>
#if defined(PLATFORM_WEB)
#include <emscripten.h>
#endif
enum GameState {
STATE_THUMBNAIL,
STATE_COVER_LOADER,
STATE_GAMEPLAY
};
struct GameEngine {
const int virtualWidth = 1280;
const int virtualHeight = 720;
RenderTexture2D target;
GameState currentState = STATE_THUMBNAIL;
float stateTimer = 0.0f;
float globalAlpha = 1.0f;
float targetAlpha = 1.0f;
float loadingProgress = 0.0f;
float lerpedProgress = 0.0f;
Texture2D thumbTexture;
Texture2D coverTexture;
bool hasThumb = false;
bool hasCover = false;
float carX = 570.0f;
};
GameEngine game;
void UpdateAndDrawFrame() {
float deltaTime = GetFrameTime();
game.stateTimer += deltaTime;
if (IsKeyPressed(KEY_F)) {
ToggleFullscreen();
}
float scale = (float)GetScreenWidth() / game.virtualWidth;
float scaleY = (float)GetScreenHeight() / game.virtualHeight;
if (scaleY < scale) scale = scaleY;
Vector2 rawMouse = GetMousePosition();
Vector2 virtualMouse = { 0 };
virtualMouse.x = (rawMouse.x - (GetScreenWidth() - (game.virtualWidth * scale)) * 0.5f) / scale;
virtualMouse.y = (rawMouse.y - (GetScreenHeight() - (game.virtualHeight * scale)) * 0.5f) / scale;
if (virtualMouse.x < 0) virtualMouse.x = 0;
if (virtualMouse.x > game.virtualWidth) virtualMouse.x = (float)game.virtualWidth;
if (virtualMouse.y < 0) virtualMouse.y = 0;
if (virtualMouse.y > game.virtualHeight) virtualMouse.y = (float)game.virtualHeight;
game.globalAlpha += (game.targetAlpha - game.globalAlpha) * 8.0f * deltaTime;
switch (game.currentState) {
case STATE_THUMBNAIL: {
if (game.stateTimer >= 2.0f) {
game.targetAlpha = 0.0f;
if (game.globalAlpha <= 0.05f) {
game.currentState = STATE_COVER_LOADER;
game.stateTimer = 0.0f;
game.targetAlpha = 1.0f;
}
}
break;
}
case STATE_COVER_LOADER: {
game.loadingProgress += deltaTime * 45.0f;
if (game.loadingProgress >= 100.0f) game.loadingProgress = 100.0f;
game.lerpedProgress += (game.loadingProgress - game.lerpedProgress) * 6.0f * deltaTime;
if (game.loadingProgress >= 99.9f) {
float pulse = sinf(game.stateTimer * 5.0f) * 4.0f;
Rectangle playButton = {
(float)game.virtualWidth / 2.0f - 150.0f - (pulse * 0.5f),
(float)game.virtualHeight - 120.0f - (pulse * 0.5f),
300.0f + pulse,
60.0f + pulse
};
bool isHovered = CheckCollisionPointRec(virtualMouse, playButton);
bool isPressed = (isHovered && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) ||
IsKeyPressed(KEY_SPACE) ||
IsGestureDetected(GESTURE_TAP) ||
IsGamepadButtonPressed(0, GAMEPAD_BUTTON_MIDDLE_RIGHT);
if (isPressed) {
game.targetAlpha = 0.0f;
}
if (game.targetAlpha == 0.0f && game.globalAlpha <= 0.05f) {
game.currentState = STATE_GAMEPLAY;
game.targetAlpha = 1.0f;
}
}
break;
}
case STATE_GAMEPLAY: {
float moveInput = 0.0f;
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) moveInput = 1.0f;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) moveInput = -1.0f;
if (IsGamepadAvailable(0)) {
float axisX = GetGamepadAxisMovement(0, GAMEPAD_AXIS_LEFT_X);
if (axisX > 0.2f || axisX < -0.2f) moveInput = axisX;
}
Rectangle leftTouchArea = { 40.0f, (float)game.virtualHeight - 140.0f, 120.0f, 100.0f };
Rectangle rightTouchArea = { 180.0f, (float)game.virtualHeight - 140.0f, 120.0f, 100.0f };
int touchCount = GetTouchPointCount();
for (int i = 0; i < touchCount; i++) {
Vector2 touchPos = GetTouchPosition(i);
Vector2 vTouch = {
(touchPos.x - (GetScreenWidth() - (game.virtualWidth * scale)) * 0.5f) / scale,
(touchPos.y - (GetScreenHeight() - (game.virtualHeight * scale)) * 0.5f) / scale
};
if (CheckCollisionPointRec(vTouch, leftTouchArea)) moveInput = -1.0f;
if (CheckCollisionPointRec(vTouch, rightTouchArea)) moveInput = 1.0f;
}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
if (CheckCollisionPointRec(virtualMouse, leftTouchArea)) moveInput = -1.0f;
if (CheckCollisionPointRec(virtualMouse, rightTouchArea)) moveInput = 1.0f;
}
game.carX += moveInput * 350.0f * deltaTime;
if (game.carX < 0) game.carX = 0;
if (game.carX > game.virtualWidth - 140) game.carX = game.virtualWidth - 140;
break;
}
}
BeginTextureMode(game.target);
ClearBackground(GetColor(0x181818FF));
switch (game.currentState) {
case STATE_THUMBNAIL: {
if (game.hasThumb) {
DrawTexturePro(game.thumbTexture,
{ 0, 0, (float)game.thumbTexture.width, (float)game.thumbTexture.height },
{ 0, 0, (float)game.virtualWidth, (float)game.virtualHeight },
{ 0, 0 }, 0.0f, Fade(WHITE, game.globalAlpha));
} else {
ClearBackground(Fade(BLACK, game.globalAlpha));
DrawText("DRIVE MAD", game.virtualWidth / 2 - 140, game.virtualHeight / 2 - 40, 48, Fade(RED, game.globalAlpha));
DrawText("Loading Preview...", game.virtualWidth / 2 - 80, game.virtualHeight / 2 + 30, 20, Fade(LIGHTGRAY, game.globalAlpha));
}
break;
}
case STATE_COVER_LOADER: {
ClearBackground(GetColor(0x111111FF));
int boxWidth = 420;
int boxHeight = 520;
int boxX = game.virtualWidth / 2 - boxWidth / 2;
int boxY = game.virtualHeight / 2 - boxHeight / 2;
DrawRectangleRounded({(float)boxX, (float)boxY, (float)boxWidth, (float)boxHeight}, 0.05f, 4, Fade(GetColor(0x1E1E1EFF), game.globalAlpha));
DrawRectangleRoundedLines({(float)boxX, (float)boxY, (float)boxWidth, (float)boxHeight}, 0.05f, 4, 2.0f, Fade(GRAY, game.globalAlpha));
if (game.hasCover) {
DrawTexturePro(game.coverTexture,
{ 0, 0, (float)game.coverTexture.width, (float)game.coverTexture.height },
{ (float)boxX + 20, (float)boxY + 20, (float)boxWidth - 40, (float)boxHeight * 0.55f },
{ 0, 0 }, 0.0f, Fade(WHITE, game.globalAlpha));
} else {
DrawRectangle(boxX + 20, boxY + 20, boxWidth - 40, boxHeight * 0.55f, Fade(DARKGRAY, game.globalAlpha));
DrawText("COVER PREVIEW", boxX + 110, boxY + 120, 20, Fade(LIGHTGRAY, game.globalAlpha));
}
DrawText("Drive Mad", boxX + 24, boxY + (boxHeight * 0.62f), 32, Fade(WHITE, game.globalAlpha));
DrawText("by Fancade", boxX + 24, boxY + (boxHeight * 0.62f) + 40, 18, Fade(GRAY, game.globalAlpha));
int barWidth = boxWidth - 48;
int barHeight = 12;
int barX = boxX + 24;
int barY = boxY + boxHeight - 140;
DrawRectangleRounded({(float)barX, (float)barY, (float)barWidth, (float)barHeight}, 1.0f, 4, Fade(GetColor(0x2A2A2AFF), game.globalAlpha));
DrawRectangleRounded({(float)barX, (float)barY, (float)barWidth * (game.lerpedProgress / 100.0f), (float)barHeight}, 1.0f, 4, Fade(RED, game.globalAlpha));
if (game.loadingProgress >= 100.0f) {
float pulse = sinf(game.stateTimer * 5.0f) * 4.0f;
Rectangle playButton = {
(float)game.virtualWidth / 2.0f - 150.0f - (pulse * 0.5f),
(float)game.virtualHeight - 120.0f - (pulse * 0.5f),
300.0f + pulse,
60.0f + pulse
};
bool isHovered = CheckCollisionPointRec(virtualMouse, playButton);
Color btnColor = isHovered ? (IsMouseButtonDown(MOUSE_BUTTON_LEFT) ? RED : MAROON) : GetColor(0xE11D48FF);
DrawRectangleRounded(playButton, 0.25f, 4, Fade(btnColor, game.globalAlpha));
DrawRectangleRoundedLines(playButton, 0.25f, 4, 2.0f, Fade(WHITE, game.globalAlpha));
DrawText("START MISSION", playButton.x + (playButton.width - MeasureText("START MISSION", 20)) / 2.0f, playButton.y + (playButton.height - 20) / 2.0f, 20, Fade(WHITE, game.globalAlpha));
}
break;
}
case STATE_GAMEPLAY: {
ClearBackground(SKYBLUE);
DrawRectangle(0, game.virtualHeight - 120, game.virtualWidth, 120, DARKGREEN);
DrawText("DRIVE MAD (C++ Scale Engine)", 40, 40, 28, DARKGRAY);
DrawText("Keyboard: A/D | Gamepad: Left Stick | Touch UI", 40, 80, 18, GRAY);
Rectangle leftTouchArea = { 40.0f, (float)game.virtualHeight - 140.0f, 120.0f, 100.0f };
Rectangle rightTouchArea = { 180.0f, (float)game.virtualHeight - 140.0f, 120.0f, 100.0f };
bool touchingLeft = false;
bool touchingRight = false;
int touchCount = GetTouchPointCount();
for (int i = 0; i < touchCount; i++) {
Vector2 touchPos = GetTouchPosition(i);
Vector2 vTouch = {
(touchPos.x - (GetScreenWidth() - (game.virtualWidth * scale)) * 0.5f) / scale,
(touchPos.y - (GetScreenHeight() - (game.virtualHeight * scale)) * 0.5f) / scale
};
if (CheckCollisionPointRec(vTouch, leftTouchArea)) touchingLeft = true;
if (CheckCollisionPointRec(vTouch, rightTouchArea)) touchingRight = true;
}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
if (CheckCollisionPointRec(virtualMouse, leftTouchArea)) touchingLeft = true;
if (CheckCollisionPointRec(virtualMouse, rightTouchArea)) touchingRight = true;
}
DrawRectangleRounded(leftTouchArea, 0.2f, 4, touchingLeft ? GetColor(0x333333AA) : GetColor(0x22222277));
DrawRectangleRoundedLines(leftTouchArea, 0.2f, 4, 2.0f, WHITE);
DrawText("<", leftTouchArea.x + 48, leftTouchArea.y + 30, 40, WHITE);
DrawRectangleRounded(rightTouchArea, 0.2f, 4, touchingRight ? GetColor(0x333333AA) : GetColor(0x22222277));
DrawRectangleRoundedLines(rightTouchArea, 0.2f, 4, 2.0f, WHITE);
DrawText(">", rightTouchArea.x + 48, rightTouchArea.y + 30, 40, WHITE);
DrawRectangle(game.carX, game.virtualHeight - 180, 140, 45, RED);
DrawRectangle(game.carX + 35, game.virtualHeight - 215, 70, 35, LIGHTGRAY);
DrawCircle(game.carX + 30, game.virtualHeight - 130, 25, BLACK);
DrawCircle(game.carX + 110, game.virtualHeight - 130, 25, BLACK);
break;
}
}
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(game.target.texture,
{ 0.0f, 0.0f, (float)game.target.texture.width, (float)-game.target.texture.height },
{ (GetScreenWidth() - ((float)game.virtualWidth * scale)) * 0.5f,
(GetScreenHeight() - ((float)game.virtualHeight * scale)) * 0.5f,
(float)game.virtualWidth * scale,
(float)game.virtualHeight * scale },
{ 0, 0 }, 0.0f, WHITE);
EndDrawing();
}
int main() {
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
InitWindow(game.virtualWidth, game.virtualHeight, "Drive Mad - UX Edition");
game.target = LoadRenderTexture(game.virtualWidth, game.virtualHeight);
SetTextureFilter(game.target.texture, TEXTURE_FILTER_BILINEAR);
Image thumbImage = LoadImage("thumbnail.png");
game.hasThumb = (thumbImage.data != nullptr);
if (game.hasThumb) game.thumbTexture = LoadTextureFromImage(thumbImage);
UnloadImage(thumbImage);
Image coverImage = LoadImage("cover.png");
game.hasCover = (coverImage.data != nullptr);
if (game.hasCover) game.coverTexture = LoadTextureFromImage(coverImage);
UnloadImage(coverImage);
SetTargetFPS(60);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateAndDrawFrame, 0, 1);
#else
while (!WindowShouldClose()) {
UpdateAndDrawFrame();
}
#endif
if (game.hasThumb) UnloadTexture(game.thumbTexture);
if (game.hasCover) UnloadTexture(game.coverTexture);
UnloadRenderTexture(game.target);
CloseWindow();
return 0;
}