Skip to content

Commit

Permalink
Canvas: Add missing doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kullingk committed Oct 22, 2024
1 parent 8e08167 commit 617083b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 77 deletions.
4 changes: 2 additions & 2 deletions src/Engine/Common/EventBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool EventBus::create() {

mCreated = true;
QueueEntryAlloctor.reserve(1000);

return mCreated;
}

Expand All @@ -58,7 +58,7 @@ bool EventBus::destroy() {
if (!mCreated) {
return false;
}

QueueEntryAlloctor.clear();
mCreated = false;

Expand Down
138 changes: 69 additions & 69 deletions src/Engine/RenderBackend/2D/CanvasRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,67 @@ inline void clip(const Rect2i &resolution, i32 x, i32 y, i32 &x_out, i32 &y_out)
}
}

static void createRectVertices(DrawCmd *drawCmd, const Color4 &penColor, const Rect2i &resolution, i32 x, i32 y, i32 w, i32 h, i32 layer) {
i32 x_clipped, y_clipped;
f32 x_model, y_model;

drawCmd->PrimType = PrimitiveType::TriangleList;
drawCmd->NumVertices = 6;
drawCmd->Vertices = new RenderVert[drawCmd->NumVertices];

clip(resolution, x, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[0].color0 = penColor.toVec4();
drawCmd->Vertices[0].position.x = x_model;
drawCmd->Vertices[0].position.y = y_model;
drawCmd->Vertices[0].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[1].color0 = penColor.toVec4();
drawCmd->Vertices[1].position.x = x_model;
drawCmd->Vertices[1].position.y = y_model;
drawCmd->Vertices[1].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[2].color0 = penColor.toVec4();
drawCmd->Vertices[2].position.x = x_model;
drawCmd->Vertices[2].position.y = y_model;
drawCmd->Vertices[2].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[3].color0 = penColor.toVec4();
drawCmd->Vertices[3].position.x = x_model;
drawCmd->Vertices[3].position.y = y_model;
drawCmd->Vertices[3].position.z = static_cast<f32>(-layer);

clip(resolution, x, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[4].color0 = penColor.toVec4();
drawCmd->Vertices[4].position.x = x_model;
drawCmd->Vertices[4].position.y = y_model;
drawCmd->Vertices[4].position.z = static_cast<f32>(-layer);

clip(resolution, x, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[5].color0 = penColor.toVec4();
drawCmd->Vertices[5].position.x = x_model;
drawCmd->Vertices[5].position.y = y_model;
drawCmd->Vertices[5].position.z = static_cast<f32>(-layer);

drawCmd->NumIndices = 6;
drawCmd->Indices = new ui16[drawCmd->NumIndices];
drawCmd->Indices[0] = 0;
drawCmd->Indices[1] = 2;
drawCmd->Indices[2] = 1;

drawCmd->Indices[3] = 3;
drawCmd->Indices[4] = 5;
drawCmd->Indices[5] = 4;
}

static TPoolAllocator<DrawCmd> sAllocator;

DrawCmd *alloc() {
Expand All @@ -112,12 +173,12 @@ void dealloc(DrawCmd *cmd) {
}

CanvasRenderer::CanvasRenderer(i32 numLayers, i32 x, i32 y, i32 w, i32 h) :
mDirty(true),
mPenColor(1, 1, 1, 0),
mResolution(),
mActiveLayer(0),
mNumLayers(numLayers),
mFont(nullptr),
mDirty(true),
mPenColor(1, 1, 1, 0),
mResolution(),
mActiveLayer(0),
mNumLayers(numLayers),
mFont(nullptr),
mMesh(nullptr),
mFont2MeshMap() {
setResolution(x, y, w, h);
Expand Down Expand Up @@ -361,67 +422,6 @@ void CanvasRenderer::drawTriangle(const Point2Di &p1, const Point2Di &p2, const
drawTriangle(p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y, filled);
}

static void createRectVertices(DrawCmd *drawCmd, const Color4 &penColor, const Rect2i &resolution, i32 x, i32 y, i32 w, i32 h, i32 layer) {
i32 x_clipped, y_clipped;
f32 x_model, y_model;

drawCmd->PrimType = PrimitiveType::TriangleList;
drawCmd->NumVertices = 6;
drawCmd->Vertices = new RenderVert[drawCmd->NumVertices];

clip(resolution, x, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[0].color0 = penColor.toVec4();
drawCmd->Vertices[0].position.x = x_model;
drawCmd->Vertices[0].position.y = y_model;
drawCmd->Vertices[0].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[1].color0 = penColor.toVec4();
drawCmd->Vertices[1].position.x = x_model;
drawCmd->Vertices[1].position.y = y_model;
drawCmd->Vertices[1].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[2].color0 = penColor.toVec4();
drawCmd->Vertices[2].position.x = x_model;
drawCmd->Vertices[2].position.y = y_model;
drawCmd->Vertices[2].position.z = static_cast<f32>(-layer);

clip(resolution, x+w, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[3].color0 = penColor.toVec4();
drawCmd->Vertices[3].position.x = x_model;
drawCmd->Vertices[3].position.y = y_model;
drawCmd->Vertices[3].position.z = static_cast<f32>(-layer);

clip(resolution, x, y+h, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[4].color0 = penColor.toVec4();
drawCmd->Vertices[4].position.x = x_model;
drawCmd->Vertices[4].position.y = y_model;
drawCmd->Vertices[4].position.z = static_cast<f32>(-layer);

clip(resolution, x, y, x_clipped, y_clipped);
mapCoordinates(resolution, x_clipped, y_clipped, x_model, y_model);
drawCmd->Vertices[5].color0 = penColor.toVec4();
drawCmd->Vertices[5].position.x = x_model;
drawCmd->Vertices[5].position.y = y_model;
drawCmd->Vertices[5].position.z = static_cast<f32>(-layer);

drawCmd->NumIndices = 6;
drawCmd->Indices = new ui16[drawCmd->NumIndices];
drawCmd->Indices[0] = 0;
drawCmd->Indices[1] = 2;
drawCmd->Indices[2] = 1;

drawCmd->Indices[3] = 3;
drawCmd->Indices[4] = 5;
drawCmd->Indices[5] = 4;
}

void CanvasRenderer::drawRect(i32 x, i32 y, i32 w, i32 h, bool filled) {
setDirty();
DrawCmd *drawCmd = nullptr;
Expand All @@ -444,7 +444,7 @@ void CanvasRenderer::drawRect(i32 x, i32 y, i32 w, i32 h, bool filled) {
drawCmd = alloc();
createRectVertices(drawCmd, mPenColor, mResolution, x, y, thickness, h, mActiveLayer);
mDrawCmdArray.add(drawCmd);

drawCmd = alloc();
createRectVertices(drawCmd, mPenColor, mResolution, x+w, y, thickness, h, mActiveLayer);
mDrawCmdArray.add(drawCmd);
Expand Down Expand Up @@ -486,7 +486,7 @@ void CanvasRenderer::drawText(i32 x, i32 y, const String &text) {
drawCmd->Vertices[posIndex].position.y = positions[posIndex].y;
drawCmd->Vertices[posIndex].position.z = static_cast<f32>(-mActiveLayer);
}

for (size_t idxIndex = 0; idxIndex < numIndices; ++idxIndex) {
drawCmd->Indices[idxIndex] = indices[idxIndex];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/RenderBackend/2D/RenderPass2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ inline RenderPass2D::~RenderPass2D() {}

inline RenderPass *RenderPass2D::build(guid id) {
RenderPass *pass = new RenderPass(id, nullptr);

return pass;
}

Expand Down
26 changes: 21 additions & 5 deletions src/Engine/RenderBackend/Mesh/MeshUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,46 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace OSRE {
namespace RenderBackend {

///-----------------------------------------------------------------
/// @class classname
/// @class MeshUtilities
///
/// @brief
/// @brief This class provides common utilities to generate different
/// kind of mesh topology and spatial data.
///-----------------------------------------------------------------
class MeshUtilities {
public:
/// @brief Named constants for the number of vertices and indices for a quad.
static constexpr size_t NumQuadVert = 4;
static constexpr ui32 NumQuadIndices = 6;

/// @brief Will calculate the numbr of vertices needed for the given string glyphes.
/// @param text The text for checking.
/// @return The number of vertives for the glyphes.
static size_t getNumTextVerts( const String &text ) {
const size_t NumTextVerts = NumQuadVert * text.size();
return NumTextVerts;
}


/// @brief Will calculate the number of indices needed for the given text glyphes.
/// @param text The text for checking.
/// @return The number of indices
static size_t getNumTextIndices(const String &text) {
const size_t numIndices = NumQuadIndices * text.size();
return numIndices;
}

static void generateTextBoxVerticesAndIndices(f32 x, f32 y, f32 textSize, const String &text, Vec3Array &positions, Vec3Array &colors, Vec2Array &tex0, ui16 **textIndices) {
/// @brief Will generate the vertices and indices for the text box and all glyphes in it.
/// @param x The upper left coordinate component in x.
/// @param y The upper left coordinate component in y.
/// @param textSize The requested text size.
/// @param text The text itself to render.
/// @param positions The positios for all glyphes.
/// @param colors The vertex colors for all glyphes.
/// @param tex0 The diffuse texture coordinates for all glyphes.
/// @param textIndices The index array for all glyphes.
static void generateTextBoxVerticesAndIndices(f32 x, f32 y, f32 textSize, const String &text,
Vec3Array &positions, Vec3Array &colors, Vec2Array &tex0, ui16 **textIndices) {
osre_assert(nullptr != textIndices);

using namespace ::OSRE::Common;
Expand Down

0 comments on commit 617083b

Please sign in to comment.