Skaruts

Game of Life (Raylib)

Jun 4th, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. /******************************************************************************
  2. *
  3. *   Game of Life by Skaruts (raylib 2.5)
  4. *
  5. *   Press
  6. *       Enter to randomize the cells
  7. *       Space to pause/unpause
  8. *       G to pass one generation (if paused)
  9. *       F1 to turn on/off the FPS counter
  10. *       Q to quit (I'm just used to having this while testing)
  11. *
  12. ******************************************************************************/
  13. #include "raylib.h"
  14.  
  15.  
  16.  
  17. // quick way to modify the size of the cells, grid and window in the same proportion
  18. // use values 1, 2, 4, 8 or 16 (higher means less performance)
  19. #define m 4        
  20.  
  21. #define CS 16/m     // cell size    
  22. #define GW 80*m     // grid width
  23. #define GH 50*m     // grid height
  24.  
  25. Color dead_cell = DARKGRAY;
  26. Color alive_cell = ORANGE;
  27.  
  28. bool draw_fps = true;
  29. bool draw_dead_cells = false; // taxing on perfomance
  30. bool paused = false;
  31.  
  32. int prev_buf = 0;    // to alternate between cell buffers
  33. int curr_buf = 1;
  34.  
  35. int pad;    // add a padding space around cells (just to look nicer)
  36. int pad2;  
  37.  
  38. int cells[2][GH][GW];
  39.  
  40.  
  41.  
  42. void randomize_cells() {
  43.     for(int buf = 0; buf < 2; buf++) {
  44.         for(int j = 0; j < GH; j++) {
  45.             for(int i = 0; i < GW; i++) {
  46.                 cells[buf][j][i] = GetRandomValue(0, 1);
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. void init_cells() {
  53.     pad = 1;        // top and left padding
  54.     pad2 = pad*2;   // bottom and right padding
  55.    
  56.     if (m > 4) {    // check if cells aren't too small, or else they become invisible with padding
  57.         pad = 0;    // the condition should be adjusted if default padding values are changed
  58.         pad2 = 0;
  59.     }
  60.    
  61.     randomize_cells();
  62. }
  63.  
  64. void pass_generation() {
  65.     prev_buf = 1 - prev_buf;      // switch buffers
  66.     curr_buf = 1 - curr_buf;
  67.  
  68.     for (int j = 0; j < GH; j++) {
  69.         for (int i = 0; i < GW; i++) {
  70.            
  71.             // wrap around
  72.             int j_ = j-1 >=  0   ?   j-1   :  GH-1;
  73.             int _j = j+1 <  GH   ?   j+1   :     0;
  74.             int i_ = i-1 >=  0   ?   i-1   :  GW-1;
  75.             int _i = i+1 <  GW   ?   i+1   :     0;
  76.  
  77.             // count alive neighbors
  78.             int n =   cells[prev_buf][ j_    ][ i_    ]
  79.                     + cells[prev_buf][ j_    ][   i   ]
  80.                     + cells[prev_buf][ j_    ][    _i ]
  81.                     + cells[prev_buf][   j   ][ i_    ]
  82.                     + cells[prev_buf][   j   ][    _i ]
  83.                     + cells[prev_buf][    _j ][ i_    ]
  84.                     + cells[prev_buf][    _j ][   i   ]
  85.                     + cells[prev_buf][    _j ][    _i ];
  86.  
  87.             // set cell according to rules
  88.             cells[curr_buf][j][i] = (int) n == 3 || ( cells[prev_buf][j][i] && n == 2);
  89.         }
  90.     }
  91. }
  92.  
  93. int main() {
  94.     // set window size according to grid and cell sizes
  95.     // (makes a 1280x800 window if only the value of 'm' is tinkered with)
  96.     int screenWidth = GW*CS;
  97.     int screenHeight = GH*CS;
  98.  
  99.     InitWindow(screenWidth, screenHeight, "Game of Life (by Skaruts) - raylib test");
  100.     SetTargetFPS(60);
  101.     bool running = true;
  102.    
  103.     init_cells();
  104.        
  105.     while (running) {
  106.         if (WindowShouldClose() || IsKeyPressed(KEY_Q)) running = false;
  107.         if (IsKeyPressed(KEY_SPACE))                    paused = !paused;
  108.         if (IsKeyPressed(KEY_ENTER))                    randomize_cells();
  109.         if (IsKeyPressed(KEY_F1))                       draw_fps = !draw_fps;
  110.        
  111.         if (paused) {
  112.             if (IsKeyPressed(KEY_G))                        
  113.                 pass_generation();
  114.         }
  115.         else {
  116.             pass_generation();
  117.         }      
  118.        
  119.         BeginDrawing();
  120.             ClearBackground(BLACK);
  121.            
  122.             for(int j = 0; j < GH; j++) {
  123.                 for(int i = 0; i < GW; i++) {
  124.                     int x = i*CS+pad;
  125.                     int y = j*CS+pad;
  126.                     int w = CS-pad2;
  127.                     int h = CS-pad2;
  128.                    
  129.                     if (cells[curr_buf][j][i])
  130.                         DrawRectangle(x, y, w, h, alive_cell);
  131.                     else if (draw_dead_cells)
  132.                         DrawRectangle(x, y, w, h, dead_cell);
  133.                 }
  134.             }
  135.  
  136.             if (draw_fps) {
  137.                 DrawFPS(10, 10);
  138.             }
  139.            
  140.         EndDrawing();
  141.     }
  142.    
  143.     CloseWindow();
  144.     return 0;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment