1 /* Project 16 Source Code~
\r
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
\r
4 * This file is part of Project 16.
\r
6 * Project 16 is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Project 16 is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program. If not, see <http://www.gnu.org/licenses/>, or
\r
18 * write to the Free Software Foundation, Inc., 51 Franklin Street,
\r
19 * Fifth Floor, Boston, MA 02110-1301 USA.
\r
23 #include "src/lib/16_map.h"
\r
25 // Ideally, preprocess json during compilation and read serialized data
\r
27 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
\r
28 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
\r
29 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
\r
35 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
\r
36 word dump(const char *js, jsmntok_t *t, size_t count, word indent, char *js_sv, map_t *map, dword q) {
\r
43 fprintf(stdout, "%s\n", js);
\r
44 fprintf(stdout, "\n");
\r
47 #ifdef DEBUG_DUMPVARS
\r
48 fprintf(stdout, "t->size=[%d] ", t->size);
\r
49 fprintf(stdout, "q=[%d] ", q);
\r
50 fprintf(stdout, "indent= [%d] ", indent);
\r
51 fprintf(stdout, "js_sv= [%s]\n", js_sv);
\r
56 /* We may want to do strtol() here to get numeric value */
\r
57 //0000fprintf(stderr, "t->type=%d\n", t->type);
\r
58 if (t->type == JSMN_PRIMITIVE) {
\r
59 if(strstr(js_sv, "data"))
\r
62 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
\r
63 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
\r
64 here to read the data.... That is my design for this... wwww
\r
66 FUCK well i am stuck.... wwww
\r
68 map->data[q] = (byte)atoi(js+t->start);
\r
69 #ifdef DEBUG_MAPDATA
\r
70 fprintf(stdout, "%d[%d]", q, map->data[q]);
\r
74 if(strstr(js_sv, "height"))
\r
76 map->height = atoi(js+t->start);
\r
78 fprintf(stdout, "indent= [%d] ", indent);
\r
79 fprintf(stdout, "h:[%d]\n", map->height);
\r
81 }else if(strstr(js_sv, "width"))
\r
83 map->width = atoi(js+t->start);
\r
85 fprintf(stdout, "indent= [%d] ", indent);
\r
86 fprintf(stdout, "w:[%d]\n", map->width);
\r
90 /* We may use strndup() to fetch string value */
\r
91 } else if (t->type == JSMN_STRING) {
\r
92 if(jsoneq(js, t, "data") == 0)
\r
94 // fprintf(stdout, "[[[[%d|%d]]]]\n", &(t+1)->size, (t+1)->size);
\r
95 // fprintf(stdout, "\n%.*s[xx[%d|%d]xx]\n", (t+1)->end - (t+1)->start, js+(t+1)->start, &(t+1)->size, (t+1)->size);
\r
96 map->data = malloc(sizeof(byte) * (t+1)->size);
\r
97 map->tiles = malloc(sizeof(tiles_t));
\r
98 map->tiles->btdata = malloc(sizeof(bitmap_t));
\r
99 //fix this to be far~
\r
100 //0000 bp = bitmapLoadPcx("data/ed.pcx");
\r
101 // bp = bitmapLoadPcx("data/koishi^^.pcx");
\r
102 map->tiles->btdata = &bp;
\r
103 //---- map->tiles->data = planar_buf_from_bitmap(&bp);
\r
104 //map->tiles->data->data = malloc((16/**2*/)*16);
\r
105 //map->tiles->data->width = (16/**2*/);
\r
106 //map->tiles->data->height= 16;
\r
107 map->tiles->tileHeight = 16;
\r
108 map->tiles->tileWidth = 16;
\r
109 map->tiles->rows = 1;
\r
110 map->tiles->cols = 1;
\r
111 map->tiles->debug_text=false;
\r
112 strcpy(js_sv, "data");//strdup(js+t->start);//, t->end - t->start);
\r
115 if (jsoneq(js, t, "height") == 0 && indent<=1)
\r
117 strcpy(js_sv, "height");//strdup(js+t->start);//, t->end - t->start);
\r
119 if(jsoneq(js, t, "width") == 0 && indent<=1)
\r
121 strcpy(js_sv, "width");//strdup(js+t->start);//, t->end - t->start);
\r
122 }else strcpy(js_sv, "\0");
\r
124 } else if (t->type == JSMN_OBJECT) {
\r
125 //fprintf(stdout, "\n");
\r
127 for (i = 0; i < t->size; i++) {
\r
128 //for (k = 0; k < indent; k++) fprintf(stdout, "\t");
\r
129 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
130 //fprintf(stdout, ": ");
\r
131 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
132 //fprintf(stdout, "\n");
\r
135 } else if (t->type == JSMN_ARRAY) {
\r
137 //fprintf(stdout, "==\n");
\r
138 for (i = 0; i < t->size; i++) {
\r
139 //for (k = 0; k < indent-1; k++) fprintf(stdout, "\t");
\r
140 //fprintf(stdout, "\t-");
\r
141 j += dump(js, t+1+j, count-j, indent+1, js_sv, map, i);
\r
142 //fprintf(stdout, "==\n");
\r
149 int loadmap(char *mn, map_t *map)
\r
152 static word incr=0;
\r
153 int eof_expected = 0;
\r
157 static char js_ss[16];
\r
161 size_t tokcount = 2;
\r
163 FILE *fh = fopen(mn, "r");
\r
165 /* Prepare parser */
\r
168 /* Allocate some tokens as a start */
\r
169 //0000fprintf(stderr, "tok malloc\n");
\r
170 tok = malloc(sizeof(*tok) * tokcount);
\r
172 fprintf(stderr, "malloc(): errno=%d\n", errno);
\r
177 /* Read another chunk */
\r
178 //0000fprintf(stderr, "read\n");
\r
179 r = fread(buf, 1, sizeof(buf), fh);
\r
181 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
\r
185 if (eof_expected != 0) {
\r
188 fprintf(stderr, "fread(): unexpected EOF\n");
\r
192 //0000fprintf(stdout, "r= [%d] BUFSIZ=%d\n", r, BUFSIZ);
\r
193 //0000fprintf(stderr, "js alloc~\n");
\r
194 js = realloc(js, jslen + r + 1);
\r
196 fprintf(stderr, "*js=%Fp\n", *js);
\r
197 fprintf(stderr, "realloc(): errno = %d\n", errno);
\r
200 strncpy(js + jslen, buf, r);
\r
204 //0000fprintf(stdout, " parse~ tok=%zu jslen=%zu r=%d _memavl()=%u BUFSIZ=%d~\n", tokcount, jslen, r, _memavl(), BUFSIZ);
\r
205 //0000fprintf(stdout, "p=[%u] [%u] [%d]\n", p.pos, p.toknext, p.toksuper);
\r
207 I think it crashes on the line below when it tries to parse the data of huge maps... wwww this is a jsmn problem wwww
\r
209 r = jsmn_parse(&p, js, jslen, tok, tokcount);
\r
210 //0000fprintf(stdout, "r= [%d]\n", r);
\r
212 if (r == JSMN_ERROR_NOMEM) {
\r
213 tokcount = tokcount * 2;
\r
214 //0000fprintf(stderr, "tok realloc~ %zu\n", tokcount);
\r
215 tok = realloc(tok, sizeof(*tok) * tokcount);
\r
217 fprintf(stderr, "realloc(): errno=%d\n", errno);
\r
223 //printf("js=%Fp\n", (js));
\r
224 //printf("*js=%Fp\n", (*(js)));
\r
225 //printf("&*js=%s\n", &(*(js)));
\r
226 //printf("&buf=[%Fp]\n", &buf);
\r
227 //printf("&buf_seg=[%x]\n", FP_SEG(&buf));
\r
228 //printf("&buf_off=[%x]\n", FP_OFF(&buf));
\r
229 //printf("&buf_fp=[%Fp]\n", MK_FP(FP_SEG(&buf), FP_OFF(&buf)));
\r
230 //printf("buf=[\n%s\n]\n", buf);
\r
231 //printf("buff=[%Fp]\n", buff);
\r
232 //printf("(*buff)=[%Fp]\n", (*buff));
\r
233 //printf("&(*buff)=[\n%s\n]\n", &(*buff));
\r
234 #ifdef DEBUG_DUMPVARS
\r
235 fprintf(stdout, "running dump~\n");
\r
237 dump(js, tok, p.toknext, incr, &js_ss, map, 0);
\r
249 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {
\r
250 int i, j, k, indent=0, inner_end;
\r
255 if(jsoneq(js, &(t[i]), "layers") == 0) {
\r
257 map->layerdata = malloc(sizeof(byte*) * t[i].size);
\r
258 inner_end = t[i].end;
\r
260 while(t[i].start < inner_end) {
\r
261 printf("%d, %d\n", t[i].start, inner_end);
\r
262 if(jsoneq(js, &(t[i]), "data") == 0) {
\r
263 #ifdef DEBUG_MAPVAR
\r
264 printf("Layer %d data: [", k);
\r
266 map->layerdata[k] = malloc(sizeof(byte) * t[i+1].size);
\r
267 for(j = 0; j < t[i+1].size; j++) {
\r
268 map->layerdata[k][j] = (byte)atoi(js + t[i+2+j].start);
\r
269 #ifdef DEBUG_MAPVAR
\r
270 printf("%d, ", map->layerdata[k][j]);
\r
275 #ifdef DEBUG_MAPVAR
\r
284 if(jsoneq(js, &(t[i]), "tilesets") == 0) {
\r
286 map->tiles = malloc(sizeof(tiles_t*) * t[i].size);
\r
287 inner_end = t[i].end;
\r
289 while(t[i].start < inner_end) {
\r
290 if(jsoneq(js, &(t[i]), "image") == 0) {
\r
291 //fix this to be far~
\r
292 map->layertile[k] = malloc(sizeof(tiles_t));
\r
293 map->layertile[k]->btdata = malloc(sizeof(bitmap_t));
\r
294 map->layertile[k]->tileHeight = 16;
\r
295 map->layertile[k]->tileWidth = 16;
\r
296 map->layertile[k]->rows = 1;
\r
297 map->layertile[k]->cols = 1;
\r
298 map->layertile[k]->debug_text=false;
\r
299 //Fix to load tileset specified.
\r
300 //And move to vrs, probably
\r
301 //bp = bitmapLoadPcx("data/ed.pcx");
\r
302 map->layertile[k]->btdata = &bp;
\r
309 if (jsoneq(js, &(t[i]), "height") == 0 && indent<=1) {
\r
310 map->height = atoi(js + t[i+1].start);
\r
311 #ifdef DEBUG_MAPVAR
\r
312 printf("Height: %d\n", map->height);
\r
316 else if(jsoneq(js, &(t[i]), "width") == 0 && indent<=1) {
\r
317 map->width = atoi(js + t[i+1].start);
\r
318 #ifdef DEBUG_MAPVAR
\r
319 printf("Width: %d\n", map->width);
\r
327 int newloadmap(char *mn, map_t *map) {
\r
331 jsmntok_t *tok = NULL;
\r
332 size_t tokcount, file_s;
\r
334 FILE *fh = fopen(mn, "r");
\r
337 /* Prepare parser */
\r
340 file_s = filesize(fh);
\r
341 js = malloc(file_s);
\r
343 fprintf(stderr, "malloc(): errno = %d", 2);
\r
347 if(fread(js, 1, file_s, fh) != file_s) {
\r
348 fprintf(stderr, "Map read error");
\r
353 tokcount = jsmn_parse(&p, js, file_s, NULL, 0);
\r
354 tok = malloc(tokcount*sizeof(jsmntok_t));
\r
355 printf("Allocated %d tokens", tokcount);
\r
357 if((status = jsmn_parse(&p, js, file_s, tok, tokcount)) < 0)
\r
359 printf("Error: %d\n", status);
\r
362 else if(status != tokcount) { printf("Warning: used %d tok\n", status);}
\r
363 extract_map(js, tok, tokcount, map);
\r