#include "rendering.h" #include "SDL_stdinc.h" #include "../game.h" #include #define GAP_SIZE (fminf(CELL_WIDTH, CELL_HEIGHT) / 1.1f) int scc(int code) { if (code < 0) { fprintf(stderr, "SDL Error: %s\n", SDL_GetError()); exit(1); } return code; } void *scp(void *ptr) { if (ptr == NULL) { fprintf(stderr, "SDL Error: %s\n", SDL_GetError()); exit(1); } return ptr; } SDL_Vertex sdl_color_hex_vertex(int x, int y, Uint32 hex) { SDL_Vertex vertex; vertex.position.x = (float)x; vertex.position.y = (float)y; vertex.color.r = (hex >> (3 * 8)) & 0xFF; vertex.color.g = (hex >> (2 * 8)) & 0xFF; vertex.color.b = (hex >> (1 * 8)) & 0xFF; vertex.color.a = (hex >> (0 * 8)) & 0xFF; vertex.tex_coord.x = 0; vertex.tex_coord.y = 0; return vertex; } void render_board_grid(SDL_Renderer *renderer) { SDL_SetRenderDrawColor(renderer, HEX_COLOR(GRID_COLOR)); for (int x = 1; x < BOARD_WIDTH; ++x) { SDL_RenderDrawLine(renderer, x * CELL_WIDTH, 0, x * CELL_WIDTH, SCREEN_HEIGHT); } for (int x = 1; x < BOARD_HEIGHT; ++x) { SDL_RenderDrawLine(renderer, 0, x * CELL_HEIGHT, SCREEN_WIDTH, x * CELL_HEIGHT); } } #define AGENT_CELL_POS_X agent.pos.x * CELL_WIDTH #define AGENT_CELL_POS_Y agent.pos.y * CELL_HEIGHT #define VERTEX_0 \ sdl_color_hex_vertex(AGENT_CELL_POS_X + GAP_SIZE, \ AGENT_CELL_POS_Y + GAP_SIZE, \ color) #define VERTEX_1 \ sdl_color_hex_vertex(AGENT_CELL_POS_X + CELL_WIDTH - GAP_SIZE, \ AGENT_CELL_POS_Y + GAP_SIZE, \ color) #define VERTEX_2 \ sdl_color_hex_vertex(AGENT_CELL_POS_X + GAP_SIZE, \ AGENT_CELL_POS_Y + CELL_HEIGHT - GAP_SIZE, \ color) #define VERTEX_3 \ sdl_color_hex_vertex(AGENT_CELL_POS_X + CELL_WIDTH - GAP_SIZE, \ AGENT_CELL_POS_Y + CELL_HEIGHT - GAP_SIZE, \ color) #define FOOD_CELL_POS_X x * CELL_WIDTH #define FOOD_CELL_POS_Y y * CELL_HEIGHT SDL_Vertex* sdl_draw_food_diamond(int y, int x) { SDL_Vertex* vertices = malloc(4 * sizeof(SDL_Vertex)); if (vertices == NULL) { fprintf(stderr, "Vertex Memory allocation failed\n"); exit(1); } vertices[0] = sdl_color_hex_vertex(FOOD_CELL_POS_X + CELL_WIDTH - GAP_SIZE, FOOD_CELL_POS_Y + (CELL_WIDTH / 2), FOOD_COLOR); vertices[1] = sdl_color_hex_vertex(FOOD_CELL_POS_X + (CELL_WIDTH / 2), FOOD_CELL_POS_Y + GAP_SIZE, FOOD_COLOR); vertices[2] = sdl_color_hex_vertex(FOOD_CELL_POS_X + GAP_SIZE, FOOD_CELL_POS_Y + (CELL_WIDTH / 2), FOOD_COLOR); vertices[3] = sdl_color_hex_vertex(FOOD_CELL_POS_X + (CELL_WIDTH / 2), FOOD_CELL_POS_Y + CELL_HEIGHT - GAP_SIZE, FOOD_COLOR); return vertices; } SDL_Vertex* sdl_draw_agent_triangle(Agent agent) { SDL_Vertex* vertices = malloc(3 * sizeof(SDL_Vertex)); if (vertices == NULL) { fprintf(stderr, "Vertex Memory allocation failed\n"); exit(1); } Uint32 color = agent.health <= 0 ? AGENT_COLOR_DEAD : AGENT_COLOR_ALIVE; switch (agent.dir) { case DIR_LEFT: vertices[0] = VERTEX_0; vertices[1] = VERTEX_2; vertices[2] = sdl_color_hex_vertex(AGENT_CELL_POS_X + CELL_WIDTH - GAP_SIZE, AGENT_CELL_POS_Y + (CELL_WIDTH / 2), color); break; case DIR_DOWN: vertices[0] = VERTEX_2; vertices[1] = VERTEX_3; vertices[2] = sdl_color_hex_vertex(AGENT_CELL_POS_X + (CELL_WIDTH / 2), AGENT_CELL_POS_Y + GAP_SIZE, color); break; case DIR_RIGHT: vertices[0] = VERTEX_1; vertices[1] = VERTEX_3; vertices[2] = sdl_color_hex_vertex(AGENT_CELL_POS_X + GAP_SIZE, AGENT_CELL_POS_Y + (CELL_WIDTH / 2), color); break; case DIR_UP: vertices[0] = VERTEX_0; vertices[1] = VERTEX_1; vertices[2] = sdl_color_hex_vertex(AGENT_CELL_POS_X + (CELL_WIDTH / 2), AGENT_CELL_POS_Y + CELL_HEIGHT - GAP_SIZE, color); break; } return vertices; } void render_agent(SDL_Renderer *renderer, Agent agent) { SDL_Vertex * vertices = sdl_draw_agent_triangle(agent); scc(SDL_RenderGeometry(renderer, NULL, vertices, 3, NULL, 0)); free(vertices); // sdl_draw_agent_triangle's malloc } void render_game(SDL_Renderer *renderer, const Game *game) { for (size_t i = 0; i < AGENTS_COUNT; ++i) { render_agent(renderer, game->agents[i]); } // Render Foods const int indices[] = {0, 1, 2, 0, 3, 2}; for(int y = 0; y < BOARD_HEIGHT; ++y) { for (int x = 0; x < BOARD_WIDTH; ++x) { if (game->foods_map[y][x]) { SDL_Vertex *vertices = sdl_draw_food_diamond(y, x); scc(SDL_RenderGeometry(renderer, NULL, vertices, 4, indices, 6)); free(vertices); // sdl_draw_food_diamond's malloc } if (game->walls_map[y][x]) { SDL_Rect rect = {// Check why int floorf (int)floorf(x * CELL_WIDTH), (int)floorf(y * CELL_HEIGHT), (int)floorf(CELL_WIDTH), (int)floorf(CELL_HEIGHT)}; SDL_SetRenderDrawColor(renderer, HEX_COLOR(WALL_COLOR)); SDL_RenderFillRect(renderer, &rect); } } } // Render Walls for (size_t i = 0; i < WALLS_COUNT; ++i) { } }