Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

225 rindas
6.9 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 Ramon Santamaria (Ray San - raysan@raysanweb.com)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "screens/screens.h" // NOTE: Defines global variable: currentScreen
  16. //----------------------------------------------------------------------------------
  17. // Global Variables Definition (local to this module)
  18. //----------------------------------------------------------------------------------
  19. // Required variables to manage screen transitions (fade-in, fade-out)
  20. float transAlpha = 0;
  21. bool onTransition = false;
  22. bool transFadeOut = false;
  23. int transFromScreen = -1;
  24. int transToScreen = -1;
  25. //----------------------------------------------------------------------------------
  26. // Local Functions Declaration
  27. //----------------------------------------------------------------------------------
  28. void TransitionToScreen(int screen);
  29. void UpdateTransition(void);
  30. void DrawTransition(void);
  31. //----------------------------------------------------------------------------------
  32. // Main entry point
  33. //----------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //---------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. const char windowTitle[30] = "<game name goes here>";
  41. InitWindow(screenWidth, screenHeight, windowTitle);
  42. // TODO: Load global data here (assets that must be available in all screens, i.e. fonts)
  43. // Setup and Init first screen
  44. currentScreen = LOGO;
  45. InitLogoScreen();
  46. SetTargetFPS(60);
  47. //----------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. if (!onTransition)
  54. {
  55. switch(currentScreen)
  56. {
  57. case LOGO:
  58. {
  59. UpdateLogoScreen();
  60. if (FinishLogoScreen()) TransitionToScreen(TITLE);
  61. } break;
  62. case TITLE:
  63. {
  64. UpdateTitleScreen();
  65. if (FinishTitleScreen() == 1) TransitionToScreen(OPTIONS);
  66. else if (FinishTitleScreen() == 2) TransitionToScreen(GAMEPLAY);
  67. } break;
  68. case OPTIONS:
  69. {
  70. UpdateOptionsScreen();
  71. if (FinishOptionsScreen()) TransitionToScreen(TITLE);
  72. } break;
  73. case GAMEPLAY:
  74. {
  75. UpdateGameplayScreen();
  76. if (FinishGameplayScreen()) TransitionToScreen(ENDING);
  77. } break;
  78. case ENDING:
  79. {
  80. UpdateEndingScreen();
  81. if (FinishEndingScreen()) TransitionToScreen(TITLE);
  82. } break;
  83. default: break;
  84. }
  85. }
  86. else
  87. {
  88. // Update transition (fade-in, fade-out)
  89. UpdateTransition();
  90. }
  91. //----------------------------------------------------------------------------------
  92. // Draw
  93. //----------------------------------------------------------------------------------
  94. BeginDrawing();
  95. ClearBackground(RAYWHITE);
  96. switch(currentScreen)
  97. {
  98. case LOGO: DrawLogoScreen(); break;
  99. case TITLE: DrawTitleScreen(); break;
  100. case OPTIONS: DrawOptionsScreen(); break;
  101. case GAMEPLAY: DrawGameplayScreen(); break;
  102. case ENDING: DrawEndingScreen(); break;
  103. default: break;
  104. }
  105. if (onTransition) DrawTransition();
  106. //DrawFPS(10, 10);
  107. EndDrawing();
  108. //----------------------------------------------------------------------------------
  109. }
  110. // De-Initialization
  111. //--------------------------------------------------------------------------------------
  112. // TODO: Unload all global loaded data (i.e. fonts) here!
  113. CloseWindow(); // Close window and OpenGL context
  114. //--------------------------------------------------------------------------------------
  115. return 0;
  116. }
  117. void TransitionToScreen(int screen)
  118. {
  119. onTransition = true;
  120. transFromScreen = currentScreen;
  121. transToScreen = screen;
  122. }
  123. void UpdateTransition(void)
  124. {
  125. if (!transFadeOut)
  126. {
  127. transAlpha += 0.01;
  128. if (transAlpha >= 1.0)
  129. {
  130. transAlpha = 1.0;
  131. switch (transFromScreen)
  132. {
  133. case LOGO: UnloadLogoScreen(); break;
  134. case TITLE: UnloadTitleScreen(); break;
  135. case OPTIONS: UnloadOptionsScreen(); break;
  136. case GAMEPLAY: UnloadGameplayScreen(); break;
  137. case ENDING: UnloadEndingScreen(); break;
  138. default: break;
  139. }
  140. switch (transToScreen)
  141. {
  142. case LOGO:
  143. {
  144. InitLogoScreen();
  145. currentScreen = LOGO;
  146. } break;
  147. case TITLE:
  148. {
  149. InitTitleScreen();
  150. currentScreen = TITLE;
  151. } break;
  152. case OPTIONS:
  153. {
  154. InitOptionsScreen();
  155. currentScreen = OPTIONS;
  156. } break;
  157. case GAMEPLAY:
  158. {
  159. InitGameplayScreen();
  160. currentScreen = GAMEPLAY;
  161. } break;
  162. case ENDING:
  163. {
  164. InitEndingScreen();
  165. currentScreen = ENDING;
  166. } break;
  167. default: break;
  168. }
  169. transFadeOut = true;
  170. }
  171. }
  172. else // Transition fade out logic
  173. {
  174. transAlpha -= 0.01f;
  175. if (transAlpha <= 0)
  176. {
  177. transAlpha = 0;
  178. transFadeOut = false;
  179. onTransition = false;
  180. transFromScreen = -1;
  181. transToScreen = -1;
  182. }
  183. }
  184. }
  185. void DrawTransition(void)
  186. {
  187. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, transAlpha));
  188. }