Skip to content

Commit

Permalink
cleanup: drawpool / lighting system
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah authored Mar 2, 2023
1 parent 1b4a1d1 commit bef0e11
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 169 deletions.
25 changes: 14 additions & 11 deletions src/client/lightview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,29 @@
*/

#include "lightview.h"
#include <framework/graphics/drawpoolmanager.h>
#include "map.h"
#include "mapview.h"
#include "spritemanager.h"

LightView::LightView() : m_pool(g_drawPool.get<DrawPool>(DrawPoolType::LIGHT)) {}
#include <framework/core/eventdispatcher.h>
#include <framework/graphics/drawpoolmanager.h>

LightView::LightView(const Size& size, const uint16_t tileSize) : m_pool(g_drawPool.get(DrawPoolType::LIGHT)) {
resize(size, tileSize);
g_mainDispatcher.addEvent([&] {
m_texture = std::make_shared<Texture>(m_mapSize);
m_texture->setSmooth(true);
});
}

void LightView::resize(const Size& size, const uint16_t tileSize) {
m_lightTexture = nullptr;
m_mapSize = size;
m_tileSize = tileSize;
m_tiles.resize(size.area());
if (m_pixels.size() < 4u * m_mapSize.area())
m_pixels.resize(m_mapSize.area() * 4);
if (m_texture)
m_texture->setupSize(m_mapSize);
}

void LightView::addLightSource(const Point& pos, const Light& light, float brightness)
Expand Down Expand Up @@ -77,15 +86,9 @@ void LightView::draw(const Rect& dest, const Rect& src)
updatePixels();

