FINALLY I CAN READ MAP FILES AND LOAD VALUES OFF OF THEM AND SUCH! code is a bit...
authorsparky4 <sparky4@cock.li>
Mon, 23 Mar 2015 17:21:36 +0000 (12:21 -0500)
committersparky4 <sparky4@cock.li>
Mon, 23 Mar 2015 17:21:36 +0000 (12:21 -0500)
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

README
scroll.exe
src/lib/jsmn/example/MAPTEST2.EXE
src/lib/jsmn/example/maptest2
src/lib/jsmn/example/maptest2.c
src/lib/lib_head.h
src/lib/mapread.c [new file with mode: 0644]
src/lib/mapread.h [new file with mode: 0644]
src/scroll.c

diff --git a/README b/README
index 975e8913fbc4105405fd84aefae0da199c82ba12..e7b058c5679bd119367a0eec758ee3ab374ed060 100644 (file)
--- a/README
+++ b/README
@@ -2,6 +2,7 @@ Open watcom to compile the code of project 16
 / = copy of pngwen's code\r
 16/ = the current code sparky4 is working with\r
 16/modex16/ = pngwen's code archived\r
+src/ = source of project~\r
 \r
 TODO not in particular order~:\r
 DONE 1. SCROLLING!!!!!!!\r
@@ -11,7 +12,7 @@ DONE 4. movement and player movement when reaching edge of map
 DONE 5. sprite rendering\r
 6. text box rendering\r
 7. text rendering\r
-8. map loading\r
+DONE! YAY! 8. map loading\r
 9. map rendering~\r
 10. map and item interation\r
 11. item inventory~\r
@@ -21,7 +22,7 @@ DONE 5. sprite rendering
 DONE 15. 8088 detection to turn off vsync!\r
 16. (very important!) optoimize draw row and draw col\r
 \r
-17. merge map system and interperator from verge 2\r
+not needed ... really... 17. merge map system and interperator from verge 2\r
 18. save feature!\r
 19. (also important) interperator! and possibly custom map system\r
 20. (also important) lua scripting system!\r
index fb9baa11cd93e38102e8be5321ba2e8beaf6020d..bd790fef504bb6c2264cea72ba06782fcbccdcf8 100644 (file)
Binary files a/scroll.exe and b/scroll.exe differ
index af8cb79d15f86e3094b4cd0047a84c6da54fcec5..df045463757b24b4e9c512b18318774448d3465b 100644 (file)
Binary files a/src/lib/jsmn/example/MAPTEST2.EXE and b/src/lib/jsmn/example/MAPTEST2.EXE differ
index b35d6cda5896f7c70dbe3f8daebafd7dba399b23..abc5640f405dfdfdec94ce713a12ff4a0dc28cbb 100644 (file)
Binary files a/src/lib/jsmn/example/maptest2 and b/src/lib/jsmn/example/maptest2 differ
index 8d4fec246beb2b2ceb55550452abd56266a2f9e5..017fbf8520111b28161a7dbdfb252496ae9beed1 100644 (file)
@@ -3,11 +3,30 @@
 #include <string.h>
 #include <errno.h>
 #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.
- */
+ */\r
+
+char *js_sv;
+
+typedef struct {\r
+       //bitmap_t *data;
+       byte *data;\r
+       word tileHeight;\r
+       word tileWidth;\r
+       unsigned int rows;\r
+       unsigned int cols;\r
+} tiles_t;\r
+\r
+typedef struct {\r
+       byte    *data;\r
+       tiles_t *tiles;\r
+       int width;\r
+       int height;\r
+} 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;
+}
index 560ea8adf4e4c8d9b4da77c152c50428b772387b..dcd854704dfa9fa488758b8331bac59cf14bca77 100644 (file)
@@ -4,7 +4,7 @@
 #include <stdio.h>
 #include <conio.h> // just for wait
 #include <time.h> // 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 (file)
index 0000000..3e2f96e
--- /dev/null
@@ -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 (file)
index 0000000..ac21074
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef _LIBMAPREAD_H_
+#define _LIBMAPREAD_H_
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include "src/lib/jsmn/jsmn.c"
+#include "src/lib/modex16.h"
+
+char *js_sv;
+
+typedef struct {\r
+       bitmap_t *data;
+       word tileHeight;\r
+       word tileWidth;\r
+       unsigned int rows;\r
+       unsigned int cols;\r
+} tiles_t;\r
+\r
+typedef struct {\r
+       byte    *data;\r
+       tiles_t *tiles;\r
+       int width;\r
+       int height;\r
+} 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_*/
index 8f485f90a8e538d91c570aeb502fb15e52d83678..0d7d021db3e6131d1a5163fccb40c565cbb9ea2b 100644 (file)
@@ -1,16 +1,13 @@
-#include "src\lib\modex16.h"\r
-#include <stdio.h>\r
-#include <stdlib.h>
-#include <string.h>\r
-#include "src\lib\dos_kb.h"\r
-#include "16\lib\x\modex.h"\r
+#include "src\lib\dos_kb.h"
+#include "src\lib\mapread.c"\r
+//#include "16\lib\x\modex.h"\r
 #include "src\lib\wtest\wtest.c"\r
