core_automation_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - automation events
  4. *
  5. * Example originally created with raylib 5.0, last time updated with raylib 5.0
  6. *
  7. * Example based on 2d_camera_platformer example by arvyy (@arvyy)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2023 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #define GRAVITY 400
  18. #define PLAYER_JUMP_SPD 350.0f
  19. #define PLAYER_HOR_SPD 200.0f
  20. #define MAX_ENVIRONMENT_ELEMENTS 5
  21. typedef struct Player {
  22. Vector2 position;
  23. float speed;
  24. bool canJump;
  25. } Player;
  26. typedef struct EnvElement {
  27. Rectangle rect;
  28. int blocking;
  29. Color color;
  30. } EnvElement;
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [core] example - automation events");
  41. // Define player
  42. Player player = { 0 };
  43. player.position = (Vector2){ 400, 280 };
  44. player.speed = 0;
  45. player.canJump = false;
  46. // Define environment elements (platforms)
  47. EnvElement envElements[MAX_ENVIRONMENT_ELEMENTS] = {
  48. {{ 0, 0, 1000, 400 }, 0, LIGHTGRAY },
  49. {{ 0, 400, 1000, 200 }, 1, GRAY },
  50. {{ 300, 200, 400, 10 }, 1, GRAY },
  51. {{ 250, 300, 100, 10 }, 1, GRAY },
  52. {{ 650, 300, 100, 10 }, 1, GRAY }
  53. };
  54. // Define camera
  55. Camera2D camera = { 0 };
  56. camera.target = player.position;
  57. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  58. camera.rotation = 0.0f;
  59. camera.zoom = 1.0f;
  60. // Automation events
  61. AutomationEventList aelist = LoadAutomationEventList(0); // Initialize list of automation events to record new events
  62. SetAutomationEventList(&aelist);
  63. bool eventRecording = false;
  64. bool eventPlaying = false;
  65. unsigned int frameCounter = 0;
  66. unsigned int playFrameCounter = 0;
  67. unsigned int currentPlayFrame = 0;
  68. SetTargetFPS(60);
  69. //--------------------------------------------------------------------------------------
  70. // Main game loop
  71. while (!WindowShouldClose())
  72. {
  73. // Update
  74. //----------------------------------------------------------------------------------
  75. float deltaTime = 0.015f;//GetFrameTime();
  76. // Dropped files logic
  77. //----------------------------------------------------------------------------------
  78. if (IsFileDropped())
  79. {
  80. FilePathList droppedFiles = LoadDroppedFiles();
  81. // Supports loading .rgs style files (text or binary) and .png style palette images
  82. if (IsFileExtension(droppedFiles.paths[0], ".txt;.rae"))
  83. {
  84. UnloadAutomationEventList(&aelist);
  85. aelist = LoadAutomationEventList(droppedFiles.paths[0]);
  86. eventRecording = false;
  87. // Reset scene state to play
  88. eventPlaying = true;
  89. playFrameCounter = 0;
  90. currentPlayFrame = 0;
  91. player.position = (Vector2){ 400, 280 };
  92. player.speed = 0;
  93. player.canJump = false;
  94. camera.target = player.position;
  95. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  96. camera.rotation = 0.0f;
  97. camera.zoom = 1.0f;
  98. }
  99. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  100. }
  101. //----------------------------------------------------------------------------------
  102. // Update player
  103. //----------------------------------------------------------------------------------
  104. if (IsKeyDown(KEY_LEFT)) player.position.x -= PLAYER_HOR_SPD*deltaTime;
  105. if (IsKeyDown(KEY_RIGHT)) player.position.x += PLAYER_HOR_SPD*deltaTime;
  106. if (IsKeyDown(KEY_SPACE) && player.canJump)
  107. {
  108. player.speed = -PLAYER_JUMP_SPD;
  109. player.canJump = false;
  110. }
  111. int hitObstacle = 0;
  112. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  113. {
  114. EnvElement *element = &envElements[i];
  115. Vector2 *p = &(player.position);
  116. if (element->blocking &&
  117. element->rect.x <= p->x &&
  118. element->rect.x + element->rect.width >= p->x &&
  119. element->rect.y >= p->y &&
  120. element->rect.y <= p->y + player.speed*deltaTime)
  121. {
  122. hitObstacle = 1;
  123. player.speed = 0.0f;
  124. p->y = element->rect.y;
  125. }
  126. }
  127. if (!hitObstacle)
  128. {
  129. player.position.y += player.speed*deltaTime;
  130. player.speed += GRAVITY*deltaTime;
  131. player.canJump = false;
  132. }
  133. else player.canJump = true;
  134. camera.zoom += ((float)GetMouseWheelMove()*0.05f);
  135. if (camera.zoom > 3.0f) camera.zoom = 3.0f;
  136. else if (camera.zoom < 0.25f) camera.zoom = 0.25f;
  137. if (IsKeyPressed(KEY_R))
  138. {
  139. // Reset game state
  140. player.position = (Vector2){ 400, 280 };
  141. player.speed = 0;
  142. player.canJump = false;
  143. camera.target = player.position;
  144. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  145. camera.rotation = 0.0f;
  146. camera.zoom = 1.0f;
  147. }
  148. //----------------------------------------------------------------------------------
  149. // Update camera
  150. //----------------------------------------------------------------------------------
  151. camera.target = player.position;
  152. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  153. float minX = 1000, minY = 1000, maxX = -1000, maxY = -1000;
  154. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  155. {
  156. EnvElement *element = &envElements[i];
  157. minX = fminf(element->rect.x, minX);
  158. maxX = fmaxf(element->rect.x + element->rect.width, maxX);
  159. minY = fminf(element->rect.y, minY);
  160. maxY = fmaxf(element->rect.y + element->rect.height, maxY);
  161. }
  162. Vector2 max = GetWorldToScreen2D((Vector2){ maxX, maxY }, camera);
  163. Vector2 min = GetWorldToScreen2D((Vector2){ minX, minY }, camera);
  164. if (max.x < screenWidth) camera.offset.x = screenWidth - (max.x - screenWidth/2);
  165. if (max.y < screenHeight) camera.offset.y = screenHeight - (max.y - screenHeight/2);
  166. if (min.x > 0) camera.offset.x = screenWidth/2 - min.x;
  167. if (min.y > 0) camera.offset.y = screenHeight/2 - min.y;
  168. //----------------------------------------------------------------------------------
  169. // Toggle events recording
  170. if (IsKeyPressed(KEY_S))
  171. {
  172. if (!eventPlaying)
  173. {
  174. if (eventRecording)
  175. {
  176. StopAutomationEventRecording();
  177. eventRecording = false;
  178. ExportAutomationEventList(aelist, "automation.rae");
  179. TraceLog(LOG_INFO, "RECORDED FRAMES: %i", aelist.count);
  180. }
  181. else
  182. {
  183. SetAutomationEventBaseFrame(180);
  184. StartAutomationEventRecording();
  185. eventRecording = true;
  186. }
  187. }
  188. }
  189. else if (IsKeyPressed(KEY_A))
  190. {
  191. if (!eventRecording && (aelist.count > 0))
  192. {
  193. // Reset scene state to play
  194. eventPlaying = true;
  195. playFrameCounter = 0;
  196. currentPlayFrame = 0;
  197. player.position = (Vector2){ 400, 280 };
  198. player.speed = 0;
  199. player.canJump = false;
  200. camera.target = player.position;
  201. camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
  202. camera.rotation = 0.0f;
  203. camera.zoom = 1.0f;
  204. }
  205. }
  206. if (eventPlaying)
  207. {
  208. // NOTE: Multiple events could be executed in a single frame
  209. while (playFrameCounter == aelist.events[currentPlayFrame].frame)
  210. {
  211. TraceLog(LOG_INFO, "PLAYING: PlayFrameCount: %i | currentPlayFrame: %i | Event Frame: %i, param: %i",
  212. playFrameCounter, currentPlayFrame, aelist.events[currentPlayFrame].frame, aelist.events[currentPlayFrame].params[0]);
  213. PlayAutomationEvent(aelist.events[currentPlayFrame]);
  214. currentPlayFrame++;
  215. if (currentPlayFrame == aelist.count)
  216. {
  217. eventPlaying = false;
  218. currentPlayFrame = 0;
  219. playFrameCounter = 0;
  220. TraceLog(LOG_INFO, "FINISH PLAYING!");
  221. break;
  222. }
  223. }
  224. playFrameCounter++;
  225. }
  226. if (eventRecording || eventPlaying) frameCounter++;
  227. else frameCounter = 0;
  228. //----------------------------------------------------------------------------------
  229. // Draw
  230. //----------------------------------------------------------------------------------
  231. BeginDrawing();
  232. ClearBackground(LIGHTGRAY);
  233. BeginMode2D(camera);
  234. // Draw environment elements
  235. for (int i = 0; i < MAX_ENVIRONMENT_ELEMENTS; i++)
  236. {
  237. DrawRectangleRec(envElements[i].rect, envElements[i].color);
  238. }
  239. // Draw player rectangle
  240. DrawRectangleRec((Rectangle){ player.position.x - 20, player.position.y - 40, 40, 40 }, RED);
  241. EndMode2D();
  242. // Draw game controls
  243. DrawRectangle(10, 10, 290, 145, Fade(SKYBLUE, 0.5f));
  244. DrawRectangleLines(10, 10, 290, 145, Fade(BLUE, 0.8f));
  245. DrawText("Controls:", 20, 20, 10, BLACK);
  246. DrawText("- RIGHT | LEFT: Player movement", 30, 40, 10, DARKGRAY);
  247. DrawText("- SPACE: Player jump", 30, 60, 10, DARKGRAY);
  248. DrawText("- R: Reset game state", 30, 80, 10, DARKGRAY);
  249. DrawText("- S: START/STOP RECORDING INPUT EVENTS", 30, 110, 10, BLACK);
  250. DrawText("- A: REPLAY LAST RECORDED INPUT EVENTS", 30, 130, 10, BLACK);
  251. // Draw automation events recording indicator
  252. if (eventRecording)
  253. {
  254. DrawRectangle(10, 160, 290, 30, Fade(RED, 0.3f));
  255. DrawRectangleLines(10, 160, 290, 30, Fade(MAROON, 0.8f));
  256. DrawCircle(30, 175, 10, MAROON);
  257. if (((frameCounter/15)%2) == 1) DrawText(TextFormat("RECORDING EVENTS... [%i]", aelist.count), 50, 170, 10, MAROON);
  258. }
  259. else if (eventPlaying)
  260. {
  261. DrawRectangle(10, 160, 290, 30, Fade(LIME, 0.3f));
  262. DrawRectangleLines(10, 160, 290, 30, Fade(DARKGREEN, 0.8f));
  263. DrawTriangle((Vector2){ 20, 155 + 10 }, (Vector2){ 20, 155 + 30 }, (Vector2){ 40, 155 + 20 }, DARKGREEN);
  264. if (((frameCounter/15)%2) == 1) DrawText(TextFormat("PLAYING RECORDED EVENTS... [%i]", currentPlayFrame), 50, 170, 10, DARKGREEN);
  265. }
  266. EndDrawing();
  267. //----------------------------------------------------------------------------------
  268. }
  269. // De-Initialization
  270. //--------------------------------------------------------------------------------------
  271. CloseWindow(); // Close window and OpenGL context
  272. //--------------------------------------------------------------------------------------
  273. return 0;
  274. }