1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
#include "imgui.h"
#include <renderer/RSoftwareBackend.hpp>
#include <SDL3/SDL_video.h>
#include <GL/glew.h>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
RSoftwareBackend::RSoftwareBackend(Renderer& renderer)
: m_renderer {renderer}
{
m_canvas.rshift = 0;
m_canvas.gshift = 8;
m_canvas.bshift = 16;
m_canvas.ashift = 24;
m_canvas.pixels = nullptr;
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &m_gltexture_id);
glBindTexture(GL_TEXTURE_2D, m_gltexture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void
RSoftwareBackend::ResizeCanvas(int32_t w, int32_t h)
{
size_t realloc_size = (size_t)(w * h) * sizeof(m_canvas.pixels[0]);
void *realloc_data = realloc(m_canvas.pixels, realloc_size);
if (!realloc_data) {
printf("could not resize offscreen buffer\n");
return;
}
m_canvas.w = w;
m_canvas.h = h;
m_canvas.pixels = (uint32_t*)realloc_data;
}
void
RSoftwareBackend::Draw()
{
ResizeCanvas(m_renderer.m_screen_w, m_renderer.m_screen_h);
SortRenderEntities();
for (RSortEntry sort_entry : m_renderer.m_sort_entries) {
REntity& entity = m_renderer.m_render_entities[sort_entry.entity_index];
switch (entity.type) {
case REntityType_Rectangle: {
DrawRectangle(entity.rect);
} break;
case REntityType_AlphaBitmap: {
DrawAlphaBitmap(entity.bitmap);
} break;
case REntityType_Text: {
DrawText(entity.text);
}; break;
case REntityType_Circle: {
}; break;
default:;
}
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_gltexture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_canvas.w, m_canvas.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_canvas.pixels);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
}
void
RSoftwareBackend::SortRenderEntities()
{
auto& sort_entries = m_renderer.m_sort_entries;
std::sort(sort_entries.begin(), sort_entries.end(),
[](const RSortEntry& e1, const RSortEntry& e2) {
return e1.z < e2.z;
});
}
void
RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity)
{
int32_t xmin = m_renderer.WorldXToScreenX(entity.rect.x0);
int32_t ymin = m_renderer.WorldYToScreenY(entity.rect.y0);
int32_t xmax = m_renderer.WorldXToScreenX(entity.rect.x1);
int32_t ymax = m_renderer.WorldYToScreenY(entity.rect.y1);
if (xmin < 0) {
xmin = 0;
}
if (ymin < 0) {
ymin = 0;
}
if (xmax >= m_canvas.w) {
xmax = m_canvas.w - 1;
}
if (ymax >= m_canvas.h) {
ymax = m_canvas.h - 1;
}
uint32_t rshift = m_canvas.rshift;
uint32_t gshift = m_canvas.gshift;
uint32_t bshift = m_canvas.bshift;
for (int32_t y = ymin; y <= ymax; ++y) {
uint32_t *pixel = m_canvas.pixels + y * m_canvas.w + xmin;
for (int32_t x = xmin; x <= xmax; ++x) {
uint32_t r = (uint32_t)(entity.color.r * 255.0f);
uint32_t g = (uint32_t)(entity.color.g * 255.0f);
uint32_t b = (uint32_t)(entity.color.b * 255.0f);
uint32_t val = r << rshift | g << gshift | b << bshift;
*pixel++ = val;
}
}
}
void
RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity)
{
int32_t x0 = m_renderer.WorldXToScreenX(entity.pos.x);
int32_t y0 = m_renderer.WorldYToScreenY(entity.pos.y);
int32_t x1 = x0 + entity.bitmap.w - 1;
int32_t y1 = y0 + entity.bitmap.h - 1;
int32_t cut_left = 0;
int32_t cut_bot = 0;
if (x0 < 0) {
cut_left = x0;
x0 = 0;
}
if (y0 < 0) {
cut_bot = y0;
y0 = 0;
}
if (x1 >= m_canvas.w) {
x1 = m_canvas.w - 1;
}
if (y1 >= m_canvas.h) {
y1 = m_canvas.h - 1;
}
uint8_t* alpha_row = (uint8_t*)entity.bitmap.pixels.get() + (-cut_bot * entity.bitmap.w) + (-cut_left);
uint32_t* rgba_row = m_canvas.pixels + y0 * m_canvas.w + x0;
for (int32_t y = y0; y <= y1; y++) {
uint8_t* alpha = alpha_row;
uint32_t* rgba = rgba_row;
for (int32_t x = x0; x <= x1; x++) {
if (*alpha == 0) {
alpha++;
rgba++;
continue;
}
float alphaf = *alpha / 255.0f;
float r1 = entity.color.r * alphaf;
float g1 = entity.color.g * alphaf;
float b1 = entity.color.b * alphaf;
uint32_t r2 = uint32_t(r1 * 255.0f) << m_canvas.rshift;
uint32_t g2 = uint32_t(g1 * 255.0f) << m_canvas.gshift;
uint32_t b2 = uint32_t(b1 * 255.0f) << m_canvas.bshift;
uint32_t rgba_result = r2 | g2 | b2;
*rgba = rgba_result;
alpha++;
rgba++;
}
alpha_row += entity.bitmap.w;
rgba_row += m_canvas.w;
}
}
void
RSoftwareBackend::DrawText(REntity_Text& entity)
{
int32_t xscreen = m_renderer.WorldXToScreenX(entity.pos.x);
int32_t yscreen = m_renderer.WorldYToScreenY(entity.pos.y);
for (char32_t ch : entity.text) {
Glyph& glyph = entity.font.GetGlyph(ch);
int32_t x = xscreen + glyph.xoff;
int32_t y = yscreen + glyph.yoff;
DrawTextGlyph(glyph, entity.color, x, y);
xscreen += glyph.xadvance;
}
}
void
RSoftwareBackend::DrawTextGlyph(Glyph& glyph, Color color, int32_t xscreen, int32_t yscreen)
{
int32_t x0 = xscreen;
int32_t y0 = yscreen;
int32_t x1 = x0 + glyph.bitmap.w - 1;
int32_t y1 = y0 + glyph.bitmap.h - 1;
int32_t cut_left = 0;
int32_t cut_bot = 0;
if (x0 < 0) {
cut_left = x0;
x0 = 0;
}
if (y0 < 0) {
cut_bot = y0;
y0 = 0;
}
if (x1 >= m_canvas.w) {
x1 = m_canvas.w - 1;
}
if (y1 >= m_canvas.h) {
y1 = m_canvas.h - 1;
}
uint8_t* alpha_row = (uint8_t*)glyph.bitmap.pixels.get() + (-cut_bot * glyph.bitmap.w) + (-cut_left);
uint32_t* rgba_row = m_canvas.pixels + y0 * m_canvas.w + x0;
for (int32_t y = y0; y <= y1; y++) {
uint8_t* alpha = alpha_row;
uint32_t* rgba = rgba_row;
for (int32_t x = x0; x <= x1; x++) {
if (*alpha == 0) {
alpha++;
rgba++;
continue;
}
float alphaf = *alpha / 255.0f;
float r1 = color.r * alphaf;
float g1 = color.g * alphaf;
float b1 = color.b * alphaf;
uint32_t r2 = uint32_t(r1 * 255.0f) << m_canvas.rshift;
uint32_t g2 = uint32_t(g1 * 255.0f) << m_canvas.gshift;
uint32_t b2 = uint32_t(b1 * 255.0f) << m_canvas.bshift;
uint32_t rgba_result = r2 | g2 | b2;
*rgba = rgba_result;
alpha++;
rgba++;
}
alpha_row += glyph.bitmap.w;
rgba_row += m_canvas.w;
}
}
|