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

master
Zed A. Shaw 2 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": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "LightSource", "strength": 45, "radius": 2.0}
{"_type": "LightSource", "strength": 35, "radius": 2.0}
]
},
"GOLD_SAVIOR": {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 103 KiB

After

Width:  |  Height:  |  Size: 94 KiB

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

@ -318,6 +318,7 @@ namespace gui {
break;
case KEY::P:
sound::mute(false);
if(!sound::playing("ambient_1")) sound::play("ambient_1", true);
$debug_ui.debug();
shaders::reload();
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>();
$levels.emplace_back(index, player.entity, map, world,
make_shared<LightRender>(map->width(), map->height()),
collider);
make_shared<LightRender>(map->tiles()), collider);
dbc::check(index == $levels.size() - 1, "Level index is not the same as $levels.size() - 1, off by one error");
return index;

@ -1,10 +1,27 @@
#include "lights.hpp"
#include "constants.hpp"
#include "textures.hpp"
#include <vector>
using std::vector;
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) {
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) {
@ -44,7 +61,7 @@ namespace lighting {
}
void LightRender::reset_light() {
matrix::assign($lightmap, lighting::MIN);
$lightmap = $ambient;
}
void LightRender::clear_light_target(const Point &at) {

@ -19,14 +19,10 @@ namespace lighting {
size_t $width;
size_t $height;
Matrix $lightmap;
Matrix $ambient;
Pathing $paths;
LightRender(size_t width, size_t height) :
$width(width),
$height(height),
$lightmap(height, matrix::Row(width, 0)),
$paths(width, height)
{}
LightRender(Matrix& walls);
void reset_light();
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 source2{4,3};
LightRender lr(map.width(), map.height());
LightRender lr(map.walls());
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() {
Config assets("assets/tiles.json");
auto &tiles = assets.json();
TMGR.surfaces.resize(tiles.size());
TMGR.ceilings.resize(tiles.size());
TMGR.map_tile_set.resize(tiles.size());
resize_shit(tiles.size());
for(auto &el : tiles.items()) {
auto &config = el.value();
@ -43,12 +48,11 @@ namespace textures {
size_t surface_i = config["id"];
if(surface_i >= tiles.size()) {
TMGR.surfaces.resize(surface_i + 1);
TMGR.ceilings.resize(surface_i + 1);
TMGR.map_tile_set.resize(surface_i + 1);
resize_shit(surface_i + 1);
}
TMGR.map_tile_set[surface_i] = config["display"];
TMGR.ambient_light[surface_i] = config["light"];
TMGR.surfaces[surface_i] = load_image(texture_fname);
// NOTE: ceilings defaults to 0 which is floor texture so only need to update
@ -93,6 +97,10 @@ namespace textures {
return texture;
}
std::vector<int>& get_ambient_light() {
return TMGR.ambient_light;
}
std::vector<wchar_t>& get_map_tile_set() {
return TMGR.map_tile_set;
}

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

Loading…
Cancel
Save