forked from Orpheon/Navmesh-Pathfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_navmesh.c
64 lines (60 loc) · 1.7 KB
/
export_navmesh.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
#include "export_navmesh.h"
#include "data_types.h"
#include <stdio.h>
#include <string.h>
void export_navmesh(Navmesh *mesh, char* name)
{
char filename[strlen(name)+strlen(".navmesh")];
strcpy(filename, name);
strcat(filename, ".navmesh");
FILE *f;
f = fopen(filename, "wb");
fwrite((const void*) &(mesh->num_rects), sizeof(int), 1, f);
RectLinkedList *l;
Rect *r;
l = mesh->list;
for (int i=0; i<mesh->num_rects; i++)
{
r = l->rect;
// Write everything to file
fwrite((const void*) &(r->topleft.x), sizeof(int), 1, f);
fwrite((const void*) &(r->topleft.y), sizeof(int), 1, f);
fwrite((const void*) &(r->bottomleft.x), sizeof(int), 1, f);
fwrite((const void*) &(r->bottomleft.y), sizeof(int), 1, f);
fwrite((const void*) &(r->topright.x), sizeof(int), 1, f);
fwrite((const void*) &(r->topright.y), sizeof(int), 1, f);
fwrite((const void*) &(r->bottomright.x), sizeof(int), 1, f);
fwrite((const void*) &(r->bottomright.y), sizeof(int), 1, f);
l = l->next;
}
l = mesh->list;
int tmp;
for (int i=0; i<mesh->num_rects; i++)
{
r = l->rect;
fwrite(&(r->num_connections), sizeof(int), 1, f);
for (int j=0; j<r->num_connections; j++)
{
tmp = find_index(mesh, r->connections[j]);
fwrite((const void*) &tmp, sizeof(int), 1, f);
}
l = l->next;
}
fclose(f);
}
int find_index(Navmesh *mesh, Rect *rect)
{
int i = 0;
RectLinkedList *l;
l = mesh->list;
while (l != NULL)
{
if (l->rect == rect)
{
return i;
}
i++;
l = l->next;
}
return -1;
}