g_drawPool.addAction([&] {
if (!m_lightTexture) {
m_lightTexture = std::make_shared<Texture>(m_mapSize);
m_lightTexture->setSmooth(true);
}

m_lightTexture->updatePixels(m_pixels.data());

m_texture->updatePixels(m_pixels.data());
g_painter->resetColor();
g_painter->setTexture(m_lightTexture.get());
g_painter->setTexture(m_texture.get());
g_painter->setCompositionMode(CompositionMode::MULTIPLY);
g_painter->drawCoords(m_coords);
});
Expand Down
4 changes: 2 additions & 2 deletions src/client/lightview.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class LightView : public LuaObject
{
public:
LightView();
LightView(const Size& size, const uint16_t tileSize);

void resize(const Size& size, uint16_t tileSize);
void draw(const Rect& dest, const Rect& src);
Expand Down Expand Up @@ -70,7 +70,7 @@ class LightView : public LuaObject
Color m_globalLightColor{ Color::white };

CoordsBuffer m_coords;
TexturePtr m_lightTexture;
TexturePtr m_texture;

std::vector<size_t> m_tiles;
std::vector<uint8_t> m_pixels;
Expand Down
12 changes: 5 additions & 7 deletions src/client/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

#include <framework/platform/platformwindow.h>

MapView::MapView() : m_pool(g_drawPool.get<DrawPoolFramed>(DrawPoolType::MAP))
MapView::MapView() : m_pool(g_drawPool.get(DrawPoolType::MAP))
{
m_pool->onBeforeDraw([this] {
float fadeOpacity = 1.f;
Expand Down Expand Up @@ -404,7 +404,7 @@ void MapView::updateGeometry(const Size& visibleDimension)

if (m_lightView) m_lightView->resize(m_drawDimension, tileSize);
g_mainDispatcher.addEvent([=, this]() {
m_pool->resize(bufferSize);
m_pool->getFrameBuffer()->resize(bufferSize);
});

const uint8_t left = std::min<uint8_t>(g_map.getAwareRange().left, (m_drawDimension.width() / 2) - 1);
Expand Down Expand Up @@ -547,7 +547,7 @@ void MapView::setAntiAliasingMode(const AntialiasingMode mode)
{
m_antiAliasingMode = mode;

g_drawPool.get<DrawPoolFramed>(DrawPoolType::MAP)
g_drawPool.get(DrawPoolType::MAP)->getFrameBuffer()
->setSmooth(mode != ANTIALIASING_DISABLED);

updateGeometry(m_visibleDimension);
Expand Down Expand Up @@ -768,16 +768,14 @@ void MapView::setShader(const std::string_view name, float fadein, float fadeout

void MapView::setDrawLights(bool enable)
{
if (auto* pool = g_drawPool.get<DrawPoolFramed>(DrawPoolType::LIGHT))
if (auto* pool = g_drawPool.get(DrawPoolType::LIGHT))
pool->setEnable(enable);

if (enable) {
if (m_lightView)
return;

m_lightView = std::make_shared<LightView>();
g_mainDispatcher.addEvent([this]
() { if (m_lightView) m_lightView->resize(m_drawDimension, m_tileSize); });
m_lightView = std::make_shared<LightView>(m_drawDimension, m_tileSize);

requestUpdateVisibleTiles();
} else m_lightView = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/client/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,5 @@ class MapView : public LuaObject
TexturePtr m_crosshairTexture;

DrawConductor m_shadowConductor{ false, DrawOrder::FIFTH };
DrawPoolFramed* m_pool;
DrawPool* m_pool;
};
51 changes: 2 additions & 49 deletions src/client/spritemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@

SpriteManager g_sprites;

void SpriteManager::init() { generateLightTexture(); generateShadeTexture(); }
void SpriteManager::terminate()
{
unload();
m_shadeTexture = nullptr;
m_lightTexture = nullptr;
}
void SpriteManager::init() { }
void SpriteManager::terminate() { unload(); }

void SpriteManager::reload() {
if (g_app.isEncrypted())
Expand Down Expand Up @@ -246,45 +241,3 @@ ImagePtr SpriteManager::getSpriteImage(int id, const FileStreamPtr& file) {
return nullptr;
}
}

void SpriteManager::generateLightTexture()
{
constexpr float brightnessIntensity = 1.5f;

constexpr int bubbleRadius = 6;
constexpr int bubbleDiameter = bubbleRadius * 2.3;

const auto& image = std::make_shared<Image>(Size(bubbleDiameter));
for (int_fast16_t x = -1; ++x < bubbleDiameter;) {
for (int_fast16_t y = -1; ++y < bubbleDiameter;) {
const float radius = std::sqrt((bubbleRadius - x) * (bubbleRadius - x) + (bubbleRadius - y) * (bubbleRadius - y));
const float intensity = std::clamp<float>(((bubbleRadius - radius) / bubbleRadius) * brightnessIntensity, .01f, 1.0f);

// light intensity varies inversely with the square of the distance
const uint8_t colorByte = intensity * 0xff;

const uint8_t pixel[4] = { colorByte, colorByte, colorByte, 0xff };
image->setPixel(x, y, pixel);
}
}

m_lightTexture = std::make_shared<Texture>(image);
m_lightTexture->setSmooth(true);
}

void SpriteManager::generateShadeTexture()
{
constexpr uint16_t diameter = 4;

const auto& image = std::make_shared<Image>(Size(diameter));
for (int_fast16_t x = -1; ++x < diameter;) {
for (int_fast16_t y = -1; ++y < diameter;) {
const uint8_t alpha = x == 0 || y == 0 || x == diameter - 1 || y == diameter - 1 ? 0 : 0xff;
const uint8_t pixel[4] = { 0xff, 0xff, 0xff, alpha };
image->setPixel(x, y, pixel);
}
}

m_shadeTexture = std::make_shared<Texture>(image);
m_shadeTexture->setSmooth(true);
}
9 changes: 0 additions & 9 deletions src/client/spritemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class SpriteManager
ImagePtr getSpriteImage(int id);
bool isLoaded() { return m_loaded; }

const TexturePtr& getLightTexture() const { return m_lightTexture; }
const TexturePtr& getShadeTexture() const { return m_shadeTexture; }

private:
ImagePtr getSpriteImage(int id, const FileStreamPtr& file);

Expand All @@ -57,14 +54,8 @@ class SpriteManager
SPRITE_DATA_SIZE = SPRITE_SIZE * SPRITE_SIZE * 4
};

void generateLightTexture(),
generateShadeTexture();

std::string m_lastFileName;

TexturePtr m_lightTexture;
TexturePtr m_shadeTexture;

bool m_loaded{ false };
uint32_t m_signature{ 0 };
uint32_t m_spritesCount{ 0 };
Expand Down
12 changes: 7 additions & 5 deletions src/framework/core/graphicalapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void GraphicalApplication::run()

g_lua.callGlobalField("g_app", "onRun");

const auto& foreground = g_drawPool.get<DrawPool>(DrawPoolType::FOREGROUND);
const auto& txt = g_drawPool.get<DrawPool>(DrawPoolType::TEXT);
const auto& map = g_drawPool.get<DrawPool>(DrawPoolType::MAP);
const auto& foreground = g_drawPool.get(DrawPoolType::FOREGROUND);
const auto& txt = g_drawPool.get(DrawPoolType::TEXT);
const auto& map = g_drawPool.get(DrawPoolType::MAP);

std::condition_variable foreCondition, txtCondition;

Expand Down Expand Up @@ -254,7 +254,9 @@ void GraphicalApplication::resize(const Size& size)

g_mainDispatcher.addEvent([size] {
g_graphics.resize(size);
g_drawPool.get<DrawPoolFramed>(DrawPoolType::FOREGROUND)->resize(size);
auto* foreGround = g_drawPool.get(DrawPoolType::FOREGROUND);
foreGround->getFrameBuffer()->resize(size);
foreGround->repaint();
});
}

Expand All @@ -265,7 +267,7 @@ void GraphicalApplication::inputEvent(const InputEvent& event)
m_onInputEvent = false;
}

void GraphicalApplication::repaint() { g_drawPool.get<DrawPool>(DrawPoolType::FOREGROUND)->repaint(); }
void GraphicalApplication::repaint() { g_drawPool.get(DrawPoolType::FOREGROUND)->repaint(); }
bool GraphicalApplication::canDrawTexts() const { return m_drawText && (!g_map.getStaticTexts().empty() || !g_map.getAnimatedTexts().empty()); }

bool GraphicalApplication::isLoadingAsyncTexture() { return m_loadingAsyncTexture || g_game.isUsingProtobuf(); }
Expand Down
1 change: 0 additions & 1 deletion src/framework/graphics/declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class ParticleEffect;
class ParticleEffectType;
class SpriteSheet;
class DrawPool;
class DrawPoolFramed;
class DrawPoolManager;
class CoordsBuffer;

Expand Down
17 changes: 8 additions & 9 deletions src/framework/graphics/drawpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,23 @@ FPS60 = 1000 / 60;

DrawPool* DrawPool::create(const DrawPoolType type)
{
DrawPool* pool;
DrawPool* pool = new DrawPool;
if (type == DrawPoolType::MAP || type == DrawPoolType::FOREGROUND) {
pool = new DrawPoolFramed;
pool = new DrawPool;

const auto& frameBuffer = pool->toPoolFramed()->m_framebuffer;
frameBuffer->m_isScene = true;
pool->m_framebuffer = std::make_shared<FrameBuffer>();
pool->m_framebuffer->m_isScene = true;

if (type == DrawPoolType::MAP) {
frameBuffer->m_useAlphaWriting = false;
frameBuffer->disableBlend();
pool->m_framebuffer->m_useAlphaWriting = false;
pool->m_framebuffer->disableBlend();
} else if (type == DrawPoolType::FOREGROUND) {
pool->setFPS(FPS10);
} else if (type == DrawPoolType::LIGHT) {
pool->m_alwaysGroupDrawings = true;
frameBuffer->setCompositionMode(CompositionMode::LIGHT);
pool->m_framebuffer->setCompositionMode(CompositionMode::LIGHT);
}
} else {
pool = new DrawPool;
pool->m_alwaysGroupDrawings = true; // CREATURE_INFORMATION & TEXT
pool->disableUpdateHash();

Expand All @@ -61,7 +60,7 @@ DrawPool* DrawPool::create(const DrawPoolType type)
}

void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::DrawMethod& method,
DrawMode drawMode, const DrawConductor& conductor, const CoordsBufferPtr& coordsBuffer)
DrawMode drawMode, const DrawConductor& conductor, const CoordsBufferPtr& coordsBuffer)
{
auto state = getState(method, texture, color);

Expand Down
44 changes: 12 additions & 32 deletions src/framework/graphics/drawpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,21 @@ class DrawPool
bool isEnabled() const { return m_enabled; }
bool isType(DrawPoolType type) const { return m_type == type; }

bool isValid() const { return !m_framebuffer || m_framebuffer->isValid(); }
bool hasFrameBuffer() const { return m_framebuffer != nullptr; }
FrameBufferPtr getFrameBuffer() const { return m_framebuffer; }

bool canRepaint() { return canRepaint(false); }
void repaint() { m_status.first = 1; }

virtual bool isValid() const { return true; };

void optimize(int size);

void setScaleFactor(float scale) { m_scaleFactor = scale; }
inline float getScaleFactor() const { return m_scaleFactor; }

void onBeforeDraw(std::function<void()> f) { m_beforeDraw = std::move(f); }
void onAfterDraw(std::function<void()> f) { m_afterDraw = std::move(f); }

std::mutex& getMutex() { return m_mutex; }

protected:
Expand Down Expand Up @@ -151,6 +156,7 @@ class DrawPool
};

private:
static DrawPool* create(const DrawPoolType type);
static void addCoords(CoordsBuffer* buffer, const DrawPool::DrawMethod& method, DrawMode drawMode);

enum STATE_TYPE : uint32_t
Expand All @@ -162,8 +168,6 @@ class DrawPool
STATE_BLEND_EQUATION = 1 << 4,
};

static DrawPool* create(const DrawPoolType type);

void add(const Color& color, const TexturePtr& texture, DrawPool::DrawMethod& method,
DrawMode drawMode = DrawMode::TRIANGLES, const DrawConductor& conductor = DEFAULT_DRAW_CONDUCTOR,
const CoordsBufferPtr& coordsBuffer = nullptr);
Expand Down Expand Up @@ -211,9 +215,6 @@ class DrawPool
m_updateHash = false;
}

virtual bool hasFrameBuffer() const { return false; };
virtual DrawPoolFramed* toPoolFramed() { return nullptr; }

bool canRepaint(bool autoUpdateStatus);

bool m_enabled{ true };
Expand All @@ -240,35 +241,14 @@ class DrawPool

float m_scaleFactor{ 1.f };

std::mutex m_mutex;

friend DrawPoolManager;
};

class DrawPoolFramed : public DrawPool
{
public:
void onBeforeDraw(std::function<void()> f) { m_beforeDraw = std::move(f); }
void onAfterDraw(std::function<void()> f) { m_afterDraw = std::move(f); }
void setSmooth(bool enabled) const { m_framebuffer->setSmooth(enabled); }
void resize(const Size& size) { if (m_framebuffer->resize(size)) repaint(); }
Size getSize() const { return m_framebuffer->getSize(); }
bool isValid() const override { return m_framebuffer->isValid(); }

protected:
DrawPoolFramed() : m_framebuffer(std::make_shared<FrameBuffer>()) {};

friend DrawPoolManager;
friend DrawPool;

private:
bool hasFrameBuffer() const override { return m_framebuffer->isValid(); }
DrawPoolFramed* toPoolFramed() override { return this; }

FrameBufferPtr m_framebuffer;

std::function<void()> m_beforeDraw;
std::function<void()> m_afterDraw;

std::mutex m_mutex;

friend DrawPoolManager;
};

extern DrawPoolManager g_drawPool;
Loading

0 comments on commit bef0e11

Please sign in to comment.