-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmf.c
More file actions
129 lines (111 loc) · 2.73 KB
/
smf.c
File metadata and controls
129 lines (111 loc) · 2.73 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
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
#include "include/smf/smf.h"
SMF* SMF_initalizeMapfile(const char *file){
SMF *map = (SMF*) malloc(sizeof(SMF));
map->file = fopen(file, "rb");
if (map->file == NULL){
return (void*)0;
}
// Verify the signature
char *sig = (char*) malloc(sizeof(char)*5);
fread(sig, 4, 1, map->file);
if (strcmp("smap", sig) != 0){
return (void*)1;
}
// Getting the file length
uint16_t *objCount = (uint16_t*) malloc(sizeof(uint16_t));
fread(objCount, 2, 1, map->file);
if (objCount <= 0){
// Uh oh, something went wrong
return (void*)2;
}
// Assign obj count to our value
map->object_count = (*objCount);
free(objCount);
// Allocated our needed space
map->objects = (SMF_MAP*) malloc(sizeof(SMF_MAP) * map->object_count);
// Get objects
for (uint16_t x = 0; x < map->object_count; x++){
int res = readObject(&(map->objects[x]), map->file);
if (res != 0){
printf("Got Error: %d\n", res);
return (void*)3;
}
}
// Verify the ending signature
fread(sig, 4, 1, map->file);
if (strcmp("maps", sig) != 0){
printf("Warning: Failed to get formal ending for file: %s\n", file);
}
// We've verified that both sigures exists, we don't need sig anymore
free(sig);
return map;
}
static int readObject(SMF_MAP *map, FILE *file){
// Usage: Read the next object
// Read the length of the 3d model file name
uint16_t *nameLen = (uint16_t*) malloc(sizeof(uint16_t));
fread(nameLen, 2, 1, file);
if (nameLen <= 0){
// Uh oh, something went wrong
return -1;
}
// Read the 3d model file
char *fileName = (char*) malloc(sizeof(char) * (*nameLen) + 1);
fread(fileName, (*nameLen), 1, file);
if ((strcmp("", fileName) == 0) || fileName == NULL){
// Name broken
return -2;
}
map->mapFile = fileName;
fread(&map->x, 2, 1, file);
if (map->x < 0){
// Uh oh, something went wrong
return -3;
}
fread(&map->y, 2, 1, file);
if (map->y < 0){
// Uh oh, something went wrong
return -4;
}
fread(&map->z, 2, 1, file);
if (map->z < 0){
// Uh oh, something went wrong
return -5;
}
fread(&map->rotation, 2, 1, file);
if (map->rotation < 0){
// Uh oh, something went wrong
return -6;
}
fread(&map->x_rot, 1, 1, file);
if (map->x_rot < 0){
// Uh oh, something went wrong
return -7;
}
fread(&map->y_rot, 1, 1, file);
if (map->y_rot < 0){
// Uh oh, something went wrong
return -8;
}
fread(&map->z_rot, 1, 1, file);
if (map->z_rot < 0){
// Uh oh, something went wrong
return -9;
}
fread(&map->x_scl, 2, 1, file);
if (map->x_scl <= 0){
// Uh oh, something went wrong
return -10;
}
fread(&map->y_scl, 2, 1, file);
if (map->y_scl <= 0){
// Uh oh, something went wrong
return -11;
}
fread(&map->z_scl, 2, 1, file);
if (map->z_scl <= 0){
// Uh oh, something went wrong
return -12;
}
return 0;
}