From: sparky4 Date: Mon, 23 Mar 2015 17:21:36 +0000 (-0500) Subject: FINALLY I CAN READ MAP FILES AND LOAD VALUES OFF OF THEM AND SUCH! code is a bit... X-Git-Url: http://4ch.mooo.com/gitweb/?a=commitdiff_plain;h=9da109784b07c519b919cb0def22009ba796e727;p=16.git FINALLY I CAN READ MAP FILES AND LOAD VALUES OFF OF THEM AND SUCH! code is a bit messy but it works~ ^^ modified: README modified: scroll.exe modified: src/lib/jsmn/example/MAPTEST2.EXE modified: src/lib/jsmn/example/maptest2 modified: src/lib/jsmn/example/maptest2.c modified: src/lib/lib_head.h new file: src/lib/mapread.c new file: src/lib/mapread.h modified: src/scroll.c --- diff --git a/README b/README index 975e8913..e7b058c5 100644 --- a/README +++ b/README @@ -2,6 +2,7 @@ Open watcom to compile the code of project 16 / = copy of pngwen's code 16/ = the current code sparky4 is working with 16/modex16/ = pngwen's code archived +src/ = source of project~ TODO not in particular order~: DONE 1. SCROLLING!!!!!!! @@ -11,7 +12,7 @@ DONE 4. movement and player movement when reaching edge of map DONE 5. sprite rendering 6. text box rendering 7. text rendering -8. map loading +DONE! YAY! 8. map loading 9. map rendering~ 10. map and item interation 11. item inventory~ @@ -21,7 +22,7 @@ DONE 5. sprite rendering DONE 15. 8088 detection to turn off vsync! 16. (very important!) optoimize draw row and draw col -17. merge map system and interperator from verge 2 +not needed ... really... 17. merge map system and interperator from verge 2 18. save feature! 19. (also important) interperator! and possibly custom map system 20. (also important) lua scripting system! diff --git a/scroll.exe b/scroll.exe index fb9baa11..bd790fef 100644 Binary files a/scroll.exe and b/scroll.exe differ diff --git a/src/lib/jsmn/example/MAPTEST2.EXE b/src/lib/jsmn/example/MAPTEST2.EXE index af8cb79d..df045463 100644 Binary files a/src/lib/jsmn/example/MAPTEST2.EXE and b/src/lib/jsmn/example/MAPTEST2.EXE differ diff --git a/src/lib/jsmn/example/maptest2 b/src/lib/jsmn/example/maptest2 index b35d6cda..abc5640f 100644 Binary files a/src/lib/jsmn/example/maptest2 and b/src/lib/jsmn/example/maptest2 differ diff --git a/src/lib/jsmn/example/maptest2.c b/src/lib/jsmn/example/maptest2.c index 8d4fec24..017fbf85 100644 --- a/src/lib/jsmn/example/maptest2.c +++ b/src/lib/jsmn/example/maptest2.c @@ -3,11 +3,30 @@ #include #include #include "../jsmn.c" +#include "../../../lib/types.h" /* * An example of reading JSON from stdin and printing its content to stdout. * The output looks like YAML, but I'm not sure if it's really compatible. - */ + */ + +char *js_sv; + +typedef struct { + //bitmap_t *data; + byte *data; + word tileHeight; + word tileWidth; + unsigned int rows; + unsigned int cols; +} tiles_t; + +typedef struct { + byte *data; + tiles_t *tiles; + int width; + int height; +} map_t; static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && @@ -17,47 +36,63 @@ static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { return -1; } -static int dump(const char *js, jsmntok_t *t, size_t count, int indent) { +static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) { int i, j, k; if (count == 0) { return 0; } + /* We may want to do strtol() here to get numeric value */ if (t->type == JSMN_PRIMITIVE) { - printf("%.*s", t->end - t->start, js+t->start); + if(js_sv == "height") + { + map->height = (int)strtol(js+t->start, (char **)js+t->end, 10); + printf("h:[%d]\n", map->height); + }else if(js_sv == "width") + { + map->width = (int)strtol(js+t->start, (char **)js+t->end, 10); + printf("w:[%d]\n", map->width); + } return 1; + /* We may use strndup() to fetch string value */ } else if (t->type == JSMN_STRING) { - printf("'%.*s'", t->end - t->start, js+t->start); - /*if (jsoneq(js, t, "image") == 0) { - printf("- image: %.*s\n", t->end-t->start, - js + t->start); - }*/ + //printf("'%.*s'", t->end - t->start, js+t->start); + if (jsoneq(js, t, "height") == 0 && indent==1) + { + js_sv="height";//strdup(js+t->start);//, t->end - t->start); + printf("%s\n", js_sv); + }else if (jsoneq(js, t, "width") == 0 && indent==1) + { + js_sv="width";//strdup(js+t->start);//, t->end - t->start); + printf("%s\n", js_sv); + }else js_sv=NULL; return 1; } else if (t->type == JSMN_OBJECT) { - printf("\n"); + //printf("\n"); j = 0; for (i = 0; i < t->size; i++) { //for (k = 0; k < indent; k++) printf("\t"); - j += dump(js, t+1+j, count-j, indent+1); - printf(": "); - j += dump(js, t+1+j, count-j, indent+1); - printf("\n"); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf(": "); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf("\n"); } return j+1; } else if (t->type == JSMN_ARRAY) { j = 0; - printf("\n"); + //printf("==\n"); for (i = 0; i < t->size; i++) { //for (k = 0; k < indent-1; k++) printf("\t"); - printf("\t-"); - j += dump(js, t+1+j, count-j, indent+1); - printf("\n"); + //printf("\t-"); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf("==\n"); } return j+1; } return 0; } -int main() { +int loadmap(char *mn, map_t *map) +{ int r; int eof_expected = 0; char *js = NULL; @@ -68,7 +103,7 @@ int main() { jsmntok_t *tok; size_t tokcount = 2; - FILE *fh = fopen("../../../../data/test.map", "r"); + FILE *fh = fopen(mn, "r"); /* Prepare parser */ jsmn_init(&p); @@ -117,12 +152,19 @@ again: goto again; } } else { - dump(js, tok, p.toknext, 0); + dump(js, tok, p.toknext, 0, map); //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok)); - printf("[\n%d\n]", jslen); + //printf("[\n%d\n]", jslen); eof_expected = 1; } } return 0; } + +int main() +{ + map_t map; + loadmap("../../../../data/test.map", &map); + return 0; +} diff --git a/src/lib/lib_head.h b/src/lib/lib_head.h index 560ea8ad..dcd85470 100644 --- a/src/lib/lib_head.h +++ b/src/lib/lib_head.h @@ -4,7 +4,7 @@ #include #include // just for wait #include // just for wait -#include "src\lib\types.h" +#include "src/lib/types.h" /* Control codes for all keys on the keyboard */ //here temperarly diff --git a/src/lib/mapread.c b/src/lib/mapread.c new file mode 100644 index 00000000..3e2f96e9 --- /dev/null +++ b/src/lib/mapread.c @@ -0,0 +1,142 @@ +#include "src/lib/mapread.h" + +static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { + if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && + strncmp(json + tok->start, s, tok->end - tok->start) == 0) { + return 0; + } + return -1; +} + +static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map) { + int i, j, k; + if (count == 0) { + return 0; + } + /* We may want to do strtol() here to get numeric value */ + if (t->type == JSMN_PRIMITIVE) { + if(js_sv == "height") + { + map->height = (int)strtol(js+t->start, (char **)js+t->end, 10); + printf("h:[%d]\n", map->height); + }else if(js_sv == "width") + { + map->width = (int)strtol(js+t->start, (char **)js+t->end, 10); + printf("w:[%d]\n", map->width); + } + return 1; + /* We may use strndup() to fetch string value */ + } else if (t->type == JSMN_STRING) { + //printf("'%.*s'", t->end - t->start, js+t->start); + if (jsoneq(js, t, "height") == 0 && indent==1) + { + js_sv="height";//strdup(js+t->start);//, t->end - t->start); + printf("%s\n", js_sv); + }else if (jsoneq(js, t, "width") == 0 && indent==1) + { + js_sv="width";//strdup(js+t->start);//, t->end - t->start); + printf("%s\n", js_sv); + }else js_sv=NULL; + return 1; + } else if (t->type == JSMN_OBJECT) { + //printf("\n"); + j = 0; + for (i = 0; i < t->size; i++) { + //for (k = 0; k < indent; k++) printf("\t"); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf(": "); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf("\n"); + } + return j+1; + } else if (t->type == JSMN_ARRAY) { + j = 0; + //printf("==\n"); + for (i = 0; i < t->size; i++) { + //for (k = 0; k < indent-1; k++) printf("\t"); + //printf("\t-"); + j += dump(js, t+1+j, count-j, indent+1, map); + //printf("==\n"); + } + return j+1; + } + return 0; +} + +int loadmap(char *mn, map_t *map) +{ + int r; + int eof_expected = 0; + char *js = NULL; + size_t jslen = 0; + char buf[BUFSIZ]; + + jsmn_parser p; + jsmntok_t *tok; + size_t tokcount = 2; + + FILE *fh = fopen(mn, "r"); + + /* Prepare parser */ + jsmn_init(&p); + + /* Allocate some tokens as a start */ + tok = malloc(sizeof(*tok) * tokcount); + if (tok == NULL) { + fprintf(stderr, "malloc(): errno=%d\n", errno); + return 3; + } + + for (;;) { + /* Read another chunk */ + r = fread(buf, 1, sizeof(buf), fh); + if (r < 0) { + fprintf(stderr, "fread(): %d, errno=%d\n", r, errno); + return 1; + } + if (r == 0) { + if (eof_expected != 0) { + return 0; + } else { + fprintf(stderr, "fread(): unexpected EOF\n"); + return 2; + } + } + + js = realloc(js, jslen + r + 1); + if (js == NULL) { + fprintf(stderr, "realloc(): errno=%d\n", errno); + return 3; + } + strncpy(js + jslen, buf, r); + jslen = jslen + r; + +again: + r = jsmn_parse(&p, js, jslen, tok, tokcount); + if (r < 0) { + if (r == JSMN_ERROR_NOMEM) { + tokcount = tokcount * 2; + tok = realloc(tok, sizeof(*tok) * tokcount); + if (tok == NULL) { + fprintf(stderr, "realloc(): errno=%d\n", errno); + return 3; + } + goto again; + } + } else { + dump(js, tok, p.toknext, 0, map); + //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok)); + //printf("[\n%d\n]", jslen); + eof_expected = 1; + } + } + + return 0; +} + +/*int main() +{ + map_t map; + loadmap("../../../../data/test.map", &map); + return 0; +}*/ diff --git a/src/lib/mapread.h b/src/lib/mapread.h new file mode 100644 index 00000000..ac210743 --- /dev/null +++ b/src/lib/mapread.h @@ -0,0 +1,30 @@ +#ifndef _LIBMAPREAD_H_ +#define _LIBMAPREAD_H_ +#include +#include +#include +#include "src/lib/jsmn/jsmn.c" +#include "src/lib/modex16.h" + +char *js_sv; + +typedef struct { + bitmap_t *data; + word tileHeight; + word tileWidth; + unsigned int rows; + unsigned int cols; +} tiles_t; + +typedef struct { + byte *data; + tiles_t *tiles; + int width; + int height; +} map_t; + +static int jsoneq(const char *json, jsmntok_t *tok, const char *s); +static int dump(const char *js, jsmntok_t *t, size_t count, int indent, /*char *js_sv,*/ map_t *map); +int loadmap(char *mn, map_t *map); + +#endif/*_LIBMAPREAD_H_*/ diff --git a/src/scroll.c b/src/scroll.c index 8f485f90..0d7d021d 100644 --- a/src/scroll.c +++ b/src/scroll.c @@ -1,16 +1,13 @@ -#include "src\lib\modex16.h" -#include -#include -#include -#include "src\lib\dos_kb.h" -#include "16\lib\x\modex.h" +#include "src\lib\dos_kb.h" +#include "src\lib\mapread.c" +//#include "16\lib\x\modex.h" #include "src\lib\wtest\wtest.c" -#include "src\lib\planar.c" +#include "src\lib\planar.c" //====#include "src\lib\ems.c" //word far *clock= (word far*) 0x046C; /* 18.2hz clock */ -typedef struct { +/*typedef struct { bitmap_t *data; word tileHeight; word tileWidth; @@ -24,7 +21,7 @@ typedef struct { tiles_t *tiles; int width; int height; -} map_t; +} map_t;*/ typedef struct { @@ -70,8 +67,8 @@ void animatePlayer(map_view_t *src, map_view_t *dest, /*map_view_t *top, */sword //#define LOOPMAX (TILEWH/SPEED) //place holder definitions -#define MAPX 200 -#define MAPY 150 +//#define MAPX 200 +//#define MAPY 150 #define TRIGGX 10 #define TRIGGY 9 @@ -120,8 +117,9 @@ void main() { exit(0); }*/ - /* create the map */ - map = allocMap(MAPX,MAPY); //20x15 is the resolution of the screen you can make maps smaller than 20x15 but the null space needs to be drawn properly + /* create the map */ + loadmap("data/test.map", &map); + map = allocMap(map.width,map.height); //20x15 is the resolution of the screen you can make maps smaller than 20x15 but the null space needs to be drawn properly //if(isEMS()) printf("%d tesuto\n", coretotalEMS()); initMap(&map); mv.map = ↦ @@ -242,7 +240,7 @@ void main() { //right movement if(npc0.d == 2) { - if(npc0.tx < MAPX && !(npc0.tx+1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty)) + if(npc0.tx < map.width && !(npc0.tx+1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty)) { if(npc0.q<=(TILEWH/SPEED)) { @@ -294,7 +292,7 @@ void main() { //down movement if(npc0.d == 3) { - if(npc0.ty < MAPY && !(npc0.tx == TRIGGX && npc0.ty+1 == TRIGGY) && !(npc0.tx == player.tx && npc0.ty == player.ty+1)) + if(npc0.ty < map.height && !(npc0.tx == TRIGGX && npc0.ty+1 == TRIGGY) && !(npc0.tx == player.tx && npc0.ty == player.ty+1)) { if(npc0.q<=(TILEWH/SPEED)) { @@ -352,7 +350,7 @@ void main() { if((keyp(77) && !keyp(75) && player.d == 0) || player.d == 2) { if(player.d == 0){ player.d = 2; } - if(bg->tx >= 0 && bg->tx+20 < MAPX && player.tx == bg->tx + 10 && !(player.tx+1 == TRIGGX && player.ty == TRIGGY)) + if(bg->tx >= 0 && bg->tx+20 < map.width && player.tx == bg->tx + 10 && !(player.tx+1 == TRIGGX && player.ty == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) { @@ -366,7 +364,7 @@ void main() { player.q++; } else { player.q = 1; player.d = 0; player.tx++; } } - else if(player.tx < MAPX && !(player.tx+1 == TRIGGX && player.ty == TRIGGY)) + else if(player.tx < map.width && !(player.tx+1 == TRIGGX && player.ty == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) { @@ -393,7 +391,7 @@ void main() { if((keyp(75) && !keyp(77) && player.d == 0) || player.d == 4) { if(player.d == 0){ player.d = 4; } - if(bg->tx > 0 && bg->tx+20 <= MAPX && player.tx == bg->tx + 10 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY)) + if(bg->tx > 0 && bg->tx+20 <= map.width && player.tx == bg->tx + 10 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) { @@ -434,7 +432,7 @@ void main() { if((keyp(80) && !keyp(72) && player.d == 0) || player.d == 3) { if(player.d == 0){ player.d = 3; } - if(bg->ty >= 0 && bg->ty+15 < MAPY && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty+1 == TRIGGY)) + if(bg->ty >= 0 && bg->ty+15 < map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty+1 == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) { @@ -448,7 +446,7 @@ void main() { player.q++; } else { player.q = 1; player.d = 0; player.ty++; } } - else if(player.ty < MAPY && !(player.tx == TRIGGX && player.ty+1 == TRIGGY)) + else if(player.ty < map.height && !(player.tx == TRIGGX && player.ty+1 == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) { @@ -475,7 +473,7 @@ void main() { if((keyp(72) && !keyp(80) && player.d == 0) || player.d == 1) { if(player.d == 0){ player.d = 1; } - if(bg->ty > 0 && bg->ty+15 <= MAPY && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty-1 == TRIGGY)) + if(bg->ty > 0 && bg->ty+15 <= map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty-1 == TRIGGY)) { if(player.q<=(TILEWH/SPEED)) {