]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_map.c
Merge remote-tracking branch 'upstream/master'
[16.git] / src / lib / 16_map.c
1 /* Project 16 Source Code~
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
3  *
4  * This file is part of Project 16.
5  *
6  * Project 16 is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Project 16 is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,
19  * Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include "16_map.h"
24
25 // Ideally, preprocess json during compilation and read serialized data
26
27 int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
28         if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
29                         strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
30                 return 0;
31         }
32         return -1;
33 }
34
35 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {
36         int i, j, k, indent, inner_end;
37         bitmap_t bp;
38         char *s;
39         i = 0;
40         while(i<count) {
41                 if(jsoneq(js, &(t[i]), "layers") == 0) {
42                         i++;
43                         map->data = malloc(sizeof(byte*) * t[i].size);
44                         inner_end = t[i].end;
45                         k = 0;
46                         while(t[i].start < inner_end) {
47                                 printf("%d, %d\n", t[i].start, inner_end);
48                                 if(jsoneq(js, &(t[i]), "data") == 0) {
49                                         #ifdef DEBUG_MAPVAR
50                                         printf("Layer %d data: [", k);
51                                         #endif
52                                         map->data[k] = malloc(sizeof(byte) * t[i+1].size);
53                                         for(j = 0; j < t[i+1].size; j++) {
54                                                 map->data[k][j] = (byte)atoi(js + t[i+2+j].start);
55                                                 #ifdef DEBUG_MAPVAR
56                                                 printf("%d, ", map->data[k][j]);
57                                                 #endif
58                                         }
59                                         i += j + 2;
60                                         k++;
61                                         #ifdef DEBUG_MAPVAR
62                                         puts("]");
63                                         #endif
64                                 }
65                                 else{
66                                         i++;
67                                 }
68                         }
69                 }
70                 if(jsoneq(js, &(t[i]), "tilesets") == 0) {
71                         i++;
72                         map->tiles = malloc(sizeof(tiles_t*) * t[i].size);
73                         inner_end = t[i].end;
74                         k = 0;
75                         while(t[i].start < inner_end) {
76                                 if(jsoneq(js, &(t[i]), "image") == 0) {
77                                         //fix this to be far~
78                                         map->tiles[k] = malloc(sizeof(tiles_t));
79                                         map->tiles[k]->btdata = malloc(sizeof(bitmap_t));
80                                         map->tiles[k]->tileHeight = 16;
81                                         map->tiles[k]->tileWidth = 16;
82                                         map->tiles[k]->rows = 1;
83                                         map->tiles[k]->cols = 1;
84                                         map->tiles[k]->debug_text=false;
85                                         //Fix to load tileset specified.
86                                         //And move to vrs, probably
87                                         bp = bitmapLoadPcx("data/ed.pcx");
88                                         map->tiles[k]->btdata = &bp;
89                                         k++;
90                                 }
91                                 i++;
92                         }
93                 }
94
95                 if (jsoneq(js, &(t[i]), "height") == 0 && indent<=1) {
96                         map->height = atoi(js + t[i+1].start);
97                         #ifdef DEBUG_MAPVAR
98                         printf("Height: %d\n", map->height);
99                         #endif
100                         i++;
101                 }
102                 else if(jsoneq(js, &(t[i]), "width") == 0 && indent<=1) {
103                         map->width = atoi(js + t[i+1].start);
104                         #ifdef DEBUG_MAPVAR
105                         printf("Width: %d\n", map->width);
106                         #endif
107                         i++;
108                 }
109                 i++;
110         }
111 }
112
113 int loadmap(char *mn, map_t *map) {
114         char *js;
115
116         jsmn_parser p;
117         jsmntok_t *tok = NULL;
118         size_t tokcount, file_s;
119
120         FILE *fh = fopen(mn, "r");
121         int status;
122
123         /* Prepare parser */
124         jsmn_init(&p);
125
126         file_s = filesize(fh);
127         js = malloc(file_s);
128         if(js == NULL) {
129                 fprintf(stderr, "malloc(): errno = %d", 2);
130                 fclose(fh);
131                 return 3;
132         }
133         if(fread(js, 1, file_s, fh) != file_s) {
134                 fprintf(stderr, "Map read error");
135                 free(js);
136                 fclose(fh);
137                 return 1;
138         }
139         tokcount = jsmn_parse(&p, js, file_s, NULL, 0);
140         tok = malloc(tokcount*sizeof(jsmntok_t));
141         printf("Allocated %d tokens", tokcount);
142         jsmn_init(&p);
143         if((status = jsmn_parse(&p, js, file_s, tok, tokcount)) < 0)
144         {
145                 printf("Error: %d\n", status);
146                 return status;
147         }
148         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}
149         extract_map(js, tok, tokcount, map);
150
151         free(js);
152         free(tok);
153         fclose(fh);
154         
155         return 0;
156 }