]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_map.c
Merge branch 'master' of github.com:sparky4/16
[16.git] / src / lib / 16_map.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\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
10  *\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
15  *\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
20  *\r
21  */\r
22 \r
23 #include "src/lib/16_map.h"\r
24 \r
25 // Ideally, preprocess json during compilation and read serialized data\r
26 \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
30                 return 0;\r
31         }\r
32         return -1;\r
33 }\r
34 \r
35 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {\r
36         int i, j, k, indent=0, inner_end;\r
37         bitmap_t bp;\r
38         //char *s;\r
39         i = 0;\r
40         while(i<count) {\r
41                 if(jsoneq(js, &(t[i]), "layers") == 0) {\r
42                         i++;\r
43                         map->data = malloc(sizeof(byte*) * t[i].size);\r
44                         inner_end = t[i].end;\r
45                         k = 0;\r
46                         while(t[i].start < inner_end) {\r
47                                 printf("%d, %d\n", t[i].start, inner_end);\r
48                                 if(jsoneq(js, &(t[i]), "data") == 0) {\r
49                                         #ifdef DEBUG_MAPVAR\r
50                                         printf("Layer %d data: [", k);\r
51                                         #endif\r
52                                         map->data[k] = malloc(sizeof(byte) * t[i+1].size);\r
53                                         for(j = 0; j < t[i+1].size; j++) {\r
54                                                 map->data[k][j] = (byte)atoi(js + t[i+2+j].start);\r
55                                                 #ifdef DEBUG_MAPVAR\r
56                                                 printf("%d, ", map->data[k][j]);\r
57                                                 #endif\r
58                                         }\r
59                                         i += j + 2;\r
60                                         k++;\r
61                                         #ifdef DEBUG_MAPVAR\r
62                                         puts("]");\r
63                                         #endif\r
64                                 }\r
65                                 else{\r
66                                         i++;\r
67                                 }\r
68                         }\r
69                 }\r
70                 if(jsoneq(js, &(t[i]), "tilesets") == 0) {\r
71                         i++;\r
72                         map->tiles = malloc(sizeof(tiles_t*) * t[i].size);\r
73                         inner_end = t[i].end;\r
74                         k = 0;\r
75                         while(t[i].start < inner_end) {\r
76                                 if(jsoneq(js, &(t[i]), "image") == 0) {\r
77                                         //fix this to be far~\r
78                                         map->tiles[k] = malloc(sizeof(tiles_t));\r
79                                         map->tiles[k]->btdata = malloc(sizeof(bitmap_t));\r
80                                         map->tiles[k]->tileHeight = 16;\r
81                                         map->tiles[k]->tileWidth = 16;\r
82                                         map->tiles[k]->rows = 1;\r
83                                         map->tiles[k]->cols = 1;\r
84                                         map->tiles[k]->debug_text=false;\r
85                                         //Fix to load tileset specified.\r
86                                         //And move to vrs, probably\r
87                                         //bp = bitmapLoadPcx("data/ed.pcx");\r
88                                         map->tiles[k]->btdata = &bp;\r
89                                         k++;\r
90                                 }\r
91                                 i++;\r
92                         }\r
93                 }\r
94 \r
95                 if (jsoneq(js, &(t[i]), "height") == 0 && indent<=1) {\r
96                         map->height = atoi(js + t[i+1].start);\r
97                         #ifdef DEBUG_MAPVAR\r
98                         printf("Height: %d\n", map->height);\r
99                         #endif\r
100                         i++;\r
101                 }\r
102                 else if(jsoneq(js, &(t[i]), "width") == 0 && indent<=1) {\r
103                         map->width = atoi(js + t[i+1].start);\r
104                         #ifdef DEBUG_MAPVAR\r
105                         printf("Width: %d\n", map->width);\r
106                         #endif\r
107                         i++;\r
108                 }\r
109                 i++;\r
110         }\r
111 }\r
112 \r
113 int loadmap(char *mn, map_t *map) {\r
114         char *js;\r
115 \r
116         jsmn_parser p;\r
117         jsmntok_t *tok = NULL;\r
118         size_t tokcount, file_s;\r
119 \r
120         FILE *fh = fopen(mn, "r");\r
121         int status;\r
122 \r
123         /* Prepare parser */\r
124         jsmn_init(&p);\r
125 \r
126         file_s = filesize(fh);\r
127         js = malloc(file_s);\r
128         if(js == NULL) {\r
129                 fprintf(stderr, "malloc(): errno = %d", 2);\r
130                 fclose(fh);\r
131                 return 3;\r
132         }\r
133         if(fread(js, 1, file_s, fh) != file_s) {\r
134                 fprintf(stderr, "Map read error");\r
135                 free(js);\r
136                 fclose(fh);\r
137                 return 1;\r
138         }\r
139         tokcount = jsmn_parse(&p, js, file_s, NULL, 0);\r
140         tok = malloc(tokcount*sizeof(jsmntok_t));\r
141         printf("Allocated %d tokens", tokcount);\r
142         jsmn_init(&p);\r
143         if((status = jsmn_parse(&p, js, file_s, tok, tokcount)) < 0)\r
144         {\r
145                 printf("Error: %d\n", status);\r
146                 return status;\r
147         }\r
148         else if(status != tokcount) { printf("Warning: used %d tok\n", status);}\r
149         extract_map(js, tok, tokcount, map);\r
150 \r
151         free(js);\r
152         free(tok);\r
153         fclose(fh);\r
154 \r
155         return 0;\r
156 }\r