Skip to content

Commit

Permalink
optimize auxiliary grid rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr3www committed Aug 14, 2024
1 parent 2bacd75 commit 1ddf1da
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion include/cells.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ CellsGrid* CellsGrid_create(size_t width, size_t height, unsigned int cell_size)
void CellsGrid_delete(CellsGrid* cells_grid);

// Drawing
void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* viewport, int draw_mesh);
void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* viewport, SDL_Texture* mesh_texture, int draw_mesh);
16 changes: 4 additions & 12 deletions src/cells.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void CellsGrid_delete(CellsGrid* cells_grid) {
free(cells_grid);
}

void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* viewport, int draw_mesh) {
void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* viewport, SDL_Texture* mesh_texture, int draw_mesh) {
for (size_t x = 0; x < cells_grid->width; ++x) {
for (size_t y = 0; y < cells_grid->height; ++y) {
if (SDL_RenderSetViewport(renderer, viewport) != 0) {
Expand All @@ -75,16 +75,8 @@ void CellsGrid_draw(CellsGrid* cells_grid, SDL_Renderer* renderer, SDL_Rect* vie
}

if (draw_mesh) {
for (size_t x = 0; x < cells_grid->width; ++x) {
for (size_t y = 0; y < cells_grid->height; ++y) {
int return_code = rectangleColor(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,
GRAY_HEX);
if (return_code != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to render mesh for cell[%llu][%llu]\n", x, y);
}
}
}
if (SDL_RenderCopy(renderer, mesh_texture, NULL, NULL) != 0) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "Failed to render mesh\n");
}
}
}
34 changes: 31 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,41 @@ int main(int argc, char* argv[]) {
return 2;
}

// Load mesh texture
const char* mesh_bmp_path = "textures/aux_grid.bmp";
SDL_Surface* mesh_bmp = SDL_LoadBMP(mesh_bmp_path);
if (mesh_bmp == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load texture '%s': %s\n", mesh_bmp_path, SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to load texture", mesh_bmp_path, window);

FC_FreeFont(font);
close_SDL(window, renderer);
return 3;
}

// Mesh texture setup
SDL_Texture* mesh_texture = SDL_CreateTextureFromSurface(renderer, mesh_bmp);
SDL_FreeSurface(mesh_bmp);
if (mesh_texture == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create mesh texture: %s\n", SDL_GetError());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to create mesh texture", SDL_GetError(), window);

FC_FreeFont(font);
close_SDL(window, renderer);
return 4;
}
SDL_SetTextureScaleMode(mesh_texture, SDL_ScaleModeBest);

// Initialize RNG
time_t unix_time = time(NULL);
if (unix_time == (time_t)(-1)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to retrieve current Unix timestamp\n");
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "RNG initialization error", "Failed to retrieve current Unix timestamp", window);

FC_FreeFont(font);
SDL_DestroyTexture(mesh_texture);
close_SDL(window, renderer);
return 3;
return 5;
}
srand(unix_time);

Expand Down Expand Up @@ -75,8 +101,9 @@ int main(int argc, char* argv[]) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Cells initialization error", "Failed to create cells", window);

FC_FreeFont(font);
SDL_DestroyTexture(mesh_texture);
close_SDL(window, renderer);
return 4;
return 6;
}

// Define directions for counting alive neighbours
Expand Down Expand Up @@ -253,7 +280,7 @@ int main(int argc, char* argv[]) {

clear_screen(renderer, BLACK_HEX);

CellsGrid_draw(cells_grid, renderer, &viewport, draw_mesh);
CellsGrid_draw(cells_grid, renderer, &viewport, mesh_texture, draw_mesh);

// Calculate FPS every second
fps_current_time = SDL_GetTicks64();
Expand Down Expand Up @@ -281,6 +308,7 @@ int main(int argc, char* argv[]) {
// Clean up
CellsGrid_delete(cells_grid);
FC_FreeFont(font);
SDL_DestroyTexture(mesh_texture);
close_SDL(window, renderer);

return 0;
Expand Down
Binary file added textures/aux_grid.bmp
Binary file not shown.

0 comments on commit 1ddf1da

Please sign in to comment.