Lightsources and lightmaps

This commit is contained in:
Linus Probert 2017-12-10 19:51:24 +01:00
parent f4e24bc8d7
commit a1c57b62d0
6 changed files with 278 additions and 158 deletions

View File

@ -36,54 +36,54 @@ local function load_decor_textures()
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 })
table.insert(floorDecor, { td0, td1, 0, 12 * 16, false, false })
table.insert(floorDecor, { td0, td1, 32, 12 * 16, false, false })
table.insert(floorDecor, { td0, td1, 64, 12 * 16, false, true })
table.insert(floorDecor, { td0, td1, 0, 13 * 16, false, false })
table.insert(floorDecor, { td0, td1, 32, 13 * 16, false, false })
table.insert(floorDecor, { td0, td1, 64, 13 * 16, false, true })
-- 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 })
table.insert(floorDecor, { td0, td1, 16, 12 * 16, false, false })
table.insert(floorDecor, { td0, td1, 48, 12 * 16, false, false })
table.insert(floorDecor, { td0, td1, 16, 13 * 16, false, false })
table.insert(floorDecor, { td0, td1, 48, 13 * 16, false, 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 })
table.insert(floorDecor, { td0, td1, 0 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 1 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 2 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 3 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 4 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 5 * 16, 48, true, false })
table.insert(floorDecor, { td0, td1, 6 * 16, 48, false, false })
table.insert(floorDecor, { td0, td1, 7 * 16, 48, false, 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 })
table.insert(floorDecor, { td0, td1, 0 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 1 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 2 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 3 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 4 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 5 * 16, 11 * 16, true, false })
table.insert(floorDecor, { td0, td1, 6 * 16, 11 * 16, true, false })
-- 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 })
table.insert(floorDecor, { td0, td1, 0 * 16, 17 * 16, true, false })
table.insert(floorDecor, { td0, td1, 1 * 16, 17 * 16, true, false })
table.insert(floorDecor, { td0, td1, 2 * 16, 17 * 16, true, false })
table.insert(floorDecor, { td0, td1, 3 * 16, 17 * 16, true, false })
table.insert(floorDecor, { td0, td1, 4 * 16, 17 * 16, true, false })
table.insert(floorDecor, { td0, td1, 0 * 16, 18 * 16, true, false })
table.insert(floorDecor, { td0, td1, 1 * 16, 18 * 16, true, false })
table.insert(floorDecor, { td0, td1, 2 * 16, 18 * 16, true, false })
table.insert(floorDecor, { td0, td1, 3 * 16, 18 * 16, true, false })
table.insert(floorDecor, { td0, td1, 4 * 16, 18 * 16, true, false })
-- 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 })
table.insert(floorDecor, { td0, td1, 0 * 16, 20 * 16, true, false })
table.insert(floorDecor, { td0, td1, 1 * 16, 20 * 16, true, false })
table.insert(floorDecor, { td0, td1, 2 * 16, 20 * 16, true, false })
-- Webs
--table.insert(floorDecor, { td0, td1, 0 * 16, 19 * 16, false })
@ -96,8 +96,8 @@ local function load_decor_textures()
--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 }
wallDecor.candle0 = { td0, td1, 0, 8 * 16, false, true }
wallDecor.candle1 = { td0, td1, 16, 8 * 16, false, true }
end
function load_textures(map)
@ -108,26 +108,26 @@ function load_textures(map)
local xo = (random(3) - 1) * 112
local yo = (random(7)) * 48
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 }
floor.center = { t_floor, -1, xo + 16, yo + 16, false }
floor.top = { t_floor, -1, xo + 16, yo + 0, false }
floor.bottom = { t_floor, -1, xo + 16, yo + 32, false }
floor.left = { t_floor, -1, xo + 0, yo + 16, false }
floor.right = { t_floor, -1, xo + 32, yo + 16, false }
floor.topleft = { t_floor, -1, xo + 0, yo + 0, false }
floor.topright = { t_floor, -1, xo + 32, yo + 0, false }
floor.bottomleft = { t_floor, -1, xo + 0, yo + 32, false }
floor.bottomright = { t_floor, -1, xo + 32, yo + 32, false }
floor.singletop = { t_floor, -1, xo + 48, yo + 0, false }
floor.singlebottom = { t_floor, -1, xo + 48, yo + 32, false }
floor.singleleft = { t_floor, -1, xo + 64, yo + 16, false }
floor.singleright = { t_floor, -1, xo + 96, yo + 16, false }
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 }
wall.topleft = { t_wall, -1, xo + 0, yo + 0, true }
wall.topright = { t_wall, -1, xo + 32, yo + 0, true }
wall.bottomleft = { t_wall, -1, xo + 0, yo + 32, true }
wall.bottomright = { t_wall, -1, xo + 32, yo + 32, true }
wall.vertical = { t_wall, -1, xo + 0, yo + 16, true }
wall.horizontal = { t_wall, -1, xo + 16, yo + 0, true }
load_decor_textures()
end
@ -142,10 +142,21 @@ function create_room ()
}
end
local function repack(data)
return {
textureIndex0 = data[1],
textureIndex1 = data[2],
tileClipX = data[3],
tileClipY = data[4],
isCollider = data[5] or false,
isLightSource = data[6] or false,
}
end
local function add_random_decor_to_room()
local decor_count = random(4) - 1
for i=1,decor_count do
add_decoration(map, random(11)+1, random(8)+1, unpack(floorDecor[random(#floorDecor)]))
add_decoration(map, random(11)+1, random(8)+1, repack(floorDecor[random(#floorDecor)]))
end
end
@ -154,23 +165,23 @@ local function add_tiles_to_room (map)
for j=0,11 do
if (i >= 1 and i <= 14 and j >= 1 and j <= 10) then
if (i == 1 and j == 1) then
add_tile(map, i, j, unpack(floor.topleft))
add_tile(map, i, j, repack(floor.topleft))
elseif (i == 14 and j == 1) then
add_tile(map, i, j, unpack(floor.topright))
add_tile(map, i, j, repack(floor.topright))
elseif (i == 1 and j == 10) then
add_tile(map, i, j, unpack(floor.bottomleft))
add_tile(map, i, j, repack(floor.bottomleft))
elseif (i == 14 and j == 10) then
add_tile(map, i, j, unpack(floor.bottomright))
add_tile(map, i, j, repack(floor.bottomright))
elseif (i == 1) then
add_tile(map, i, j, unpack(floor.left))
add_tile(map, i, j, repack(floor.left))
elseif (i == 14) then
add_tile(map, i, j, unpack(floor.right))
add_tile(map, i, j, repack(floor.right))
elseif (j == 1) then
add_tile(map, i, j, unpack(floor.top))
add_tile(map, i, j, repack(floor.top))
elseif (j == 10) then
add_tile(map, i, j, unpack(floor.bottom))
add_tile(map, i, j, repack(floor.bottom))
else
add_tile(map, i, j, unpack(floor.center))
add_tile(map, i, j, repack(floor.center))
end
end
end
@ -183,17 +194,21 @@ local function add_walls_to_room (map)
for i=0,15 do
for j=0,11 do
if (i == 0 and j == 0) then
add_tile(map, i, j, unpack(wall.topleft))
add_tile(map, i, j, repack(wall.topleft))
--add_decoration(map, i, j, repack(wallDecor.candle0))
elseif (i == 15 and j == 0) then
add_tile(map, i, j, unpack(wall.topright))
add_tile(map, i, j, repack(wall.topright))
--add_decoration(map, i, j, repack(wallDecor.candle0))
elseif (i == 0 and j == 11) then
add_tile(map, i, j, unpack(wall.bottomleft))
add_tile(map, i, j, repack(wall.bottomleft))
--add_decoration(map, i, j, repack(wallDecor.candle0))
elseif (i == 15 and j == 11) then
add_tile(map, i, j, unpack(wall.bottomright))
add_tile(map, i, j, repack(wall.bottomright))
--add_decoration(map, i, j, repack(wallDecor.candle0))
elseif (i == 0 or i == 15) then
add_tile(map, i, j, unpack(wall.vertical))
add_tile(map, i, j, repack(wall.vertical))
elseif (j == 0 or j == 11) then
add_tile(map, i, j, unpack(wall.horizontal))
add_tile(map, i, j, repack(wall.horizontal))
end
end
end
@ -203,25 +218,25 @@ local function add_exit(map, direction)
if direction > 4 then return end
if direction == UP then
add_tile(map, 6, 0, unpack(wall.bottomright))
add_tile(map, 7, 0, unpack(floor.singleleft))
add_tile(map, 8, 0, unpack(floor.singleright))
add_tile(map, 9, 0, unpack(wall.bottomleft))
add_tile(map, 6, 0, repack(wall.bottomright))
add_tile(map, 7, 0, repack(floor.singleleft))
add_tile(map, 8, 0, repack(floor.singleright))
add_tile(map, 9, 0, repack(wall.bottomleft))
elseif direction == LEFT then
add_tile(map, 0, 4, unpack(wall.bottomright))
add_tile(map, 0, 5, unpack(floor.singletop))
add_tile(map, 0, 6, unpack(floor.singlebottom))
add_tile(map, 0, 7, unpack(wall.topright))
add_tile(map, 0, 4, repack(wall.bottomright))
add_tile(map, 0, 5, repack(floor.singletop))
add_tile(map, 0, 6, repack(floor.singlebottom))
add_tile(map, 0, 7, repack(wall.topright))
elseif direction == RIGHT then
add_tile(map, 15, 4, unpack(wall.bottomleft))
add_tile(map, 15, 5, unpack(floor.singletop))
add_tile(map, 15, 6, unpack(floor.singlebottom))
add_tile(map, 15, 7, unpack(wall.topleft))
add_tile(map, 15, 4, repack(wall.bottomleft))
add_tile(map, 15, 5, repack(floor.singletop))
add_tile(map, 15, 6, repack(floor.singlebottom))
add_tile(map, 15, 7, repack(wall.topleft))
elseif direction == DOWN then
add_tile(map, 6, 11, unpack(wall.topright))
add_tile(map, 7, 11, unpack(floor.singleleft))
add_tile(map, 8, 11, unpack(floor.singleright))
add_tile(map, 9, 11, unpack(wall.topleft))
add_tile(map, 6, 11, repack(wall.topright))
add_tile(map, 7, 11, repack(floor.singleleft))
add_tile(map, 8, 11, repack(floor.singleright))
add_tile(map, 9, 11, repack(wall.topleft))
end
end
@ -235,51 +250,51 @@ end
local function build_vert_center_coridoor(map, offset)
for j=0,4 do
add_tile(map, 6, offset+j, unpack(wall.vertical));
add_tile(map, 7, offset+j, unpack(floor.center));
add_tile(map, 8, offset+j, unpack(floor.center));
add_tile(map, 9, offset+j, unpack(wall.vertical));
add_tile(map, 6, offset+j, repack(wall.vertical));
add_tile(map, 7, offset+j, repack(floor.center));
add_tile(map, 8, offset+j, repack(floor.center));
add_tile(map, 9, offset+j, repack(wall.vertical));
end
end
local function build_horiz_center_coridoor(map, offset)
for i=0,6 do
add_tile(map, offset+i, 4, unpack(wall.horizontal));
add_tile(map, offset+i, 5, unpack(floor.center));
add_tile(map, offset+i, 6, unpack(floor.center));
add_tile(map, offset+i, 7, unpack(wall.horizontal));
add_tile(map, offset+i, 4, repack(wall.horizontal));
add_tile(map, offset+i, 5, repack(floor.center));
add_tile(map, offset+i, 6, repack(floor.center));
add_tile(map, offset+i, 7, repack(wall.horizontal));
end
end
local function build_center_corner_walls(map, exits)
if exits.down then
if exits.left then
add_tile(map, 6, 7, unpack(wall.topright))
add_tile(map, 6, 7, repack(wall.topright))
end
if exits.right then
add_tile(map, 9, 7, unpack(wall.topleft))
add_tile(map, 9, 7, repack(wall.topleft))
end
else
if not exits.left then
add_tile(map, 6, 7, unpack(wall.bottomleft))
add_tile(map, 6, 7, repack(wall.bottomleft))
end
if not exits.right then
add_tile(map, 9, 7, unpack(wall.bottomright))
add_tile(map, 9, 7, repack(wall.bottomright))
end
end
if exits.up then
if exits.left then
add_tile(map, 6, 4, unpack(wall.bottomright))
add_tile(map, 6, 4, repack(wall.bottomright))
end
if exits.right then
add_tile(map, 9, 4, unpack(wall.bottomleft))
add_tile(map, 9, 4, repack(wall.bottomleft))
end
else
if not exits.left then
add_tile(map, 6, 4, unpack(wall.topleft))
add_tile(map, 6, 4, repack(wall.topleft))
end
if not exits.right then
add_tile(map, 9, 4, unpack(wall.topright))
add_tile(map, 9, 4, repack(wall.topright))
end
end
end
@ -301,18 +316,18 @@ function build_coridoor_room(map, room)
end
-- Fill the center
add_tile(map, 6, 5, unpack(wall.vertical))
add_tile(map, 6, 6, unpack(wall.vertical))
add_tile(map, 7, 4, unpack(wall.horizontal))
add_tile(map, 7, 5, unpack(floor.center))
add_tile(map, 7, 6, unpack(floor.center))
add_tile(map, 7, 7, unpack(wall.horizontal))
add_tile(map, 8, 4, unpack(wall.horizontal))
add_tile(map, 8, 5, unpack(floor.center))
add_tile(map, 8, 6, unpack(floor.center))
add_tile(map, 8, 7, unpack(wall.horizontal))
add_tile(map, 9, 5, unpack(wall.vertical))
add_tile(map, 9, 6, unpack(wall.vertical))
add_tile(map, 6, 5, repack(wall.vertical))
add_tile(map, 6, 6, repack(wall.vertical))
add_tile(map, 7, 4, repack(wall.horizontal))
add_tile(map, 7, 5, repack(floor.center))
add_tile(map, 7, 6, repack(floor.center))
add_tile(map, 7, 7, repack(wall.horizontal))
add_tile(map, 8, 4, repack(wall.horizontal))
add_tile(map, 8, 5, repack(floor.center))
add_tile(map, 8, 6, repack(floor.center))
add_tile(map, 8, 7, repack(wall.horizontal))
add_tile(map, 9, 5, repack(wall.vertical))
add_tile(map, 9, 6, repack(wall.vertical))
-- Build the coridoors
if exits.down then build_vert_center_coridoor(map, 7) end

View File

@ -62,6 +62,10 @@ bool initSDL()
printf("[!!] Unable to create renderer: %s\n", SDL_GetError());
return false;
}
if (SDL_SetRenderDrawBlendMode(gRenderer, SDL_BLENDMODE_BLEND) < 0) {
printf("[!!] Unable to set blend mode: %s\n", SDL_GetError());
return false;
}
if (SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT) < 0)
{
printf("[!!] Unable to initiate scaling: %s\n", SDL_GetError());
@ -134,11 +138,15 @@ void run()
quit = handle_events();
roommatrix_populate_from_map(gRoomMatrix, gMap);
roommatrix_add_lightsource(gRoomMatrix,
&gPlayer->pos);
roommatrix_build_lightmap(gRoomMatrix);
SDL_RenderClear(gRenderer);
map_render(gMap, &gCamera);
sprite_render(gPlayer, &gCamera);
roommatrix_render_lightmap(gRoomMatrix, &gCamera);
SDL_RenderPresent(gRenderer);

View File

@ -16,6 +16,7 @@ typedef struct MapTile_t {
int textureIndex1;
SDL_Rect clip;
bool collider;
bool lightsource;
} MapTile;
typedef struct Room_t {

View File

@ -86,57 +86,61 @@ int l_add_texture(lua_State *L)
return 1;
}
static
int l_add_tile(lua_State *L)
{
Map *map;
int tile_x, tile_y;
int texture_index, 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);
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, -1, clip, collider };
map_add_tile(map, &tilePos, tile);
return 0;
}
static int l_add_decoration(lua_State *L)
static void
extract_tile_data(lua_State *L,
void (*f_add_tile)(Map*, Position*, MapTile*))
{
Map *map;
int tile_x, tile_y;
int t_index0, t_index1, tile_clip_x, tile_clip_y;
bool collider;
bool collider, lightsource;
map = luaL_checkmap(L, 1);
tile_x = luaL_checkinteger(L, 2);
tile_y = luaL_checkinteger(L, 3);
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);
// Read the table
lua_settop(L, 4);
luaL_checktype(L, 4, LUA_TTABLE);
// Get the fields from the table
lua_getfield(L, 4, "textureIndex0");
lua_getfield(L, 4, "textureIndex1");
lua_getfield(L, 4, "tileClipX");
lua_getfield(L, 4, "tileClipY");
lua_getfield(L, 4, "isCollider");
lua_getfield(L, 4, "isLightSource");
t_index0 = luaL_checkinteger(L, -6);
t_index1 = luaL_checkinteger(L, -5);
tile_clip_x = luaL_checkinteger(L, -4);
tile_clip_y = luaL_checkinteger(L, -3);
collider = lua_toboolean(L, -2);
lightsource = lua_toboolean(L, -1);
// Clear the stack
lua_pop(L, 6);
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) { t_index0, t_index1, clip, collider };
*tile = (MapTile) { t_index0, t_index1, clip, collider, lightsource };
map_add_decoration(map, &tilePos, tile);
f_add_tile(map, &tilePos, tile);
}
static
int l_add_tile(lua_State *L)
{
extract_tile_data(L, &map_add_tile);
return 0;
}
static
int l_add_decoration(lua_State *L)
{
extract_tile_data(L, &map_add_decoration);
return 0;
}

View File

@ -1,3 +1,4 @@
#include <stdlib.h>
#include "roommatrix.h"
#include "util.h"
#include "map.h"
@ -21,10 +22,90 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
if (r->tiles[i][j])
if (r->tiles[i][j]) {
rm->spaces[i][j].occupied = r->tiles[i][j]->collider;
if (!rm->spaces[i][j].occupied && r->decorations[i][j])
rm->spaces[i][j].occupied = r->decorations[i][j]->collider;
rm->spaces[i][j].lightsource = r->tiles[i][j]->lightsource;
}
if (r->decorations[i][j]) {
rm->spaces[i][j].occupied |= r->decorations[i][j]->collider;
rm->spaces[i][j].lightsource |= r->decorations[i][j]->lightsource;
}
}
}
}
static int
min(int a, int b)
{
return a > b ? b : a;
}
static int
max(int a, int b)
{
return a > b ? a : b;
}
void
roommatrix_add_lightsource(RoomMatrix *matrix, Position *pos)
{
Position mpos = position_to_matrix_coords(pos);
matrix->spaces[mpos.x][mpos.y].lightsource = true;
}
static void
set_light_for_tile(RoomMatrix *matrix, int x, int y)
{
int x_max, x_min, y_max, y_min, i, j;
int lightval;
RoomSpace *space;
space = &matrix->spaces[x][y];
if (!space->lightsource)
return;
space->light = 255;
x_max = min(x + 5, MAP_ROOM_WIDTH - 1);
x_min = max(x - 5, 0);
y_max = min(y + 5, MAP_ROOM_HEIGHT - 1);
y_min = max(y - 5, 0);
for (i = x_min; i <= x_max; ++i) {
for (j = y_min; j <= y_max; ++j) {
lightval = matrix->spaces[i][j].light;
lightval += 255 - (max(abs(x-i), abs(y-j)) * 50);
lightval = min(255, lightval);
lightval = max(0, lightval);
matrix->spaces[i][j].light = lightval;
}
}
}
void
roommatrix_build_lightmap(RoomMatrix *matrix)
{
int i, j;
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
set_light_for_tile(matrix, i, j);
}
}
}
void
roommatrix_render_lightmap(RoomMatrix *matrix, Camera *cam)
{
int i, j, light;
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
light = max(0, 230 - matrix->spaces[i][j].light);
SDL_SetRenderDrawColor(cam->renderer,
0, 0, 0, light);
SDL_Rect box = (SDL_Rect) { i*64, j*64, 64, 64 };
SDL_RenderFillRect(cam->renderer, &box);
}
}
}
@ -36,6 +117,8 @@ void roommatrix_reset(RoomMatrix *m)
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
m->spaces[i][j].occupied = false;
m->spaces[i][j].lightsource = false;
m->spaces[i][j].light = 0;
m->spaces[i][j].character = NULL;
m->spaces[i][j].player = NULL;
}

View File

@ -4,12 +4,15 @@
#include <stdbool.h>
#include "defines.h"
#include "position.h"
#include "camera.h"
typedef struct Sprite_t Sprite;
typedef struct Map_t Map;
typedef struct {
bool occupied;
bool lightsource;
unsigned int light;
Sprite* character;
Sprite* player;
} RoomSpace;
@ -23,6 +26,12 @@ RoomMatrix* roommatrix_create();
void roommatrix_populate_from_map(RoomMatrix*, Map*);
void roommatrix_add_lightsource(RoomMatrix*, Position*);
void roommatrix_build_lightmap(RoomMatrix*);
void roommatrix_render_lightmap(RoomMatrix*, Camera*);
void roommatrix_reset(RoomMatrix*);
void roommatrix_destroy(RoomMatrix*);