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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/*******************************************************************************************
*
* raylib - transmission mission
*
* Code and transmit the right message
*
* This game has been created using raylib (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "screens/screens.h" // NOTE: Defines global variable: currentScreen
#include <stdlib.h>
#include <stdio.h>
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
//----------------------------------------------------------------------------------
// Global Variables Definition (local to this module)
//----------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
// Required variables to manage screen transitions (fade-in, fade-out)
static float transAlpha = 0.0f;
static bool onTransition = false;
static bool transFadeOut = false;
static int transFromScreen = -1;
static int transToScreen = -1;
// NOTE: Some global variables that require to be visible for all screens,
// are defined in screens.h (i.e. currentScreen)
//----------------------------------------------------------------------------------
// Local Functions Declaration
//----------------------------------------------------------------------------------
static void ChangeToScreen(int screen); // No transition effect
static void TransitionToScreen(int screen);
static void UpdateTransition(void);
static void DrawTransition(void);
static void UpdateDrawFrame(void); // Update and Draw one frame
//----------------------------------------------------------------------------------
// Main entry point
//----------------------------------------------------------------------------------
int main(void)
{
// Initialization
//---------------------------------------------------------
#ifndef PLATFORM_ANDROID
SetConfigFlags(FLAG_SHOW_LOGO); // | FLAG_FULLSCREEN_MODE);
#endif
// Note windowTitle is unused on Android
InitWindow(screenWidth, screenHeight, "raylib game - transmission mission");
// Global data loading (assets that must be available in all screens, i.e. fonts)
InitAudioDevice();
music = LoadMusicStream("resources/audio/music_title.ogg");
fxButton = LoadSound("resources/audio/fx_newspaper.ogg");
SetMusicVolume(music, 1.0f);
PlayMusicStream(music);
fontMission = LoadFontEx("resources/fonts/traveling_typewriter.ttf", 64, 0, 250);
texButton = LoadTexture("resources/textures/title_ribbon.png");
// UI BUTTON
recButton.width = texButton.width;
recButton.height = texButton.height;
recButton.x = screenWidth - recButton.width;
recButton.y = screenHeight - recButton.height - 50;
fadeButton = 0.8f;
colorButton = RED;
textPositionButton = (Vector2){recButton.x + recButton.width/2, recButton.y + recButton.height/2};
fontSizeButton = 30;
textColorButton = WHITE;
currentMission = 0;
totalMissions = 4;
// Setup and Init first screen
currentScreen = LOGO;
InitLogoScreen();
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
UpdateDrawFrame();
}
#endif
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload current screen data before closing
switch (currentScreen)
{
case LOGO: UnloadLogoScreen(); break;
case TITLE: UnloadTitleScreen(); break;
case MISSION: UnloadMissionScreen(); break;
case GAMEPLAY: UnloadGameplayScreen(); break;
case ENDING: UnloadEndingScreen(); break;
default: break;
}
// Unload all global loaded data (i.e. fonts) here!
UnloadMusicStream(music);
UnloadSound(fxButton);
UnloadFont(fontMission);
UnloadTexture(texButton);
CloseAudioDevice(); // Close audio context
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------
// Change to next screen, no transition
static void ChangeToScreen(int screen)
{
// Unload current screen
switch (currentScreen)
{
case LOGO: UnloadLogoScreen(); break;
case TITLE: UnloadTitleScreen(); break;
case MISSION: UnloadMissionScreen(); break;
case GAMEPLAY: UnloadGameplayScreen(); break;
case ENDING: UnloadEndingScreen(); break;
default: break;
}
// Init next screen
switch (screen)
{
case LOGO: InitLogoScreen(); break;
case TITLE: InitTitleScreen(); break;
case MISSION: InitMissionScreen(); break;
case GAMEPLAY: InitGameplayScreen(); break;
case ENDING: InitEndingScreen(); break;
default: break;
}
currentScreen = screen;
}
// Define transition to next screen
static void TransitionToScreen(int screen)
{
onTransition = true;
transFadeOut = false;
transFromScreen = currentScreen;
transToScreen = screen;
transAlpha = 0.0f;
}
// Update transition effect
static void UpdateTransition(void)
{
if (!transFadeOut)
{
transAlpha += 0.02f;
// NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
// For that reason we compare against 1.01f, to avoid last frame loading stop
if (transAlpha > 1.01f)
{
transAlpha = 1.0f;
// Unload current screen
switch (transFromScreen)
{
case LOGO: UnloadLogoScreen(); break;
case TITLE: UnloadTitleScreen(); break;
case MISSION: UnloadMissionScreen(); break;
case GAMEPLAY: UnloadGameplayScreen(); break;
case ENDING: UnloadEndingScreen(); break;
default: break;
}
// Load next screen
switch (transToScreen)
{
case LOGO: InitLogoScreen(); break;
case TITLE: InitTitleScreen(); break;
case MISSION: InitMissionScreen(); break;
case GAMEPLAY: InitGameplayScreen(); break;
case ENDING: InitEndingScreen(); break;
default: break;
}
currentScreen = transToScreen;
// Activate fade out effect to next loaded screen
transFadeOut = true;
}
}
else // Transition fade out logic
{
transAlpha -= 0.02f;
if (transAlpha < -0.01f)
{
transAlpha = 0.0f;
transFadeOut = false;
onTransition = false;
transFromScreen = -1;
transToScreen = -1;
}
}
}
// Draw transition effect (full-screen rectangle)
static void DrawTransition(void)
{
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
}
// Update and draw game frame
static void UpdateDrawFrame(void)
{
// Update
//----------------------------------------------------------------------------------
UpdateMusicStream(music); // NOTE: Music keeps playing between screens
if (!onTransition)
{
switch(currentScreen)
{
case LOGO:
{
UpdateLogoScreen();
if (FinishLogoScreen()) TransitionToScreen(TITLE);
} break;
case TITLE:
{
UpdateTitleScreen();
if (FinishTitleScreen())
{
StopMusicStream(music);
TransitionToScreen(MISSION);
}
} break;
case MISSION:
{
UpdateMissionScreen();
if (FinishMissionScreen())
{
StopMusicStream(music);
TransitionToScreen(GAMEPLAY);
}
} break;
case GAMEPLAY:
{
UpdateGameplayScreen();
if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
//else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
} break;
case ENDING:
{
UpdateEndingScreen();
if (FinishEndingScreen() == 1) // Continue to next mission
{
TransitionToScreen(MISSION);
}
else if (FinishEndingScreen() == 2) // Replay current mission
{
PlayMusicStream(music);
TransitionToScreen(TITLE);
}
} break;
default: break;
}
}
else UpdateTransition(); // Update transition (fade-in, fade-out)
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
switch(currentScreen)
{
case LOGO: DrawLogoScreen(); break;
case TITLE: DrawTitleScreen(); break;
case MISSION: DrawMissionScreen(); break;
case GAMEPLAY: DrawGameplayScreen(); break;
case ENDING: DrawEndingScreen(); break;
default: break;
}
// Draw full screen rectangle in front of everything
if (onTransition) DrawTransition();
//DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// Load missions from text file
Mission *LoadMissions(const char *fileName)
{
Mission *missions = NULL;
char buffer[512];
int missionsCount = 0;
FILE *misFile = fopen(fileName, "rt");
if (misFile == NULL) printf("[%s] Missions file could not be opened\n", fileName);
else
{
// First pass to get total missions count
while (!feof(misFile))
{
fgets(buffer, 512, misFile);
switch (buffer[0])
{
case 't': sscanf(buffer, "t %i", &missionsCount); break;
default: break;
}
}
if (missionsCount > 0) missions = (Mission *)malloc(missionsCount*sizeof(Mission));
else return NULL;
rewind(misFile); // Return to the beginning of the file, to read again
int missionNum = 0;
while (!feof(misFile))
{
fgets(buffer, 512, misFile);
if (missionNum < missionsCount)
{
switch (buffer[0])
{
case 'b':
{
// New mission brief starts!
missions[missionNum].id = missionNum;
sscanf(buffer, "b %[^\n]s", missions[missionNum].brief);
} break;
case 'k': sscanf(buffer, "k %[^\n]s", missions[missionNum].key); break;
case 'm':
{
// NOTE: Message is loaded as is, needs to be processed!
sscanf(buffer, "m %[^\n]s", missions[missionNum].msg);
} break;
case 's':
{
sscanf(buffer, "s %i %i %i %i %i %i %i %i",
&missions[missionNum].sols[0],
&missions[missionNum].sols[1],
&missions[missionNum].sols[2],
&missions[missionNum].sols[3],
&missions[missionNum].sols[4],
&missions[missionNum].sols[5],
&missions[missionNum].sols[6],
&missions[missionNum].sols[7]);
missions[missionNum].wordsCount = 0;
for (int i = 0; i < 8; i++)
{
if (missions[missionNum].sols[i] > -1)
{
missions[missionNum].wordsCount++;
}
}
TraceLog(LOG_WARNING, "Mission %i - Words count %i", missionNum, missions[missionNum].wordsCount);
missionNum++;
} break;
default: break;
}
}
}
if (missionsCount != missionNum) TraceLog(LOG_WARNING, "Missions count and loaded missions don't match!");
}
fclose(misFile);
if (missions != NULL)
{
TraceLog(LOG_INFO, "Missions loaded: %i", missionsCount);
TraceLog(LOG_INFO, "Missions loaded successfully!");
}
return missions;
}
bool IsButtonPressed()
{
if (CheckCollisionPointRec(GetMousePosition(), recButton))
{
fadeButton = 1.0f;
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsGestureDetected(GESTURE_TAP))
{
PlaySound(fxButton);
return true;
}
}
else fadeButton = 0.80f;
return false;
}
void DrawButton(const char *text)
{
//DrawRectangleRec(recButton, Fade(colorButton, fadeButton));
DrawTexturePro(texButton, (Rectangle){0,0,texButton.width, texButton.height}, recButton, (Vector2){0,0},0, Fade(WHITE, fadeButton));
Vector2 measure = MeasureTextEx(fontMission, text, fontSizeButton, 0);
Vector2 textPos = {textPositionButton.x - measure.x/2 + 10, textPositionButton.y - measure.y/2 - 10};
DrawTextEx(fontMission, text, textPos , fontSizeButton, 0, textColorButton);
}
|