@@ -24,7 +24,7 @@ private: | |||
static constexpr float JUMP_VEL = 11; | |||
static constexpr float DOWN_FORCE = 20 * MASS; | |||
static constexpr Swan::Vec2 SIZE = Swan::Vec2(0.6, 1.9); | |||
static constexpr int LIGHT_LEVEL = 30; | |||
static constexpr float LIGHT_LEVEL = 30/255.0; | |||
enum class State { | |||
IDLE, |
@@ -48,7 +48,7 @@ public: | |||
.name = "torch", | |||
.image = "core/tile/torch", | |||
.is_solid = false, | |||
.light_level = 80, | |||
.light_level = 80/255.0, | |||
}); | |||
registerItem({ |
@@ -51,7 +51,7 @@ public: | |||
private: | |||
static constexpr int LIGHT_CUTOFF_DIST = 64; | |||
static constexpr float LIGHT_CUTOFF = 1; | |||
static constexpr float LIGHT_CUTOFF = 0.25/255.0; | |||
struct Event { | |||
enum class Tag { |
@@ -19,7 +19,7 @@ public: | |||
std::string name; | |||
std::string image; | |||
bool is_solid = true; | |||
uint8_t light_level = 0; | |||
float light_level = 0; | |||
std::optional<std::string> dropped_item = std::nullopt; | |||
}; | |||
@@ -31,7 +31,7 @@ public: | |||
const std::string name_; | |||
const ImageResource &image_; | |||
const bool is_solid_; | |||
const uint8_t light_level_; | |||
const float light_level_; | |||
const std::optional<std::string> dropped_item_; | |||
static std::unique_ptr<Tile> createInvalid(const ResourceManager &ctx); |
@@ -76,8 +76,8 @@ public: | |||
void debugBox(TilePos pos); | |||
void addLight(TilePos pos, uint8_t level); | |||
void removeLight(TilePos pos, uint8_t level); | |||
void addLight(TilePos pos, float level); | |||
void removeLight(TilePos pos, float level); | |||
// LightingCallback implementation | |||
void onLightChunkUpdated(const LightChunk &chunk, Vec2i pos) final; |
@@ -94,9 +94,7 @@ void Chunk::renderLight(const Context &ctx, SDL_Renderer *rnd) { | |||
RenderDrawColor color(rnd, 0, 0, 0, 0); | |||
for (int y = 0; y < CHUNK_HEIGHT; ++y) { | |||
for (int x = 0; x < CHUNK_WIDTH; ++x) { | |||
float level = (float)getLightLevel({ x, y }) / 255; | |||
float l = 1.055 * pow(level, 1/2.4) - 0.055; | |||
int b = std::clamp((int)(l * 1.5 * 255), 0, 255); | |||
int b = getLightLevel({ x, y }); | |||
color.change(0, 0, 0, 255 - b); | |||
SDL_Rect rect{ x, y, 1, 1 }; | |||
SDL_RenderFillRect(rnd, &rect); |
@@ -24,8 +24,19 @@ static Vec2i lightRelPos(TilePos pos) { | |||
CHUNK_HEIGHT)) % CHUNK_HEIGHT); | |||
} | |||
static uint8_t linToSRGB(float lin) { | |||
float s; | |||
if (lin <= 0.0031308) { | |||
s = lin / 12.92; | |||
} else { | |||
s = 1.055 * std::pow(lin, 1/2.4) - 0.055; | |||
} | |||
return std::clamp((int)(s * 255), 0, 255); | |||
} | |||
static float attenuate(float dist, float squareDist) { | |||
float a = 1 / (1 + 1.0 * dist + 0.5 * squareDist); | |||
float a = 1 / (1 + 1.0 * dist + 0.1 * squareDist); | |||
return a > 1 ? 1 : a; | |||
} | |||
@@ -375,8 +386,8 @@ void LightServer::processChunkSmoothing(LightChunk &chunk, ChunkPos cpos) { | |||
if (b > light) { light += b; count += 1; } | |||
if (l > light) { light += l; count += 1; } | |||
if (r > light) { light += r; count += 1; } | |||
light = std::min(light / count, 255.0f); | |||
chunk.light_levels[y * CHUNK_WIDTH + x] = light; | |||
light /= count; | |||
chunk.light_levels[y * CHUNK_WIDTH + x] = linToSRGB(light); | |||
} | |||
} | |||
}; |
@@ -294,12 +294,12 @@ void WorldPlane::debugBox(TilePos pos) { | |||
debug_boxes_.push_back(pos); | |||
} | |||
void WorldPlane::addLight(TilePos pos, uint8_t level) { | |||
void WorldPlane::addLight(TilePos pos, float level) { | |||
getChunk(chunkPos(pos)); | |||
lighting_->onLightAdded(pos, level); | |||
} | |||
void WorldPlane::removeLight(TilePos pos, uint8_t level) { | |||
void WorldPlane::removeLight(TilePos pos, float level) { | |||
getChunk(chunkPos(pos)); | |||
lighting_->onLightRemoved(pos, level); | |||
} |