Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Nov 24, 2023
1 parent 5eab35b commit ea07201
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
18 changes: 9 additions & 9 deletions src/framework/graphics/drawpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DrawPool* DrawPool::create(const DrawPoolType type)
return pool;
}

void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::DrawMethod& method,
void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::DrawMethod&& method,
DrawMode drawMode, const DrawConductor& conductor, const CoordsBufferPtr& coordsBuffer)
{
updateHash(method, texture, color);
Expand All @@ -64,8 +64,8 @@ void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::Draw
if (m_alwaysGroupDrawings || conductor.agroup) {
auto& coords = m_coords.try_emplace(m_state.hash, nullptr).first->second;
if (!coords) {
auto state = getState(method, texture, color);
coords = m_objects[m_depthLevel][order].emplace_back(state).coords.get();
auto state = getState(texture, color);
coords = m_objects[m_depthLevel][order].emplace_back(std::move(state)).coords.get();
}

if (coordsBuffer)
Expand All @@ -78,9 +78,9 @@ void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::Draw
auto& list = m_objects[m_depthLevel][order];
if (!list.empty()) {
auto& prevObj = list.back();
if (prevObj.state.hash == m_state.hash) {
if (prevObj.state == m_state) {
if (!prevObj.coords)
prevObj.addMethod(method);
prevObj.addMethod(std::move(method));
else if (coordsBuffer)
prevObj.coords->append(coordsBuffer.get());
else
Expand All @@ -91,11 +91,11 @@ void DrawPool::add(const Color& color, const TexturePtr& texture, DrawPool::Draw
}

if (addNewObj) {
auto state = getState(method, texture, color);
auto state = getState(texture, color);
if (coordsBuffer) {
list.emplace_back(state).coords->append(coordsBuffer.get());
list.emplace_back(std::move(state)).coords->append(coordsBuffer.get());
} else
list.emplace_back(drawMode, state, method);
list.emplace_back(drawMode, std::move(state), std::move(method));
}
}

Expand Down Expand Up @@ -189,7 +189,7 @@ void DrawPool::updateHash(const DrawPool::DrawMethod& method, const TexturePtr&
}
}

DrawPool::PoolState DrawPool::getState(const DrawPool::DrawMethod& method, const TexturePtr& texture, const Color& color)
DrawPool::PoolState DrawPool::getState(const TexturePtr& texture, const Color& color)
{
return PoolState{
std::move(m_state.transformMatrix), m_state.opacity,
Expand Down
10 changes: 5 additions & 5 deletions src/framework/graphics/drawpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ class DrawPool
struct DrawObject
{
DrawObject(std::function<void()> action) : action(std::move(action)) {}
DrawObject(PoolState& state) : state(std::move(state)), coords(std::make_unique<CoordsBuffer>()) {}
DrawObject(const DrawMode drawMode, PoolState& state, DrawMethod& method) :
DrawObject(PoolState&& state) : state(std::move(state)), coords(std::make_unique<CoordsBuffer>()) {}
DrawObject(const DrawMode drawMode, PoolState&& state, DrawMethod&& method) :
drawMode(drawMode), state(std::move(state)) { methods.emplace_back(std::move(method)); }

void addMethod(DrawMethod& method)
void addMethod(DrawMethod&& method)
{
drawMode = DrawMode::TRIANGLES;
methods.emplace_back(std::move(method));
Expand Down Expand Up @@ -181,7 +181,7 @@ class DrawPool
STATE_BLEND_EQUATION = 1 << 4,
};

void add(const Color& color, const TexturePtr& texture, DrawPool::DrawMethod& method,
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 All @@ -192,7 +192,7 @@ class DrawPool
inline void setFPS(uint16_t fps) { m_refreshDelay = fps; }

void updateHash(const DrawPool::DrawMethod& method, const TexturePtr& texture, const Color& color);
PoolState getState(const DrawPool::DrawMethod& method, const TexturePtr& texture, const Color& color);
PoolState getState(const TexturePtr& texture, const Color& color);

float getOpacity() const { return m_state.opacity; }
Rect getClipRect() { return m_state.clipRect; }
Expand Down
36 changes: 14 additions & 22 deletions src/framework/graphics/drawpoolmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,75 +115,67 @@ void DrawPoolManager::drawObject(const DrawPool::DrawObject& obj)

void DrawPoolManager::addTexturedCoordsBuffer(const TexturePtr& texture, const CoordsBufferPtr& coords, const Color& color) const
{
DrawPool::DrawMethod method;
getCurrentPool()->add(color, texture, method, DrawMode::TRIANGLE_STRIP, DEFAULT_DRAW_CONDUCTOR, coords);
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{}, DrawMode::TRIANGLE_STRIP, DEFAULT_DRAW_CONDUCTOR, coords);
}

void DrawPoolManager::addTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color, const DrawConductor& condutor) const
{
if (dest.isEmpty() || src.isEmpty())
return;

DrawPool::DrawMethod method{
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{
.type = DrawPool::DrawMethodType::RECT,
.dest = dest, .src = src
};

getCurrentPool()->add(color, texture, method, DrawMode::TRIANGLE_STRIP, condutor);
}, DrawMode::TRIANGLE_STRIP, condutor);
}

void DrawPoolManager::addUpsideDownTexturedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color) const
{
if (dest.isEmpty() || src.isEmpty())
return;

DrawPool::DrawMethod method{ DrawPool::DrawMethodType::UPSIDEDOWN_RECT, dest, src };

getCurrentPool()->add(color, texture, method, DrawMode::TRIANGLE_STRIP);
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{ DrawPool::DrawMethodType::UPSIDEDOWN_RECT, dest, src }, DrawMode::TRIANGLE_STRIP);
}

void DrawPoolManager::addTexturedRepeatedRect(const Rect& dest, const TexturePtr& texture, const Rect& src, const Color& color) const
{
if (dest.isEmpty() || src.isEmpty())
return;

DrawPool::DrawMethod method{ DrawPool::DrawMethodType::REPEATED_RECT, dest, src };

getCurrentPool()->add(color, texture, method);
getCurrentPool()->add(color, texture, DrawPool::DrawMethod{ DrawPool::DrawMethodType::REPEATED_RECT, dest, src });
}

void DrawPoolManager::addFilledRect(const Rect& dest, const Color& color, const DrawConductor& condutor) const
{
if (dest.isEmpty())
return;

DrawPool::DrawMethod method{ DrawPool::DrawMethodType::RECT, dest };

getCurrentPool()->add(color, nullptr, method, DrawMode::TRIANGLES, condutor);
getCurrentPool()->add(color, nullptr, DrawPool::DrawMethod{ DrawPool::DrawMethodType::RECT, dest }, DrawMode::TRIANGLES, condutor);
}

void DrawPoolManager::addFilledTriangle(const Point& a, const Point& b, const Point& c, const Color& color) const
{
if (a == b || a == c || b == c)
return;

DrawPool::DrawMethod method{ .type = DrawPool::DrawMethodType::TRIANGLE, .a = a, .b = b, .c = c };

getCurrentPool()->add(color, nullptr, method);
getCurrentPool()->add(color, nullptr, DrawPool::DrawMethod{
.type = DrawPool::DrawMethodType::TRIANGLE,
.a = a,
.b = b,
.c = c
});
}

void DrawPoolManager::addBoundingRect(const Rect& dest, const Color& color, uint16_t innerLineWidth) const
{
if (dest.isEmpty() || innerLineWidth == 0)
return;

DrawPool::DrawMethod method{
getCurrentPool()->add(color, nullptr, DrawPool::DrawMethod{
.type = DrawPool::DrawMethodType::BOUNDING_RECT,
.dest = dest,
.intValue = innerLineWidth
};

getCurrentPool()->add(color, nullptr, method);
});
}

void DrawPoolManager::preDraw(const DrawPoolType type, const std::function<void()>& f, const Rect& dest, const Rect& src, const Color& colorClear)
Expand Down

0 comments on commit ea07201

Please sign in to comment.