1 #include "src/lib/mapread.h"
4 initMap(map_t *map, int maps) {
\r
5 // just a place holder to fill out an alternating pattern
\r
9 //if(!isEMS() || !checkEMS())
\r
10 map->tiles = malloc(sizeof(tiles_t));
\r
12 // map->tiles = (tiles_t *)alloc_emem(sizeof(tiles_t));
\r
14 // create the tile set
\r
15 //if(!isEMS() || !checkEMS())
\r
16 map->tiles->data = malloc(sizeof(bitmap_t));
\r
18 // map->tiles->data = (bitmap_t *)alloc_emem(sizeof(bitmap_t));
\r
19 map->tiles->data->width = (16*2);
\r
20 map->tiles->data->height= 16;
\r
21 //if(!isEMS() || !checkEMS())
\r
22 map->tiles->data->data = malloc((16*2)*16);
\r
24 // map->tiles->data->data = (byte *)alloc_emem((TILEWH*2)*TILEWH);
\r
25 map->tiles->tileHeight = 16;
\r
26 map->tiles->tileWidth =16;
\r
27 map->tiles->rows = 1;
\r
28 map->tiles->cols = 2;
\r
31 //for(y=0; y<TILEWH; y++) {
\r
32 //for(x=0; x<(TILEWH*2); x++) {
\r
34 // map->tiles->data->data[i] = 28;//0x24;
\r
36 // map->tiles->data->data[i] = 0;//0x34;
\r
42 for(y=0; y<map->height; y++) {
\r
43 for(x=0; x<map->width; x++) {
\r
44 map->data[i] = tile;
\r
45 tile = tile ? 0 : 1;
\r
48 tile = tile ? 0 : 1;
\r
52 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
53 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
54 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
60 //this function is quite messy ^^; sorry! it is a quick and dirty fix~
61 static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map, int q/*, int w*/) {
66 /* We may want to do strtol() here to get numeric value */
67 if (t->type == JSMN_PRIMITIVE) {
72 here we should recursivly call dump again here to skip over the array until we get the facking width of the map.
73 so we can initiate the map which allocates the facking map->tiles->data->data properly and THEN we can return
74 here to read the data.... That is my design for this... wwww
76 FUCK well i am stuck.... wwww
78 //dump(js, t+1, count, indent, map, i);
80 //bgdata[q] = strtol(js+t->start, (char **)js+t->end, 10);
81 if(strtol(js+t->start, (char **)js+t->end, 10)==0){ /*printf("%d\n", sizeof(map->tiles->data->data));*/ /*fprintf(stderr, "\nFACK! %d\n", errno);*/ return 1; /*exit(-1); */}
82 map->tiles->data->data[q] = (byte)strtol(js+t->start, (char **)js+t->end, 10);
83 printf("%d[%d]", q, map->tiles->data->data[q]);
84 //printf("%d[%d]", q, bgdata[q]);
89 map->height = (int)strtol(js+t->start, (char **)js+t->end, 10);
90 printf("h:[%d]\n", map->height);
91 }else if(js_sv == "width")
93 map->width = (int)strtol(js+t->start, (char **)js+t->end, 10);
94 printf("w:[%d]\n", map->width);
97 /* We may use strndup() to fetch string value */
98 } else if (t->type == JSMN_STRING) {
99 //printf("'%.*s'", t->end - t->start, js+t->start);
101 if(jsoneq(js, t, "data") == 0 )
104 //map->tiles->data->data = malloc((16*2)*16);
105 js_sv="data";//strdup(js+t->start);//, t->end - t->start);
106 //printf("%s\n", js_sv);
109 if (jsoneq(js, t, "height") == 0 && indent==1)
111 js_sv="height";//strdup(js+t->start);//, t->end - t->start);
112 //printf("%s\n", js_sv);
113 }else if (jsoneq(js, t, "width") == 0 && indent==1)
115 js_sv="width";//strdup(js+t->start);//, t->end - t->start);
116 //printf("%s\n", js_sv);
119 } else if (t->type == JSMN_OBJECT) {
122 for (i = 0; i < t->size; i++) {
123 //for (k = 0; k < indent; k++) printf("\t");
124 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
126 j += dump(js, t+1+j, count-j, indent+1, map, i/*, w*/);
130 } else if (t->type == JSMN_ARRAY) {
133 for (i = 0; i < t->size; i++) {
134 //printf("[[[[%d]]]]\n", t->size);
135 //if(bgdata==NULL) bgdata=malloc(sizeof(char)*t->size);
136 //for (k = 0; k < indent-1; k++) printf("\t");
138 j += dump(js, t+1+j, count-j, indent+1, map, i/*, t->size*/);
146 static int loadmap(char *mn, map_t *map/*, word w*/)
149 int eof_expected = 0;
158 FILE *fh = fopen(mn, "r");
163 /* Allocate some tokens as a start */
164 tok = malloc(sizeof(*tok) * tokcount);
166 fprintf(stderr, "malloc(): errno=%d\n", errno);
171 /* Read another chunk */
172 r = fread(buf, 1, sizeof(buf), fh);
174 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
178 if (eof_expected != 0) {
181 fprintf(stderr, "fread(): unexpected EOF\n");
186 js = realloc(js, jslen + r + 1);
188 fprintf(stderr, "realloc(): errno = %d\n", errno);
191 strncpy(js + jslen, buf, r);
195 r = jsmn_parse(&p, js, jslen, tok, tokcount);
197 if (r == JSMN_ERROR_NOMEM) {
198 tokcount = tokcount * 2;
199 tok = realloc(tok, sizeof(*tok) * tokcount);
201 fprintf(stderr, "realloc(): errno=%d\n", errno);
207 dump(js, tok, p.toknext, 0, map, 0/*, w*/);
208 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
209 //printf("[\n%d\n]", jslen);
224 loadmap("../../../../data/test.map", &map);