core_screen_recording.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - screen recording
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  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) 2025 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. // Using msf_gif library to record frames into GIF
  17. #define MSF_GIF_IMPL
  18. #include "msf_gif.h" // GIF recording functionality
  19. #include <math.h> // Required for: sinf()
  20. #define GIF_RECORD_FRAMERATE 5 // Record framerate, we get a frame every N frames
  21. #define MAX_SINEWAVE_POINTS 256
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. InitWindow(screenWidth, screenHeight, "raylib [core] example - screen recording");
  32. bool gifRecording = false; // GIF recording state
  33. unsigned int gifFrameCounter = 0; // GIF frames counter
  34. MsfGifState gifState = { 0 }; // MSGIF context state
  35. Vector2 circlePosition = { 0.0f, screenHeight/2.0f };
  36. float timeCounter = 0.0f;
  37. // Get sine wave points for line drawing
  38. Vector2 sinePoints[MAX_SINEWAVE_POINTS] = { 0 };
  39. for (int i = 0; i < MAX_SINEWAVE_POINTS; i++)
  40. {
  41. sinePoints[i].x = i*GetScreenWidth()/180.0f;
  42. sinePoints[i].y = screenHeight/2.0f + 150*sinf((2*PI/1.5f)*(1.0f/60.0f)*(float)i); // Calculate for 60 fps
  43. }
  44. SetTargetFPS(60);
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. // Update circle sinusoidal movement
  52. timeCounter += GetFrameTime();
  53. circlePosition.x += GetScreenWidth()/180.0f;
  54. circlePosition.y = screenHeight/2.0f + 150*sinf((2*PI/1.5f)*timeCounter);
  55. if (circlePosition.x > screenWidth)
  56. {
  57. circlePosition.x = 0.0f;
  58. circlePosition.y = screenHeight/2.0f;
  59. timeCounter = 0.0f;
  60. }
  61. // Start-Stop GIF recording on CTRL+R
  62. if (IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_R))
  63. {
  64. if (gifRecording)
  65. {
  66. // Stop current recording and save file
  67. gifRecording = false;
  68. MsfGifResult result = msf_gif_end(&gifState);
  69. SaveFileData(TextFormat("%s/screenrecording.gif", GetApplicationDirectory()), result.data, (unsigned int)result.dataSize);
  70. msf_gif_free(result);
  71. TraceLog(LOG_INFO, "Finish animated GIF recording");
  72. }
  73. else
  74. {
  75. // Start a new recording
  76. gifRecording = true;
  77. gifFrameCounter = 0;
  78. msf_gif_begin(&gifState, GetRenderWidth(), GetRenderHeight());
  79. TraceLog(LOG_INFO, "Start animated GIF recording");
  80. }
  81. }
  82. if (gifRecording)
  83. {
  84. gifFrameCounter++;
  85. // NOTE: We record one gif frame depending on the desired gif framerate
  86. if (gifFrameCounter > GIF_RECORD_FRAMERATE)
  87. {
  88. // Get image data for the current frame (from backbuffer)
  89. // WARNING: This process is quite slow, it can generate stuttering
  90. Image imScreen = LoadImageFromScreen();
  91. // Add the frame to the gif recording, providing and "estimated" time for display in centiseconds
  92. msf_gif_frame(&gifState, imScreen.data, (int)((1.0f/60.0f)*GIF_RECORD_FRAMERATE)/10, 16, imScreen.width*4);
  93. gifFrameCounter = 0;
  94. UnloadImage(imScreen); // Free image data
  95. }
  96. }
  97. //----------------------------------------------------------------------------------
  98. // Draw
  99. //----------------------------------------------------------------------------------
  100. BeginDrawing();
  101. ClearBackground(RAYWHITE);
  102. for (int i = 0; i < (MAX_SINEWAVE_POINTS - 1); i++)
  103. {
  104. DrawLineV(sinePoints[i], sinePoints[i + 1], MAROON);
  105. DrawCircleV(sinePoints[i], 3, MAROON);
  106. }
  107. DrawCircleV(circlePosition, 30, RED);
  108. DrawFPS(10, 10);
  109. /*
  110. // Draw record indicator
  111. // WARNING: If drawn here, it will appear in the recorded image,
  112. // use a render texture instead for the recording and LoadImageFromTexture(rt.texture)
  113. if (gifRecording)
  114. {
  115. // Display the recording indicator every half-second
  116. if ((int)(GetTime()/0.5)%2 == 1)
  117. {
  118. DrawCircle(30, GetScreenHeight() - 20, 10, MAROON);
  119. DrawText("GIF RECORDING", 50, GetScreenHeight() - 25, 10, RED);
  120. }
  121. }
  122. */
  123. EndDrawing();
  124. //----------------------------------------------------------------------------------
  125. }
  126. // De-Initialization
  127. //--------------------------------------------------------------------------------------
  128. // If still recording a GIF on close window, just finish
  129. if (gifRecording)
  130. {
  131. MsfGifResult result = msf_gif_end(&gifState);
  132. msf_gif_free(result);
  133. gifRecording = false;
  134. }
  135. CloseWindow(); // Close window and OpenGL context
  136. //--------------------------------------------------------------------------------------
  137. return 0;
  138. }