-#include "src\lib\planar.c"\r
+#include "src\lib\planar.c"
 //====#include "src\lib\ems.c"\r
 \r
 //word far *clock= (word far*) 0x046C; /* 18.2hz clock */\r
 \r
-typedef struct {\r
+/*typedef struct {\r
        bitmap_t *data;\r
        word tileHeight;\r
        word tileWidth;\r
@@ -24,7 +21,7 @@ typedef struct {
        tiles_t *tiles;\r
        int width;\r
        int height;\r
-} map_t;\r
+} map_t;*/\r
 \r
 \r
 typedef struct {\r
@@ -70,8 +67,8 @@ void animatePlayer(map_view_t *src, map_view_t *dest, /*map_view_t *top, */sword
 //#define LOOPMAX (TILEWH/SPEED)\r
 \r
 //place holder definitions\r
-#define MAPX 200\r
-#define MAPY 150\r
+//#define MAPX 200\r
+//#define MAPY 150\r
 #define TRIGGX 10\r
 #define TRIGGY 9\r
 \r
@@ -120,8 +117,9 @@ void main() {
                exit(0);\r
        }*/\r
 \r
-       /* create the map */\r
-       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\r
+       /* create the map */
+       loadmap("data/test.map", &map);\r
+       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\r
        //if(isEMS()) printf("%d tesuto\n", coretotalEMS());\r
        initMap(&map);\r
        mv.map = &map;\r
@@ -242,7 +240,7 @@ void main() {
        //right movement\r
        if(npc0.d == 2)\r
        {\r
-               if(npc0.tx < MAPX && !(npc0.tx+1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty))\r
+               if(npc0.tx < map.width && !(npc0.tx+1 == TRIGGX && npc0.ty == TRIGGY) && !(npc0.tx+1 == player.tx && npc0.ty == player.ty))\r
                {\r
                        if(npc0.q<=(TILEWH/SPEED))\r
                        {\r
@@ -294,7 +292,7 @@ void main() {
        //down movement\r
        if(npc0.d == 3)\r
        {\r
-               if(npc0.ty < MAPY && !(npc0.tx == TRIGGX && npc0.ty+1 == TRIGGY) && !(npc0.tx == player.tx && npc0.ty == player.ty+1))\r
+               if(npc0.ty < map.height && !(npc0.tx == TRIGGX && npc0.ty+1 == TRIGGY) && !(npc0.tx == player.tx && npc0.ty == player.ty+1))\r
                {\r
                        if(npc0.q<=(TILEWH/SPEED))\r
                        {\r
@@ -352,7 +350,7 @@ void main() {
        if((keyp(77) && !keyp(75) && player.d == 0) || player.d == 2)\r
        {\r
                if(player.d == 0){ player.d = 2; }\r
-               if(bg->tx >= 0 && bg->tx+20 < MAPX && player.tx == bg->tx + 10 && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
+               if(bg->tx >= 0 && bg->tx+20 < map.width && player.tx == bg->tx + 10 && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r
@@ -366,7 +364,7 @@ void main() {
                                player.q++;\r
                        } else { player.q = 1; player.d = 0; player.tx++; }\r
                }\r
-               else if(player.tx < MAPX && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
+               else if(player.tx < map.width && !(player.tx+1 == TRIGGX && player.ty == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r
@@ -393,7 +391,7 @@ void main() {
        if((keyp(75) && !keyp(77) && player.d == 0) || player.d == 4)\r
        {\r
                if(player.d == 0){ player.d = 4; }\r
-               if(bg->tx > 0 && bg->tx+20 <= MAPX && player.tx == bg->tx + 10 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY))\r
+               if(bg->tx > 0 && bg->tx+20 <= map.width && player.tx == bg->tx + 10 && !(player.tx-1 == TRIGGX && player.ty == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r
@@ -434,7 +432,7 @@ void main() {
        if((keyp(80) && !keyp(72) && player.d == 0) || player.d == 3)\r
        {\r
                if(player.d == 0){ player.d = 3; }\r
-               if(bg->ty >= 0 && bg->ty+15 < MAPY && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
+               if(bg->ty >= 0 && bg->ty+15 < map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r
@@ -448,7 +446,7 @@ void main() {
                                player.q++;\r
                        } else { player.q = 1; player.d = 0; player.ty++; }\r
                }\r
-               else if(player.ty < MAPY && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
+               else if(player.ty < map.height && !(player.tx == TRIGGX && player.ty+1 == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r
@@ -475,7 +473,7 @@ void main() {
        if((keyp(72) && !keyp(80) && player.d == 0) || player.d == 1)\r
        {\r
                if(player.d == 0){ player.d = 1; }\r
-               if(bg->ty > 0 && bg->ty+15 <= MAPY && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty-1 == TRIGGY))\r
+               if(bg->ty > 0 && bg->ty+15 <= map.height && player.ty == bg->ty + 8 && !(player.tx == TRIGGX && player.ty-1 == TRIGGY))\r
                {\r
                        if(player.q<=(TILEWH/SPEED))\r
                        {\r