22*
33* raylib [core] example - screen recording
44*
5- * Example complexity rating: [★☆☆☆] 1 /4
5+ * Example complexity rating: [★★☆☆] 2 /4
66*
77* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
88*
9- * Example contributed by Ramon Santamaria (@raysan5) and reviewed by Ramon Santamaria (@raysan5)
10- *
119* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
1210* BSD-like license that allows static linking with closed source software
1311*
1715
1816#include "raylib.h"
1917
18+ // Using msf_gif library to record frames into GIF
19+ #define MSF_GIF_IMPL
20+ #include "msf_gif.h" // GIF recording functionality
21+
22+ #include <math.h> // Required for: sinf()
23+
24+ #define GIF_RECORD_FRAMERATE 5 // Record framerate, we get a frame every N frames
25+
26+ #define MAX_SINEWAVE_POINTS 256
27+
2028//------------------------------------------------------------------------------------
2129// Program main entry point
2230//------------------------------------------------------------------------------------
@@ -29,7 +37,20 @@ int main(void)
2937
3038 InitWindow (screenWidth , screenHeight , "raylib [core] example - screen recording" );
3139
32- // TODO: Load resources / Initialize variables at this point
40+ bool gifRecording = false; // GIF recording state
41+ unsigned int gifFrameCounter = 0 ; // GIF frames counter
42+ MsfGifState gifState = { 0 }; // MSGIF context state
43+
44+ Vector2 circlePosition = { 0.0f , screenHeight /2.0f };
45+ float timeCounter = 0.0f ;
46+
47+ // Get sine wave points for line drawing
48+ Vector2 sinePoints [MAX_SINEWAVE_POINTS ] = { 0 };
49+ for (int i = 0 ; i < MAX_SINEWAVE_POINTS ; i ++ )
50+ {
51+ sinePoints [i ].x = i * GetScreenWidth ()/180.0f ;
52+ sinePoints [i ].y = screenHeight /2.0f + 150 * sinf ((2 * PI /1.5f )* (1.0f /60.0f )* (float )i ); // Calculate for 60 fps
53+ }
3354
3455 SetTargetFPS (60 );
3556 //--------------------------------------------------------------------------------------
@@ -39,7 +60,59 @@ int main(void)
3960 {
4061 // Update
4162 //----------------------------------------------------------------------------------
42- // TODO: Update variables / Implement example logic at this point
63+ // Update circle sinusoidal movement
64+ timeCounter += GetFrameTime ();
65+ circlePosition .x += GetScreenWidth ()/180.0f ;
66+ circlePosition .y = screenHeight /2.0f + 150 * sinf ((2 * PI /1.5f )* timeCounter );
67+ if (circlePosition .x > screenWidth )
68+ {
69+ circlePosition .x = 0.0f ;
70+ circlePosition .y = screenHeight /2.0f ;
71+ timeCounter = 0.0f ;
72+ }
73+
74+ // Start-Stop GIF recording on CTRL+R
75+ if (IsKeyDown (KEY_LEFT_CONTROL ) && IsKeyPressed (KEY_R ))
76+ {
77+ if (gifRecording )
78+ {
79+ // Stop current recording and save file
80+ gifRecording = false;
81+ MsfGifResult result = msf_gif_end (& gifState );
82+ SaveFileData (TextFormat ("%s/screenrecording.gif" , GetApplicationDirectory ()), result .data , (unsigned int )result .dataSize );
83+ msf_gif_free (result );
84+
85+ TraceLog (LOG_INFO , "Finish animated GIF recording" );
86+ }
87+ else
88+ {
89+ // Start a new recording
90+ gifRecording = true;
91+ gifFrameCounter = 0 ;
92+ msf_gif_begin (& gifState , GetRenderWidth (), GetRenderHeight ());
93+
94+ TraceLog (LOG_INFO , "Start animated GIF recording" );
95+ }
96+ }
97+
98+ if (gifRecording )
99+ {
100+ gifFrameCounter ++ ;
101+
102+ // NOTE: We record one gif frame depending on the desired gif framerate
103+ if (gifFrameCounter > GIF_RECORD_FRAMERATE )
104+ {
105+ // Get image data for the current frame (from backbuffer)
106+ // WARNING: This process is quite slow, it can generate stuttering
107+ Image imScreen = LoadImageFromScreen ();
108+
109+ // Add the frame to the gif recording, providing and "estimated" time for display in centiseconds
110+ msf_gif_frame (& gifState , imScreen .data , (int )((1.0f /60.0f )* GIF_RECORD_FRAMERATE )/10 , 16 , imScreen .width * 4 );
111+ gifFrameCounter = 0 ;
112+
113+ UnloadImage (imScreen ); // Free image data
114+ }
115+ }
43116 //----------------------------------------------------------------------------------
44117
45118 // Draw
@@ -48,20 +121,43 @@ int main(void)
48121
49122 ClearBackground (RAYWHITE );
50123
51- // TODO: Draw everything that requires to be drawn at this point
124+ for (int i = 0 ; i < (MAX_SINEWAVE_POINTS - 1 ); i ++ )
125+ {
126+ DrawLineV (sinePoints [i ], sinePoints [i + 1 ], MAROON );
127+ DrawCircleV (sinePoints [i ], 3 , MAROON );
128+ }
129+
130+ DrawCircleV (circlePosition , 30 , RED );
52131
53- DrawLineEx ((Vector2 ){ 0 , 0 }, (Vector2 ){ screenWidth , screenHeight }, 2.0f , RED );
54- DrawLineEx ((Vector2 ){ 0 , screenHeight }, (Vector2 ){ screenWidth , 0 }, 2.0f , RED );
55- DrawText ("example base code template" , 260 , 400 , 20 , LIGHTGRAY );
132+ DrawFPS (10 , 10 );
56133
134+ /*
135+ // Draw record indicator
136+ // WARNING: If drawn here, it will appear in the recorded image,
137+ // use a render texture instead for the recording and LoadImageFromTexture(rt.texture)
138+ if (gifRecording)
139+ {
140+ // Display the recording indicator every half-second
141+ if ((int)(GetTime()/0.5)%2 == 1)
142+ {
143+ DrawCircle(30, GetScreenHeight() - 20, 10, MAROON);
144+ DrawText("GIF RECORDING", 50, GetScreenHeight() - 25, 10, RED);
145+ }
146+ }
147+ */
57148 EndDrawing ();
58149 //----------------------------------------------------------------------------------
59150 }
60151
61152 // De-Initialization
62153 //--------------------------------------------------------------------------------------
63-
64- // TODO: Unload all loaded resources at this point
154+ // If still recording a GIF on close window, just finish
155+ if (gifRecording )
156+ {
157+ MsfGifResult result = msf_gif_end (& gifState );
158+ msf_gif_free (result );
159+ gifRecording = false;
160+ }
65161
66162 CloseWindow (); // Close window and OpenGL context
67163 //--------------------------------------------------------------------------------------
0 commit comments