diff --git a/data/maproombuilder.lua b/data/maproombuilder.lua index d5f6e95..07c1347 100644 --- a/data/maproombuilder.lua +++ b/data/maproombuilder.lua @@ -7,13 +7,6 @@ local LEFT = 2 local RIGHT = 3 local DOWN = 4 --- Textures -local textures = { - floor = nil, - walls = nil, - decor = nil, -} - local floor = { center = nil, top = nil, @@ -38,92 +31,105 @@ local wall = { local floorDecor = { } local wallDecor = { } +local function load_decor_textures() + td0 = add_texture(map, "assets/Objects/Decor0.png") + td1 = add_texture(map, "assets/Objects/Decor1.png") + + -- Skulls + table.insert(floorDecor, { td0, td1, 0, 12 * 16, false }) + table.insert(floorDecor, { td0, td1, 32, 12 * 16, false }) + table.insert(floorDecor, { td0, td1, 64, 12 * 16, false }) + table.insert(floorDecor, { td0, td1, 0, 13 * 16, false }) + table.insert(floorDecor, { td0, td1, 32, 13 * 16, false }) + table.insert(floorDecor, { td0, td1, 64, 13 * 16, false }) + + -- Bones + table.insert(floorDecor, { td0, td1, 16, 12 * 16, false }) + table.insert(floorDecor, { td0, td1, 48, 12 * 16, false }) + table.insert(floorDecor, { td0, td1, 16, 13 * 16, false }) + table.insert(floorDecor, { td0, td1, 48, 13 * 16, false }) + + -- Urns + table.insert(floorDecor, { td0, td1, 0 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 1 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 2 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 3 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 4 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 5 * 16, 48, true }) + table.insert(floorDecor, { td0, td1, 6 * 16, 48, false }) + table.insert(floorDecor, { td0, td1, 7 * 16, 48, false }) + + -- Racks + table.insert(floorDecor, { td0, td1, 0 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 1 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 2 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 3 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 4 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 5 * 16, 11 * 16, true }) + table.insert(floorDecor, { td0, td1, 6 * 16, 11 * 16, true }) + + -- Headstones + table.insert(floorDecor, { td0, td1, 0 * 16, 17 * 16, true }) + table.insert(floorDecor, { td0, td1, 1 * 16, 17 * 16, true }) + table.insert(floorDecor, { td0, td1, 2 * 16, 17 * 16, true }) + table.insert(floorDecor, { td0, td1, 3 * 16, 17 * 16, true }) + table.insert(floorDecor, { td0, td1, 4 * 16, 17 * 16, true }) + table.insert(floorDecor, { td0, td1, 0 * 16, 18 * 16, true }) + table.insert(floorDecor, { td0, td1, 1 * 16, 18 * 16, true }) + table.insert(floorDecor, { td0, td1, 2 * 16, 18 * 16, true }) + table.insert(floorDecor, { td0, td1, 3 * 16, 18 * 16, true }) + table.insert(floorDecor, { td0, td1, 4 * 16, 18 * 16, true }) + + -- Altars + table.insert(floorDecor, { td0, td1, 0 * 16, 20 * 16, true }) + table.insert(floorDecor, { td0, td1, 1 * 16, 20 * 16, true }) + table.insert(floorDecor, { td0, td1, 2 * 16, 20 * 16, true }) + + -- Webs + --table.insert(floorDecor, { td0, td1, 0 * 16, 19 * 16, false }) + --table.insert(floorDecor, { td0, td1, 1 * 16, 19 * 16, false }) + --table.insert(floorDecor, { td0, td1, 2 * 16, 19 * 16, false }) + --table.insert(floorDecor, { td0, td1, 3 * 16, 19 * 16, false }) + --table.insert(floorDecor, { td0, td1, 4 * 16, 19 * 16, false }) + + -- Statues + --table.insert(floorDecor, { td0, td1, 3 * 16, 20 * 16, true }) + --table.insert(floorDecor, { td0, td1, 4 * 16, 20 * 16, true }) + + wallDecor.candle0 = { td0, td1, 0, 8 * 16, false } + wallDecor.candle1 = { td0, td1, 16, 8 * 16, false } +end + function load_textures(map) - textures.floor = add_texture(map, "assets/Objects/Floor.png") - textures.walls = add_texture(map, "assets/Objects/Wall.png") - textures.decor = add_texture(map, "assets/Objects/Decor0.png") + t_floor = add_texture(map, "assets/Objects/Floor.png") + t_wall = add_texture(map, "assets/Objects/Wall.png") math.randomseed(os.time()) local xo = (random(3) - 1) * 112 local yo = (random(7)) * 48 - floor.center = { textures.floor, xo + 16, yo + 16, false } - floor.top = { textures.floor, xo + 16, yo + 0, false } - floor.bottom = { textures.floor, xo + 16, yo + 32, false } - floor.left = { textures.floor, xo + 0, yo + 16, false } - floor.right = { textures.floor, xo + 32, yo + 16, false } - floor.topleft = { textures.floor, xo + 0, yo + 0, false } - floor.topright = { textures.floor, xo + 32, yo + 0, false } - floor.bottomleft = { textures.floor, xo + 0, yo + 32, false } - floor.bottomright = { textures.floor, xo + 32, yo + 32, false } - floor.singletop = { textures.floor, xo + 48, yo + 0, false } - floor.singlebottom = { textures.floor, xo + 48, yo + 32, false } - floor.singleleft = { textures.floor, xo + 64, yo + 16, false } - floor.singleright = { textures.floor, xo + 96, yo + 16, false } + floor.center = { t_floor, xo + 16, yo + 16, false } + floor.top = { t_floor, xo + 16, yo + 0, false } + floor.bottom = { t_floor, xo + 16, yo + 32, false } + floor.left = { t_floor, xo + 0, yo + 16, false } + floor.right = { t_floor, xo + 32, yo + 16, false } + floor.topleft = { t_floor, xo + 0, yo + 0, false } + floor.topright = { t_floor, xo + 32, yo + 0, false } + floor.bottomleft = { t_floor, xo + 0, yo + 32, false } + floor.bottomright = { t_floor, xo + 32, yo + 32, false } + floor.singletop = { t_floor, xo + 48, yo + 0, false } + floor.singlebottom = { t_floor, xo + 48, yo + 32, false } + floor.singleleft = { t_floor, xo + 64, yo + 16, false } + floor.singleright = { t_floor, xo + 96, yo + 16, false } - wall.topleft = { textures.walls, xo + 0, yo + 0, true } - wall.topright = { textures.walls, xo + 32, yo + 0, true } - wall.bottomleft = { textures.walls, xo + 0, yo + 32, true } - wall.bottomright = { textures.walls, xo + 32, yo + 32, true } - wall.vertical = { textures.walls, xo + 0, yo + 16, true } - wall.horizontal = { textures.walls, xo + 16, yo + 0, true } + wall.topleft = { t_wall, xo + 0, yo + 0, true } + wall.topright = { t_wall, xo + 32, yo + 0, true } + wall.bottomleft = { t_wall, xo + 0, yo + 32, true } + wall.bottomright = { t_wall, xo + 32, yo + 32, true } + wall.vertical = { t_wall, xo + 0, yo + 16, true } + wall.horizontal = { t_wall, xo + 16, yo + 0, true } - -- Skulls - table.insert(floorDecor, { textures.decor, 0, 12 * 16, false }) - table.insert(floorDecor, { textures.decor, 32, 12 * 16, false }) - table.insert(floorDecor, { textures.decor, 0, 13 * 16, false }) - table.insert(floorDecor, { textures.decor, 32, 13 * 16, false }) - - -- Bones - table.insert(floorDecor, { textures.decor, 16, 12 * 16, false }) - table.insert(floorDecor, { textures.decor, 48, 12 * 16, false }) - table.insert(floorDecor, { textures.decor, 16, 13 * 16, false }) - table.insert(floorDecor, { textures.decor, 48, 13 * 16, false }) - - -- Urns - table.insert(floorDecor, { textures.decor, 0 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 1 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 2 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 3 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 4 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 5 * 16, 48, true }) - table.insert(floorDecor, { textures.decor, 6 * 16, 48, false }) - table.insert(floorDecor, { textures.decor, 7 * 16, 48, false }) - - -- Racks - table.insert(floorDecor, { textures.decor, 0 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 1 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 2 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 3 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 4 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 5 * 16, 11 * 16, true }) - table.insert(floorDecor, { textures.decor, 6 * 16, 11 * 16, true }) - - -- Headstones - table.insert(floorDecor, { textures.decor, 0 * 16, 17 * 16, true }) - table.insert(floorDecor, { textures.decor, 1 * 16, 17 * 16, true }) - table.insert(floorDecor, { textures.decor, 2 * 16, 17 * 16, true }) - table.insert(floorDecor, { textures.decor, 3 * 16, 17 * 16, true }) - table.insert(floorDecor, { textures.decor, 4 * 16, 17 * 16, true }) - table.insert(floorDecor, { textures.decor, 0 * 16, 18 * 16, true }) - table.insert(floorDecor, { textures.decor, 1 * 16, 18 * 16, true }) - table.insert(floorDecor, { textures.decor, 2 * 16, 18 * 16, true }) - table.insert(floorDecor, { textures.decor, 3 * 16, 18 * 16, true }) - table.insert(floorDecor, { textures.decor, 4 * 16, 18 * 16, true }) - - -- Webs - --table.insert(floorDecor, { textures.decor, 0 * 16, 19 * 16, false }) - --table.insert(floorDecor, { textures.decor, 1 * 16, 19 * 16, false }) - --table.insert(floorDecor, { textures.decor, 2 * 16, 19 * 16, false }) - --table.insert(floorDecor, { textures.decor, 3 * 16, 19 * 16, false }) - --table.insert(floorDecor, { textures.decor, 4 * 16, 19 * 16, false }) - - -- Altars - table.insert(floorDecor, { textures.decor, 0 * 16, 20 * 16, true }) - table.insert(floorDecor, { textures.decor, 1 * 16, 20 * 16, true }) - table.insert(floorDecor, { textures.decor, 2 * 16, 20 * 16, true }) - --table.insert(floorDecor, { textures.decor, 3 * 16, 20 * 16, true }) - --table.insert(floorDecor, { textures.decor, 4 * 16, 20 * 16, true }) + load_decor_textures() end function create_room () diff --git a/src/map.c b/src/map.c index 8e771ce..173e90b 100644 --- a/src/map.c +++ b/src/map.c @@ -25,6 +25,7 @@ Map* map_create() Map *map = ec_malloc(sizeof(Map)); map->textures = linkedlist_create(); map->currentRoom = (Position) { 0, 0 }; + map->renderTimer = timer_create(); map->level = 1; for (i=0; i < MAP_H_ROOM_COUNT; ++i) { @@ -76,9 +77,16 @@ int map_add_texture(Map *map, const char *path, SDL_Renderer *renderer) static void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam) { + static bool second_texture = false; + if (tile == NULL) return; + if (timer_get_ticks(map->renderTimer) > 300) { + timer_start(map->renderTimer); + second_texture = !second_texture; + } + Position camPos = camera_to_camera_position(cam, pos); SDL_Rect draw_box = (SDL_Rect) { camPos.x, @@ -87,7 +95,12 @@ void map_tile_render(Map *map, MapTile *tile, Position *pos, Camera *cam) TILE_DIMENSION }; - Texture *texture = linkedlist_get(&map->textures, tile->textureIndex); + Texture *texture; + if (tile->textureIndex1 >= 0 && second_texture) { + texture = linkedlist_get(&map->textures, tile->textureIndex1); + } else { + texture = linkedlist_get(&map->textures, tile->textureIndex0); + } SDL_RenderCopy(cam->renderer, texture->texture, @@ -101,6 +114,11 @@ void map_render(Map *map, Camera *cam) { int i, j; Room *room; + + if (!timer_started(map->renderTimer)) { + timer_start(map->renderTimer); + } + Position roomPos = { map->currentRoom.x, map->currentRoom.y }; Position roomCords = { roomPos.x * MAP_ROOM_WIDTH * TILE_DIMENSION, @@ -175,5 +193,6 @@ void map_destroy(Map *map) while (map->textures != NULL) { texture_destroy(linkedlist_poplast(&map->textures)); } + timer_destroy(map->renderTimer); free(map); } diff --git a/src/map.h b/src/map.h index 9792a92..3a5ccb5 100644 --- a/src/map.h +++ b/src/map.h @@ -8,16 +8,19 @@ #include "sprite.h" #include "camera.h" #include "position.h" +#include "timer.h" #include "defines.h" typedef struct MapTile_t { - unsigned int textureIndex; + int textureIndex0; + int textureIndex1; SDL_Rect clip; bool collider; } MapTile; typedef struct Room_t { MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; + MapTile* secondary_tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; MapTile* decorations[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT]; } Room; @@ -25,6 +28,7 @@ typedef struct Map_t { Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT]; LinkedList *textures; Position currentRoom; + Timer *renderTimer; int level; } Map; diff --git a/src/map_lua.c b/src/map_lua.c index 7a927c0..d45f12e 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -106,7 +106,7 @@ int l_add_tile(lua_State *L) SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 }; MapTile *tile = malloc(sizeof(MapTile)); - *tile = (MapTile) { texture_index, clip, collider }; + *tile = (MapTile) { texture_index, -1, clip, collider }; map_add_tile(map, &tilePos, tile); @@ -117,22 +117,23 @@ static int l_add_decoration(lua_State *L) { Map *map; int tile_x, tile_y; - int texture_index, tile_clip_x, tile_clip_y; + int t_index0, t_index1, tile_clip_x, tile_clip_y; bool collider; map = luaL_checkmap(L, 1); tile_x = luaL_checkinteger(L, 2); tile_y = luaL_checkinteger(L, 3); - texture_index = luaL_checkinteger(L, 4); - tile_clip_x = luaL_checkinteger(L, 5); - tile_clip_y = luaL_checkinteger(L, 6); - collider = lua_toboolean(L, 7); + t_index0 = luaL_checkinteger(L, 4); + t_index1 = luaL_checkinteger(L, 5); + tile_clip_x = luaL_checkinteger(L, 6); + tile_clip_y = luaL_checkinteger(L, 7); + collider = lua_toboolean(L, 8); Position tilePos = (Position) { tile_x, tile_y }; SDL_Rect clip = (SDL_Rect) { tile_clip_x, tile_clip_y, 16, 16 }; MapTile *tile = malloc(sizeof(MapTile)); - *tile = (MapTile) { texture_index, clip, collider }; + *tile = (MapTile) { t_index0, t_index1, clip, collider }; map_add_decoration(map, &tilePos, tile); diff --git a/src/timer.c b/src/timer.c index db9b68d..d3ab744 100644 --- a/src/timer.c +++ b/src/timer.c @@ -19,6 +19,11 @@ void timer_stop(Timer *t) t->startTime = 0; } +bool timer_started(Timer *t) +{ + return t->startTime != 0; +} + unsigned int timer_get_ticks(Timer *t) { if (!t->startTime) diff --git a/src/timer.h b/src/timer.h index 32f4624..0007611 100644 --- a/src/timer.h +++ b/src/timer.h @@ -1,6 +1,8 @@ #ifndef TIMER_H_ #define TIMER_H_ +#include + typedef struct { unsigned int startTime; } Timer; @@ -8,6 +10,7 @@ typedef struct { Timer* timer_create(); void timer_start(Timer*); void timer_stop(Timer*); +bool timer_started(Timer*); unsigned int timer_get_ticks(Timer*); void timer_destroy(Timer*);