Better hashtable destruction. Monsters prepared.

This commit is contained in:
Linus Probert 2017-12-13 14:26:30 +01:00
parent f90a7c735e
commit 17c3b817a0
9 changed files with 53 additions and 6 deletions

2
.vimrc
View File

@ -1,3 +1,3 @@
nnoremap <F1> :Make -C build --no-print-directory -l<cr> nnoremap <F1> :Make -C build --no-print-directory -l<cr>
set makeprg=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' ]

View File

@ -43,6 +43,7 @@ add_executable(breakhack
src/timer src/timer
src/roommatrix src/roommatrix
src/position src/position
src/monster
) )
target_link_libraries(breakhack target_link_libraries(breakhack

View File

@ -1,10 +1,14 @@
x Add walls and stuff to maps x Add walls and stuff to maps
- Implement simple box collisions x Implement simple box collisions
- Add enemies (generated through lua) o Add enemies (generated through lua)
- Htting enemies x Monster object created
- Lua bindings for creation
- Hitting enemies
- Moving enemies - Moving enemies
- Lower levels - Lower levels
- XP - XP
- gui - gui
- Items - Items
- More gui - More gui
Legend: ( '-' = future) ( 'x' = completed ) ( 'o' = begun )

View File

@ -127,6 +127,12 @@ ht_get(Hashtable *table, char *key)
void void
ht_destroy(Hashtable *table) ht_destroy(Hashtable *table)
{
ht_destroy_custom(table, free);
}
void
ht_destroy_custom(Hashtable *table, void (*destroy_value)(void *value))
{ {
Entry *entry, *next; Entry *entry, *next;
unsigned int i; unsigned int i;
@ -141,11 +147,12 @@ ht_destroy(Hashtable *table)
continue; continue;
while (entry) { while (entry) {
next = entry->next; next = entry->next;
free(entry->value); destroy_value(entry->value);
entry->value = NULL; entry->value = NULL;
free(entry); free(entry);
entry = next; entry = next;
} }
} }
free(table->entries);
free(table); free(table);
} }

View File

@ -20,4 +20,6 @@ void* ht_get(Hashtable*, char *key);
void ht_destroy(Hashtable*); void ht_destroy(Hashtable*);
void ht_destroy_custom(Hashtable*, void (*destroy_value)(void*));
#endif // HASHTABLE_H_ #endif // HASHTABLE_H_

View File

@ -24,6 +24,8 @@ Map* map_create()
Map *map = ec_malloc(sizeof(Map)); Map *map = ec_malloc(sizeof(Map));
map->textures = linkedlist_create(); map->textures = linkedlist_create();
map->monsterTextures = ht_create(100);
map->monsters = linkedlist_create();
map->currentRoom = (Position) { 0, 0 }; map->currentRoom = (Position) { 0, 0 };
map->renderTimer = timer_create(); map->renderTimer = timer_create();
map->level = 1; map->level = 1;
@ -191,8 +193,12 @@ void map_destroy(Map *map)
} }
} }
while (map->textures != NULL) { 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); timer_destroy(map->renderTimer);
free(map); free(map);
} }

View File

@ -4,12 +4,14 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdbool.h> #include <stdbool.h>
#include <linkedlist.h> #include <linkedlist.h>
#include <hashtable.h>
#include "sprite.h" #include "sprite.h"
#include "camera.h" #include "camera.h"
#include "position.h" #include "position.h"
#include "timer.h" #include "timer.h"
#include "defines.h" #include "defines.h"
#include "monster.h"
typedef struct MapTile_t { typedef struct MapTile_t {
int textureIndex0; int textureIndex0;
@ -28,6 +30,8 @@ typedef struct Room_t {
typedef struct Map_t { typedef struct Map_t {
Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT]; Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT];
LinkedList *textures; LinkedList *textures;
Hashtable *monsterTextures;
LinkedList *monsters;
Position currentRoom; Position currentRoom;
Timer *renderTimer; Timer *renderTimer;
int level; int level;
@ -35,6 +39,8 @@ typedef struct Map_t {
Map* map_create(); Map* map_create();
void map_add_monster(Map*, Monster*);
int map_add_texture(Map*, const char *path, SDL_Renderer*); int map_add_texture(Map*, const char *path, SDL_Renderer*);
void map_add_tile(Map *map, Position *tile_pos, MapTile*); void map_add_tile(Map *map, Position *tile_pos, MapTile*);

17
src/monster.c Normal file
View File

@ -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);
}

View File

@ -10,4 +10,8 @@ typedef struct {
Stats stats; Stats stats;
} Monster; } Monster;
Monster* monster_create();
void monster_destroy(Monster*);
#endif // MONSTER_H_ #endif // MONSTER_H_