Fix some memory leaks
This commit is contained in:
parent
2c8d01df49
commit
8dbdea6ac4
@ -93,6 +93,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
quit:
|
quit:
|
||||||
game_teardown(gamemem);
|
game_teardown(gamemem);
|
||||||
|
free(gamemem);
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(window);
|
SDL_DestroyWindow(window);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
69
game/main.c
69
game/main.c
@ -164,6 +164,15 @@ static inline bool tilepassable(const map_t map, int x, int y)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int propint(xmlNodePtr node, const char *prop)
|
||||||
|
{
|
||||||
|
xmlChar *str = xmlGetProp(node, (const xmlChar *)prop);
|
||||||
|
assert(str);
|
||||||
|
int val = atoi((const char *)str);
|
||||||
|
xmlFree(str);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
static void maploadlayer(map_t map, xmlNodePtr layernode, int layeridx)
|
static void maploadlayer(map_t map, xmlNodePtr layernode, int layeridx)
|
||||||
{
|
{
|
||||||
xmlNodePtr node = layernode->xmlChildrenNode;
|
xmlNodePtr node = layernode->xmlChildrenNode;
|
||||||
@ -178,11 +187,8 @@ static void maploadlayer(map_t map, xmlNodePtr layernode, int layeridx)
|
|||||||
if (0 != xmlStrcmp(node->name, (const xmlChar *)"chunk"))
|
if (0 != xmlStrcmp(node->name, (const xmlChar *)"chunk"))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
xmlChar *x_attr, *y_attr;
|
const int chunk_x = propint(node, "x");
|
||||||
x_attr = xmlGetProp(node, (const xmlChar *)"x");
|
const int chunk_y = propint(node, "y");
|
||||||
y_attr = xmlGetProp(node, (const xmlChar *)"y");
|
|
||||||
const int chunk_x = atoi((const char *)x_attr);
|
|
||||||
const int chunk_y = atoi((const char *)y_attr);
|
|
||||||
|
|
||||||
chunk_contents = node->xmlChildrenNode;
|
chunk_contents = node->xmlChildrenNode;
|
||||||
assert(
|
assert(
|
||||||
@ -243,18 +249,15 @@ objtypeload(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
|||||||
&& 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
|
unsigned id = propint(node, "gid");
|
||||||
= atoi((const char *)xmlGetProp(node, (const xmlChar *)"gid"));
|
|
||||||
assert(id < MAXOBJTYPES);
|
assert(id < MAXOBJTYPES);
|
||||||
|
|
||||||
// Populate objtype struct if not already loaded
|
// Populate objtype struct if not already loaded
|
||||||
if (NULL == state->objtypes[id].tex) {
|
if (NULL == state->objtypes[id].tex) {
|
||||||
state->objtypes[id].src.x = 0;
|
state->objtypes[id].src.x = 0;
|
||||||
state->objtypes[id].src.y = 0;
|
state->objtypes[id].src.y = 0;
|
||||||
state->objtypes[id].src.w
|
state->objtypes[id].src.w = propint(node, "width");
|
||||||
= atoi((const char *)xmlGetProp(node, (const xmlChar *)"width"));
|
state->objtypes[id].src.h = propint(node, "height");
|
||||||
state->objtypes[id].src.h = atoi(
|
|
||||||
(const char *)xmlGetProp(node, (const xmlChar *)"height"));
|
|
||||||
|
|
||||||
node = node->xmlChildrenNode;
|
node = node->xmlChildrenNode;
|
||||||
while (node != NULL
|
while (node != NULL
|
||||||
@ -264,24 +267,29 @@ objtypeload(gamestate_t *state, const char *templ, SDL_Renderer *renderer)
|
|||||||
for (node = node->xmlChildrenNode; NULL != node; node = node->next) {
|
for (node = node->xmlChildrenNode; NULL != node; node = node->next) {
|
||||||
if (xmlStrcmp(node->name, (const xmlChar *)"property") != 0)
|
if (xmlStrcmp(node->name, (const xmlChar *)"property") != 0)
|
||||||
continue;
|
continue;
|
||||||
const char *key
|
xmlChar *key = xmlGetProp(node, (const xmlChar *)"name");
|
||||||
= (const char *)xmlGetProp(node, (const xmlChar *)"name");
|
assert(key);
|
||||||
const char *val
|
xmlChar *val = xmlGetProp(node, (const xmlChar *)"value");
|
||||||
= (const char *)xmlGetProp(node, (const xmlChar *)"value");
|
assert(val);
|
||||||
if (strcmp(key, "animframes") == 0) {
|
if (xmlStrcmp(key, (const xmlChar *)"animframes") == 0) {
|
||||||
state->objtypes[id].animframes = atoi(val);
|
state->objtypes[id].animframes = atoi((const char *)val);
|
||||||
} else if (strcmp(key, "assetpath") == 0) {
|
} else if (xmlStrcmp(key, (const xmlChar *)"assetpath") == 0) {
|
||||||
assert(strlen(state->assetdir) + strlen(val) < MAX_PATH_LEN);
|
assert(
|
||||||
|
strlen(state->assetdir) + strlen((const char *)val)
|
||||||
|
< MAX_PATH_LEN);
|
||||||
strcpy(buf, state->assetdir);
|
strcpy(buf, state->assetdir);
|
||||||
strcat(buf, val);
|
strcat(buf, (const char *)val);
|
||||||
state->objtypes[id].tex = IMG_LoadTexture(renderer, buf);
|
state->objtypes[id].tex = IMG_LoadTexture(renderer, buf);
|
||||||
assert(NULL != state->objtypes[id].tex);
|
assert(NULL != state->objtypes[id].tex);
|
||||||
} else {
|
} else {
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
xmlFree(key);
|
||||||
|
xmlFree(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
xmlFreeDoc(doc);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -309,26 +317,23 @@ maploadobjects(gamestate_t *state, xmlNodePtr node, SDL_Renderer *renderer)
|
|||||||
obj_t *o = &state->objcol.buf[state->objcol.n++];
|
obj_t *o = &state->objcol.buf[state->objcol.n++];
|
||||||
|
|
||||||
// Load object type
|
// Load object type
|
||||||
const xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template");
|
xmlChar *templ = xmlGetProp(node, (const xmlChar *)"template");
|
||||||
assert(NULL != templ);
|
assert(templ);
|
||||||
o->type = objtypeload(state, (const char *)templ, renderer);
|
o->type = objtypeload(state, (const char *)templ, renderer);
|
||||||
|
xmlFree(templ);
|
||||||
|
|
||||||
// Load object location and set size from objtype
|
// Load object location
|
||||||
o->pos.x
|
o->pos.x = propint(node, "x");
|
||||||
= atoi((const char *)xmlGetProp(node, (const xmlChar *)"x"));
|
o->pos.y = propint(node, "y");
|
||||||
o->pos.y
|
|
||||||
= atoi((const char *)xmlGetProp(node, (const xmlChar *)"y"));
|
|
||||||
} while ((node = node->next) != NULL);
|
} while ((node = node->next) != NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
mapload(gamestate_t *state, const char *path, SDL_Renderer *renderer)
|
mapload(gamestate_t *state, const char *path, SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
xmlDocPtr doc;
|
xmlDocPtr doc = xmlParseFile(path);
|
||||||
xmlNodePtr node;
|
|
||||||
doc = xmlParseFile(path);
|
|
||||||
assert(NULL != doc);
|
assert(NULL != doc);
|
||||||
node = xmlDocGetRootElement(doc);
|
xmlNodePtr node = xmlDocGetRootElement(doc);
|
||||||
assert(0 == xmlStrcmp(node->name, (const xmlChar *)"map"));
|
assert(0 == xmlStrcmp(node->name, (const xmlChar *)"map"));
|
||||||
node = node->xmlChildrenNode;
|
node = node->xmlChildrenNode;
|
||||||
int layer = 0;
|
int layer = 0;
|
||||||
@ -339,6 +344,7 @@ mapload(gamestate_t *state, const char *path, SDL_Renderer *renderer)
|
|||||||
else if (xmlStrcmp(node->name, (const xmlChar *)"objectgroup") == 0)
|
else if (xmlStrcmp(node->name, (const xmlChar *)"objectgroup") == 0)
|
||||||
maploadobjects(state, node, renderer);
|
maploadobjects(state, node, renderer);
|
||||||
} while ((node = node->next) != NULL);
|
} while ((node = node->next) != NULL);
|
||||||
|
xmlFreeDoc(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mapdraw(const gamestate_t *state, SDL_Renderer *renderer)
|
static void mapdraw(const gamestate_t *state, SDL_Renderer *renderer)
|
||||||
@ -521,6 +527,7 @@ void game_teardown(void *mem)
|
|||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
free(state->objcol.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
gamestatus_t game_evthandle(void *mem, const SDL_Event *evt)
|
gamestatus_t game_evthandle(void *mem, const SDL_Event *evt)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user