1 /* Project 16 Source Code~
2 * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover
4 * This file is part of Project 16.
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.
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.
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.
25 // Ideally, preprocess json during compilation and read serialized data
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) {
35 void extract_map(const char *js, jsmntok_t *t, size_t count, map_t *map) {
36 int i, j, k, indent, inner_end;
39 fprintf(stderr, "t->size=[%d] ", t->size);
40 fprintf(stderr, "q=[%d] ", q);
41 fprintf(stderr, "indent= [%d] ", indent);
42 fprintf(stderr, "js_sv= [%s]\n", js_sv);
44 map->tiles = malloc(sizeof(tiles_t));
45 map->tiles->btdata = malloc(sizeof(bitmap_t));
48 bp = bitmapLoadPcx("data/ed.pcx");
49 map->tiles->btdata = &bp;
50 map->tiles->tileHeight = 16;
51 map->tiles->tileWidth = 16;
54 map->tiles->debug_text=false;
57 if(jsoneq(js, t, "layers" == 0)) {
59 map->data = malloc(sizeof(*byte) * t[i].size);
62 while(t[i].start < inner_end) {
63 if(jsoneq(js, t, "data") == 0) {
64 map->data[k] = malloc(sizeof(byte) * t[i+1].size);
65 for(j = 0; j < t[i+1].size; j++) {
66 map->data[k][j] = (byte)atoi(js + t[i+1+j].start);
73 if(jsoneq(js, t, "tilesets" == 0)) {
75 map->tiles = malloc(sizeof(*tiles_t) * t[i].size);
78 while(t[i].start < inner_end) {
79 if(jsoneq(js, t, "data") == 0) {
80 map->tiles[k] = malloc(sizeof(tiles_t));
81 bp = bitmapLoadPcx("data/ed.pcx");
82 map->tiles[k].btdata = &bp;
88 if (jsoneq(js, t, "height") == 0 && indent<=1) {
89 map->height = atoi(js + t[i+1].start);
91 fprintf(stderr, "indent= [%d] ", indent);
92 fprintf(stderr, "h:[%d]\n", map->height);
96 else if(jsoneq(js, t, "width") == 0 && indent<=1) {
97 map->width = atoi(js+t[i+1]->start);
99 fprintf(stderr, "indent= [%d] ", indent);
100 fprintf(stderr, "w:[%d]\n", map->width);
108 int loadmap(char *mn, map_t *map) {
113 jsmntok_t *tok = NULL;
114 size_t tokcount, file_s;
116 FILE *fh = fopen(mn, "r");
121 file_s = filesize(fh);
124 fprintf(stderr, "malloc(): errno = %d", 2);
128 if(fread(js, 1, file_s, fh) != file_s) {
129 fprintf(stderr, "Map read error");
135 tokcount = jsmn_parse(&p, js, file_s, NULL, 0);
136 tok = malloc(tokcount*sizeof(jsmntok_t));
137 jsmn_parse(&p, js, file_s, tok, tokcount);
138 fprintf(stderr, "running dump~\n");
139 printf("%d\n", p.toknext);
140 extract_map(js, tok, tokcount, 0);