Added simple Makefile, introduced rendering, fixed few bugs, found one 'scrolling' bug

This commit is contained in:
Marcelina Szostak 2024-12-07 19:55:00 +01:00
parent 95a1ed8bc6
commit d6c720b14f
No known key found for this signature in database
GPG Key ID: BE51722E80D2B63F
2 changed files with 59 additions and 11 deletions

3
Makefile Normal file
View File

@ -0,0 +1,3 @@
default:
clang gol.c -L raylib-5.5_linux_amd64/lib -l:libraylib.a -lm

63
gol.c
View File

@ -1,10 +1,16 @@
#include <stdbool.h>
#include "./raylib-5.5_linux_amd64/include/raylib.h"
#define SIZE 512
#define SIZE 100
#define WIDTH 1000
#define HEIGHT 1000
#define TITLE "Conway's Game of Life"
#define FPS 15
void initialize_grid(bool grid[SIZE][SIZE]){
for(int i = 0; i < SIZE; i++){
for(int j = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
grid[i][j] = false;
}
}
@ -12,7 +18,7 @@ void initialize_grid(bool grid[SIZE][SIZE]){
void mirror_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
for(int i = 0; i < SIZE; i++){
for(int j = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
mirror[i][j] = grid[i][j];
}
}
@ -22,20 +28,20 @@ void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
mirror_grid(grid, mirror);
for(int i = 0; i < SIZE; i++){
for(int j = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
int neighbours = 0;
int nx_prawo = i + 1 > SIZE ? 0 + (i + 1 - SIZE) : i + 1;
int nx_prawo = i == SIZE ? 0 : i + 1;
int ny_prawo = j;
int nx_lewo = i - 1 < 0 ? SIZE - (i - 1 + SIZE) : i - 1;
int nx_lewo = i == 0 ? SIZE : i - 1;
int ny_lewo = j;
int nx_gora = i;
int ny_gora = j + 1 > SIZE ? 0 + (j + 1 - SIZE) : j + 1;
int ny_gora = j == 0 ? SIZE : j - 1;
int nx_dol = i;
int ny_dol = j - 1 < 0 ? SIZE - (j - 1 + SIZE) : j - 1;
int ny_dol = j == SIZE ? 0 : j + 1;
neighbours = mirror[nx_prawo][ny_prawo] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_lewo][ny_lewo] == true ? neighbours + 1 : neighbours;
@ -47,7 +53,7 @@ void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
neighbours = mirror[nx_lewo][ny_gora] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_lewo][ny_dol] == true ? neighbours + 1 : neighbours;
int state = neighbours < 2 ? 1 : neighbours == 2 || neighbours == 3 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0;
int state = neighbours < 2 ? 1 : neighbours == 2 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0;
switch (state)
{
@ -71,6 +77,19 @@ void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
}
void draw_grid(bool grid[SIZE][SIZE], int cellw, int cellh){
BeginDrawing();
ClearBackground((Color){ 0, 24, 24, 255 });
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
if(grid[i][j]){
DrawRectangle(cellw*i, cellh*j, cellw, cellh, WHITE);
}
}
}
EndDrawing();
}
int main()
{
bool grid[SIZE][SIZE];
@ -78,7 +97,33 @@ int main()
initialize_grid(grid);
mirror_grid(grid, mirror);
//grid[SIZE/2][SIZE/2] = true; //drawing famous glider
//grid[(SIZE/2)+1][(SIZE/2)+1] = true;
//grid[(SIZE/2)-1][(SIZE/2)+2] = true;
//grid[(SIZE/2)+0][(SIZE/2)+2] = true;
//grid[(SIZE/2)+1][(SIZE/2)+2] = true;
grid[(SIZE/2)][(SIZE/2)] = true; //funny pattern
grid[(SIZE/2)+1][(SIZE/2)] = true;
grid[(SIZE/2)+1][(SIZE/2)-1] = true;
grid[(SIZE/2)+1][(SIZE/2)-2] = true;
grid[(SIZE/2)-1][(SIZE/2)] = true;
grid[(SIZE/2)-1][(SIZE/2)-1] = true;
grid[(SIZE/2)-1][(SIZE/2)-2] = true;
InitWindow(WIDTH, HEIGHT, TITLE);
SetTargetFPS(FPS);
int cellw = WIDTH/SIZE;
int cellh = HEIGHT/SIZE;
while(!WindowShouldClose()){
simulate_grid(grid, mirror);
draw_grid(grid, cellw, cellh);
}
return 0;
}