Load object bounding box from objectsprites XML
This commit is contained in:
parent
3f8ac4f890
commit
48ae6b7873
61
game/main.c
61
game/main.c
@ -32,6 +32,8 @@
|
|||||||
#define MAPMINY (TILESIZE * (-32))
|
#define MAPMINY (TILESIZE * (-32))
|
||||||
#define MAPMAXY (TILESIZE * 32)
|
#define MAPMAXY (TILESIZE * 32)
|
||||||
|
|
||||||
|
#define OBJSPRITES "/objectsprites.tsx"
|
||||||
|
|
||||||
#define TSASSET "/overworld.png"
|
#define TSASSET "/overworld.png"
|
||||||
#define TSCOLS 40
|
#define TSCOLS 40
|
||||||
|
|
||||||
@ -118,12 +120,14 @@ typedef struct {
|
|||||||
unsigned animframes;
|
unsigned animframes;
|
||||||
SDL_Rect src;
|
SDL_Rect src;
|
||||||
SDL_Texture *tex;
|
SDL_Texture *tex;
|
||||||
|
dbox_t bbox;
|
||||||
} objtype_t;
|
} objtype_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
entity_t e;
|
entity_t e;
|
||||||
unsigned type;
|
unsigned type;
|
||||||
ivec_t pos;
|
ivec_t pos;
|
||||||
|
dbox_t bbox;
|
||||||
} objentity_t;
|
} objentity_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -240,8 +244,9 @@ static void load_layer(map_t map, xmlNodePtr layernode, int layeridx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned
|
static unsigned load_objtype(
|
||||||
load_objtype(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
gamestate_t *state, const char *templ, SDL_Renderer *renderer,
|
||||||
|
xmlNodePtr sprites)
|
||||||
{
|
{
|
||||||
static char buf[MAX_PATH_LEN];
|
static char buf[MAX_PATH_LEN];
|
||||||
assert(strlen(state->assetdir) + strlen(templ) + 1 < MAX_PATH_LEN);
|
assert(strlen(state->assetdir) + strlen(templ) + 1 < MAX_PATH_LEN);
|
||||||
@ -249,18 +254,23 @@ load_objtype(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
|||||||
strcat(buf, "/");
|
strcat(buf, "/");
|
||||||
strcat(buf, templ);
|
strcat(buf, templ);
|
||||||
|
|
||||||
// Identify object type ID
|
// Identify object type ID and sprite ID
|
||||||
xmlDocPtr doc = xmlParseFile(buf);
|
xmlDocPtr doc = xmlParseFile(buf);
|
||||||
assert(NULL != doc);
|
assert(NULL != doc);
|
||||||
xmlNodePtr node = xmlDocGetRootElement(doc);
|
xmlNodePtr node = xmlDocGetRootElement(doc);
|
||||||
assert(NULL != node);
|
assert(NULL != node);
|
||||||
assert(xmlStrcmp(node->name, (const xmlChar *)"template") == 0);
|
assert(xmlStrcmp(node->name, (const xmlChar *)"template") == 0);
|
||||||
node = node->xmlChildrenNode;
|
node = node->xmlChildrenNode;
|
||||||
|
while (node != NULL
|
||||||
|
&& xmlStrcmp(node->name, (const xmlChar *)"tileset") != 0)
|
||||||
|
node = node->next;
|
||||||
|
assert(NULL != node);
|
||||||
|
const int sidoff = prop_int(node, "firstgid");
|
||||||
while (node != NULL
|
while (node != NULL
|
||||||
&& xmlStrcmp(node->name, (const xmlChar *)"object") != 0)
|
&& xmlStrcmp(node->name, (const xmlChar *)"object") != 0)
|
||||||
node = node->next;
|
node = node->next;
|
||||||
assert(NULL != node);
|
assert(NULL != node);
|
||||||
unsigned id = prop_int(node, "gid");
|
const unsigned id = prop_int(node, "type");
|
||||||
assert(id < MAXOBJTYPES);
|
assert(id < MAXOBJTYPES);
|
||||||
|
|
||||||
// Populate objtype struct if not already loaded
|
// Populate objtype struct if not already loaded
|
||||||
@ -270,6 +280,31 @@ load_objtype(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
|||||||
state->objtypes[id].src.w = prop_int(node, "width");
|
state->objtypes[id].src.w = prop_int(node, "width");
|
||||||
state->objtypes[id].src.h = prop_int(node, "height");
|
state->objtypes[id].src.h = prop_int(node, "height");
|
||||||
|
|
||||||
|
// Load bounding box from sprites document
|
||||||
|
const int sid = prop_int(node, "gid") - sidoff;
|
||||||
|
xmlNodePtr snode = sprites->xmlChildrenNode;
|
||||||
|
while (snode
|
||||||
|
&& (xmlStrcmp(snode->name, (const xmlChar *)"tile") != 0
|
||||||
|
|| prop_int(snode, "id") != sid))
|
||||||
|
snode = snode->next;
|
||||||
|
assert(snode);
|
||||||
|
snode = snode->xmlChildrenNode;
|
||||||
|
while (snode
|
||||||
|
&& xmlStrcmp(snode->name, (const xmlChar *)"objectgroup")
|
||||||
|
!= 0)
|
||||||
|
snode = snode->next;
|
||||||
|
assert(snode);
|
||||||
|
snode = snode->xmlChildrenNode;
|
||||||
|
while (snode
|
||||||
|
&& xmlStrcmp(snode->name, (const xmlChar *)"object") != 0)
|
||||||
|
snode = snode->next;
|
||||||
|
assert(snode);
|
||||||
|
state->objtypes[id].bbox.off.x = (double)prop_int(snode, "x");
|
||||||
|
state->objtypes[id].bbox.off.y = (double)prop_int(snode, "y");
|
||||||
|
state->objtypes[id].bbox.ext.x = (double)prop_int(snode, "width");
|
||||||
|
state->objtypes[id].bbox.ext.y = (double)prop_int(snode, "height");
|
||||||
|
|
||||||
|
// Load custom properties
|
||||||
node = node->xmlChildrenNode;
|
node = node->xmlChildrenNode;
|
||||||
while (node != NULL
|
while (node != NULL
|
||||||
&& xmlStrcmp(node->name, (const xmlChar *)"properties") != 0)
|
&& xmlStrcmp(node->name, (const xmlChar *)"properties") != 0)
|
||||||
@ -307,6 +342,13 @@ load_objtype(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
|||||||
static void
|
static void
|
||||||
load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
|
load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
|
char path[MAX_PATH_LEN];
|
||||||
|
assert(strlen(state->assetdir) + strlen(OBJSPRITES) < MAX_PATH_LEN);
|
||||||
|
strcpy(path, state->assetdir);
|
||||||
|
strcat(path, OBJSPRITES);
|
||||||
|
xmlDocPtr spritesdoc = xmlParseFile(path);
|
||||||
|
xmlNodePtr sprites = xmlDocGetRootElement(spritesdoc);
|
||||||
|
|
||||||
state->objcol.n = 0;
|
state->objcol.n = 0;
|
||||||
state->objcol.cap = INITOBJCOLCAP;
|
state->objcol.cap = INITOBJCOLCAP;
|
||||||
state->objcol.buf = malloc(sizeof(objentity_t) * state->objcol.cap);
|
state->objcol.buf = malloc(sizeof(objentity_t) * state->objcol.cap);
|
||||||
@ -331,13 +373,20 @@ load_objects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
|
|||||||
// Load object type
|
// Load object type
|
||||||
xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template");
|
xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template");
|
||||||
assert(templ);
|
assert(templ);
|
||||||
o->type = load_objtype(state, (const char *)templ, renderer);
|
o->type
|
||||||
|
= load_objtype(state, (const char *)templ, renderer, sprites);
|
||||||
xmlFree(templ);
|
xmlFree(templ);
|
||||||
|
const objtype_t *type = state->objtypes + o->type;
|
||||||
|
|
||||||
// Load object location
|
// Load object location
|
||||||
o->pos.x = prop_int(node, "x");
|
o->pos.x = prop_int(node, "x");
|
||||||
o->pos.y = prop_int(node, "y");
|
o->pos.y = prop_int(node, "y") - type->src.h;
|
||||||
|
|
||||||
|
// Load bounding box from object type
|
||||||
|
o->bbox = type->bbox;
|
||||||
} while ((node = node->next) != NULL);
|
} while ((node = node->next) != NULL);
|
||||||
|
|
||||||
|
xmlFreeDoc(spritesdoc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user