@@ -59,6 +59,8 @@ int main(void)
5959 //--------------------------------------------------------------------------------------
6060 const int screenWidth = 800 ;
6161 const int screenHeight = 450 ;
62+
63+ InitWindow (screenWidth , screenHeight , "raylib [shaders] example - game of life" );
6264
6365 const int menuWidth = 100 ;
6466 const int windowWidth = screenWidth - menuWidth ;
@@ -79,10 +81,9 @@ int main(void)
7981 { "Puffer train" , { 0.1f , 0.5f } }, { "Glider Gun" , { 0.2f , 0.2f } }, { "Breeder" , { 0.1f , 0.5f } },
8082 { "Random" , { 0.5f , 0.5f } }
8183 };
82- const int numberOfPresets = sizeof (presetPatterns ) / sizeof (presetPatterns [0 ]);
84+
85+ const int numberOfPresets = sizeof (presetPatterns )/sizeof (presetPatterns [0 ]);
8386
84- // Variable declaration
85- //--------------------------------------------------------------------------------------
8687 int zoom = 1 ;
8788 float offsetX = (worldWidth - windowWidth )/2.0f ; // Centered on window
8889 float offsetY = (worldHeight - windowHeight )/2.0f ; // Centered on window
@@ -96,8 +97,6 @@ int main(void)
9697 bool buttonFaster = false;
9798 bool buttonSlower = false;
9899
99- InitWindow (screenWidth , screenHeight , "raylib [shaders] example - game of life" );
100-
101100 // Load shader
102101 Shader shdrGameOfLife = LoadShader (0 , TextFormat ("resources/shaders/glsl%i/game_of_life.fs" , GLSL_VERSION ));
103102
@@ -114,7 +113,7 @@ int main(void)
114113 EndTextureMode ();
115114
116115 Image startPattern = LoadImage ("resources/game_of_life/r_pentomino.png" );
117- UpdateTextureRec (world2 .texture , (Rectangle ) { worldWidth / 2.0f , worldHeight / 2.0f , (float )(startPattern .width ), (float )(startPattern .height ) }, startPattern .data );
116+ UpdateTextureRec (world2 .texture , (Rectangle ){ worldWidth / 2.0f , worldHeight / 2.0f , (float )(startPattern .width ), (float )(startPattern .height ) }, startPattern .data );
118117 UnloadImage (startPattern );
119118
120119 // Pointers to the two textures, to be swapped
@@ -142,10 +141,8 @@ int main(void)
142141
143142 const float centerX = offsetX + (windowWidth /2.0f )/zoom ;
144143 const float centerY = offsetY + (windowHeight /2.0f )/zoom ;
145- if (buttonZoomIn || (mouseWheelMove > 0.0f ))
146- zoom *= 2 ;
147- if ((buttonZomOut || (mouseWheelMove < 0.0f )) && (zoom > 1 ))
148- zoom /= 2 ;
144+ if (buttonZoomIn || (mouseWheelMove > 0.0f )) zoom *= 2 ;
145+ if ((buttonZomOut || (mouseWheelMove < 0.0f )) && (zoom > 1 )) zoom /= 2 ;
149146 offsetX = centerX - (windowWidth /2.0f )/zoom ;
150147 offsetY = centerY - (windowHeight /2.0f )/zoom ;
151148 }
@@ -155,7 +152,6 @@ int main(void)
155152 if (buttonSlower ) framesPerStep ++ ;
156153
157154 // Mouse management
158- //----------------------------------------------------------------------------------
159155 if ((mode == MODE_RUN ) || (mode == MODE_PAUSE ))
160156 {
161157 FreeImageToDraw (& imageToDraw ); // Free the image to draw: no longer needed in these modes
@@ -176,10 +172,8 @@ int main(void)
176172 const float offsetDecimalY = offsetY - floorf (offsetY );
177173 int sizeInWorldX = (int )(ceilf ((float )(windowWidth + offsetDecimalX * zoom )/zoom ));
178174 int sizeInWorldY = (int )(ceilf ((float )(windowHeight + offsetDecimalY * zoom )/zoom ));
179- if (offsetX + sizeInWorldX >= worldWidth )
180- sizeInWorldX = worldWidth - (int )floorf (offsetX );
181- if (offsetY + sizeInWorldY >= worldHeight )
182- sizeInWorldY = worldHeight - (int )floorf (offsetY );
175+ if (offsetX + sizeInWorldX >= worldWidth ) sizeInWorldX = worldWidth - (int )floorf (offsetX );
176+ if (offsetY + sizeInWorldY >= worldHeight ) sizeInWorldY = worldHeight - (int )floorf (offsetY );
183177
184178 // Create image to draw if not created yet
185179 if (imageToDraw == NULL )
@@ -191,6 +185,7 @@ int main(void)
191185 EndTextureMode ();
192186 imageToDraw = (Image * )RL_MALLOC (sizeof (Image ));
193187 * imageToDraw = LoadImageFromTexture (worldOnScreen .texture );
188+
194189 UnloadRenderTexture (worldOnScreen );
195190 }
196191
@@ -200,23 +195,19 @@ int main(void)
200195 {
201196 int mouseX = (int )(mousePosition .x + offsetDecimalX * zoom )/zoom ;
202197 int mouseY = (int )(mousePosition .y + offsetDecimalY * zoom )/zoom ;
203- if (mouseX >= sizeInWorldX )
204- mouseX = sizeInWorldX - 1 ;
205- if (mouseY >= sizeInWorldY )
206- mouseY = sizeInWorldY - 1 ;
207- if (firstColor == -1 )
208- firstColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
198+ if (mouseX >= sizeInWorldX ) mouseX = sizeInWorldX - 1 ;
199+ if (mouseY >= sizeInWorldY ) mouseY = sizeInWorldY - 1 ;
200+ if (firstColor == -1 ) firstColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
209201 const int prevColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
202+
210203 ImageDrawPixel (imageToDraw , mouseX , mouseY , (firstColor ) ? BLACK : RAYWHITE );
211- if ( prevColor != firstColor )
212- UpdateTextureRec (currentWorld -> texture , (Rectangle ){ floorf (offsetX ), floorf (offsetY ), (float )(sizeInWorldX ), (float )(sizeInWorldY ) }, imageToDraw -> data );
204+
205+ if ( prevColor != firstColor ) UpdateTextureRec (currentWorld -> texture , (Rectangle ){ floorf (offsetX ), floorf (offsetY ), (float )(sizeInWorldX ), (float )(sizeInWorldY ) }, imageToDraw -> data );
213206 }
214- else
215- firstColor = -1 ;
207+ else firstColor = -1 ;
216208 }
217209
218210 // Load selected preset
219- //----------------------------------------------------------------------------------
220211 if (preset >= 0 )
221212 {
222213 Image pattern ;
@@ -237,6 +228,7 @@ int main(void)
237228 BeginTextureMode (* currentWorld );
238229 ClearBackground (RAYWHITE );
239230 EndTextureMode ();
231+
240232 UpdateTextureRec (currentWorld -> texture , (Rectangle ){ worldWidth * presetPatterns [preset ].position .x - pattern .width /2.0f ,
241233 worldHeight * presetPatterns [preset ].position .y - pattern .height /2.0f ,
242234 (float )(pattern .width ), (float )(pattern .height ) }, pattern .data );
@@ -250,9 +242,12 @@ int main(void)
250242 {
251243 ImageClearBackground (& pattern , RAYWHITE );
252244 for (int x = 0 ; x < pattern .width ; x ++ )
245+ {
253246 for (int y = 0 ; y < pattern .height ; y ++ )
254- if (GetRandomValue (0 , 100 ) < 15 )
255- ImageDrawPixel (& pattern , x , y , BLACK );
247+ {
248+ if (GetRandomValue (0 , 100 ) < 15 ) ImageDrawPixel (& pattern , x , y , BLACK );
249+ }
250+ }
256251 UpdateTextureRec (currentWorld -> texture ,
257252 (Rectangle ){ (float )(pattern .width * i ), (float )(pattern .height * j ),
258253 (float )(pattern .width ), (float )(pattern .height ) }, pattern .data );
@@ -261,26 +256,25 @@ int main(void)
261256 }
262257
263258 UnloadImage (pattern );
259+
264260 mode = MODE_PAUSE ;
265- offsetX = worldWidth * presetPatterns [preset ].position .x - windowWidth /zoom /2.0f ;
266- offsetY = worldHeight * presetPatterns [preset ].position .y - windowHeight /zoom /2.0f ;
261+ offsetX = worldWidth * presetPatterns [preset ].position .x - windowWidth /zoom /2.0f ;
262+ offsetY = worldHeight * presetPatterns [preset ].position .y - windowHeight /zoom /2.0f ;
267263 }
268264
269265 // Check window draw inside world limits
270266 if (offsetX < 0 ) offsetX = 0 ;
271267 if (offsetY < 0 ) offsetY = 0 ;
272- if (offsetX > worldWidth - (float )(windowWidth )/zoom )
273- offsetX = worldWidth - (float )(windowWidth )/zoom ;
274- if (offsetY > worldHeight - (float )(windowHeight )/zoom )
275- offsetY = worldHeight - (float )(windowHeight )/zoom ;
268+ if (offsetX > worldWidth - (float )(windowWidth )/zoom ) offsetX = worldWidth - (float )(windowWidth )/zoom ;
269+ if (offsetY > worldHeight - (float )(windowHeight )/zoom ) offsetY = worldHeight - (float )(windowHeight )/zoom ;
276270
277271 // Rectangles for drawing texture portion to screen
278- //----------------------------------------------------------------------------------
279272 const Rectangle textureSourceToScreen = { offsetX , offsetY , (float )windowWidth /zoom , (float )windowHeight /zoom };
273+ //----------------------------------------------------------------------------------
280274
281275 // Draw to texture
282276 //----------------------------------------------------------------------------------
283- if ((mode == MODE_RUN ) && ((frame % framesPerStep ) == 0 ))
277+ if ((mode == MODE_RUN ) && ((frame % framesPerStep ) == 0 ))
284278 {
285279 // Swap worlds
286280 RenderTexture2D * tempWorld = currentWorld ;
@@ -294,10 +288,12 @@ int main(void)
294288 EndShaderMode ();
295289 EndTextureMode ();
296290 }
291+ //----------------------------------------------------------------------------------
297292
298293 // Draw to screen
299294 //----------------------------------------------------------------------------------
300295 BeginDrawing ();
296+
301297 DrawTexturePro (currentWorld -> texture , textureSourceToScreen , textureOnScreen , (Vector2 ){ 0 , 0 }, 0.0f , WHITE );
302298
303299 DrawLine (windowWidth , 0 , windowWidth , screenHeight , (Color ){ 218 , 218 , 218 , 255 });
@@ -311,8 +307,7 @@ int main(void)
311307 DrawText ("Presets" , 710 , 58 , 8 , GRAY );
312308 preset = -1 ;
313309 for (int i = 0 ; i < numberOfPresets ; i ++ )
314- if (GuiButton ((Rectangle ){ 710.0f , 70.0f + 18 * i , 80.0f , 16.0f }, presetPatterns [i ].name ))
315- preset = i ;
310+ if (GuiButton ((Rectangle ){ 710.0f , 70.0f + 18 * i , 80.0f , 16.0f }, presetPatterns [i ].name )) preset = i ;
316311
317312 GuiToggleGroup ((Rectangle ){ 710 , 258 , 80 , 16 }, "Run\nPause\nDraw" , & mode );
318313
@@ -324,8 +319,6 @@ int main(void)
324319 buttonFaster = GuiButton ((Rectangle ){ 710 , 382 , 80 , 16 }, "Faster" );
325320 buttonSlower = GuiButton ((Rectangle ){ 710 , 400 , 80 , 16 }, "Slower" );
326321
327- //------------------------------------------------------------------------------
328-
329322 DrawFPS (712 , 426 );
330323
331324 EndDrawing ();
0 commit comments