Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::ImmediateDraw Class Reference

Immediate mode 2D drawing API. More...

#include <ImmediateDraw.hpp>

Public Member Functions

 ImmediateDraw (GfxDevice &device, RenderContext &context, resource::ResourceManager &resource_manager)
 Constructs an immediate draw instance.
 ~ImmediateDraw ()
 ImmediateDraw (const ImmediateDraw &)=delete
ImmediateDraw & operator= (const ImmediateDraw &)=delete
void init ()
void shutdown ()
void begin (const glm::mat4 &viewProjection, i32 viewportWidth=0, i32 viewportHeight=0)
 Begins a new draw frame.
void end ()
 Ends the frame and submits all draw commands.
void deferEnd ()
void flushDeferred ()
void flushDeferred (ShaderHandle program)
bool hasDeferred () const
const glm::mat4 & viewProjection () const
void flush ()
 Flushes pending draw commands without ending the frame.
void line (const glm::vec2 &from, const glm::vec2 &to, const glm::vec4 &color, f32 thickness=1.0f)
void line3D (const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f)
 A line between two points in SPACE, widened across the view.
void line3DScreen (const glm::vec3 &from, const glm::vec3 &to, const glm::vec4 &color, f32 thickness=1.0f)
void line3DScreenOffset (const glm::vec3 &anchor, const glm::vec2 &fromOffset, const glm::vec2 &toOffset, const glm::vec4 &color, f32 thickness=1.0f)
 A framebuffer-sized line attached to one world-space anchor.
void polyline (std::span< const glm::vec2 > vertices, const glm::vec4 &color, f32 thickness=1.0f, bool closed=false)
void rect (const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, bool filled=true)
void rectOutline (const glm::vec2 &position, const glm::vec2 &size, const glm::vec4 &color, f32 thickness=1.0f)
void circle (const glm::vec2 &center, f32 radius, const glm::vec4 &color, bool filled=true, i32 segments=32)
void circleOutline (const glm::vec2 &center, f32 radius, const glm::vec4 &color, f32 thickness=1.0f, i32 segments=32)
void polygon (std::span< const glm::vec2 > vertices, const glm::vec4 &color)
void triangles (const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount, const glm::vec4 &tint, const f32 xform[6])
 Triangles with a colour at every vertex, in one call: a shape tessellated once and drawn as often as asked.
u32 createShape (const f32 *positions, u32 vertexCount, const f32 *colors, const u32 *indices, u32 indexCount)
 Puts a shape on the GPU once, for drawMesh to draw any number of times without sending its vertices again.
void destroyMesh (u32 id)
void drawMesh (u32 id, const glm::vec4 &tint, const f32 xform[6])
void drawMeshMany (u32 id, const void *records, u32 count)
 count copies of a mesh in one call: drawMesh for each, without a call a copy.
u32 meshCount () const
void texture (const glm::vec2 &position, const glm::vec2 &size, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f))
void textureRotated (const glm::vec2 &position, const glm::vec2 &size, f32 rotation, u32 textureId, const glm::vec4 &tint=glm::vec4(1.0f))
void setLayer (i32 layer)
i32 getLayer () const
void setDepth (f32 depth)
f32 getDepth () const
void setBlendMode (BlendMode mode)
void setDepthTest (bool enabled)
void setDepthState (bool test, bool write)
BlendMode blendMode () const
bool depthTest () const
bool depthWrite () const
void setDither (bool on, bool phase=false)
 Draw what follows dithered: one half of a checkerboard (phase 0 or 1), at full colour; the two halves drawn in turn cover every pixel once. Per vertex, so it batches with everything else; it applies to the immediate geometry (lines, triangles, quads), not to a retained mesh's runs.
bool dither () const
bool ditherPhase () const
void deferBegin ()
u32 getDrawCallCount () const
u32 getPrimitiveCount () const

Detailed Description

Immediate mode 2D drawing API.

Provides a simple API for drawing 2D primitives. All commands submitted between begin() and end() are batched (per texture) and drawn in submission order. The buffer is cleared each frame.

ImmediateDraw draw(device, state, context, resourceManager);
draw.init();
draw.begin(viewProjection);
draw.line({0, 0}, {100, 100}, {1, 0, 0, 1});
draw.rect({50, 50}, {30, 30}, {0, 1, 0, 1});
draw.circle({150, 150}, 25, {0, 0, 1, 1});
draw.end();
ImmediateDraw(GfxDevice &device, RenderContext &context, resource::ResourceManager &resource_manager)
Constructs an immediate draw instance.
const glm::mat4 & viewProjection() const

Definition at line 60 of file ImmediateDraw.hpp.

