Extended linked list and re-thought the map structure
This commit is contained in:
parent
23abb0f48c
commit
0cdd0f6d8d
|
@ -27,6 +27,7 @@ target_link_libraries(breakhack
|
|||
-lSDL2_mixer
|
||||
-lSDL2_ttf
|
||||
-lX11
|
||||
-lXrandr
|
||||
)
|
||||
|
||||
# TESTS:
|
||||
|
|
|
@ -114,6 +114,34 @@ START_TEST(test_linkedlist_pop)
|
|||
}
|
||||
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 *s;
|
||||
|
@ -127,6 +155,7 @@ Suite* t_suite_create()
|
|||
tcase_add_test(tc_core, test_linkedlist_pop);
|
||||
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);
|
||||
suite_add_tcase(s, tc_core);
|
||||
|
||||
return s;
|
||||
|
|
|
@ -93,6 +93,16 @@ void* linkedlist_poplast(LinkedList **head)
|
|||
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)
|
||||
{
|
||||
if (*head == NULL) {
|
||||
|
|
|
@ -17,6 +17,8 @@ void linkedlist_append(LinkedList **head, void *value, unsigned int size);
|
|||
|
||||
void* linkedlist_poplast(LinkedList **head);
|
||||
|
||||
void* linkedlist_get(LinkedList **head, unsigned int index);
|
||||
|
||||
void linkedlist_destroy(LinkedList **head);
|
||||
|
||||
unsigned int linkedlist_size(LinkedList *head);
|
||||
|
|
31
src/map.c
31
src/map.c
|
@ -1,16 +1,6 @@
|
|||
#include "map.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
|
||||
Room* create_room()
|
||||
{
|
||||
|
@ -31,9 +21,9 @@ Map* map_create(SDL_Renderer *renderer)
|
|||
int i, j;
|
||||
|
||||
Map *map = ec_malloc(sizeof(Map));
|
||||
map->textures = linkedlist_create();
|
||||
map->currentRoom = (Position) { 0, 0 };
|
||||
map->level = 1;
|
||||
map->defaultTile = create_default_tile(renderer);
|
||||
|
||||
for (i=0; i < MAP_V_ROOM_COUNT; ++i) {
|
||||
for (j=0; j < MAP_H_ROOM_COUNT; ++j) {
|
||||
|
@ -44,6 +34,13 @@ Map* map_create(SDL_Renderer *renderer)
|
|||
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)
|
||||
{
|
||||
int i, j;
|
||||
|
@ -53,16 +50,11 @@ void map_render(Map *map, Camera *cam)
|
|||
roomPos.x * MAP_ROOM_WIDTH * 64,
|
||||
roomPos.y * MAP_ROOM_HEIGHT * 64
|
||||
};
|
||||
Sprite *dTile = map->defaultTile;
|
||||
|
||||
room = map->rooms[roomPos.x][roomPos.y];
|
||||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
||||
for (j=0; j < MAP_ROOM_WIDTH; ++j) {
|
||||
if (room->tiles[i][j] == NULL) {
|
||||
dTile->pos.x = roomCords.x + (64*j);
|
||||
dTile->pos.y = roomCords.y + (64*i);
|
||||
sprite_render(dTile, cam);
|
||||
}
|
||||
map_tile_render(map, room->tiles[i][j], &roomCords, cam);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +66,7 @@ void map_room_destroy(Room *room)
|
|||
for (i=0; i < MAP_ROOM_HEIGHT; ++i) {
|
||||
for (j=0; j < MAP_ROOM_WIDTH; ++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]);
|
||||
}
|
||||
}
|
||||
while (map->textures != NULL) {
|
||||
SDL_DestroyTexture(linkedlist_poplast(&map->textures));
|
||||
}
|
||||
free(map);
|
||||
}
|
||||
|
|
10
src/map.h
10
src/map.h
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "sprite.h"
|
||||
#include "camera.h"
|
||||
#include "position.h"
|
||||
|
||||
#define MAP_ROOM_HEIGHT 12
|
||||
#define MAP_ROOM_WIDTH 16
|
||||
|
@ -13,13 +14,18 @@
|
|||
#define MAP_H_ROOM_COUNT 10
|
||||
|
||||
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;
|
||||
|
||||
typedef struct {
|
||||
Room* rooms[MAP_V_ROOM_COUNT][MAP_H_ROOM_COUNT];
|
||||
LinkedList *textures;
|
||||
Position currentRoom;
|
||||
Sprite* defaultTile;
|
||||
int level;
|
||||
} Map;
|
||||
|
||||
|
|
Loading…
Reference in New Issue