Skip to content

Commit

Permalink
add colors animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr3www committed May 24, 2024
1 parent 390add0 commit 33de373
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/cells.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef struct CellStruct {
Sint16 pos_x, pos_y;
int is_alive;
unsigned int alive_neighbours;
Uint8 r, g, b;
} Cell;

typedef struct CellsGridStruct {
Expand Down
2 changes: 0 additions & 2 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "SDL2_framerate.h"

#define WHITE_HEX 0xffffffff

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define BLACK_HEX 0x000000ff
#define GRAY_HEX 0x777777ff
Expand Down
9 changes: 7 additions & 2 deletions src/cells.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ CellsGrid* CellsGrid_create(size_t width, size_t height, unsigned int cell_size)
cell[x][y].pos_y = y * cell_size;
cell[x][y].is_alive = rand() % 2;
cell[x][y].alive_neighbours = 0;

Uint8 color = cell[x][y].is_alive ? 255 : 0;
cell[x][y].r = color;
cell[x][y].g = color;
cell[x][y].b = color;
}
}

Expand All @@ -59,10 +64,10 @@ void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* vie
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to set viewport for cells grid: %s\n", SDL_GetError());
}

int return_code = boxColor(renderer,
int return_code = boxRGBA(renderer,
cells_grid->cell[x][y].pos_x, cells_grid->cell[x][y].pos_y,
cells_grid->cell[x][y].pos_x + cells_grid->cell_size, cells_grid->cell[x][y].pos_y + cells_grid->cell_size,
cells_grid->cell[x][y].is_alive ? WHITE_HEX : BLACK_HEX);
cells_grid->cell[x][y].r, cells_grid->cell[x][y].g, cells_grid->cell[x][y].b, 255);
if (return_code != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to render cell[%llu][%llu]\n", x, y);
}
Expand Down
46 changes: 44 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static const unsigned int CELL_NUMBER_HEIGHT = 100;
static const int WINDOW_WIDTH = CELL_NUMBER_WIDTH * CELL_SIZE;
static const int WINDOW_HEIGHT = CELL_NUMBER_HEIGHT * CELL_SIZE;

static const float COLOR_ANIM_FACTOR = 14.0f;

int main(int argc, char* argv[]) {
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Expand Down Expand Up @@ -114,6 +116,11 @@ int main(int argc, char* argv[]) {
for (size_t x = 0; x < cells_grid->width; ++x) {
for (size_t y = 0; y < cells_grid->height; ++y) {
cells_grid->cell[x][y].is_alive = rand() % 2;

Uint8 color = cells_grid->cell[x][y].is_alive ? 255 : 0;
cells_grid->cell[x][y].r = color;
cells_grid->cell[x][y].g = color;
cells_grid->cell[x][y].b = color;
}
}
tick = 0;
Expand All @@ -122,6 +129,10 @@ int main(int argc, char* argv[]) {
for (size_t x = 0; x < cells_grid->width; ++x) {
for (size_t y = 0; y < cells_grid->height; ++y) {
cells_grid->cell[x][y].is_alive = 0;

cells_grid->cell[x][y].r = 0;
cells_grid->cell[x][y].g = 0;
cells_grid->cell[x][y].b = 0;
}
}
tick = 0;
Expand Down Expand Up @@ -158,6 +169,11 @@ int main(int argc, char* argv[]) {
if (mouse_x >= cells_grid->cell[x][y].pos_x && (unsigned)mouse_x <= cells_grid->cell[x][y].pos_x + cells_grid->cell_size &&
mouse_y >= cells_grid->cell[x][y].pos_y && (unsigned)mouse_y <= cells_grid->cell[x][y].pos_y + cells_grid->cell_size) {
cells_grid->cell[x][y].is_alive = mouse_button == 1 ? 1 : 0;

Uint8 color = cells_grid->cell[x][y].is_alive ? 255 : 0;
cells_grid->cell[x][y].r = color;
cells_grid->cell[x][y].g = color;
cells_grid->cell[x][y].b = color;
}
}
}
Expand Down Expand Up @@ -197,12 +213,38 @@ int main(int argc, char* argv[]) {
// Check rules
for (size_t x = 0; x < cells_grid->width; ++x) {
for (size_t y = 0; y < cells_grid->height; ++y) {
if (cells_grid->cell[x][y].is_alive && (cells_grid->cell[x][y].alive_neighbours < 2 || cells_grid->cell[x][y].alive_neighbours > 3)) {
cells_grid->cell[x][y].is_alive = 0;

cells_grid->cell[x][y].r = 255;
cells_grid->cell[x][y].g = 255;
cells_grid->cell[x][y].b = 255;
}
else if (!cells_grid->cell[x][y].is_alive && cells_grid->cell[x][y].alive_neighbours == 3) {
cells_grid->cell[x][y].is_alive = 1;

cells_grid->cell[x][y].r = 255;
cells_grid->cell[x][y].g = 255;
cells_grid->cell[x][y].b = 255;
}


// Change color accordingly
float dr, dg, db;
if (cells_grid->cell[x][y].is_alive) {
cells_grid->cell[x][y].is_alive = !(cells_grid->cell[x][y].alive_neighbours < 2 || cells_grid->cell[x][y].alive_neighbours > 3);
dr = cells_grid->cell[x][y].r > COLOR_ANIM_FACTOR / 8 ? COLOR_ANIM_FACTOR / 8 : 0.0f;
dg = cells_grid->cell[x][y].g > COLOR_ANIM_FACTOR / 16 ? COLOR_ANIM_FACTOR / 16 : 0.0f;
db = cells_grid->cell[x][y].b > 150 + COLOR_ANIM_FACTOR / 32 ? COLOR_ANIM_FACTOR / 32 : 0.0f;
}
else {
cells_grid->cell[x][y].is_alive = (cells_grid->cell[x][y].alive_neighbours == 3);
dr = cells_grid->cell[x][y].r > COLOR_ANIM_FACTOR / 4 ? COLOR_ANIM_FACTOR / 4 : 0.0f;
dg = cells_grid->cell[x][y].g > COLOR_ANIM_FACTOR / 2 ? COLOR_ANIM_FACTOR / 2 : 0.0f;
db = cells_grid->cell[x][y].b > COLOR_ANIM_FACTOR ? COLOR_ANIM_FACTOR : 0.0f;
}

cells_grid->cell[x][y].r -= dr;
cells_grid->cell[x][y].g -= dg;
cells_grid->cell[x][y].b -= db;
}
}

Expand Down

0 comments on commit 33de373

Please sign in to comment.