-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_from_template.c
160 lines (149 loc) · 5.67 KB
/
map_from_template.c
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
/**
* @file map_from_template.c
* @author Lukas Nejezchleb (nejezluk@fel.cvut.cz)
* @brief Module for creating map for the screen from the templates in the form described in the map template.h
* @version 0.1
* @date 2021-05-04
*
* @copyright Copyright (c) 2021
*
*/
#include "map_from_template.h"
#include <stdlib.h>
map_data *create_map_data(int screen_w, int screen_h, map_template *template)
{
map_data *map = malloc(sizeof(map_data));
if (map != NULL)
{
char *map_array = calloc(screen_h * screen_w, sizeof(char));
if (map_array == NULL)
{
free(map);
map = NULL;
}
else
{
map->board_arr = map_array;
}
}
if (map != NULL)
{
int cell_width = screen_w / template->width;
cell_width = (cell_width % 2 == 0) ? (cell_width - 1) : cell_width;
int cell_height = screen_h / template->height;
cell_height = (cell_height % 2 == 0) ? (cell_height - 1) : cell_height;
if (cell_height > cell_width)
{
map->max_object_diameter = cell_width;
}
else
{
map->max_object_diameter = cell_height;
}
map->width = screen_w;
map->height = screen_h;
for (int row = 0; row < template->height; row++)
{
for (int col = 0; col < template->width; col++)
{
// create map for each cell in template
create_cell(row, col, template, map);
}
}
map->ghost_spawn = get_coords_from_template(template->ghost_spawn_y, template->ghost_spawn_x, template,
screen_w, screen_h);
map->pacman_spawn = get_coords_from_template(template->pacman_spawn_y, template->pacman_spawn_x, template,
screen_w, screen_h);
}
return map;
}
void create_cell(int row, int col, map_template *template, map_data *map)
{
char *map_array = map->board_arr;
int screen_w = map->width;
int screen_h = map->height;
char cell_value = template->board[template->width * row + col];
if (cell_value == template->coin)
{
create_cross(row, col, template, map, COIN);
}
else if (cell_value == template->blank)
{
create_cross(row, col, template, map, PASSAGE);
}
else if (cell_value == template->wall)
{
int cell_width = screen_w / template->width;
cell_width = (cell_width % 2 == 0) ? (cell_width - 1) : cell_width;
int cell_height = screen_h / template->height;
cell_height = (cell_height % 2 == 0) ? (cell_height - 1) : cell_height;
// fill every pixel with wall
for (int i = 0; i < cell_width; ++i)
{
for (int j = 0; j < cell_height; ++j)
{
map_array[(row * cell_height + j) * screen_w + col * cell_width + i] = WALL;
}
}
}
else if (cell_value == template->special)
{
create_cross(row, col, template, map, SUPERCOIN);
}
}
void create_cross(int row, int col, map_template *template, map_data *map, char center_val)
{
int screen_w = map->width;
int screen_h = map->height;
char *map_array = map->board_arr;
int cell_width = screen_w / template->width;
cell_width = (cell_width % 2 == 0) ? (cell_width - 1) : cell_width;
int cell_height = screen_h / template->height;
cell_height = (cell_height % 2 == 0) ? (cell_height - 1) : cell_height;
int offset_x = col * cell_width;
int offset_y = row * cell_height;
// from top to bottom
int begin_index = offset_x + cell_width / 2 + screen_w * (offset_y + cell_height / 2);
if ((row > 0) && (template->board[(row - 1) * template->width + col] != template->wall))
{
begin_index = offset_x + cell_width / 2 + screen_w * offset_y;
}
int end_index = offset_x + cell_width / 2 + screen_w * (offset_y + cell_height / 2);
if ((row < template->height - 1) && (template->board[(row + 1) * template->width + col] != template->wall))
{
end_index = offset_x + cell_width / 2 + screen_w * (offset_y + cell_height);
}
for (int index = begin_index; index < end_index; index = index + screen_w)
{
map_array[index] = PASSAGE;
}
// from left to right
begin_index = offset_x + cell_width / 2 + screen_w * (offset_y + cell_height / 2);
if ((col > 0) && (template->board[(row) * template->width + col - 1] != template->wall))
{
begin_index = offset_x + screen_w * (offset_y + cell_height / 2);
}
end_index = offset_x + cell_width / 2 + screen_w * (offset_y + cell_height / 2);
if ((col < template->width - 1) && (template->board[(row) * template->width + col + 1] != template->wall))
{
end_index = offset_x + cell_width + screen_w * (offset_y + cell_height / 2);
}
for (int index = begin_index; index < end_index; index = index + 1)
{
map_array[index] = PASSAGE;
}
// fill in the centre val
map_array[offset_x + cell_width / 2 + screen_w * (offset_y + cell_height / 2)] = center_val;
}
coords get_coords_from_template(int row, int col, map_template *template, int screen_w, int screen_h)
{
int cell_width = screen_w / template->width;
cell_width = (cell_width % 2 == 0) ? (cell_width - 1) : cell_width;
int cell_height = screen_h / template->height;
cell_height = (cell_height % 2 == 0) ? (cell_height - 1) : cell_height;
coords coords = {
.x = col * cell_width + cell_width / 2,
.y = row * cell_height + cell_height / 2,
};
return coords;
}