Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- *
- * Game of Life by Skaruts (raylib 2.5)
- *
- * Press
- * Enter to randomize the cells
- * Space to pause/unpause
- * G to pass one generation (if paused)
- * F1 to turn on/off the FPS counter
- * Q to quit (I'm just used to having this while testing)
- *
- ******************************************************************************/
- #include "raylib.h"
- // quick way to modify the size of the cells, grid and window in the same proportion
- // use values 1, 2, 4, 8 or 16 (higher means less performance)
- #define m 4
- #define CS 16/m // cell size
- #define GW 80*m // grid width
- #define GH 50*m // grid height
- Color dead_cell = DARKGRAY;
- Color alive_cell = ORANGE;
- bool draw_fps = true;
- bool draw_dead_cells = false; // taxing on perfomance
- bool paused = false;
- int prev_buf = 0; // to alternate between cell buffers
- int curr_buf = 1;
- int pad; // add a padding space around cells (just to look nicer)
- int pad2;
- int cells[2][GH][GW];
- void randomize_cells() {
- for(int buf = 0; buf < 2; buf++) {
- for(int j = 0; j < GH; j++) {
- for(int i = 0; i < GW; i++) {
- cells[buf][j][i] = GetRandomValue(0, 1);
- }
- }
- }
- }
- void init_cells() {
- pad = 1; // top and left padding
- pad2 = pad*2; // bottom and right padding
- if (m > 4) { // check if cells aren't too small, or else they become invisible with padding
- pad = 0; // the condition should be adjusted if default padding values are changed
- pad2 = 0;
- }
- randomize_cells();
- }
- void pass_generation() {
- prev_buf = 1 - prev_buf; // switch buffers
- curr_buf = 1 - curr_buf;
- for (int j = 0; j < GH; j++) {
- for (int i = 0; i < GW; i++) {
- // wrap around
- int j_ = j-1 >= 0 ? j-1 : GH-1;
- int _j = j+1 < GH ? j+1 : 0;
- int i_ = i-1 >= 0 ? i-1 : GW-1;
- int _i = i+1 < GW ? i+1 : 0;
- // count alive neighbors
- int n = cells[prev_buf][ j_ ][ i_ ]
- + cells[prev_buf][ j_ ][ i ]
- + cells[prev_buf][ j_ ][ _i ]
- + cells[prev_buf][ j ][ i_ ]
- + cells[prev_buf][ j ][ _i ]
- + cells[prev_buf][ _j ][ i_ ]
- + cells[prev_buf][ _j ][ i ]
- + cells[prev_buf][ _j ][ _i ];
- // set cell according to rules
- cells[curr_buf][j][i] = (int) n == 3 || ( cells[prev_buf][j][i] && n == 2);
- }
- }
- }
- int main() {
- // set window size according to grid and cell sizes
- // (makes a 1280x800 window if only the value of 'm' is tinkered with)
- int screenWidth = GW*CS;
- int screenHeight = GH*CS;
- InitWindow(screenWidth, screenHeight, "Game of Life (by Skaruts) - raylib test");
- SetTargetFPS(60);
- bool running = true;
- init_cells();
- while (running) {
- if (WindowShouldClose() || IsKeyPressed(KEY_Q)) running = false;
- if (IsKeyPressed(KEY_SPACE)) paused = !paused;
- if (IsKeyPressed(KEY_ENTER)) randomize_cells();
- if (IsKeyPressed(KEY_F1)) draw_fps = !draw_fps;
- if (paused) {
- if (IsKeyPressed(KEY_G))
- pass_generation();
- }
- else {
- pass_generation();
- }
- BeginDrawing();
- ClearBackground(BLACK);
- for(int j = 0; j < GH; j++) {
- for(int i = 0; i < GW; i++) {
- int x = i*CS+pad;
- int y = j*CS+pad;
- int w = CS-pad2;
- int h = CS-pad2;
- if (cells[curr_buf][j][i])
- DrawRectangle(x, y, w, h, alive_cell);
- else if (draw_dead_cells)
- DrawRectangle(x, y, w, h, dead_cell);
- }
- }
- if (draw_fps) {
- DrawFPS(10, 10);
- }
- EndDrawing();
- }
- CloseWindow();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment