Browse Source

LightingThread -> LightServer

fix/style
Martin Dørum 3 years ago
parent
commit
569df98e5c

+ 1
- 1
libswan/CMakeLists.txt View File

@@ -11,7 +11,7 @@ add_library(libswan SHARED
src/gfxutil.cc
src/Item.cc
src/ItemStack.cc
src/LightingThread.cc
src/LightServer.cc
src/Mod.cc
src/OS.cc
src/Resource.cc

libswan/include/swan/LightingThread.h → libswan/include/swan/LightServer.h View File

@@ -30,15 +30,15 @@ struct LightChunk {
bool was_updated = false;
};

class LightingCallback {
class LightCallback {
public:
virtual void onLightChunkUpdated(const LightChunk &chunk, ChunkPos pos) = 0;
};

class LightingThread {
class LightServer {
public:
LightingThread(LightingCallback &cb);
~LightingThread();
LightServer(LightCallback &cb);
~LightServer();

void onSolidBlockAdded(TilePos pos);
void onSolidBlockRemoved(TilePos pos);
@@ -70,7 +70,7 @@ private:
void processEvent(const Event &event, std::vector<NewLightChunk> &newChunks);
void run();

LightingCallback &cb_;
LightCallback &cb_;
bool running_ = true;
std::map<std::pair<int, int>, LightChunk> chunks_;
std::set<std::pair<int, int>> updated_chunks_;
@@ -85,31 +85,31 @@ private:
std::mutex mut_;
};

inline void LightingThread::onSolidBlockAdded(TilePos pos) {
inline void LightServer::onSolidBlockAdded(TilePos pos) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::BLOCK_ADDED, pos, { 0 } });
cond_.notify_one();
}

inline void LightingThread::onSolidBlockRemoved(TilePos pos) {
inline void LightServer::onSolidBlockRemoved(TilePos pos) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::BLOCK_REMOVED, pos, { 0 } });
cond_.notify_one();
}

inline void LightingThread::onLightAdded(TilePos pos, uint8_t level) {
inline void LightServer::onLightAdded(TilePos pos, uint8_t level) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::LIGHT_ADDED, pos, { .num = level } });
cond_.notify_one();
}

inline void LightingThread::onLightRemoved(TilePos pos, uint8_t level) {
inline void LightServer::onLightRemoved(TilePos pos, uint8_t level) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::LIGHT_REMOVED, pos, { .num = level } });
cond_.notify_one();
}

inline void LightingThread::onChunkAdded(Vec2i pos, NewLightChunk &&chunk) {
inline void LightServer::onChunkAdded(Vec2i pos, NewLightChunk &&chunk) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::CHUNK_ADDED, pos,
{ .num = new_chunk_buffers_[buffer_].size() } });
@@ -117,7 +117,7 @@ inline void LightingThread::onChunkAdded(Vec2i pos, NewLightChunk &&chunk) {
cond_.notify_one();
}

