foo
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 21 Sep 2021 22:10:05 +0000 (01:10 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 21 Sep 2021 22:10:05 +0000 (01:10 +0300)
src/level.c
src/level.h
src/mesh.c
src/mesh.h
src/scenefile.c
src/scenefile.h
src/tileset.c

index e741667..fcf2556 100644 (file)
@@ -4,10 +4,9 @@
 #include <errno.h>
 #include <treestore.h>
 #include "level.h"
+#include "tileset.h"
 #include "fs.h"
 
-static int load_tileset(struct level *lvl, struct ts_node *tsn);
-
 int init_level(struct level *lvl, int xsz, int ysz)
 {
        memset(lvl, 0, sizeof *lvl);
@@ -78,7 +77,7 @@ int load_level(struct level *lvl, const char *fname)
                iter = iter->next;
 
                if(strcmp(node->name, "tileset") == 0) {
-                       load_tileset(lvl, node);
+                       /* TODO */
 
                } else if(strcmp(node->name, "cell") == 0) {
                        cx = ts_get_attr_int(node, "x", -1);
@@ -109,18 +108,20 @@ int load_level(struct level *lvl, const char *fname)
                        node = (struct ts_node*)cell->next;
                        cell->next = 0;
 
+                       /*
                        if(i >= lvl->height - 1 || cell[lvl->width].type == CELL_SOLID) {
-                               cell->wall[0] = TILE_STRAIGHT;
+                               cell->wall[0] = TILE_STR;
                        }
                        if(j >= lvl->width - 1 || cell[1].type == CELL_SOLID) {
-                               cell->wall[1] = TILE_STRAIGHT;
+                               cell->wall[1] = TILE_STR;
                        }
                        if(i <= 0 || cell[-lvl->width].type == CELL_SOLID) {
-                               cell->wall[2] = TILE_STRAIGHT;
+                               cell->wall[2] = TILE_STR;
                        }
                        if(j <= 0 || cell[-1].type == CELL_SOLID) {
-                               cell->wall[3] = TILE_STRAIGHT;
+                               cell->wall[3] = TILE_STR;
                        }
+                       */
 
                        cell++;
                }
@@ -214,82 +215,16 @@ err:
 
 #ifndef LEVEL_EDITOR
 
-static int load_tileset(struct level *lvl, struct ts_node *tsn)
-{
-       static const char *tile_types[] = {
-               "open", "straight", "corner", "tee", "cross", "str2open", 0
-       };
-
-       int i;
-       char *path;
-       const char *str;
-       struct ts_node *node;
-       struct tile *tile;
-
-       node = tsn->child_list;
-       while(node) {
-               if(strcmp(node->name, "tile") == 0) {
-                       if(!(tile = calloc(1, sizeof *tile))) {
-                               fprintf(stderr, "failed to allocate tile\n");
-                               return -1;
-                       }
-                       if((str = ts_get_attr_str(node, "name", 0))) {
-                               tile->name = strdup(str);
-                       }
-                       if((str = ts_get_attr_str(node, "type", 0))) {
-                               for(i=0; tile_types[i]; i++) {
-                                       if(strcmp(str, tile_types[i]) == 0) {
-                                               tile->type = i;
-                                               break;
-                                       }
-                               }
-                       }
-                       if((str = ts_get_attr_str(node, "scene", 0))) {
-                               if(lvl->dirname) {
-                                       path = alloca(strlen(lvl->dirname) + strlen(str) + 2);
-                                       combine_path(lvl->dirname, str, path);
-                               } else {
-                                       path = (char*)str;
-                               }
-                               load_scenefile(&tile->scn, path);
-                       }
-
-                       if(tile->name && tile->scn.meshlist) {  /* valid tile */
-                               tile->next = lvl->tiles;
-                               lvl->tiles = tile;
-                       } else {
-                               fprintf(stderr, "load_tileset: skipping invalid tile: %s\n",
-                                               tile->name ? tile->name : "missing tile name");
-                               free(tile);
-                       }
-               }
-               node = node->next;
-       }
-
-       return 0;
-}
-
-struct tile *find_level_tile(struct level *lvl, int type)
-{
-       struct tile *tile = lvl->tiles;
-       while(tile) {
-               if(tile->type == type) {
-                       return tile;
-               }
-               tile = tile->next;
-       }
-       return 0;
-}
-
 int gen_cell_geom(struct level *lvl, struct cell *cell)
 {
+#if 0
        int i;
        struct meshgroup *wallgeom;
        struct tile *tstr;
        struct mesh *mesh, *tmesh;
        float xform[16];
 
-       if(!(tstr = find_level_tile(lvl, TILE_STRAIGHT))) {
+       if(!(tstr = find_level_tile(lvl, TILE_STR))) {
                return -1;
        }
 
@@ -299,7 +234,7 @@ int gen_cell_geom(struct level *lvl, struct cell *cell)
        init_meshgroup(wallgeom);
 
        for(i=0; i<4; i++) {
-               if(cell->wall[i] == TILE_STRAIGHT) {    /* TODO: support other wall types */
+               if(cell->wall[i] == TILE_STR) { /* TODO: support other wall types */
                        cgm_mrotation_y(xform, i * M_PI / 2.0f);
 
                        tmesh = tstr->scn.meshlist;
@@ -334,7 +269,7 @@ int gen_cell_geom(struct level *lvl, struct cell *cell)
        /* TODO: append to other existing meshgroups for detail objects */
        cell->mgrp = wallgeom;
        cell->num_mgrp = 1;
-
+#endif
        return 0;
 }
 
@@ -357,11 +292,4 @@ int gen_level_geom(struct level *lvl)
        return 0;
 }
 
-#else
-
-static int load_tileset(struct level *lvl, struct ts_node *tsn)
-{
-       return 0;               /* in the level editor we don't need tileset loading */
-}
-
 #endif /* !LEVEL_EDITOR */
index 3a31af6..6e6eed7 100644 (file)
@@ -2,6 +2,7 @@
 #define LEVEL_H_
 
 #include "scenefile.h"
+#include "tileset.h"
 
 #define DEF_CELL_SIZE  3.0f
 
index 9f208a4..91f8fcf 100644 (file)
@@ -16,6 +16,7 @@ void init_mesh(struct mesh *m)
 
 void destroy_mesh(struct mesh *m)
 {
+       free(m->name);
        free(m->varr);
        free(m->iarr);
 
@@ -29,9 +30,11 @@ void destroy_mesh(struct mesh *m)
 
 void clear_mesh(struct mesh *m)
 {
+       free(m->name);
        free(m->varr);
        free(m->iarr);
 
+       m->name = 0;
        m->varr = 0;
        m->iarr = 0;
        m->num_verts = m->max_verts = m->num_idx = m->max_idx = 0;
@@ -43,6 +46,10 @@ int copy_mesh(struct mesh *dest, struct mesh *src)
 {
        init_mesh(dest);
 
+       if(src->name) {
+               dest->name = strdup(src->name);
+       }
+
        if(src->max_verts && !(dest->varr = malloc(src->max_verts * sizeof *dest->varr))) {
                return -1;
        }
index 1f94a8e..f8a7585 100644 (file)
@@ -39,6 +39,8 @@ struct material {
 };
 
 struct mesh {
+       char *name;
+
        struct vertex *varr;
        unsigned int *iarr;
        int num_verts, num_idx;
index 8baf91a..25cdf28 100644 (file)
@@ -170,6 +170,7 @@ int load_scenefile(struct scenefile *scn, const char *fname)
                                        goto fail;
                                }
                                init_mesh(mesh);
+                               mesh->name = strdup(cleanline(line + 2));
                        }
                        break;
 
@@ -272,6 +273,19 @@ void destroy_scenefile(struct scenefile *scn)
        }
 }
 
+struct mesh *find_mesh_prefix(struct scenefile *scn, const char *prefix)
+{
+       int len = strlen(prefix);
+       struct mesh *m = scn->meshlist;
+       while(m) {
+               if(m->name && memcmp(m->name, prefix, len) == 0) {
+                       return m;
+               }
+               m = m->next;
+       }
+       return 0;
+}
+
 static char *cleanline(char *s)
 {
        char *ptr;
index 745b020..ab59b6a 100644 (file)
@@ -16,4 +16,6 @@ struct scenefile {
 int load_scenefile(struct scenefile *scn, const char *fname);
 void destroy_scenefile(struct scenefile *scn);
 
+struct mesh *find_mesh_prefix(struct scenefile *scn, const char *prefix);
+
 #endif /* SCENEFILE_H_ */
index 53a3d38..cde3021 100644 (file)
@@ -5,12 +5,14 @@
 #include "treestore.h"
 #include "tileset.h"
 #include "level.h"
+#include "fs.h"
 
 int load_tileset(struct tileset *tset, const char *fname)
 {
-       struct ts_node *ts, *node;
+       struct ts_node *ts, *node, *iter;
        const char *str;
        char *path;
+       struct mesh *mesh;
 
        if(!(ts = ts_load(fname))) {
                fprintf(stderr, "failed to load tileset: %s\n", fname);
@@ -38,4 +40,22 @@ int load_tileset(struct tileset *tset, const char *fname)
        }
 
        tset->name = strdup(ts_get_attr_str(ts, "name", fname));
+
+       iter = ts->child_list;
+       while(node) {
+               node = iter;
+               iter = iter->next;
+               if(strcmp(node->name, "tile") == 0) {
+                       if(!(str = ts_get_attr_str(node, "prefix", 0))) {
+                               continue;
+                       }
+                       if(!(mesh = find_mesh_prefix(&tset->scn, str))) {
+                               fprintf(stderr, "load_tileset: failed to find mesh with prefix: %s\n", str);
+                               continue;
+                       }
+                       /* TOOD cont */
+               }
+       }
+
+       return 0;
 }