about summary refs log tree commit diff
path: root/src/simulator/rendering.c
blob: 05a93f43ea3e1ef5cbde90213d5d179e48cb42d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "rendering.h"
#include "SDL_stdinc.h"
#include "../game.h"

#include <SDL2/SDL.h>

#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) {

  }

}