Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

289 linhas
9.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib - Advance Game template
  4. *
  5. * <Game title>
  6. * <Game description>
  7. *
  8. * This game has been created using raylib (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2014-2018 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  16. #if defined(PLATFORM_WEB)
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. //----------------------------------------------------------------------------------
  20. // Global Variables Definition (local to this module)
  21. //----------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. // Required variables to manage screen transitions (fade-in, fade-out)
  25. static float transAlpha = 0.0f;
  26. static bool onTransition = false;
  27. static bool transFadeOut = false;
  28. static int transFromScreen = -1;
  29. static int transToScreen = -1;
  30. // NOTE: Some global variables that require to be visible for all screens,
  31. // are defined in screens.h (i.e. currentScreen)
  32. //----------------------------------------------------------------------------------
  33. // Local Functions Declaration
  34. //----------------------------------------------------------------------------------
  35. static void ChangeToScreen(int screen); // No transition effect
  36. static void TransitionToScreen(int screen);
  37. static void UpdateTransition(void);
  38. static void DrawTransition(void);
  39. static void UpdateDrawFrame(void); // Update and Draw one frame
  40. //----------------------------------------------------------------------------------
  41. // Main entry point
  42. //----------------------------------------------------------------------------------
  43. int main(void)
  44. {
  45. // Initialization (Note windowTitle is unused on Android)
  46. //---------------------------------------------------------
  47. InitWindow(screenWidth, screenHeight, "raylib template - advance game");
  48. // Global data loading (assets that must be available in all screens, i.e. fonts)
  49. InitAudioDevice();
  50. font = LoadFont("resources/mecha.png");
  51. music = LoadMusicStream("resources/ambient.ogg");
  52. fxCoin = LoadSound("resources/coin.wav");
  53. SetMusicVolume(music, 1.0f);
  54. PlayMusicStream(music);
  55. // Setup and Init first screen
  56. currentScreen = LOGO;
  57. InitLogoScreen();
  58. #if defined(PLATFORM_WEB)
  59. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  60. #else
  61. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  62. //--------------------------------------------------------------------------------------
  63. // Main game loop
  64. while (!WindowShouldClose()) // Detect window close button or ESC key
  65. {
  66. UpdateDrawFrame();
  67. }
  68. #endif
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. // Unload current screen data before closing
  72. switch (currentScreen)
  73. {
  74. case LOGO: UnloadLogoScreen(); break;
  75. case TITLE: UnloadTitleScreen(); break;
  76. case GAMEPLAY: UnloadGameplayScreen(); break;
  77. case ENDING: UnloadEndingScreen(); break;
  78. default: break;
  79. }
  80. // Unload all global loaded data (i.e. fonts) here!
  81. UnloadFont(font);
  82. UnloadMusicStream(music);
  83. UnloadSound(fxCoin);
  84. CloseAudioDevice(); // Close audio context
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }
  89. //----------------------------------------------------------------------------------
  90. // Module specific Functions Definition
  91. //----------------------------------------------------------------------------------
  92. // Change to next screen, no transition
  93. static void ChangeToScreen(int screen)
  94. {
  95. // Unload current screen
  96. switch (currentScreen)
  97. {
  98. case LOGO: UnloadLogoScreen(); break;
  99. case TITLE: UnloadTitleScreen(); break;
  100. case GAMEPLAY: UnloadGameplayScreen(); break;
  101. case ENDING: UnloadEndingScreen(); break;
  102. default: break;
  103. }
  104. // Init next screen
  105. switch (screen)
  106. {
  107. case LOGO: InitLogoScreen(); break;
  108. case TITLE: InitTitleScreen(); break;
  109. case GAMEPLAY: InitGameplayScreen(); break;
  110. case ENDING: InitEndingScreen(); break;
  111. default: break;
  112. }
  113. currentScreen = screen;
  114. }
  115. // Define transition to next screen
  116. static void TransitionToScreen(int screen)
  117. {
  118. onTransition = true;
  119. transFadeOut = false;
  120. transFromScreen = currentScreen;
  121. transToScreen = screen;
  122. transAlpha = 0.0f;
  123. }
  124. // Update transition effect
  125. static void UpdateTransition(void)
  126. {
  127. if (!transFadeOut)
  128. {
  129. transAlpha += 0.02f;
  130. // NOTE: Due to float internal representation, condition jumps on 1.0f instead of 1.05f
  131. // For that reason we compare against 1.01f, to avoid last frame loading stop
  132. if (transAlpha > 1.01f)
  133. {
  134. transAlpha = 1.0f;
  135. // Unload current screen
  136. switch (transFromScreen)
  137. {
  138. case LOGO: UnloadLogoScreen(); break;
  139. case TITLE: UnloadTitleScreen(); break;
  140. case OPTIONS: UnloadOptionsScreen(); break;
  141. case GAMEPLAY: UnloadGameplayScreen(); break;
  142. case ENDING: UnloadEndingScreen(); break;
  143. default: break;
  144. }
  145. // Load next screen
  146. switch (transToScreen)
  147. {
  148. case LOGO: InitLogoScreen(); break;
  149. case TITLE: InitTitleScreen(); break;
  150. case GAMEPLAY: InitGameplayScreen(); break;
  151. case ENDING: InitEndingScreen(); break;
  152. default: break;
  153. }
  154. currentScreen = transToScreen;
  155. // Activate fade out effect to next loaded screen
  156. transFadeOut = true;
  157. }
  158. }
  159. else // Transition fade out logic
  160. {
  161. transAlpha -= 0.02f;
  162. if (transAlpha < -0.01f)
  163. {
  164. transAlpha = 0.0f;
  165. transFadeOut = false;
  166. onTransition = false;
  167. transFromScreen = -1;
  168. transToScreen = -1;
  169. }
  170. }
  171. }
  172. // Draw transition effect (full-screen rectangle)
  173. static void DrawTransition(void)
  174. {
  175. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  176. }
  177. // Update and draw game frame
  178. static void UpdateDrawFrame(void)
  179. {
  180. // Update
  181. //----------------------------------------------------------------------------------
  182. UpdateMusicStream(music); // NOTE: Music keeps playing between screens
  183. if (!onTransition)
  184. {
  185. switch(currentScreen)
  186. {
  187. case LOGO:
  188. {
  189. UpdateLogoScreen();
  190. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  191. } break;
  192. case TITLE:
  193. {
  194. UpdateTitleScreen();
  195. if (FinishTitleScreen() == 1) TransitionToScreen(OPTIONS);
  196. else if (FinishTitleScreen() == 2) TransitionToScreen(GAMEPLAY);
  197. } break;
  198. case OPTIONS:
  199. {
  200. UpdateOptionsScreen();
  201. if (FinishOptionsScreen()) TransitionToScreen(TITLE);
  202. } break;
  203. case GAMEPLAY:
  204. {
  205. UpdateGameplayScreen();
  206. if (FinishGameplayScreen() == 1) TransitionToScreen(ENDING);
  207. //else if (FinishGameplayScreen() == 2) TransitionToScreen(TITLE);
  208. } break;
  209. case ENDING:
  210. {
  211. UpdateEndingScreen();
  212. if (FinishEndingScreen() == 1) TransitionToScreen(TITLE);
  213. } break;
  214. default: break;
  215. }
  216. }
  217. else UpdateTransition(); // Update transition (fade-in, fade-out)
  218. //----------------------------------------------------------------------------------
  219. // Draw
  220. //----------------------------------------------------------------------------------
  221. BeginDrawing();
  222. ClearBackground(RAYWHITE);
  223. switch(currentScreen)
  224. {
  225. case LOGO: DrawLogoScreen(); break;
  226. case TITLE: DrawTitleScreen(); break;
  227. case OPTIONS: DrawOptionsScreen(); break;
  228. case GAMEPLAY: DrawGameplayScreen(); break;
  229. case ENDING: DrawEndingScreen(); break;
  230. default: break;
  231. }
  232. // Draw full screen rectangle in front of everything
  233. if (onTransition) DrawTransition();
  234. //DrawFPS(10, 10);
  235. EndDrawing();
  236. //----------------------------------------------------------------------------------
  237. }