This implements base ambient lighting for tiles which helps with tiles like lava and ceiling lights.

master
Zed A. Shaw 3 days ago
parent 74a1801069
commit 3dc70c3af6
  1. BIN
      assets/ceiling_moss_brick_blue_light-256.png
  2. 2
      assets/enemies.json
  3. BIN
      assets/large_stone_floor-256.png
  4. 8
      assets/tiles.json
  5. 1
      gui/fsm.cpp
  6. 3
      levelmanager.cpp
  7. 19
      lights.cpp
  8. 8
      lights.hpp
  9. 2
      tests/lighting.cpp
  10. 20
      textures.cpp
  11. 2
      textures.hpp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 72 KiB

@ -8,7 +8,7 @@
}, },
{"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 10, "dead": false}, {"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 10, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false}, {"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "LightSource", "strength": 45, "radius": 2.0} {"_type": "LightSource", "strength": 35, "radius": 2.0}
] ]
}, },
"GOLD_SAVIOR": { "GOLD_SAVIOR": {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 103 KiB

After

Width:  |  Height:  |  Size: 94 KiB

@ -4,30 +4,35 @@
"collision": false, "collision": false,
"display": 10398, "display": 10398,
"ceiling": "ceiling_plain", "ceiling": "ceiling_plain",
"light": 0,
"id": 0 "id": 0
}, },
"wall_plain": { "wall_plain": {
"texture": "assets/wall_texture_test-256.png", "texture": "assets/wall_texture_test-256.png",
"collision": true, "collision": true,
"display": 9608, "display": 9608,
"light": 0,
"id": 1 "id": 1
}, },
"wall_moss": { "wall_moss": {
"texture": "assets/glowing_moss_wall-256.png", "texture": "assets/glowing_moss_wall-256.png",
"collision": true, "collision": true,
"display": 8820, "display": 8820,
"light": 20,
"id": 2 "id": 2
}, },
"wall_vines": { "wall_vines": {
"texture": "assets/wall_with_vines-256.png", "texture": "assets/wall_with_vines-256.png",
"collision": true, "collision": true,
"display": 35, "display": 35,
"light": 0,
"id": 3 "id": 3
}, },
"ceiling_plain": { "ceiling_plain": {
"texture": "assets/ceiling_test-256.png", "texture": "assets/ceiling_test-256.png",
"collision": false, "collision": false,
"display": 35, "display": 35,
"light": 0,
"id": 4 "id": 4
}, },
"lava_floor": { "lava_floor": {
@ -35,6 +40,7 @@
"collision": false, "collision": false,
"display": 35, "display": 35,
"ceiling": "ceiling_plain", "ceiling": "ceiling_plain",
"light": 20,
"id": 5 "id": 5
}, },
"large_stone_floor": { "large_stone_floor": {
@ -42,12 +48,14 @@
"collision": false, "collision": false,
"display": 35, "display": 35,
"ceiling": "ceiling_moss_brick_blue_light", "ceiling": "ceiling_moss_brick_blue_light",
"light": 40,
"id": 6 "id": 6
}, },
"ceiling_moss_brick_blue_light": { "ceiling_moss_brick_blue_light": {
"texture": "assets/ceiling_moss_brick_blue_light-256.png", "texture": "assets/ceiling_moss_brick_blue_light-256.png",
"collision": false, "collision": false,
"display": 35, "display": 35,
"light": 0,
"id": 7 "id": 7
} }
} }

