Skip to content

Commit a895de7

Browse files
Add files via upload
1 parent 7e79631 commit a895de7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2087
-2
lines changed

Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
TARGET = game-of-life
2+
BUILD_DIR = .build
3+
SRC_DIR ?= src
4+
5+
CC ?= gcc
6+
CFLAGS_BASE = -std=c99 $(shell pkg-config --cflags sdl2 SDL2_image SDL2_ttf SDL2_mixer)
7+
CFLAGS_RELEASE = -O2
8+
LDFLAGS ?=
9+
LDLIBS_BASE = $(shell pkg-config --libs sdl2 SDL2_image SDL2_ttf SDL2_mixer)
10+
11+
SRCS = $(wildcard $(SRC_DIR)/*.c)
12+
OBJS = $(addprefix $(BUILD_DIR)/, $(notdir $(SRCS:.c=.o)))
13+
14+
ifeq ($(OS),Windows_NT)
15+
CFLAGS_DEV = -O0 -ggdb3 -Wall -Wextra -Werror -Wpedantic -Wwrite-strings -Wconversion \
16+
-Wshadow -Wmissing-prototypes -Wmissing-declarations -Wfloat-equal \
17+
-Wsign-compare -Wundef -Wcast-align -Wstrict-prototypes -Wswitch-default \
18+
-Wold-style-definition -Wmissing-include-dirs
19+
LDLIBS_DEV =
20+
CLEAN = del /f $(TARGET).exe & if exist $(BUILD_DIR) rmdir /s /q $(BUILD_DIR)
21+
else
22+
CFLAGS_DEV = -O0 -ggdb3 -Wall -Wextra -Werror -Wpedantic -Wwrite-strings -Wconversion \
23+
-Wshadow -Wmissing-prototypes -Wmissing-declarations -Wfloat-equal \
24+
-Wsign-compare -Wundef -Wcast-align -Wstrict-prototypes -Wswitch-default \
25+
-Wold-style-definition -Wmissing-include-dirs -fsanitize=address \
26+
-fsanitize-address-use-after-scope
27+
LDLIBS_DEV = -fsanitize=address -fsanitize-address-use-after-scope
28+
CLEAN = $(RM) -r $(TARGET) $(BUILD_DIR)
29+
endif
30+
31+
CFLAGS ?= $(CFLAGS_BASE) $(CFLAGS_DEV)
32+
LDLIBS ?= $(LDLIBS_BASE) $(LDLIBS_DEV)
33+
34+
$(BUILD_DIR):
35+
mkdir $(BUILD_DIR)
36+
37+
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/%.h | $(BUILD_DIR)
38+
$(CC) $(CFLAGS) -c $< -o $@
39+
40+
$(TARGET): $(OBJS)
41+
$(CC) $^ -o $@ $(LDLIBS)
42+
43+
.PHONY: all clean run rebuild release
44+
45+
all: $(TARGET)
46+
47+
release: CFLAGS = $(CFLAGS_BASE) $(CFLAGS_RELEASE)
48+
release: LDLIBS = $(LDLIBS_BASE)
49+
release: all
50+
51+
clean:
52+
$(CLEAN)
53+
54+
run: $(TARGET)
55+
./$<
56+
57+
rebuild: clean all

README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
1-
# Conways-Game-of-Life-C-SDL2
2-
Conway's Game of Life written in C++ and using SDL2 game library.
1+
![Screenshot](screenshot.png)
2+
3+
# Conway's Game of Life (C++ - SDL2)
4+
Conway's Game of Life is a classic cellular automaton devised by British mathematician John Horton Conway in 1970. It's a zero-player game, meaning its evolution is determined by its initial state, with no further input from humans. Despite its simplicity, the Game of Life exhibits complex and fascinating patterns.
5+
6+
* If a dead cell has exactly three live neighbors, it becomes alive in the next generation.
7+
* If a live cell has two or three live neighbors, it remains alive in the next generation.
8+
* In all other cases, a cell dies or remains dead.
9+
10+
# ArchLinux instructions.
11+
```
12+
sudo pacman -S --needed base-devel sdl2 sdl2_image sdl2_mixer sdl2_ttf
13+
cd
14+
git clone https://github.com/ProgrammingRainbow/Conways-Game-of-Life-C-SDL2
15+
cd Conways-Game-of-Life-C-SDL2
16+
make run
17+
```
18+
# Controls
19+
Up Arrow - Speeds up the game.\
20+
Down Arrow - Slows down the game.\
21+
Space Bar - Toggles game pauses.\
22+
Escape - Quits the game.\
23+
R - Resets and randomizes the board.\
24+
C - Clears the board.\
25+
F - Toggles display FPS.\
26+
Mouse Click - Toggles a cell on/off.

Video2/game.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "game.h"
2+
#include "init_sdl.h"
3+
4+
bool game_events(struct Game *g);
5+
void game_draw(struct Game *g);
6+
7+
bool game_new(struct Game **game) {
8+
*game = calloc(1, sizeof(struct Game));
9+
if (*game == NULL) {
10+
fprintf(stderr, "Error in calloc of new game.\n");
11+
return false;
12+
}
13+
struct Game *g = *game;
14+
15+
g->is_running = true;
16+
17+
if (!game_init_sdl(g)) {
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
24+
void game_free(struct Game **game) {
25+
if (*game) {
26+
SDL_DestroyRenderer((*game)->renderer);
27+
(*game)->renderer = NULL;
28+
29+
SDL_DestroyWindow((*game)->window);
30+
(*game)->window = NULL;
31+
32+
IMG_Quit();
33+
SDL_Quit();
34+
35+
free(*game);
36+
*game = NULL;
37+
38+
printf("all clean!\n");
39+
}
40+
}
41+
42+
bool game_events(struct Game *g) {
43+
while (SDL_PollEvent(&g->event)) {
44+
switch (g->event.type) {
45+
case SDL_QUIT:
46+
g->is_running = false;
47+
break;
48+
case SDL_KEYDOWN:
49+
switch (g->event.key.keysym.scancode) {
50+
case SDL_SCANCODE_ESCAPE:
51+
g->is_running = false;
52+
break;
53+
default:
54+
break;
55+
}
56+
default:
57+
break;
58+
}
59+
}
60+
61+
return true;
62+
}
63+
64+
void game_draw(struct Game *g) {
65+
SDL_RenderClear(g->renderer);
66+
67+
SDL_RenderPresent(g->renderer);
68+
}
69+
70+
bool game_run(struct Game *g) {
71+
while (g->is_running) {
72+
73+
if (!game_events(g)) {
74+
return false;
75+
}
76+
77+
game_draw(g);
78+
79+
SDL_Delay(16);
80+
}
81+
82+
return true;
83+
}

Video2/game.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef GAME_H
2+
#define GAME_H
3+
4+
#include "main.h"
5+
6+
struct Game {
7+
SDL_Window *window;
8+
SDL_Renderer *renderer;
9+
SDL_Event event;
10+
bool is_running;
11+
};
12+
13+
bool game_new(struct Game **game);
14+
void game_free(struct Game **game);
15+
bool game_run(struct Game *g);
16+
17+
#endif

Video2/init_sdl.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "init_sdl.h"
2+
3+
bool game_init_sdl(struct Game *g) {
4+
5+
if (SDL_Init(SDL_FLAGS)) {
6+
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
7+
return false;
8+
}
9+
10+
int img_init = IMG_Init(IMG_FLAGS);
11+
if ((img_init & IMG_FLAGS) != IMG_FLAGS) {
12+
fprintf(stderr, "Error initializing SDL_image: %s\n", IMG_GetError());
13+
return false;
14+
}
15+
16+
g->window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED,
17+
SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH,
18+
WINDOW_HEIGHT, 0);
19+
if (!g->window) {
20+
fprintf(stderr, "Error creating window: %s\n", SDL_GetError());
21+
return false;
22+
}
23+
24+
g->renderer = SDL_CreateRenderer(g->window, -1, SDL_RENDERER_ACCELERATED);
25+
if (!g->renderer) {
26+
fprintf(stderr, "Error creating renderer: %s\n", SDL_GetError());
27+
return false;
28+
}
29+
30+
SDL_Surface *icon_surf = IMG_Load("images/icon.png");
31+
if (icon_surf) {
32+
SDL_SetWindowIcon(g->window, icon_surf);
33+
SDL_FreeSurface(icon_surf);
34+
} else {
35+
fprintf(stderr, "Error creating icon surface: %s\n", SDL_GetError());
36+
return false;
37+
}
38+
39+
return true;
40+
}

Video2/init_sdl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef INIT_SDL_H
2+
#define INIT_SDL_H
3+
4+
#include "game.h"
5+
6+
bool game_init_sdl(struct Game *g);
7+
8+
#endif

Video2/main.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "main.h"
2+
#include "game.h"
3+
4+
int main(void) {
5+
bool exit_status = EXIT_FAILURE;
6+
7+
struct Game *game = NULL;
8+
9+
if (game_new(&game)) {
10+
if (game_run(game)) {
11+
exit_status = EXIT_SUCCESS;
12+
}
13+
}
14+
15+
game_free(&game);
16+
17+
return exit_status;
18+
}

Video2/main.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef MAIN_H
2+
#define MAIN_H
3+
4+
#include <SDL2/SDL.h>
5+
#include <SDL2/SDL_image.h>
6+
#include <stdbool.h>
7+
#include <stdio.h>
8+
9+
#define SDL_FLAGS (SDL_INIT_VIDEO | SDL_INIT_AUDIO)
10+
#define IMG_FLAGS IMG_INIT_PNG
11+
#define WINDOW_TITLE "Conway's Game of Life"
12+
#define WINDOW_WIDTH 1280
13+
#define WINDOW_HEIGHT 720
14+
15+
#endif

Video3/board.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "board.h"
2+
3+
bool board_new(struct Board **board, SDL_Renderer *renderer) {
4+
*board = calloc(1, sizeof(struct Board));
5+
if (*board == NULL) {
6+
fprintf(stderr, "Error in calloc of new Board.\n");
7+
return false;
8+
}
9+
struct Board *b = *board;
10+
11+
b->renderer = renderer;
12+
b->rows = WINDOW_HEIGHT / SIZE;
13+
b->columns = WINDOW_WIDTH / SIZE;
14+
b->rect.w = SIZE - 1;
15+
b->rect.h = SIZE - 1;
16+
17+
b->board = calloc(1, sizeof(bool) * (Uint64)(b->rows * b->columns));
18+
if (b->board == NULL) {
19+
fprintf(stderr, "Error in calloc of new board.\n");
20+
return false;
21+
}
22+
23+
b->next_board = calloc(1, sizeof(bool) * (Uint64)(b->rows * b->columns));
24+
if (b->next_board == NULL) {
25+
fprintf(stderr, "Error in calloc of new board.\n");
26+
return false;
27+
}
28+
29+
board_reset(b);
30+
31+
return true;
32+
}
33+
34+
void board_free(struct Board **board) {
35+
if (*board) {
36+
free((*board)->board);
37+
(*board)->board = NULL;
38+
39+
free((*board)->next_board);
40+
(*board)->next_board = NULL;
41+
42+
(*board)->renderer = NULL;
43+
44+
free(*board);
45+
*board = NULL;
46+
}
47+
}
48+
49+
void board_reset(struct Board *b) {
50+
for (int row = 0; row < b->rows * b->columns; row += b->columns) {
51+
for (int column = 0; column < b->columns; column++) {
52+
if (rand() % 2 == 0) {
53+
b->board[row + column] = true;
54+
} else {
55+
b->board[row + column] = false;
56+
}
57+
}
58+
}
59+
}
60+
61+
void board_clear(struct Board *b) {
62+
for (int row = 0; row < b->rows * b->columns; row += b->columns) {
63+
for (int column = 0; column < b->columns; column++) {
64+
b->board[row + column] = false;
65+
}
66+
}
67+
}
68+
69+
void board_draw(struct Board *b) {
70+
SDL_SetRenderDrawColor(b->renderer, CELL_COLOR);
71+
for (int row = 0; row < b->rows; row++) {
72+
b->rect.y = SIZE * row;
73+
for (int column = 0; column < b->columns; column++) {
74+
b->rect.x = SIZE * column;
75+
if (b->board[row * b->columns + column]) {
76+
SDL_RenderFillRect(b->renderer, &b->rect);
77+
}
78+
}
79+
}
80+
}

Video3/board.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef BOARD_H
2+
#define BOARD_H
3+
4+
#include "main.h"
5+
6+
struct Board {
7+
SDL_Renderer *renderer;
8+
bool *board;
9+
bool *next_board;
10+
int rows;
11+
int columns;
12+
SDL_Rect rect;
13+
};
14+
15+
bool board_new(struct Board **board, SDL_Renderer *renderer);
16+
void board_free(struct Board **board);
17+
void board_reset(struct Board *b);
18+
void board_clear(struct Board *b);
19+
void board_draw(struct Board *b);
20+
21+
#endif

0 commit comments

Comments
 (0)