]> 4ch.mooo.com Git - 16.git/blob - src/lib/16_map.c
Rewriting map system bit by bit
[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         #ifdef DEBUG_DUMPVARS
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);
43         #endif
44         map->tiles = malloc(sizeof(tiles_t));
45         map->tiles->btdata = malloc(sizeof(bitmap_t));
46         //fix this to be far~
47         //And a pointer
48         bp = bitmapLoadPcx("data/ed.pcx");
49         map->tiles->btdata = &bp;
50         map->tiles->tileHeight = 16;
51         map->tiles->tileWidth = 16;
52         map->tiles->rows = 1;
53         map->tiles->cols = 1;
54         map->tiles->debug_text=false;
55         
56         while(i<count) {
57                 if(jsoneq(js, t, "layers" == 0)) {
58                         i++;
59                         map->data = malloc(sizeof(*byte) * t[i].size);
60                         inner_end = t[i].end;
61                         k = 0;
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);
67                                         }
68                                         i += j + 1;
69                                         k++;
70                                 }
71                         }
72                 }/*
73                 if(jsoneq(js, t, "tilesets" == 0)) {
74                         i++;
75                         map->tiles = malloc(sizeof(*tiles_t) * t[i].size);
76                         inner_end = t[i].end;
77                         k = 0;
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;
83                                         k++;
84                                 }
85                         }
86                 }*/
87
88                 if (jsoneq(js, t, "height") == 0 && indent<=1) {
89                         map->height = atoi(js + t[i+1].start);
90                         #ifdef DEBUG_MAPVAR
91                         fprintf(stderr, "indent= [%d]   ", indent);
92                         fprintf(stderr, "h:[%d]\n", map->height);
93                         #endif
94                         i++;
95                 }
96                 else if(jsoneq(js, t, "width") == 0 && indent<=1) {
97                         map->width = atoi(js+t[i+1]->start);
98                         #ifdef DEBUG_MAPVAR
99                         fprintf(stderr, "indent= [%d]   ", indent);
100                         fprintf(stderr, "w:[%d]\n", map->width);
101                         #endif
102                         i++;
103                 }
104                 i++;
105         }
106 }
107
108 int loadmap(char *mn, map_t *map) {
109         char *js;
110         char js_ss[16];
111
112         jsmn_parser p;
113         jsmntok_t *tok = NULL;
114         size_t tokcount, file_s;
115
116         FILE *fh = fopen(mn, "r");
117
118         /* Prepare parser */
119         jsmn_init(&p);
120
121         file_s = filesize(fh);
122         js = malloc(file_s);
123         if(js == NULL) {
124                 fprintf(stderr, "malloc(): errno = %d", 2);
125                 fclose(fh);
126                 return 3;
127         }
128         if(fread(js, 1, file_s, fh) != file_s) {
129                 fprintf(stderr, "Map read error");
130                 free(js);
131                 fclose(fh);
132                 return 1;
133         }
134
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);
141
142         free(js);
143         free(tok);
144         fclose(fh);
145         
146         return 0;
147 }