@ -318,6 +318,7 @@ namespace gui {
break; break;
case KEY::P: case KEY::P:
sound::mute(false); sound::mute(false);
if(!sound::playing("ambient_1")) sound::play("ambient_1", true);
$debug_ui.debug(); $debug_ui.debug();
shaders::reload(); shaders::reload();
dbc::log("save map!"); dbc::log("save map!");

@ -99,8 +99,7 @@ size_t LevelManager::create_level(shared_ptr<DinkyECS::World> prev_world) {
auto player = world->get_the<Player>(); auto player = world->get_the<Player>();
$levels.emplace_back(index, player.entity, map, world, $levels.emplace_back(index, player.entity, map, world,
make_shared<LightRender>(map->width(), map->height()), make_shared<LightRender>(map->tiles()), collider);
collider);
dbc::check(index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error"); dbc::check(index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error");
return index; return index;

@ -1,10 +1,27 @@
#include "lights.hpp" #include "lights.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "textures.hpp"
#include <vector> #include <vector>
using std::vector; using std::vector;
namespace lighting { namespace lighting {
LightRender::LightRender(Matrix& tiles) :
$width(matrix::width(tiles)),
$height(matrix::height(tiles)),
$lightmap(matrix::make($width, $height)),
$ambient(matrix::make($width, $height)),
$paths($width, $height)
{
auto& tile_ambient = textures::get_ambient_light();
for(matrix::each_cell it{tiles}; it.next();) {
size_t tile_id = tiles[it.y][it.x];
$ambient[it.y][it.x] = MIN + tile_ambient[tile_id];
}
}
void LightRender::render_square_light(LightSource source, Point at, PointList &has_light) { void LightRender::render_square_light(LightSource source, Point at, PointList &has_light) {
for(matrix::box it{$lightmap, at.x, at.y, (size_t)floor(source.radius)}; it.next();) { for(matrix::box it{$lightmap, at.x, at.y, (size_t)floor(source.radius)}; it.next();) {
if($paths.$paths[it.y][it.x] != WALL_PATH_LIMIT) { if($paths.$paths[it.y][it.x] != WALL_PATH_LIMIT) {
@ -44,7 +61,7 @@ namespace lighting {
} }
void LightRender::reset_light() { void LightRender::reset_light() {
matrix::assign($lightmap, lighting::MIN); $lightmap = $ambient;
} }
void LightRender::clear_light_target(const Point &at) { void LightRender::clear_light_target(const Point &at) {

@ -19,14 +19,10 @@ namespace lighting {
size_t $width; size_t $width;
size_t $height; size_t $height;
Matrix $lightmap; Matrix $lightmap;
Matrix $ambient;
Pathing $paths; Pathing $paths;
LightRender(size_t width, size_t height) : LightRender(Matrix& walls);
$width(width),
$height(height),
$lightmap(height, matrix::Row(width, 0)),
$paths(width, height)
{}
void reset_light(); void reset_light();
void set_light_target(const Point &at, int value=0); void set_light_target(const Point &at, int value=0);

@ -22,7 +22,7 @@ TEST_CASE("lighting a map works", "[lighting]") {
LightSource source1{6, 1.0}; LightSource source1{6, 1.0};
LightSource source2{4,3}; LightSource source2{4,3};
LightRender lr(map.width(), map.height()); LightRender lr(map.walls());
lr.reset_light(); lr.reset_light();

@ -29,13 +29,18 @@ namespace textures {
} }
} }
inline void resize_shit(size_t size) {
TMGR.surfaces.resize(size);
TMGR.ceilings.resize(size);
TMGR.map_tile_set.resize(size);
TMGR.ambient_light.resize(size);
}
void load_tiles() { void load_tiles() {
Config assets("assets/tiles.json"); Config assets("assets/tiles.json");
auto &tiles = assets.json(); auto &tiles = assets.json();
TMGR.surfaces.resize(tiles.size()); resize_shit(tiles.size());
TMGR.ceilings.resize(tiles.size());
TMGR.map_tile_set.resize(tiles.size());
for(auto &el : tiles.items()) { for(auto &el : tiles.items()) {
auto &config = el.value(); auto &config = el.value();
@ -43,12 +48,11 @@ namespace textures {
size_t surface_i = config["id"]; size_t surface_i = config["id"];
if(surface_i >= tiles.size()) { if(surface_i >= tiles.size()) {
TMGR.surfaces.resize(surface_i + 1); resize_shit(surface_i + 1);
TMGR.ceilings.resize(surface_i + 1);
TMGR.map_tile_set.resize(surface_i + 1);
} }
TMGR.map_tile_set[surface_i] = config["display"]; TMGR.map_tile_set[surface_i] = config["display"];
TMGR.ambient_light[surface_i] = config["light"];
TMGR.surfaces[surface_i] = load_image(texture_fname); TMGR.surfaces[surface_i] = load_image(texture_fname);
// NOTE: ceilings defaults to 0 which is floor texture so only need to update // NOTE: ceilings defaults to 0 which is floor texture so only need to update
@ -93,6 +97,10 @@ namespace textures {
return texture; return texture;
} }
std::vector<int>& get_ambient_light() {
return TMGR.ambient_light;
}
std::vector<wchar_t>& get_map_tile_set() { std::vector<wchar_t>& get_map_tile_set() {
return TMGR.map_tile_set; return TMGR.map_tile_set;
} }

@ -18,6 +18,7 @@ namespace textures {
std::vector<sf::Image> surfaces; std::vector<sf::Image> surfaces;
std::vector<size_t> ceilings; std::vector<size_t> ceilings;
std::vector<wchar_t> map_tile_set; std::vector<wchar_t> map_tile_set;
std::vector<int> ambient_light;
std::unordered_map<std::string, SpriteTexture> sprite_textures; std::unordered_map<std::string, SpriteTexture> sprite_textures;
}; };
@ -27,6 +28,7 @@ namespace textures {
sf::Image load_image(const std::string& filename); sf::Image load_image(const std::string& filename);
std::vector<int>& get_ambient_light();
std::vector<wchar_t>& get_map_tile_set(); std::vector<wchar_t>& get_map_tile_set();
const uint32_t* get_surface(size_t num); const uint32_t* get_surface(size_t num);

Loading…
Cancel
Save