Constructor & Destructor Documentation

◆ ImmediateDraw() [1/2]

esengine::ImmediateDraw::ImmediateDraw ( GfxDevice & device,
RenderContext & context,
resource::ResourceManager & resource_manager )

Constructs an immediate draw instance.

Parameters
deviceGraphics device (GPU command sink)
contextRender context for shared resources (white texture)
resource_managerResource manager for shader access

Definition at line 95 of file ImmediateDraw.cpp.

Referenced by ImmediateDraw(), and operator=().

◆ ~ImmediateDraw()

esengine::ImmediateDraw::~ImmediateDraw ( )

Definition at line 103 of file ImmediateDraw.cpp.

References shutdown().

◆ ImmediateDraw() [2/2]

esengine::ImmediateDraw::ImmediateDraw ( const ImmediateDraw & )
delete

References ImmediateDraw().

Member Function Documentation

◆ begin()

void esengine::ImmediateDraw::begin ( const glm::mat4 & viewProjection,
i32 viewportWidth = 0,
i32 viewportHeight = 0 )

Begins a new draw frame.

Definition at line 180 of file ImmediateDraw.cpp.

References esengine::Invalid, and viewProjection().

◆ blendMode()

BlendMode esengine::ImmediateDraw::blendMode ( ) const
inline

Definition at line 244 of file ImmediateDraw.hpp.

◆ circle()

void esengine::ImmediateDraw::circle ( const glm::vec2 & center,
f32 radius,
const glm::vec4 & color,
bool filled = true,
i32 segments = 32 )

Definition at line 592 of file ImmediateDraw.cpp.

References circleOutline().

◆ circleOutline()

void esengine::ImmediateDraw::circleOutline ( const glm::vec2 & center,
f32 radius,
const glm::vec4 & color,
f32 thickness = 1.0f,
i32 segments = 32 )

Definition at line 610 of file ImmediateDraw.cpp.

References line().

Referenced by circle().

◆ createShape()

u32 esengine::ImmediateDraw::createShape ( const f32 * positions,
u32 vertexCount,
const f32 * colors,
const u32 * indices,
u32 indexCount )

Puts a shape on the GPU once, for drawMesh to draw any number of times without sending its vertices again.

The same arrays as triangles(). Returns the mesh's id, or 0 when there is nothing to keep or an index names a vertex the shape does not hold. Ids outlive frames; destroyMesh gives the GPU memory back.

Definition at line 662 of file ImmediateDraw.cpp.

References esengine::Index, esengine::Invalid, esengine::packColor(), esengine::GfxContent::retained(), and esengine::Vertex.

◆ deferBegin()

void esengine::ImmediateDraw::deferBegin ( )
inline

Collects all following primitive batches for the owning scene pass.

Definition at line 256 of file ImmediateDraw.hpp.

◆ deferEnd()

void esengine::ImmediateDraw::deferEnd ( )

Close collection now and submit it when the owning scene pass executes.

Definition at line 303 of file ImmediateDraw.cpp.

References flush().

Referenced by end().

◆ depthTest()

bool esengine::ImmediateDraw::depthTest ( ) const
inline

Definition at line 245 of file ImmediateDraw.hpp.

◆ depthWrite()

bool esengine::ImmediateDraw::depthWrite ( ) const
inline

Definition at line 246 of file ImmediateDraw.hpp.

◆ destroyMesh()

void esengine::ImmediateDraw::destroyMesh ( u32 id)

Gives a mesh's GPU memory back. A draw already collected this frame still draws; the id is not drawn again.

Definition at line 700 of file ImmediateDraw.cpp.

◆ dither()

bool esengine::ImmediateDraw::dither ( ) const
inline

Definition at line 253 of file ImmediateDraw.hpp.

◆ ditherPhase()

bool esengine::ImmediateDraw::ditherPhase ( ) const
inline

Definition at line 254 of file ImmediateDraw.hpp.

◆ drawMesh()

void esengine::ImmediateDraw::drawMesh ( u32 id,
const glm::vec4 & tint,
const f32 xform[6] )

The transform is applied on the GPU: a copy costs one 28-byte instance record, and consecutive copies of one mesh in one state are one instanced draw. Anything else drawn between two copies keeps its place.

Definition at line 707 of file ImmediateDraw.cpp.

References flush(), and esengine::packColor().

◆ drawMeshMany()

void esengine::ImmediateDraw::drawMeshMany ( u32 id,
const void * records,
u32 count )

count copies of a mesh in one call: drawMesh for each, without a call a copy.

