I am trying to make Conway's Game of Life using C and raylib. In this program, I am facing issues to generate a grid greater than 30 such as 40x40 or 100x100. The program works fine for 30x30, but as soon as I change the value the program breaks. Only half the grid is generated and half the screen is blank.
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "raylib.h"
#include "raymath.h"
int main()
{
int up, down, left, right;
int life;
int k, i, j;
int COLS = 30;
int ROWS = 30;
int spaceC = 0;
int mouseC = 0;
int screenWidth = 600;
int screenHeight = 600;
int cellWidth = screenWidth / ROWS;
int cellHeight = screenHeight / COLS;
int mat[ROWS][COLS];
int buffer[ROWS][COLS];
SetTargetFPS(60);
InitWindow(screenWidth, screenHeight, "try render");
//default
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat[i][j] = 0;
}
}
while (!WindowShouldClose())
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (mat[i][j] == 0)
{
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, WHITE);
}
else
{
DrawRectangle(i * cellWidth, j * cellHeight, cellWidth, cellHeight, BLACK);
}
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, GRAY);
}
}
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mPos = GetMousePosition();
int x = mPos.x / cellWidth;
int y = mPos.y / cellHeight;
if (x >= 0 && x < ROWS && y >= 0 && y < COLS)
{
if (mat[x][y] != 1)
{
mouseC = 1;
mat[x][y] = 1;
}
else if (mouseC == 1 && mat[x][y] == 1) {
mouseC=0;
mat[x][y] = 0;
}
}
}
if (IsKeyPressed(KEY_SPACE))
{
spaceC++;
}
if (spaceC % 2 != 0)
{
SetTargetFPS(4);
for (k = 0; k < 1; k++)
{
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
right = (j + 1) % COLS;
left = ((j - 1) + COLS) % COLS;
up = ((i - 1) + ROWS) % ROWS;
down = (i + 1) % ROWS;
life = mat[down][j] + mat[i][left] + mat[up][left] + mat[up][j] + mat[up][right] + mat[i][right] + mat[down][right] + mat[down][left];
if (mat[i][j] == 1 && (life < 2 || life > 3))
{
buffer[i][j] = 0;
}
else if (mat[i][j] == 1 && (life == 2 || life == 3))
{
buffer[i][j] = 1;
}
else if (mat[i][j] == 0 && life == 3)
{
buffer[i][j] = 1;
}
else
{
buffer[i][j] = mat[i][j];
}
}
}
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
mat[i][j] = buffer[i][j];
}
}
}
}
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}
The reason the display is broken is you do not redisplay between the calls
BeginDrawing(); ClearBackground(RAYWHITE);
andEndDrawing();
.Note also these remarks:
there is still a confusion between rows and columns: you set
mat[x][y]
in the cell update code instead ofmat[y][x]
, and you use the wrong variables in the display code: it should bej * cellWidth, i * cellHeight
instead ofi * cellWidth, j * cellHeight
using
row
andcol
as index variables would make the code more readable and less error prone.there is no need for
DrawRectangleLines(i * cellWidth, j * cellHeight, cellWidth, cellHeight, WHITE);
in the display code if the matrix cell is not set because the background has been cleared.you can use the xor operator to simplify the mouse toggle code:
the game logic can also be simplified:
Here is a modified version: