Extended linked list and re-thought the map structure

This commit is contained in:
Linus Probert 2017-12-02 16:24:31 +01:00
parent 23abb0f48c
commit 0cdd0f6d8d
6 changed files with 63 additions and 20 deletions

View File

@ -27,6 +27,7 @@ target_link_libraries(breakhack
-lSDL2_mixer -lSDL2_mixer
-lSDL2_ttf -lSDL2_ttf
-lX11 -lX11
-lXrandr
) )
# TESTS: # TESTS:

View File

@ -114,6 +114,34 @@ START_TEST(test_linkedlist_pop)
} }
END_TEST END_TEST
START_TEST(test_linkedlist_get_index)
{
int value1 = 1;
int 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));
ck_assert(linkedlist_size(list) == 2);
get = linkedlist_get(&list, 1);
ck_assert(linkedlist_size(list) == 2);
ck_assert(*get == value2);
linkedlist_destroy(&list);
ck_assert(list == NULL);
}
END_TEST
Suite* t_suite_create() Suite* t_suite_create()
{ {
Suite *s; Suite *s;
@ -127,6 +155,7 @@ Suite* t_suite_create()
tcase_add_test(tc_core, test_linkedlist_pop); tcase_add_test(tc_core, test_linkedlist_pop);
tcase_add_test(tc_core, test_linkedlist_append); tcase_add_test(tc_core, test_linkedlist_append);
tcase_add_test(tc_core, test_linkedlist_poplast); tcase_add_test(tc_core, test_linkedlist_poplast);
tcase_add_test(tc_core, test_linkedlist_get_index);
suite_add_tcase(s, tc_core); suite_add_tcase(s, tc_core);
return s; return s;

View File

@ -93,6 +93,16 @@ void* linkedlist_poplast(LinkedList **head)
return linkedlist_poplast(&nextNode); return linkedlist_poplast(&nextNode);
} }
void* linkedlist_get(LinkedList **head, unsigned int index)
{
if (*head == NULL)
return NULL;
if (index == 0)
return (*head)->data;
return linkedlist_get(&(*head)->next, --index);
}
void linkedlist_destroy(LinkedList **head) void linkedlist_destroy(LinkedList **head)
{ {
if (*head == NULL) { if (*head == NULL) {

View File

@ -17,6 +17,8 @@ void linkedlist_append(LinkedList **head, void *value, unsigned int size);
void* linkedlist_poplast(LinkedList **head); void* linkedlist_poplast(LinkedList **head);
void* linkedlist_get(LinkedList **head, unsigned int index);
void linkedlist_destroy(LinkedList **head); void linkedlist_destroy(LinkedList **head);
unsigned int linkedlist_size(LinkedList *head); unsigned int linkedlist_size(LinkedList *head);

View File

@ -1,16 +1,6 @@
#include "map.h" #include "map.h"
#include "util.h" #include "util.h"
static
Sprite* create_default_tile(SDL_Renderer *renderer)
{
Sprite *s = sprite_create();
sprite_load_texture(s, "assets/Objects/Floor.png", renderer);
s->texture->clip = (SDL_Rect) { 16, 54, 16, 16 };
s->texture->dim = (Dimension) { 64, 64 };
return s;
}
static static
Room* create_room() Room* create_room()
{ {
@ -31,9 +21,9 @@ Map* map_create(SDL_Renderer *renderer)
int i, j; int i, j;
Map *map = ec_malloc(sizeof(Map)); Map *map = ec_malloc(sizeof(Map));
map->textures = linkedlist_create();
map->currentRoom = (Position) { 0, 0 }; map->currentRoom = (Position) { 0, 0 };
map->level = 1; map->level = 1;
map->defaultTile = create_default_tile(renderer);
for (i=0; i < MAP_V_ROOM_COUNT; ++i) { for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
for (j=0; j < MAP_H_ROOM_COUNT; ++j) { for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
@ -44,6 +34,13 @@ Map* map_create(SDL_Renderer *renderer)
return map; return map;
} }
static
void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam)
{
if (tile == NULL)
return;
}
void map_render(Map *map, Camera *cam) void map_render(Map *map, Camera *cam)
{ {
int i, j; int i, j;
@ -53,16 +50,11 @@ void map_render(Map *map, Camera *cam)
roomPos.x * MAP_ROOM_WIDTH * 64, roomPos.x * MAP_ROOM_WIDTH * 64,
roomPos.y * MAP_ROOM_HEIGHT * 64 roomPos.y * MAP_ROOM_HEIGHT * 64
}; };
Sprite *dTile = map->defaultTile;
room = map->rooms[roomPos.x][roomPos.y]; room = map->rooms[roomPos.x][roomPos.y];
for (i=0; i < MAP_ROOM_HEIGHT; ++i) { for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
for (j=0; j < MAP_ROOM_WIDTH; ++j) { for (j=0; j < MAP_ROOM_WIDTH; ++j) {
if (room->tiles[i][j] == NULL) { map_tile_render(map, room->tiles[i][j], &roomCords, cam);
dTile->pos.x = roomCords.x + (64*j);
dTile->pos.y = roomCords.y + (64*i);
sprite_render(dTile, cam);
}
} }
} }
} }
@ -74,7 +66,7 @@ void map_room_destroy(Room *room)
for (i=0; i < MAP_ROOM_HEIGHT; ++i) { for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
for (j=0; j < MAP_ROOM_WIDTH; ++j) { for (j=0; j < MAP_ROOM_WIDTH; ++j) {
if (room->tiles[i][j]) { if (room->tiles[i][j]) {
sprite_destroy(room->tiles[i][j]); free(room->tiles[i][j]);
} }
} }
} }
@ -89,5 +81,8 @@ void map_destroy(Map *map)
map_room_destroy(map->rooms[i][j]); map_room_destroy(map->rooms[i][j]);
} }
} }
while (map->textures != NULL) {
SDL_DestroyTexture(linkedlist_poplast(&map->textures));
}
free(map); free(map);
} }

View File

@ -6,6 +6,7 @@
#include "sprite.h" #include "sprite.h"
#include "camera.h" #include "camera.h"
#include "position.h"
#define MAP_ROOM_HEIGHT 12 #define MAP_ROOM_HEIGHT 12
#define MAP_ROOM_WIDTH 16 #define MAP_ROOM_WIDTH 16
@ -13,13 +14,18 @@
#define MAP_H_ROOM_COUNT 10 #define MAP_H_ROOM_COUNT 10
typedef struct { typedef struct {
Sprite* tiles[MAP_ROOM_HEIGHT][MAP_ROOM_WIDTH]; unsigned int textureIndex;
Position clipPosition;
} MapTile;
typedef struct {
MapTile* tiles[MAP_ROOM_HEIGHT][MAP_ROOM_WIDTH];
} Room; } Room;
typedef struct { typedef struct {
Room* rooms[MAP_V_ROOM_COUNT][MAP_H_ROOM_COUNT]; Room* rooms[MAP_V_ROOM_COUNT][MAP_H_ROOM_COUNT];
LinkedList *textures;
Position currentRoom; Position currentRoom;
Sprite* defaultTile;
int level; int level;
} Map; } Map;