@@ -60,6 +60,8 @@ int main(void)
6060 //--------------------------------------------------------------------------------------
6161 const int screenWidth = 800 ;
6262 const int screenHeight = 450 ;
63+
64+ InitWindow (screenWidth , screenHeight , "raylib [shaders] example - game of life" );
6365
6466 const int menuWidth = 100 ;
6567 const int windowWidth = screenWidth - menuWidth ;
@@ -80,10 +82,9 @@ int main(void)
8082 { "Puffer train" , "puffer_train" , { 0.1f , 0.5f } }, { "Glider Gun" , "glider_gun" , { 0.2f , 0.2f } }, { "Breeder" , "breeder" , { 0.1f , 0.5f } },
8183 { "Random" , "" , { 0.5f , 0.5f } }
8284 };
83- const int numberOfPresets = sizeof (presetPatterns ) / sizeof (presetPatterns [0 ]);
85+
86+ const int numberOfPresets = sizeof (presetPatterns )/sizeof (presetPatterns [0 ]);
8487
85- // Variable declaration
86- //--------------------------------------------------------------------------------------
8788 int zoom = 1 ;
8889 float offsetX = (worldWidth - windowWidth )/2.0f ; // Centered on window
8990 float offsetY = (worldHeight - windowHeight )/2.0f ; // Centered on window
@@ -97,8 +98,6 @@ int main(void)
9798 bool buttonFaster = false;
9899 bool buttonSlower = false;
99100
100- InitWindow (screenWidth , screenHeight , "raylib [shaders] example - game of life" );
101-
102101 // Load shader
103102 Shader shdrGameOfLife = LoadShader (0 , TextFormat ("resources/shaders/glsl%i/game_of_life.fs" , GLSL_VERSION ));
104103
@@ -115,7 +114,7 @@ int main(void)
115114 EndTextureMode ();
116115
117116 Image startPattern = LoadImage ("resources/game_of_life/r_pentomino.png" );
118- UpdateTextureRec (world2 .texture , (Rectangle ) { worldWidth / 2.0f , worldHeight / 2.0f , (float )(startPattern .width ), (float )(startPattern .height ) }, startPattern .data );
117+ UpdateTextureRec (world2 .texture , (Rectangle ){ worldWidth / 2.0f , worldHeight / 2.0f , (float )(startPattern .width ), (float )(startPattern .height ) }, startPattern .data );
119118 UnloadImage (startPattern );
120119
121120 // Pointers to the two textures, to be swapped
@@ -143,10 +142,8 @@ int main(void)
143142
144143 const float centerX = offsetX + (windowWidth /2.0f )/zoom ;
145144 const float centerY = offsetY + (windowHeight /2.0f )/zoom ;
146- if (buttonZoomIn || (mouseWheelMove > 0.0f ))
147- zoom *= 2 ;
148- if ((buttonZomOut || (mouseWheelMove < 0.0f )) && (zoom > 1 ))
149- zoom /= 2 ;
145+ if (buttonZoomIn || (mouseWheelMove > 0.0f )) zoom *= 2 ;
146+ if ((buttonZomOut || (mouseWheelMove < 0.0f )) && (zoom > 1 )) zoom /= 2 ;
150147 offsetX = centerX - (windowWidth /2.0f )/zoom ;
151148 offsetY = centerY - (windowHeight /2.0f )/zoom ;
152149 }
@@ -156,7 +153,6 @@ int main(void)
156153 if (buttonSlower ) framesPerStep ++ ;
157154
158155 // Mouse management
159- //----------------------------------------------------------------------------------
160156 if ((mode == MODE_RUN ) || (mode == MODE_PAUSE ))
161157 {
162158 FreeImageToDraw (& imageToDraw ); // Free the image to draw: no longer needed in these modes
@@ -177,10 +173,8 @@ int main(void)
177173 const float offsetDecimalY = offsetY - floorf (offsetY );
178174 int sizeInWorldX = (int )(ceilf ((float )(windowWidth + offsetDecimalX * zoom )/zoom ));
179175 int sizeInWorldY = (int )(ceilf ((float )(windowHeight + offsetDecimalY * zoom )/zoom ));
180- if (offsetX + sizeInWorldX >= worldWidth )
181- sizeInWorldX = worldWidth - (int )floorf (offsetX );
182- if (offsetY + sizeInWorldY >= worldHeight )
183- sizeInWorldY = worldHeight - (int )floorf (offsetY );
176+ if (offsetX + sizeInWorldX >= worldWidth ) sizeInWorldX = worldWidth - (int )floorf (offsetX );
177+ if (offsetY + sizeInWorldY >= worldHeight ) sizeInWorldY = worldHeight - (int )floorf (offsetY );
184178
185179 // Create image to draw if not created yet
186180 if (imageToDraw == NULL )
@@ -192,6 +186,7 @@ int main(void)
192186 EndTextureMode ();
193187 imageToDraw = (Image * )RL_MALLOC (sizeof (Image ));
194188 * imageToDraw = LoadImageFromTexture (worldOnScreen .texture );
189+
195190 UnloadRenderTexture (worldOnScreen );
196191 }
197192
@@ -201,32 +196,30 @@ int main(void)
201196 {
202197 int mouseX = (int )(mousePosition .x + offsetDecimalX * zoom )/zoom ;
203198 int mouseY = (int )(mousePosition .y + offsetDecimalY * zoom )/zoom ;
204- if (mouseX >= sizeInWorldX )
205- mouseX = sizeInWorldX - 1 ;
206- if (mouseY >= sizeInWorldY )
207- mouseY = sizeInWorldY - 1 ;
208- if (firstColor == -1 )
209- firstColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
199+ if (mouseX >= sizeInWorldX ) mouseX = sizeInWorldX - 1 ;
200+ if (mouseY >= sizeInWorldY ) mouseY = sizeInWorldY - 1 ;
201+ if (firstColor == -1 ) firstColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
210202 const int prevColor = (GetImageColor (* imageToDraw , mouseX , mouseY ).r < 5 )? 0 : 1 ;
203+
211204 ImageDrawPixel (imageToDraw , mouseX , mouseY , (firstColor ) ? BLACK : RAYWHITE );
212- if ( prevColor != firstColor )
213- UpdateTextureRec (currentWorld -> texture , (Rectangle ){ floorf (offsetX ), floorf (offsetY ), (float )(sizeInWorldX ), (float )(sizeInWorldY ) }, imageToDraw -> data );
205+
206+ if ( prevColor != firstColor ) UpdateTextureRec (currentWorld -> texture , (Rectangle ){ floorf (offsetX ), floorf (offsetY ), (float )(sizeInWorldX ), (float )(sizeInWorldY ) }, imageToDraw -> data );
214207 }
215- else
216- firstColor = -1 ;
208+ else firstColor = -1 ;
217209 }
218210
219211 // Load selected preset
220- //----------------------------------------------------------------------------------
221212 if (preset >= 0 )
222213 {
223214 Image pattern ;
224215 if (preset < numberOfPresets - 1 ) // Preset with pattern image lo load
225216 {
226217 pattern = LoadImage (TextFormat ("resources/game_of_life/%s.png" , presetPatterns [preset ].fileName ));
218+
227219 BeginTextureMode (* currentWorld );
228220 ClearBackground (RAYWHITE );
229221 EndTextureMode ();
222+
230223 UpdateTextureRec (currentWorld -> texture , (Rectangle ){ worldWidth * presetPatterns [preset ].position .x - pattern .width /2.0f ,
231224 worldHeight * presetPatterns [preset ].position .y - pattern .height /2.0f ,
232225 (float )(pattern .width ), (float )(pattern .height ) }, pattern .data );
@@ -240,9 +233,12 @@ int main(void)
240233 {
241234 ImageClearBackground (& pattern , RAYWHITE );
242235 for (int x = 0 ; x < pattern .width ; x ++ )
236+ {
243237 for (int y = 0 ; y < pattern .height ; y ++ )
244- if (GetRandomValue (0 , 100 ) < 15 )
245- ImageDrawPixel (& pattern , x , y , BLACK );
238+ {
239+ if (GetRandomValue (0 , 100 ) < 15 ) ImageDrawPixel (& pattern , x , y , BLACK );
240+ }
241+ }
246242 UpdateTextureRec (currentWorld -> texture ,
247243 (Rectangle ){ (float )(pattern .width * i ), (float )(pattern .height * j ),
248244 (float )(pattern .width ), (float )(pattern .height ) }, pattern .data );
@@ -251,26 +247,25 @@ int main(void)
251247 }
252248
253249 UnloadImage (pattern );
250+
254251 mode = MODE_PAUSE ;
255- offsetX = worldWidth * presetPatterns [preset ].position .x - windowWidth /zoom /2.0f ;
256- offsetY = worldHeight * presetPatterns [preset ].position .y - windowHeight /zoom /2.0f ;
252+ offsetX = worldWidth * presetPatterns [preset ].position .x - windowWidth /zoom /2.0f ;
253+ offsetY = worldHeight * presetPatterns [preset ].position .y - windowHeight /zoom /2.0f ;
257254 }
258255
259256 // Check window draw inside world limits
260257 if (offsetX < 0 ) offsetX = 0 ;
261258 if (offsetY < 0 ) offsetY = 0 ;
262- if (offsetX > worldWidth - (float )(windowWidth )/zoom )
263- offsetX = worldWidth - (float )(windowWidth )/zoom ;
264- if (offsetY > worldHeight - (float )(windowHeight )/zoom )
265- offsetY = worldHeight - (float )(windowHeight )/zoom ;
259+ if (offsetX > worldWidth - (float )(windowWidth )/zoom ) offsetX = worldWidth - (float )(windowWidth )/zoom ;
260+ if (offsetY > worldHeight - (float )(windowHeight )/zoom ) offsetY = worldHeight - (float )(windowHeight )/zoom ;
266261
267262 // Rectangles for drawing texture portion to screen
268- //----------------------------------------------------------------------------------
269263 const Rectangle textureSourceToScreen = { offsetX , offsetY , (float )windowWidth /zoom , (float )windowHeight /zoom };
264+ //----------------------------------------------------------------------------------
270265
271266 // Draw to texture
272267 //----------------------------------------------------------------------------------
273- if ((mode == MODE_RUN ) && ((frame % framesPerStep ) == 0 ))
268+ if ((mode == MODE_RUN ) && ((frame % framesPerStep ) == 0 ))
274269 {
275270 // Swap worlds
276271 RenderTexture2D * tempWorld = currentWorld ;
@@ -284,10 +279,12 @@ int main(void)
284279 EndShaderMode ();
285280 EndTextureMode ();
286281 }
282+ //----------------------------------------------------------------------------------
287283
288284 // Draw to screen
289285 //----------------------------------------------------------------------------------
290286 BeginDrawing ();
287+
291288 DrawTexturePro (currentWorld -> texture , textureSourceToScreen , textureOnScreen , (Vector2 ){ 0 , 0 }, 0.0f , WHITE );
292289
293290 DrawLine (windowWidth , 0 , windowWidth , screenHeight , (Color ){ 218 , 218 , 218 , 255 });
@@ -301,8 +298,7 @@ int main(void)
301298 DrawText ("Presets" , 710 , 58 , 8 , GRAY );
302299 preset = -1 ;
303300 for (int i = 0 ; i < numberOfPresets ; i ++ )
304- if (GuiButton ((Rectangle ){ 710.0f , 70.0f + 18 * i , 80.0f , 16.0f }, presetPatterns [i ].name ))
305- preset = i ;
301+ if (GuiButton ((Rectangle ){ 710.0f , 70.0f + 18 * i , 80.0f , 16.0f }, presetPatterns [i ].name )) preset = i ;
306302
307303 GuiToggleGroup ((Rectangle ){ 710 , 258 , 80 , 16 }, "Run\nPause\nDraw" , & mode );
308304
@@ -314,8 +310,6 @@ int main(void)
314310 buttonFaster = GuiButton ((Rectangle ){ 710 , 382 , 80 , 16 }, "Faster" );
315311 buttonSlower = GuiButton ((Rectangle ){ 710 , 400 , 80 , 16 }, "Slower" );
316312
317- //------------------------------------------------------------------------------
318-
319313 DrawFPS (712 , 426 );
320314
321315 EndDrawing ();
0 commit comments