inline void LightingThread::onChunkRemoved(Vec2i pos) {
inline void LightServer::onChunkRemoved(Vec2i pos) {
std::lock_guard<std::mutex> lock(mut_);
buffers_[buffer_].push_back({ Event::Tag::CHUNK_ADDED, pos, { 0 } });
cond_.notify_one();

+ 3
- 3
libswan/include/swan/WorldPlane.h View File

@@ -16,14 +16,14 @@
#include "WorldGen.h"
#include "Entity.h"
#include "Collection.h"
#include "LightingThread.h"
#include "LightServer.h"

namespace Swan {

class World;
class Game;

class WorldPlane final: NonCopyable, public LightingCallback {
class WorldPlane final: NonCopyable, public LightCallback {
public:
using ID = uint16_t;

@@ -86,7 +86,7 @@ private:
void addLight(TilePos pos, uint8_t level);
void removeLight(TilePos pos, uint8_t level);

std::unique_ptr<LightingThread> lighting_;
std::unique_ptr<LightServer> lighting_;

std::map<std::pair<int, int>, Chunk> chunks_;
std::vector<Chunk *> active_chunks_;

libswan/src/LightingThread.cc → libswan/src/LightServer.cc View File

@@ -1,4 +1,4 @@
#include "LightingThread.h"
#include "LightServer.h"

#include "log.h"

@@ -22,16 +22,16 @@ static Vec2i lightRelPos(TilePos pos) {
CHUNK_HEIGHT)) % CHUNK_HEIGHT);
}

LightingThread::LightingThread(LightingCallback &cb):
cb_(cb), thread_(&LightingThread::run, this) {}
LightServer::LightServer(LightCallback &cb):
cb_(cb), thread_(&LightServer::run, this) {}

LightingThread::~LightingThread() {
LightServer::~LightServer() {
running_ = false;
cond_.notify_one();
thread_.join();
}

bool LightingThread::tileIsSolid(TilePos pos) {
bool LightServer::tileIsSolid(TilePos pos) {
ChunkPos cpos = lightChunkPos(pos);
LightChunk *chunk = getChunk(cpos);
if (chunk == nullptr) {
@@ -42,7 +42,7 @@ bool LightingThread::tileIsSolid(TilePos pos) {
return chunk->blocks[rpos.y * CHUNK_WIDTH + rpos.x];
}

LightChunk *LightingThread::getChunk(ChunkPos cpos) {
LightChunk *LightServer::getChunk(ChunkPos cpos) {
if (cached_chunk_ && cached_chunk_pos_ == cpos) {
return cached_chunk_;
}
@@ -57,7 +57,7 @@ LightChunk *LightingThread::getChunk(ChunkPos cpos) {
return nullptr;
}

void LightingThread::processEvent(const Event &evt, std::vector<NewLightChunk> &newChunks) {
void LightServer::processEvent(const Event &evt, std::vector<NewLightChunk> &newChunks) {
info << "event " << (int)evt.tag;

// TODO: Only mark chunks within some sphere
@@ -114,7 +114,7 @@ void LightingThread::processEvent(const Event &evt, std::vector<NewLightChunk> &
}
}

int LightingThread::recalcTile(
int LightServer::recalcTile(
LightChunk &chunk, ChunkPos cpos, Vec2i rpos, TilePos base,
std::vector<std::pair<TilePos, uint8_t>> &lights) {
TilePos pos = rpos + base;
@@ -186,7 +186,7 @@ int LightingThread::recalcTile(
return acc;
}

void LightingThread::processUpdatedChunk(LightChunk &chunk, ChunkPos cpos) {
void LightServer::processUpdatedChunk(LightChunk &chunk, ChunkPos cpos) {
auto start = std::chrono::steady_clock::now();
TilePos base = cpos * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT);
std::vector<std::pair<TilePos, uint8_t>> lights;
@@ -227,7 +227,7 @@ void LightingThread::processUpdatedChunk(LightChunk &chunk, ChunkPos cpos) {
cb_.onLightChunkUpdated(chunk, cpos);
}

void LightingThread::run() {
void LightServer::run() {
std::unique_lock<std::mutex> lock(mut_, std::defer_lock);
while (running_) {
lock.lock();

+ 1
- 1
libswan/src/WorldPlane.cc View File

@@ -46,7 +46,7 @@ WorldPlane::WorldPlane(
ID id, World *world, std::unique_ptr<WorldGen> gen,
std::vector<std::unique_ptr<EntityCollection>> &&colls):
id_(id), world_(world), gen_(std::move(gen)),
lighting_(std::make_unique<LightingThread>(*this)),
lighting_(std::make_unique<LightServer>(*this)),
ent_colls_(std::move(colls)) {

for (auto &coll: ent_colls_) {

+ 3
- 3
src/lighting-test.cc View File

@@ -1,9 +1,9 @@
#include <swan/LightingThread.h>
#include <swan/LightServer.h>
#include <swan/log.h>
#include <png++/png.hpp>
#include <chrono>

class CB final: public Swan::LightingCallback {
class CB final: public Swan::LightCallback {
public:
void onLightChunkUpdated(const Swan::LightChunk &chunk, Swan::Vec2i pos) final {
Swan::info << "light chunk at " << pos;
@@ -20,7 +20,7 @@ public:

int main() {
CB cb;
Swan::LightingThread lt(cb);
Swan::LightServer lt(cb);

Swan::NewLightChunk nc;
auto set = [&](int x, int y) { nc.blocks[y * Swan::CHUNK_WIDTH + x] = true; };

Loading…
Cancel
Save