records holds one 28-byte record a copy, as the instance stream is laid out: six floats (the affine, in drawMesh's order) and the tint packed as packColor packs it. They are copied into the frame's stream whole.

Definition at line 720 of file ImmediateDraw.cpp.

References flush().

◆ end()

void esengine::ImmediateDraw::end ( )

Ends the frame and submits all draw commands.

Definition at line 288 of file ImmediateDraw.cpp.

References deferEnd(), flush(), and flushDeferred().

Referenced by flush().

◆ flush()

void esengine::ImmediateDraw::flush ( )

Flushes pending draw commands without ending the frame.

Use before operations that change GL state to ensure accumulated primitives are rendered with the correct state.

Definition at line 262 of file ImmediateDraw.cpp.

References esengine::Batch, end(), and esengine::UnsignedInt.

Referenced by deferEnd(), drawMesh(), drawMeshMany(), end(), flushDeferred(), setBlendMode(), and setDepthState().

◆ flushDeferred() [1/2]

void esengine::ImmediateDraw::flushDeferred ( )

Submit a collection closed by deferEnd inside the active render pass.

Definition at line 311 of file ImmediateDraw.cpp.

References flushDeferred(), and esengine::Invalid.

Referenced by esengine::RenderFrame::drawOverScreen(), end(), and flushDeferred().

◆ flushDeferred() [2/2]

void esengine::ImmediateDraw::flushDeferred ( ShaderHandle program)

The same, with every batch drawn through program instead of the one it was collected with: a pass that writes the display itself draws a linear frame's colours through the variant that encodes them, as the scene's present did.

Definition at line 315 of file ImmediateDraw.cpp.

References esengine::Batch, flush(), esengine::Invalid, esengine::PipelineDesc::program, and esengine::UnsignedInt.

◆ getDepth()

f32 esengine::ImmediateDraw::getDepth ( ) const
inline

Definition at line 236 of file ImmediateDraw.hpp.

◆ getDrawCallCount()

u32 esengine::ImmediateDraw::getDrawCallCount ( ) const
inline

Definition at line 262 of file ImmediateDraw.hpp.

◆ getLayer()

i32 esengine::ImmediateDraw::getLayer ( ) const
inline

Definition at line 234 of file ImmediateDraw.hpp.

◆ getPrimitiveCount()

u32 esengine::ImmediateDraw::getPrimitiveCount ( ) const
inline

Definition at line 263 of file ImmediateDraw.hpp.

◆ hasDeferred()

bool esengine::ImmediateDraw::hasDeferred ( ) const
inline

Whether a collection closed by deferEnd is waiting for its pass.

Definition at line 107 of file ImmediateDraw.hpp.

Referenced by esengine::RenderFrame::drawOverScreen().

◆ init()

◆ line()

void esengine::ImmediateDraw::line ( const glm::vec2 & from,
const glm::vec2 & to,
const glm::vec4 & color,
f32 thickness = 1.0f )

Definition at line 411 of file ImmediateDraw.cpp.

Referenced by circleOutline(), polyline(), and rectOutline().

◆ line3D()

void esengine::ImmediateDraw::line3D ( const glm::vec3 & from,
const glm::vec3 & to,
const glm::vec4 & color,
f32 thickness = 1.0f )

A line between two points in SPACE, widened across the view.

Widened along cross(direction, view forward) rather than in a plane, so it reads as a line from wherever the camera is. Thickness is in WORLD units, so a distant line thins out with the geometry beside it.

Definition at line 427 of file ImmediateDraw.cpp.

References esengine::Batch, and esengine::packColor().

◆ line3DScreen()

void esengine::ImmediateDraw::line3DScreen ( const glm::vec3 & from,
const glm::vec3 & to,
const glm::vec4 & color,
f32 thickness = 1.0f )

A world-space line with an explicit framebuffer-pixel thickness.

Definition at line 467 of file ImmediateDraw.cpp.

References esengine::Batch, and esengine::packColor().

Referenced by line3DScreenOffset().

◆ line3DScreenOffset()

void esengine::ImmediateDraw::line3DScreenOffset ( const glm::vec3 & anchor,
const glm::vec2 & fromOffset,
const glm::vec2 & toOffset,
const glm::vec4 & color,
f32 thickness = 1.0f )

A framebuffer-sized line attached to one world-space anchor.

Offsets use framebuffer pixels (+X right, +Y up). The anchor is projected through the active draw frame, so editor handles keep a stable size without sampling a second camera.

Definition at line 528 of file ImmediateDraw.cpp.

References line3DScreen().

◆ meshCount()

u32 esengine::ImmediateDraw::meshCount ( ) const
inline

Definition at line 216 of file ImmediateDraw.hpp.

◆ operator=()

ImmediateDraw & esengine::ImmediateDraw::operator= ( const ImmediateDraw & )
delete

◆ polygon()

void esengine::ImmediateDraw::polygon ( std::span< const glm::vec2 > vertices,
const glm::vec4 & color )

Definition at line 623 of file ImmediateDraw.cpp.

◆ polyline()

void esengine::ImmediateDraw::polyline ( std::span< const glm::vec2 > vertices,
const glm::vec4 & color,
f32 thickness = 1.0f,
bool closed = false )

Definition at line 550 of file ImmediateDraw.cpp.

References line().

◆ rect()

void esengine::ImmediateDraw::rect ( const glm::vec2 & position,
const glm::vec2 & size,
const glm::vec4 & color,
bool filled = true )

Definition at line 562 of file ImmediateDraw.cpp.

References rectOutline().

◆ rectOutline()

void esengine::ImmediateDraw::rectOutline ( const glm::vec2 & position,
const glm::vec2 & size,
const glm::vec4 & color,
f32 thickness = 1.0f )

Definition at line 574 of file ImmediateDraw.cpp.

References line().

Referenced by rect().

◆ setBlendMode()

void esengine::ImmediateDraw::setBlendMode ( BlendMode mode)

Switches the blend mode for subsequent primitives (flushes pending geometry).

Definition at line 243 of file ImmediateDraw.cpp.

References flush().

◆ setDepth()

void esengine::ImmediateDraw::setDepth ( f32 depth)
inline

Definition at line 235 of file ImmediateDraw.hpp.

◆ setDepthState()

void esengine::ImmediateDraw::setDepthState ( bool test,
bool write )

Changes depth testing and writes together, flushing the previous batch.

Definition at line 254 of file ImmediateDraw.cpp.

References flush().

Referenced by setDepthTest().

◆ setDepthTest()

void esengine::ImmediateDraw::setDepthTest ( bool enabled)

Toggles depth testing for subsequent primitives (flushes pending geometry).

Definition at line 250 of file ImmediateDraw.cpp.

References setDepthState().

◆ setDither()

void esengine::ImmediateDraw::setDither ( bool on,
bool phase = false )
inline

Draw what follows dithered: one half of a checkerboard (phase 0 or 1), at full colour; the two halves drawn in turn cover every pixel once. Per vertex, so it batches with everything else; it applies to the immediate geometry (lines, triangles, quads), not to a retained mesh's runs.

Definition at line 252 of file ImmediateDraw.hpp.

◆ setLayer()

void esengine::ImmediateDraw::setLayer ( i32 layer)
inline

Definition at line 233 of file ImmediateDraw.hpp.

◆ shutdown()

void esengine::ImmediateDraw::shutdown ( )

Definition at line 157 of file ImmediateDraw.cpp.

References ES_LOG_INFO, and esengine::Invalid.

Referenced by ~ImmediateDraw().

◆ texture()

void esengine::ImmediateDraw::texture ( const glm::vec2 & position,
const glm::vec2 & size,
u32 textureId,
const glm::vec4 & tint = glm::vec4(1.0f) )

Definition at line 841 of file ImmediateDraw.cpp.

◆ textureRotated()

void esengine::ImmediateDraw::textureRotated ( const glm::vec2 & position,
const glm::vec2 & size,
f32 rotation,
u32 textureId,
const glm::vec4 & tint = glm::vec4(1.0f) )

Definition at line 848 of file ImmediateDraw.cpp.

◆ triangles()

void esengine::ImmediateDraw::triangles ( const f32 * positions,
u32 vertexCount,
const f32 * colors,
const u32 * indices,
u32 indexCount,
const glm::vec4 & tint,
const f32 xform[6] )

Triangles with a colour at every vertex, in one call: a shape tessellated once and drawn as often as asked.

positions holds an x, y pair a vertex, colors an r, g, b, a quadruple a vertex (null: white), indices three a triangle. Every colour is multiplied by tint, and every point is moved by the 2D affine xform (x' = xform[0] x + xform[2] y + xform[4], y' = xform[1] x + xform[3] y + xform[5]), so one shape is drawn where and as faded as this frame says without being built again. A call naming a vertex it does not hold draws nothing.

Definition at line 632 of file ImmediateDraw.cpp.

References esengine::Batch, and esengine::packColor().

◆ viewProjection()

const glm::mat4 & esengine::ImmediateDraw::viewProjection ( ) const
inline

The view-projection the current or last collection began with.

Definition at line 110 of file ImmediateDraw.hpp.

Referenced by begin(), esengine::RenderFrame::drawOverScreen(), and operator=().


The documentation for this class was generated from the following files: