From 17c3b817a0c5b223e3ebc02f9e9206009c1a79ea Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Wed, 13 Dec 2017 14:26:30 +0100 Subject: [PATCH] Better hashtable destruction. Monsters prepared. --- .vimrc | 2 +- CMakeLists.txt | 1 + TODO.txt | 10 +++++++--- hashtable/hashtable.c | 9 ++++++++- hashtable/hashtable.h | 2 ++ src/map.c | 8 +++++++- src/map.h | 6 ++++++ src/monster.c | 17 +++++++++++++++++ src/monster.h | 4 ++++ 9 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/monster.c diff --git a/.vimrc b/.vimrc index d4273f4..b80e862 100644 --- a/.vimrc +++ b/.vimrc @@ -1,3 +1,3 @@ nnoremap :Make -C build --no-print-directory -l set makeprg=make\ -C\ build\ --no-print-directory\ -l -let g:syntastic_c_include_dirs = [ 'linkedlist' ] +let g:syntastic_c_include_dirs = [ 'linkedlist', 'hashtable' ] diff --git a/CMakeLists.txt b/CMakeLists.txt index 2fa8497..8929d39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,7 @@ add_executable(breakhack src/timer src/roommatrix src/position + src/monster ) target_link_libraries(breakhack diff --git a/TODO.txt b/TODO.txt index 3efac50..c8f3fff 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,10 +1,14 @@ x Add walls and stuff to maps -- Implement simple box collisions -- Add enemies (generated through lua) -- Htting enemies +x Implement simple box collisions +o Add enemies (generated through lua) + x Monster object created + - Lua bindings for creation +- Hitting enemies - Moving enemies - Lower levels - XP - gui - Items - More gui + +Legend: ( '-' = future) ( 'x' = completed ) ( 'o' = begun ) diff --git a/hashtable/hashtable.c b/hashtable/hashtable.c index f3d7370..5188ae3 100644 --- a/hashtable/hashtable.c +++ b/hashtable/hashtable.c @@ -127,6 +127,12 @@ ht_get(Hashtable *table, char *key) void ht_destroy(Hashtable *table) +{ + ht_destroy_custom(table, free); +} + +void +ht_destroy_custom(Hashtable *table, void (*destroy_value)(void *value)) { Entry *entry, *next; unsigned int i; @@ -141,11 +147,12 @@ ht_destroy(Hashtable *table) continue; while (entry) { next = entry->next; - free(entry->value); + destroy_value(entry->value); entry->value = NULL; free(entry); entry = next; } } + free(table->entries); free(table); } diff --git a/hashtable/hashtable.h b/hashtable/hashtable.h index 731244a..bc4090e 100644 --- a/hashtable/hashtable.h +++ b/hashtable/hashtable.h @@ -20,4 +20,6 @@ void* ht_get(Hashtable*, char *key); void ht_destroy(Hashtable*); +void ht_destroy_custom(Hashtable*, void (*destroy_value)(void*)); + #endif // HASHTABLE_H_ diff --git a/src/map.c b/src/map.c index 173e90b..3313b99 100644 --- a/src/map.c +++ b/src/map.c @@ -24,6 +24,8 @@ Map* map_create() Map *map = ec_malloc(sizeof(Map)); map->textures = linkedlist_create(); + map->monsterTextures = ht_create(100); + map->monsters = linkedlist_create(); map->currentRoom = (Position) { 0, 0 }; map->renderTimer = timer_create(); map->level = 1; @@ -191,8 +193,12 @@ void map_destroy(Map *map) } } while (map->textures != NULL) { - texture_destroy(linkedlist_poplast(&map->textures)); + texture_destroy(linkedlist_pop(&map->textures)); } + while (map->monsters != NULL) { + monster_destroy(linkedlist_pop(&map->textures)); + } + ht_destroy_custom(map->monsterTextures, (void (*)(void*)) texture_destroy); timer_destroy(map->renderTimer); free(map); } diff --git a/src/map.h b/src/map.h index 18b87bc..f6da8f9 100644 --- a/src/map.h +++ b/src/map.h @@ -4,12 +4,14 @@ #include #include #include +#include #include "sprite.h" #include "camera.h" #include "position.h" #include "timer.h" #include "defines.h" +#include "monster.h" typedef struct MapTile_t { int textureIndex0; @@ -28,6 +30,8 @@ typedef struct Room_t { typedef struct Map_t { Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT]; LinkedList *textures; + Hashtable *monsterTextures; + LinkedList *monsters; Position currentRoom; Timer *renderTimer; int level; @@ -35,6 +39,8 @@ typedef struct Map_t { Map* map_create(); +void map_add_monster(Map*, Monster*); + int map_add_texture(Map*, const char *path, SDL_Renderer*); void map_add_tile(Map *map, Position *tile_pos, MapTile*); diff --git a/src/monster.c b/src/monster.c new file mode 100644 index 0000000..2943e4d --- /dev/null +++ b/src/monster.c @@ -0,0 +1,17 @@ +#include "monster.h" +#include "util.h" + +Monster* +monster_create() +{ + Monster *m = ec_malloc(sizeof(Monster)); + m->sprite = sprite_create(); + return m; +} + +void +monster_destroy(Monster *m) +{ + sprite_destroy(m->sprite); + free(m); +} diff --git a/src/monster.h b/src/monster.h index 5e8cebd..4beec76 100644 --- a/src/monster.h +++ b/src/monster.h @@ -10,4 +10,8 @@ typedef struct { Stats stats; } Monster; +Monster* monster_create(); + +void monster_destroy(Monster*); + #endif // MONSTER_H_