]> 4ch.mooo.com Git - 16.git/blob - src/lib/mapread.c
The game can now load little tilesets... much improvement is needed in the map readin...
[16.git] / src / lib / mapread.c
1 #include "src/lib/mapread.h"
2
3 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
4         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
5                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
6                 return 0;
7         }
8         return -1;
9 }
10
11 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
12 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map, int q/*, int *w*/) {
13         int i, j, k;
14         bitmap_t bp;
15         if (count == 0) {
16                 return 0;
17         }
18         /* We may want to do strtol() here to get numeric value */
19         if (t->type == JSMN_PRIMITIVE) {
20                 if(js_sv == "data")
21                 {
22                         /*
23                                 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
24                                 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
25                                 here to read the data.... That is my design for this... wwww
26
27                                 FUCK well i am stuck.... wwww
28                         */
29                         map->data[q] = (byte)strtol(js+t->start, &(char *)t->end, 10)%4;
30 //                      printf("%d[%d]", q, map->data[q]);
31                 }
32                 else
33                 if(js_sv == "height")
34                 {
35                         //map->height = (int)malloc(sizeof(int));
36                         map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
37                         //printf("h:[%d]\n", map->height);
38                 }else if(js_sv == "width")
39                 {
40                         map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
41 //                      printf("w:[%d]\n", map->width);
42                 }
43                 return 1;
44                 /* We may use strndup() to fetch string value */
45         } else if (t->type == JSMN_STRING) {
46                 if(jsoneq(js, t, "data") == 0 )
47                 {
48 //                      printf("[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
49 //                      printf("\n%.*s[xx[%d|%d]xx]\n", (t+1)->end - (t+1)->start, js+(t+1)->start, &(t+1)->size, (t+1)->size);
50                         map->data = malloc(sizeof(byte) * (t+1)->size);
51                         map->tiles = malloc(sizeof(tiles_t));
52                         //map->tiles->data = malloc(sizeof(bitmap_t));
53                         printf("ed.pcx\n");
54                         bp = bitmapLoadPcx("data/ed.pcx");
55                         printf("ed.pcx\n");
56                         map->tiles->data = &bp;
57                         //map->tiles->data->data = malloc((16/**2*/)*16);
58                         //map->tiles->data->width = (16/**2*/);\r
59                         //map->tiles->data->height= 16;\r
60                         map->tiles->tileHeight = 16;\r
61                         map->tiles->tileWidth = 16;\r
62                         map->tiles->rows = 1;\r
63                         map->tiles->cols = 1;
64                         js_sv="data";//strdup(js+t->start);//, t->end - t->start);
65                 }
66                 else
67                 if (jsoneq(js, t, "height") == 0 && indent==1)
68                 {
69                         js_sv="height";//strdup(js+t->start);//, t->end - t->start);
70                 }else if (jsoneq(js, t, "width") == 0 && indent==1)
71                 {
72                         js_sv="width";//strdup(js+t->start);//, t->end - t->start);
73                 }else js_sv=NULL;
74                 return 1;
75         } else if (t->type == JSMN_OBJECT) {
76                 //printf("\n");
77                 j = 0;
78                 for (i = 0; i < t->size; i++) {
79                         //for (k = 0; k < indent; k++) printf("\t");
80                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
81                         //printf(": ");
82                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
83                         //printf("\n");
84                 }
85                 return j+1;
86         } else if (t->type == JSMN_ARRAY) {
87                 j = 0;
88                 //printf("==\n");
89                 for (i = 0; i < t->size; i++) {
90                         //for (k = 0; k < indent-1; k++) printf("\t");
91                         //printf("\t-");
92                         j += dump(js, t+1+j, count-j, indent+1, map, i/*, &t->size*/);
93                         //printf("==\n");
94                 }
95                 return j+1;
96         }
97         return 0;
98 }
99
100 static int loadmap(char *mn, map_t *map/*, word w*/)
101 {
102         int r;
103         int eof_expected = 0;
104         char *js = NULL;
105         size_t jslen = 0;
106         char buf[BUFSIZ];
107
108         jsmn_parser p;
109         jsmntok_t *tok;
110         size_t tokcount = 2;
111
112         FILE *fh = fopen(mn, "r");
113
114         /* Prepare parser */
115         jsmn_init(&p);
116
117         /* Allocate some tokens as a start */
118         tok = malloc(sizeof(*tok) * tokcount);
119         if (tok == NULL) {
120                 fprintf(stderr, "malloc(): errno=%d\n", errno);
121                 return 3;
122         }
123
124         for (;;) {
125                 /* Read another chunk */
126                 r = fread(buf, 1, sizeof(buf), fh);
127                 if (r < 0) {
128                         fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
129                         return 1;
130                 }
131                 if (r == 0) {
132                         if (eof_expected != 0) {
133                                 return 0;
134                         } else {
135                                 fprintf(stderr, "fread(): unexpected EOF\n");
136                                 return 2;
137                         }
138                 }
139
140                 js = realloc(js, jslen + r + 1);
141                 if (js == NULL) {
142                         fprintf(stderr, "realloc(): errno = %d\n", errno);
143                         return 3;
144                 }
145                 strncpy(js + jslen, buf, r);
146                 jslen = jslen + r;
147
148 again:
149                 r = jsmn_parse(&p, js, jslen, tok, tokcount);
150                 if (r < 0) {
151                         if (r == JSMN_ERROR_NOMEM) {
152                                 tokcount = tokcount * 2;
153                                 tok = realloc(tok, sizeof(*tok) * tokcount);
154                                 if (tok == NULL) {
155                                         fprintf(stderr, "realloc(): errno=%d\n", errno);
156                                         return 3;
157                                 }
158                                 goto again;
159                         }
160                 } else {
161                         dump(js, tok, p.toknext, 0, map, 0);
162                         eof_expected = 1;
163                 }
164         }
165
166         free(js);
167         free(tok);
168         fclose(fh);
169
170         return 0;
171 }