Made linked list stupid and naive. Prepared for monster lua
This commit is contained in:
parent
17c3b817a0
commit
95c9ed0a67
|
@ -36,4 +36,4 @@ script:
|
|||
- cd build
|
||||
- cmake ..
|
||||
- make
|
||||
#- make test
|
||||
- make test
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <check.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "linkedlist.h"
|
||||
|
||||
START_TEST(test_linkedlist_create)
|
||||
|
@ -12,12 +13,19 @@ END_TEST
|
|||
|
||||
START_TEST(test_linkedlist_append)
|
||||
{
|
||||
int value = 14;
|
||||
int *v1, *v2;
|
||||
|
||||
v1 = malloc(sizeof(int));
|
||||
v2 = malloc(sizeof(int));
|
||||
|
||||
*v1 = 34;
|
||||
*v2 = 44;
|
||||
|
||||
LinkedList *list = linkedlist_create();
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
linkedlist_append(&list, &value, sizeof(value));
|
||||
linkedlist_append(&list, v1);
|
||||
ck_assert(linkedlist_size(list) == 1);
|
||||
linkedlist_append(&list, &value, sizeof(value));
|
||||
linkedlist_append(&list, v2);
|
||||
ck_assert(linkedlist_size(list) == 2);
|
||||
linkedlist_destroy(&list);
|
||||
ck_assert( list == NULL );
|
||||
|
@ -26,9 +34,15 @@ END_TEST
|
|||
|
||||
START_TEST(test_linkedlist_poplast)
|
||||
{
|
||||
int value1 = 1;
|
||||
int value2 = 2;
|
||||
int value3 = 3;
|
||||
int *v1, *v2, *v3;
|
||||
|
||||
v1 = malloc(sizeof(int));
|
||||
v2 = malloc(sizeof(int));
|
||||
v3 = malloc(sizeof(int));
|
||||
|
||||
*v1 = 1;
|
||||
*v2 = 2;
|
||||
*v3 = 3;
|
||||
|
||||
int *pop1;
|
||||
int *pop2;
|
||||
|
@ -38,9 +52,9 @@ START_TEST(test_linkedlist_poplast)
|
|||
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
|
||||
linkedlist_append(&list, &value3, sizeof(int));
|
||||
linkedlist_append(&list, &value2, sizeof(int));
|
||||
linkedlist_append(&list, &value1, sizeof(int));
|
||||
linkedlist_append(&list, v3);
|
||||
linkedlist_append(&list, v2);
|
||||
linkedlist_append(&list, v1);
|
||||
|
||||
ck_assert(linkedlist_size(list) == 3);
|
||||
|
||||
|
@ -50,26 +64,35 @@ START_TEST(test_linkedlist_poplast)
|
|||
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
|
||||
ck_assert(*pop1 == value1);
|
||||
ck_assert(*pop2 == value2);
|
||||
ck_assert(*pop3 == value3);
|
||||
ck_assert(*pop1 == *v1);
|
||||
ck_assert(*pop2 == *v2);
|
||||
ck_assert(*pop3 == *v3);
|
||||
|
||||
free(pop1);
|
||||
free(pop2);
|
||||
free(pop3);
|
||||
|
||||
linkedlist_destroy(&list);
|
||||
|
||||
ck_assert(list == NULL);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_linkedlist_push)
|
||||
{
|
||||
int value = 14;
|
||||
int *v1, *v2;
|
||||
|
||||
v1 = malloc(sizeof(int));
|
||||
v2 = malloc(sizeof(int));
|
||||
|
||||
*v1 = 1;
|
||||
*v2 = 1;
|
||||
|
||||
LinkedList *list = linkedlist_create();
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
linkedlist_push(&list, &value, sizeof(value));
|
||||
linkedlist_push(&list, v1);
|
||||
ck_assert(linkedlist_size(list) == 1);
|
||||
linkedlist_push(&list, &value, sizeof(value));
|
||||
linkedlist_push(&list, v2);
|
||||
ck_assert(linkedlist_size(list) == 2);
|
||||
linkedlist_destroy(&list);
|
||||
ck_assert( list == NULL );
|
||||
|
@ -78,21 +101,24 @@ END_TEST
|
|||
|
||||
START_TEST(test_linkedlist_pop)
|
||||
{
|
||||
int value1 = 1;
|
||||
int value2 = 2;
|
||||
int value3 = 3;
|
||||
int *value1, *value2, *value3;
|
||||
int *pop1, *pop2, *pop3;
|
||||
|
||||
int *pop1;
|
||||
int *pop2;
|
||||
int *pop3;
|
||||
value1 = malloc(sizeof(int));
|
||||
value2 = malloc(sizeof(int));
|
||||
value3 = malloc(sizeof(int));
|
||||
|
||||
*value1 = 1;
|
||||
*value2 = 2;
|
||||
*value3 = 3;
|
||||
|
||||
LinkedList *list = linkedlist_create();
|
||||
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
|
||||
linkedlist_push(&list, &value3, sizeof(int));
|
||||
linkedlist_push(&list, &value2, sizeof(int));
|
||||
linkedlist_push(&list, &value1, sizeof(int));
|
||||
linkedlist_push(&list, value3);
|
||||
linkedlist_push(&list, value2);
|
||||
linkedlist_push(&list, value1);
|
||||
|
||||
ck_assert(linkedlist_size(list) == 3);
|
||||
|
||||
|
@ -102,9 +128,11 @@ START_TEST(test_linkedlist_pop)
|
|||
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
|
||||
ck_assert(*pop1 == value1);
|
||||
ck_assert(*pop2 == value2);
|
||||
ck_assert(*pop3 == value3);
|
||||
ck_assert(*pop1 == *value1);
|
||||
ck_assert(*pop2 == *value2);
|
||||
ck_assert(*pop3 == *value3);
|
||||
|
||||
linkedlist_destroy(&list);
|
||||
|
||||
free(pop1);
|
||||
free(pop2);
|
||||
|
@ -116,17 +144,21 @@ END_TEST
|
|||
|
||||
START_TEST(test_linkedlist_get_index)
|
||||
{
|
||||
int value1 = 1;
|
||||
int value2 = 2;
|
||||
int *value1, *value2, *get;
|
||||
|
||||
value1 = malloc(sizeof(int));
|
||||
value2 = malloc(sizeof(int));
|
||||
|
||||
*value1 = 1;
|
||||
*value2 = 2;
|
||||
|
||||
int *get;
|
||||
|
||||
LinkedList *list = linkedlist_create();
|
||||
|
||||
ck_assert(linkedlist_size(list) == 0);
|
||||
|
||||
linkedlist_push(&list, &value2, sizeof(int));
|
||||
linkedlist_push(&list, &value1, sizeof(int));
|
||||
linkedlist_push(&list, value2);
|
||||
linkedlist_push(&list, value1);
|
||||
|
||||
ck_assert(linkedlist_size(list) == 2);
|
||||
|
||||
|
@ -134,7 +166,8 @@ START_TEST(test_linkedlist_get_index)
|
|||
|
||||
ck_assert(linkedlist_size(list) == 2);
|
||||
|
||||
ck_assert(*get == value2);
|
||||
ck_assert(*get == *value2);
|
||||
ck_assert(get == value2);
|
||||
|
||||
linkedlist_destroy(&list);
|
||||
|
||||
|
@ -142,6 +175,39 @@ START_TEST(test_linkedlist_get_index)
|
|||
}
|
||||
END_TEST
|
||||
|
||||
static void
|
||||
increase(int *number)
|
||||
{
|
||||
(*number)++;
|
||||
}
|
||||
|
||||
START_TEST (test_linkedlist_each)
|
||||
{
|
||||
LinkedList *list;
|
||||
int *pop, *append;
|
||||
int i;
|
||||
|
||||
list = linkedlist_create();
|
||||
|
||||
for (i = 0; i < 10; ++i) {
|
||||
append = malloc(sizeof(int));
|
||||
*append = i;
|
||||
linkedlist_append(&list, append);
|
||||
}
|
||||
|
||||
linkedlist_each(&list, (void (*)(void*)) increase);
|
||||
|
||||
for (i = 0; i < 10; ++i) {
|
||||
pop = linkedlist_pop(&list);
|
||||
ck_assert( i+1 == *pop );
|
||||
free(pop);
|
||||
pop = NULL;
|
||||
}
|
||||
|
||||
linkedlist_destroy(&list);
|
||||
}
|
||||
END_TEST
|
||||
|
||||
Suite* t_suite_create()
|
||||
{
|
||||
Suite *s;
|
||||
|
@ -156,6 +222,7 @@ Suite* t_suite_create()
|
|||
tcase_add_test(tc_core, test_linkedlist_append);
|
||||
tcase_add_test(tc_core, test_linkedlist_poplast);
|
||||
tcase_add_test(tc_core, test_linkedlist_get_index);
|
||||
tcase_add_test(tc_core, test_linkedlist_each);
|
||||
suite_add_tcase(s, tc_core);
|
||||
|
||||
return s;
|
||||
|
|
|
@ -28,24 +28,11 @@ LinkedList* linkedlist_create()
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void copy_data(void *dest, void *src, unsigned int size)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < size; ++i)
|
||||
*(char*)(dest + i) = *(char*)(src + i);
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning! This can get a bit wonky if you append/push a complex struct that
|
||||
* contains pointers. The pointers will be copied and not duplicated in that
|
||||
* case. Be careful.
|
||||
*/
|
||||
void linkedlist_push(LinkedList **head, void *value, unsigned int size)
|
||||
void linkedlist_push(LinkedList **head, void *value)
|
||||
{
|
||||
|
||||
LinkedList *node = linkedlist_node_create();
|
||||
node->data = linkedlist_malloc(size);
|
||||
copy_data(node->data, value, size);
|
||||
node->data = value;
|
||||
node->next = *head;
|
||||
|
||||
*head = node;
|
||||
|
@ -64,20 +51,22 @@ void* linkedlist_pop(LinkedList **head)
|
|||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Warning! This can get a bit wonky if you append/push a complex struct that
|
||||
* contains pointers. The pointers will be copied and not duplicated in that
|
||||
* case. Be careful.
|
||||
*/
|
||||
void linkedlist_append(LinkedList **head, void *value, unsigned int size)
|
||||
void linkedlist_append(LinkedList **head, void *value)
|
||||
{
|
||||
LinkedList *node;
|
||||
|
||||
if (*head == NULL) {
|
||||
*head = linkedlist_node_create();
|
||||
(*head)->data = linkedlist_malloc(size);
|
||||
copy_data((*head)->data, value, size);
|
||||
} else {
|
||||
linkedlist_append(&(*head)->next, value, size);
|
||||
(*head)->data = value;
|
||||
return;
|
||||
}
|
||||
|
||||
node = *head;
|
||||
while (node->next != NULL)
|
||||
node = node->next;
|
||||
|
||||
node->next = linkedlist_node_create();
|
||||
node->next->data = value;
|
||||
}
|
||||
|
||||
void* linkedlist_poplast(LinkedList **head)
|
||||
|
@ -113,16 +102,23 @@ void* linkedlist_get(LinkedList **head, unsigned int index)
|
|||
return linkedlist_get(&(*head)->next, --index);
|
||||
}
|
||||
|
||||
void linkedlist_each(LinkedList **head, void (*fun)(void*))
|
||||
{
|
||||
LinkedList *next = *head;
|
||||
|
||||
while (next != NULL) {
|
||||
fun(next->data);
|
||||
next = next->next;
|
||||
}
|
||||
}
|
||||
|
||||
void linkedlist_destroy(LinkedList **head)
|
||||
{
|
||||
if (*head == NULL) {
|
||||
return;
|
||||
}
|
||||
if ((*head)->next != NULL) {
|
||||
linkedlist_destroy(&(*head)->next);
|
||||
free((*head)->next);
|
||||
(*head)->next = NULL;
|
||||
}
|
||||
|
||||
linkedlist_destroy(&(*head)->next);
|
||||
|
||||
free((*head)->data);
|
||||
(*head)->data = NULL;
|
||||
|
|
|
@ -9,16 +9,18 @@ typedef struct Node LinkedList;
|
|||
|
||||
LinkedList* linkedlist_create();
|
||||
|
||||
void linkedlist_push(LinkedList **head, void *value, unsigned int size);
|
||||
void linkedlist_push(LinkedList **head, void *value);
|
||||
|
||||
void* linkedlist_pop(LinkedList **head);
|
||||
|
||||
void linkedlist_append(LinkedList **head, void *value, unsigned int size);
|
||||
void linkedlist_append(LinkedList **head, void *value);
|
||||
|
||||
void* linkedlist_poplast(LinkedList **head);
|
||||
|
||||
void* linkedlist_get(LinkedList **head, unsigned int index);
|
||||
|
||||
void linkedlist_each(LinkedList **head, void (*fun)(void*));
|
||||
|
||||
void linkedlist_destroy(LinkedList **head);
|
||||
|
||||
unsigned int linkedlist_size(LinkedList *head);
|
||||
|
|
44
src/map.c
44
src/map.c
|
@ -61,18 +61,30 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
|
|||
*oldTile = tile;
|
||||
}
|
||||
|
||||
Texture*
|
||||
map_add_monster_texture(Map *map, char *path, SDL_Renderer *renderer)
|
||||
{
|
||||
Texture *t;
|
||||
|
||||
t = ht_get(map->monsterTextures, path);
|
||||
if (!t) {
|
||||
t = texture_create(path, renderer);
|
||||
ht_set(map->monsterTextures, path, t);
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void
|
||||
map_add_monster(Map *map, Monster *m)
|
||||
{
|
||||
linkedlist_append(&map->monsters, m);
|
||||
}
|
||||
|
||||
int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer)
|
||||
{
|
||||
Texture *t = texture_create(path, renderer);
|
||||
linkedlist_append(&map->textures, t, sizeof(*t));
|
||||
|
||||
/* Freeing the texture preserves the underlying SDL_Texture* which
|
||||
* isn't duplicated when it's being added to the list.
|
||||
* texture_destroy() would destroy that too and break rendering.
|
||||
* Unstable solution. Might cause problems if Texture is ever extended
|
||||
* with more data.
|
||||
*/
|
||||
free(t);
|
||||
linkedlist_append(&map->textures, t);
|
||||
return linkedlist_size(map->textures) - 1;
|
||||
}
|
||||
|
||||
|
@ -114,8 +126,9 @@ void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam)
|
|||
|
||||
void map_render(Map *map, Camera *cam)
|
||||
{
|
||||
int i, j;
|
||||
unsigned int i, j, monster_count;
|
||||
Room *room;
|
||||
Monster *monster;
|
||||
|
||||
if (!timer_started(map->renderTimer)) {
|
||||
timer_start(map->renderTimer);
|
||||
|
@ -135,9 +148,18 @@ void map_render(Map *map, Camera *cam)
|
|||
roomCords.y + j*TILE_DIMENSION
|
||||
};
|
||||
map_tile_render(map, room->tiles[i][j], &tilePos, cam);
|
||||
map_tile_render(map, room->decorations[i][j], &tilePos, cam);
|
||||
map_tile_render(map,
|
||||
room->decorations[i][j],
|
||||
&tilePos,
|
||||
cam);
|
||||
}
|
||||
}
|
||||
|
||||
monster_count = linkedlist_size(map->monsters);
|
||||
for (i = 0; i < monster_count; ++i) {
|
||||
monster = linkedlist_get(&map->monsters, i);
|
||||
monster_render(monster, cam);
|
||||
}
|
||||
}
|
||||
|
||||
void map_set_current_room(Map *map, Position *pos)
|
||||
|
|
|
@ -39,7 +39,6 @@ typedef struct Map_t {
|
|||
|
||||
Map* map_create();
|
||||
|
||||
void map_add_monster(Map*, Monster*);
|
||||
|
||||
int map_add_texture(Map*, const char *path, SDL_Renderer*);
|
||||
|
||||
|
@ -47,6 +46,10 @@ void map_add_tile(Map *map, Position *tile_pos, MapTile*);
|
|||
|
||||
void map_add_decoration(Map *map, Position *tile_pos, MapTile*);
|
||||
|
||||
Texture* map_add_monster_texture(Map*, char *path, SDL_Renderer*);
|
||||
|
||||
void map_add_monster(Map*, Monster*);
|
||||
|
||||
void map_render(Map*, Camera*);
|
||||
|
||||
void map_set_current_room(Map*, Position*);
|
||||
|
|
|
@ -9,6 +9,12 @@ monster_create()
|
|||
return m;
|
||||
}
|
||||
|
||||
void
|
||||
monster_render(Monster *m, Camera *cam)
|
||||
{
|
||||
sprite_render(m->sprite, cam);
|
||||
}
|
||||
|
||||
void
|
||||
monster_destroy(Monster *m)
|
||||
{
|
||||
|
|
|
@ -12,6 +12,8 @@ typedef struct {
|
|||
|
||||
Monster* monster_create();
|
||||
|
||||
void monster_render(Monster*, Camera*);
|
||||
|
||||
void monster_destroy(Monster*);
|
||||
|
||||
#endif // MONSTER_H_
|
||||
|
|
Loading…
Reference in New Issue