This repository was archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_saver.c
More file actions
100 lines (91 loc) · 1.97 KB
/
Copy pathmap_saver.c
File metadata and controls
100 lines (91 loc) · 1.97 KB
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
/*
** EPITECH PROJECT, 2018
** map_saver.c
** File description:
** map_saver
*/
#include <map.h>
#include <game.h>
#include <my_world.h>
void save_vertices(int fd, t_map *map)
{
list_node_t *node = map->vertices->first;
t_vertex *pos = NULL;
int index = 0;
while (node) {
pos = node->data;
pos->id = index;
write(fd, "v ", 2);
my_put_nbr(fd, pos->x);
write(fd, " ", 1);
my_put_nbr(fd, pos->y);
write(fd, " ", 1);
my_put_nbr(fd, pos->z);
write(fd, "\n", 1);
index++;
node = node->next;
}
}
void save_textures(int fd, t_map *map)
{
list_node_t *node = map->textures->list->first;
t_hmap_node *hmap_node = NULL;
t_texture *tex = NULL;
while (node) {
hmap_node = node->data;
tex = hmap_node->value;
write(fd, "t ", 2);
write(fd, tex->name, my_strlen(tex->name));
write(fd, " ", 1);
my_put_nbr(fd, (int) tex->x);
write(fd, " ", 1);
my_put_nbr(fd, (int) tex->y);
write(fd, " ", 1);
my_put_nbr(fd, (int) tex->xsize);
write(fd, " ", 1);
my_put_nbr(fd, (int) tex->ysize);
write(fd, "\n", 1);
node = node->next;
}
}
void save_faces(int fd, t_map *map)
{
list_node_t *node = map->faces->first;
t_face *face = NULL;
while (node) {
face = node->data;
write(fd, "f ", 2);
for (int i = 0; i < 4; i++) {
my_put_nbr(fd, face->vertices[i]->id + 1);
write(fd, " ", 1);
}
write(fd, face->texture->name, my_strlen(face->texture->name));
if (face->rotation) {
write(fd, " -r", 3);
my_put_nbr(fd, face->rotation);
}
write(fd, "\n", 1);
node = node->next;
}
}
void save_map(t_game *game, char *file)
{
int perm = O_WRONLY | O_CREAT | O_TRUNC;
t_map *map = game->map;
int fd = open(file, perm, 0664);
char *tmfp = NULL;
if (fd == -1)
update_state(game, "Error while saving");
else {
tmfp = map->tilemap_filepath;
write(fd, "tm ", 3);
write(fd, tmfp, my_strlen(tmfp));
write(fd, "\n\n", 2);
save_vertices(fd, map);
write(fd, "\n", 1);
save_textures(fd, map);
write(fd, "\n", 1);
save_faces(fd, map);
close(fd);
}
}