-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_fitting.c
67 lines (62 loc) · 1.98 KB
/
map_fitting.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* map_fitting.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/25 16:45:33 by cschuijt #+# #+# */
/* Updated: 2022/12/25 16:45:33 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdlib.h>
void fit_map_array_to_window(char ***map_array)
{
size_t missing_width;
size_t original_width;
size_t i;
char *str;
add_top_row(map_array);
i = 0;
original_width = ft_strlen((*map_array)[i]);
if (original_width >= MIN_MAP_W)
return ;
missing_width = MIN_MAP_W - original_width;
while ((*map_array)[i])
{
str = ft_calloc(MIN_MAP_W + 1, 1);
if (!str)
exit_perror("malloc error");
ft_memset(str, '1', MIN_MAP_W);
ft_memcpy(str + (missing_width / 2), (*map_array)[i], original_width);
free((*map_array)[i]);
(*map_array)[i] = str;
i++;
}
}
void add_top_row(char ***map_array)
{
size_t i;
size_t row_size;
char **new_array;
i = 0;
row_size = ft_strlen(*map_array[0]);
while ((*map_array)[i])
i++;
new_array = ft_calloc(sizeof(char *), i + 2);
if (!new_array)
exit_perror("malloc error");
new_array[0] = ft_calloc(1, row_size + 1);
if (!new_array[0])
exit_perror("malloc error");
ft_memset(new_array[0], '1', row_size);
i = 0;
while ((*map_array)[i])
{
new_array[i + 1] = (*map_array)[i];
i++;
}
free(*map_array);
*map_array = new_array;
}