Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ImmediateDrawBindings.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3
5#include "BoundarySpan.hpp"
6#include "ActiveContext.hpp"
15
16#include <glm/glm.hpp>
17#include <glm/gtc/type_ptr.hpp>
18
19namespace esengine {
20
21static EstellaContext& ctx() { return activeCtx(); }
22
23#define g_device (ctx().tryGet<GfxDevice>())
24#define g_initialized (ctx().state().initialized)
25#define g_immediateDraw (ctx().tryGet<ImmediateDraw>())
26#define g_immediateDrawActive (ctx().state().immediate_draw_active)
27#define g_viewportWidth (ctx().state().viewport_width)
28#define g_viewportHeight (ctx().state().viewport_height)
29#define g_currentViewProjection (ctx().state().current_view_projection)
30
33 g_immediateDraw->flush();
34 }
35}
36
37void draw_begin(uintptr_t matrixPtr, i32 viewportWidth, i32 viewportHeight) {
38 if (!g_initialized || !g_immediateDraw) return;
39
40 g_device->setViewport(0, 0, g_viewportWidth, g_viewportHeight);
41
42 const f32* matrixData = boundarySpan<f32>(matrixPtr, 16, "draw_begin.matrix");
43 if (!matrixData) return;
44 ctx().state().current_view_projection = glm::make_mat4(matrixData);
45 g_immediateDraw->begin(
47 viewportWidth > 0 ? viewportWidth : static_cast<i32>(g_viewportWidth),
48 viewportHeight > 0 ? viewportHeight : static_cast<i32>(g_viewportHeight));
50}
51
52void draw_end() {
54
55 g_immediateDraw->end();
57}
58
61
62 g_immediateDraw->deferEnd();
64}
65
68 g_immediateDraw->deferBegin();
69}
70
71void draw_line(f32 fromX, f32 fromY, f32 toX, f32 toY,
72 f32 r, f32 g, f32 b, f32 a, f32 thickness) {
74
75 g_immediateDraw->line(
76 glm::vec2(fromX, fromY),
77 glm::vec2(toX, toY),
78 glm::vec4(r, g, b, a),
79 thickness
80 );
81}
82
83void draw_line3D(f32 fromX, f32 fromY, f32 fromZ, f32 toX, f32 toY, f32 toZ,
84 f32 r, f32 g, f32 b, f32 a, f32 thickness) {
86
87 g_immediateDraw->line3D(
88 glm::vec3(fromX, fromY, fromZ),
89 glm::vec3(toX, toY, toZ),
90 glm::vec4(r, g, b, a),
91 thickness
92 );
93}
94
95void draw_line3DScreen(f32 fromX, f32 fromY, f32 fromZ, f32 toX, f32 toY, f32 toZ,
96 f32 r, f32 g, f32 b, f32 a, f32 thickness) {
98
99 g_immediateDraw->line3DScreen(
100 glm::vec3(fromX, fromY, fromZ),
101 glm::vec3(toX, toY, toZ),
102 glm::vec4(r, g, b, a),
103 thickness
104 );
105}
106
107void draw_line3DScreenOffset(f32 anchorX, f32 anchorY, f32 anchorZ,
108 f32 fromX, f32 fromY, f32 toX, f32 toY,
109 f32 r, f32 g, f32 b, f32 a, f32 thickness) {
110 if (!g_immediateDraw || !g_immediateDrawActive) return;
111
112 g_immediateDraw->line3DScreenOffset(
113 glm::vec3(anchorX, anchorY, anchorZ),
114 glm::vec2(fromX, fromY),
115 glm::vec2(toX, toY),
116 glm::vec4(r, g, b, a),
117 thickness
118 );
119}
120
121void draw_rect(f32 x, f32 y, f32 width, f32 height,
122 f32 r, f32 g, f32 b, f32 a, bool filled) {
123 if (!g_immediateDraw || !g_immediateDrawActive) return;
124
125 g_immediateDraw->rect(
126 glm::vec2(x, y),
127 glm::vec2(width, height),
128 glm::vec4(r, g, b, a),
129 filled
130 );
131}
132
133void draw_rectOutline(f32 x, f32 y, f32 width, f32 height,
134 f32 r, f32 g, f32 b, f32 a, f32 thickness) {
135 if (!g_immediateDraw || !g_immediateDrawActive) return;
136
137 g_immediateDraw->rectOutline(
138 glm::vec2(x, y),
139 glm::vec2(width, height),
140 glm::vec4(r, g, b, a),
141 thickness
142 );
143}
144
145void draw_circle(f32 centerX, f32 centerY, f32 radius,
146 f32 r, f32 g, f32 b, f32 a, bool filled, i32 segments) {
147 if (!g_immediateDraw || !g_immediateDrawActive) return;
148
149 g_immediateDraw->circle(
150 glm::vec2(centerX, centerY),
151 radius,
152 glm::vec4(r, g, b, a),
153 filled,
154 segments
155 );
156}
157
158void draw_circleOutline(f32 centerX, f32 centerY, f32 radius,
159 f32 r, f32 g, f32 b, f32 a, f32 thickness, i32 segments) {
160 if (!g_immediateDraw || !g_immediateDrawActive) return;
161
162 g_immediateDraw->circleOutline(
163 glm::vec2(centerX, centerY),
164 radius,
165 glm::vec4(r, g, b, a),
166 thickness,
167 segments
168 );
169}
170
171void draw_triangles(uintptr_t positionsPtr, u32 vertexCount, uintptr_t colorsPtr, uintptr_t indicesPtr, u32 indexCount,
172 f32 r, f32 g, f32 b, f32 a,
173 f32 xx, f32 xy, f32 yx, f32 yy, f32 tx, f32 ty) {
174 if (!g_immediateDraw || !g_immediateDrawActive) return;
175 const f32 xform[6] = {xx, xy, yx, yy, tx, ty};
176 g_immediateDraw->triangles(
177 reinterpret_cast<const f32*>(positionsPtr), vertexCount,
178 colorsPtr ? reinterpret_cast<const f32*>(colorsPtr) : nullptr,
179 reinterpret_cast<const u32*>(indicesPtr), indexCount,
180 glm::vec4(r, g, b, a), xform
181 );
182}
183
184u32 draw_createTriangleMesh(uintptr_t positionsPtr, u32 vertexCount, uintptr_t colorsPtr, uintptr_t indicesPtr, u32 indexCount) {
185 if (!g_immediateDraw) return 0;
186 return g_immediateDraw->createShape(
187 reinterpret_cast<const f32*>(positionsPtr), vertexCount,
188 colorsPtr ? reinterpret_cast<const f32*>(colorsPtr) : nullptr,
189 reinterpret_cast<const u32*>(indicesPtr), indexCount);
190}
191
193 if (!g_immediateDraw) return;
194 g_immediateDraw->destroyMesh(id);
195}
196
197void draw_triangleMesh(u32 id, f32 r, f32 g, f32 b, f32 a, f32 xx, f32 xy, f32 yx, f32 yy, f32 tx, f32 ty) {
198 if (!g_immediateDraw || !g_immediateDrawActive) return;
199 const f32 xform[6] = {xx, xy, yx, yy, tx, ty};
200 g_immediateDraw->drawMesh(id, glm::vec4(r, g, b, a), xform);
201}
202
203void draw_triangleMeshMany(u32 id, uintptr_t recordsPtr, u32 count) {
204 if (!g_immediateDraw || !g_immediateDrawActive) return;
205 g_immediateDraw->drawMeshMany(id, reinterpret_cast<const void*>(recordsPtr), count);
206}
207
208void draw_texture(f32 x, f32 y, f32 width, f32 height, u32 textureId,
209 f32 r, f32 g, f32 b, f32 a) {
210 if (!g_immediateDraw || !g_immediateDrawActive) return;
211
212 g_immediateDraw->texture(
213 glm::vec2(x, y),
214 glm::vec2(width, height),
215 textureId,
216 glm::vec4(r, g, b, a)
217 );
218}
219
220void draw_textureRotated(f32 x, f32 y, f32 width, f32 height, f32 rotation,
221 u32 textureId, f32 r, f32 g, f32 b, f32 a) {
222 if (!g_immediateDraw || !g_immediateDrawActive) return;
223
224 g_immediateDraw->textureRotated(
225 glm::vec2(x, y),
226 glm::vec2(width, height),
227 rotation,
228 textureId,
229 glm::vec4(r, g, b, a)
230 );
231}
232
233void draw_setLayer(i32 layer) {
234 if (!g_immediateDraw) return;
235 g_immediateDraw->setLayer(layer);
236}
237
238void draw_setDepth(f32 depth) {
239 if (!g_immediateDraw) return;
240 g_immediateDraw->setDepth(depth);
241}
242
244 if (!g_immediateDraw) return 0;
245 return g_immediateDraw->getDrawCallCount();
246}
247
249 if (!g_immediateDraw) return 0;
250 return g_immediateDraw->getPrimitiveCount();
251}
252
254 if (!g_immediateDraw) return;
255 g_immediateDraw->setBlendMode(static_cast<BlendMode>(mode));
256}
257
258void draw_setDepthTest(bool enabled) {
259 if (!g_immediateDraw) return;
260 g_immediateDraw->setDepthTest(enabled);
261}
262
264 if (!g_immediateDraw) return 0;
265 return (g_immediateDraw->depthTest() ? 1u : 0u)
266 | (g_immediateDraw->depthWrite() ? 2u : 0u)
267 | (g_immediateDraw->dither() ? 4u : 0u)
268 | (g_immediateDraw->ditherPhase() ? 8u : 0u);
269}
270
272 if (!g_immediateDraw) return;
273 g_immediateDraw->setDepthState((flags & 1u) != 0, (flags & 2u) != 0);
274 g_immediateDraw->setDither((flags & 4u) != 0, (flags & 8u) != 0);
275}
276
277} // namespace esengine
278
Blend mode enumeration for custom rendering.
Always-on validation for JS-supplied pointer+length pairs at the WASM boundary.
Custom geometry for user-defined mesh rendering.
#define g_immediateDrawActive
#define g_initialized
#define g_immediateDraw
#define g_currentViewProjection
#define g_device
Abstract graphics device interface.
#define g_viewportWidth
#define g_viewportHeight
Immediate mode 2D drawing API.
Rendering context containing all renderer state.
Frame resources and the shared scene rendering contract.
Central resource management system.
System for computing hierarchical world transforms.
const T * boundarySpan(uintptr_t ptr, u64 count, const char *what)
Validates a JS-supplied pointer to count elements of T and returns it typed.
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
void draw_rectOutline(f32 x, f32 y, f32 width, f32 height, f32 r, f32 g, f32 b, f32 a, f32 thickness)
void draw_line(f32 fromX, f32 fromY, f32 toX, f32 toY, f32 r, f32 g, f32 b, f32 a, f32 thickness)
void draw_setDepthTest(bool enabled)
void draw_line3DScreen(f32 fromX, f32 fromY, f32 fromZ, f32 toX, f32 toY, f32 toZ, f32 r, f32 g, f32 b, f32 a, f32 thickness)
void draw_destroyTriangleMesh(u32 id)
void draw_circle(f32 centerX, f32 centerY, f32 radius, f32 r, f32 g, f32 b, f32 a, bool filled, i32 segments)
void draw_setBlendMode(i32 mode)
void draw_circleOutline(f32 centerX, f32 centerY, f32 radius, f32 r, f32 g, f32 b, f32 a, f32 thickness, i32 segments)
void draw_setDepth(f32 depth)
static void flushImmediateDrawIfActive()
void draw_begin(uintptr_t matrixPtr, i32 viewportWidth, i32 viewportHeight)
void draw_triangleMeshMany(u32 id, uintptr_t recordsPtr, u32 count)
void draw_triangleMesh(u32 id, f32 r, f32 g, f32 b, f32 a, f32 xx, f32 xy, f32 yx, f32 yy, f32 tx, f32 ty)
u32 draw_createTriangleMesh(uintptr_t positionsPtr, u32 vertexCount, uintptr_t colorsPtr, uintptr_t indicesPtr, u32 indexCount)
void draw_triangles(uintptr_t positionsPtr, u32 vertexCount, uintptr_t colorsPtr, uintptr_t indicesPtr, u32 indexCount, f32 r, f32 g, f32 b, f32 a, f32 xx, f32 xy, f32 yx, f32 yy, f32 tx, f32 ty)
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
BlendMode
Predefined blend modes for rendering.
Definition BlendMode.hpp:22
void draw_rect(f32 x, f32 y, f32 width, f32 height, f32 r, f32 g, f32 b, f32 a, bool filled)
void draw_textureRotated(f32 x, f32 y, f32 width, f32 height, f32 rotation, u32 textureId, f32 r, f32 g, f32 b, f32 a)
void draw_line3D(f32 fromX, f32 fromY, f32 fromZ, f32 toX, f32 toY, f32 toZ, f32 r, f32 g, f32 b, f32 a, f32 thickness)
void draw_texture(f32 x, f32 y, f32 width, f32 height, u32 textureId, f32 r, f32 g, f32 b, f32 a)
static EstellaContext & ctx()
EstellaContext & activeCtx()
The engine context every binding routes through — single source of truth.
void draw_setDepthState(u32 flags)
void draw_setLayer(i32 layer)
void draw_line3DScreenOffset(f32 anchorX, f32 anchorY, f32 anchorZ, f32 fromX, f32 fromY, f32 toX, f32 toY, f32 r, f32 g, f32 b, f32 a, f32 thickness)
float f32
32-bit floating point
Definition Types.hpp:59
glm::mat4 current_view_projection