You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.5 KiB

5 years ago
5 years ago
  1. /*******************************************************************************************
  2. *
  3. * WAVE COLLECTOR [GLOBAL GAME JAM 2017]
  4. *
  5. * The ultimate wave particles collector is here!
  6. * You must follow the wave and collect all the particles
  7. * The level is actually the wave and the wave is the level!
  8. * Be fast! Be smart! Be the best wave collector!
  9. *
  10. * This game has been created using raylib v1.7 (www.raylib.com)
  11. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  12. *
  13. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  18. #include <stdlib.h>
  19. #include <stdio.h> // Required for: printf()
  20. #include <string.h> // Required for: strcpy()
  21. #if defined(PLATFORM_WEB)
  22. #include <emscripten/emscripten.h>
  23. #endif
  24. //----------------------------------------------------------------------------------
  25. // Global Variables Definition (local to this module)
  26. //----------------------------------------------------------------------------------
  27. const int screenWidth = 1280;
  28. const int screenHeight = 720;
  29. // Required variables to manage screen transitions (fade-in, fade-out)
  30. static float transAlpha = 0.0f;
  31. static bool onTransition = false;
  32. static bool transFadeOut = false;
  33. static int transFromScreen = -1;
  34. static int transToScreen = -1;
  35. // NOTE: Some global variables that require to be visible for all screens,
  36. // are defined in screens.h (i.e. currentScreen)
  37. //----------------------------------------------------------------------------------
  38. // Local Functions Declaration
  39. //----------------------------------------------------------------------------------
  40. static void ChangeToScreen(int screen); // No transition effect
  41. static void TransitionToScreen(int screen);
  42. static void UpdateTransition(void);
  43. static void DrawTransition(void);
  44. static void UpdateDrawFrame(void); // Update and Draw one frame
  45. //----------------------------------------------------------------------------------
  46. // Main entry point
  47. //----------------------------------------------------------------------------------
  48. int main(int argc, char *argv[])
  49. {
  50. // Initialization
  51. //---------------------------------------------------------
  52. #if defined(PLATFORM_DESKTOP)
  53. // TODO: Support for dropped files on the exe
  54. // Support command line argument for custom music file
  55. if (argc > 1)
  56. {
  57. // Just supporting an input argument parameter!!! o__O
  58. if ((IsFileExtension(argv[1], ".ogg")) ||
  59. (IsFileExtension(argv[1], ".wav")))
  60. {
  61. if (sampleFilename != NULL) free(sampleFilename);
  62. sampleFilename = (char *)malloc(256);
  63. strcpy(sampleFilename, argv[1]);
  64. printf("Custom audio file: %s", sampleFilename);
  65. }
  66. }
  67. #endif
  68. #ifndef PLATFORM_ANDROID
  69. SetConfigFlags(FLAG_MSAA_4X_HINT);
  70. #endif
  71. // Note windowTitle is unused on Android
  72. InitWindow(screenWidth, screenHeight, "WAVE COLLECTOR [GGJ17]");
  73. // Global data loading (assets that must be available in all screens, i.e. fonts)
  74. InitAudioDevice();
  75. font = LoadFont("resources/font.fnt");
  76. music = LoadMusicStream("resources/audio/wave.ogg");
  77. SetMusicVolume(music, 1.0f);
  78. // Setup and Init first screen
  79. currentScreen = LOGO;
  80. InitLogoScreen();
  81. //InitTitleScreen();
  82. //InitGameplayScreen();
  83. //InitEndingScreen();
  84. #if defined(PLATFORM_WEB)
  85. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  86. #else
  87. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  88. //--------------------------------------------------------------------------------------
  89. // Main game loop
  90. while (!WindowShouldClose()) // Detect window close button or ESC key
  91. {
  92. UpdateDrawFrame();
  93. }
  94. #endif
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. switch (currentScreen)
  98. {
  99. case LOGO: UnloadLogoScreen(); break;
  100. case TITLE: UnloadTitleScreen(); break;
  101. case GAMEPLAY: UnloadGameplayScreen(); break;
  102. case ENDING: UnloadEndingScreen(); break;
  103. default: break;
  104. }
  105. // Unload all global loaded data (i.e. fonts) here!
  106. UnloadFont(font);
  107. UnloadMusicStream(music);
  108. CloseAudioDevice(); // Close audio context
  109. CloseWindow(); // Close window and OpenGL context
  110. //--------------------------------------------------------------------------------------
  111. return 0;
  112. }
  113. //----------------------------------------------------------------------------------
  114. // Module specific Functions Definition
  115. //----------------------------------------------------------------------------------
  116. // Change to next screen, no transition
  117. static void ChangeToScreen(int screen)
  118. {
  119. // Unload current screen
  120. switch (currentScreen)
  121. {
  122. case LOGO: UnloadLogoScreen(); break;
  123. case TITLE: UnloadTitleScreen(); break;
  124. case GAMEPLAY: UnloadGameplayScreen(); break;
  125. case ENDING: UnloadEndingScreen(); break;
  126. default: break;
  127. }
  128. // Init next screen
  129. switch (screen)
  130. {
  131. case LOGO: InitLogoScreen(); break;
  132. case TITLE: InitTitleScreen(); break;
  133. case GAMEPLAY: InitGameplayScreen(); break;
  134. case ENDING: InitEndingScreen(); break;
  135. default: break;
  136. }
  137. currentScreen = screen;
  138. }
  139. // Define transition to next screen
  140. static void TransitionToScreen(int screen)
  141. {
  142. onTransition = true;
  143. transFadeOut = false;
  144. transFromScreen = currentScreen;
  145. transToScreen = screen;
  146. transAlpha = 0.0f;
  147. }
  148. // Update transition effect
  149. static void UpdateTransition(void)
  150. {
  151. if (!transFadeOut)
  152. {
  153. transAlpha += 0.05f;
  154. // NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
  155. // For that reason we compare against 1.01f, to avoid last frame loading stop
  156. if ((int)transAlpha >= 1)
  157. {
  158. transAlpha = 1.0f;
  159. // Unload current screen
  160. switch (transFromScreen)
  161. {
  162. case LOGO: UnloadLogoScreen(); break;
  163. case TITLE: UnloadTitleScreen(); break;
  164. case GAMEPLAY: UnloadGameplayScreen(); break;
  165. case ENDING: UnloadEndingScreen(); break;
  166. default: break;
  167. }
  168. // Load next screen
  169. switch (transToScreen)
  170. {
  171. case LOGO: InitLogoScreen(); break;
  172. case TITLE: InitTitleScreen(); break;
  173. case GAMEPLAY: InitGameplayScreen(); break;
  174. case ENDING: InitEndingScreen(); break;
  175. default: break;
  176. }
  177. currentScreen = transToScreen;
  178. // Activate fade out effect to next loaded screen
  179. transFadeOut = true;
  180. }
  181. }
  182. else // Transition fade out logic
  183. {
  184. transAlpha -= 0.05f;
  185. if ((int)transAlpha <= 0)
  186. {
  187. transAlpha = 0.0f;
  188. transFadeOut = false;
  189. onTransition = false;
  190. transFromScreen = -1;
  191. transToScreen = -1;
  192. }
  193. }
  194. }
  195. // Draw transition effect (full-screen rectangle)
  196. static void DrawTransition(void)
  197. {
  198. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(RAYWHITE, transAlpha));
  199. }
  200. // Update and draw game frame
  201. static void UpdateDrawFrame(void)
  202. {
  203. // Update
  204. //----------------------------------------------------------------------------------
  205. if (!onTransition)
  206. {
  207. switch(currentScreen)
  208. {
  209. case LOGO:
  210. {
  211. UpdateLogoScreen();
  212. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  213. } break;
  214. case TITLE:
  215. {
  216. UpdateTitleScreen();
  217. if (FinishTitleScreen() == 1) TransitionToScreen(GAMEPLAY);
  218. } break;
  219. case GAMEPLAY:
  220. {
  221. UpdateGameplayScreen();
  222. if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
  223. //else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  224. } break;
  225. case ENDING:
  226. {
  227. UpdateEndingScreen();
  228. if (FinishEndingScreen() == 1) TransitionToScreen(TITLE);
  229. } break;
  230. default: break;
  231. }
  232. }
  233. else UpdateTransition(); // Update transition (fade-in, fade-out)
  234. // TODO: Review! It breaks the game... issues with audio buffering...
  235. if (currentScreen != ENDING) UpdateMusicStream(music);
  236. //----------------------------------------------------------------------------------
  237. // Draw
  238. //----------------------------------------------------------------------------------
  239. BeginDrawing();
  240. ClearBackground(RAYWHITE);
  241. switch(currentScreen)
  242. {
  243. case LOGO: DrawLogoScreen(); break;
  244. case TITLE: DrawTitleScreen(); break;
  245. case GAMEPLAY: DrawGameplayScreen(); break;
  246. case ENDING: DrawEndingScreen(); break;
  247. default: break;
  248. }
  249. // Draw full screen rectangle in front of everything
  250. if (onTransition) DrawTransition();
  251. EndDrawing();
  252. //----------------------------------------------------------------------------------
  253. }