Skip to content

Commit b8503a5

Browse files
authored
Merge pull request #44 from zao/feat/render-evolve
Evolve renderer backend and API surface
2 parents 166d251 + 84e98fb commit b8503a5

File tree

14 files changed

+855
-367
lines changed

14 files changed

+855
-367
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "dep/imgui"]
88
path = dep/imgui
99
url = https://github.com/ocornut/imgui.git
10+
[submodule "dep/glm"]
11+
path = dep/glm
12+
url = https://github.com/g-truc/glm.git

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ include(${PROJECT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake)
1818

1919
set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ".")
2020
include(InstallRequiredSystemLibraries)
21+
add_subdirectory(dep/glm)
2122

2223
set(SIMPLEGRAPHIC_SOURCES
2324
"config.h"
2425
"dep/stb/stb_image.h"
2526
"dep/stb/stb_image_resize.h"
2627
"dep/stb/stb_image_write.h"
2728
"engine/common/common.cpp"
29+
"engine/common.h"
2830
"engine/common/console.cpp"
2931
"engine/common/console.h"
3032
"engine/common/keylist.h"
@@ -137,6 +139,7 @@ target_compile_definitions(imgui PUBLIC
137139
target_include_directories(imgui PUBLIC
138140
dep/imgui
139141
dep/imgui/backends
142+
dep/imgui/misc/cpp
140143
)
141144

142145
target_link_libraries(imgui PUBLIC
@@ -181,6 +184,7 @@ target_link_libraries(SimpleGraphic
181184
unofficial::angle::libGLESv2
182185
fmt::fmt
183186
glfw
187+
glm::glm
184188
imgui
185189
LuaJIT::LuaJIT
186190
re2::re2

dep/glm

Submodule glm added at 47585fd

engine/core/core_image.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ image_c::~image_c()
5050

5151
void image_c::CopyRaw(int inType, dword inWidth, dword inHeight, const byte* inDat)
5252
{
53-
if (dat) delete dat;
53+
if (dat) delete[] dat;
5454
comp = inType & 0xF;
5555
type = inType;
5656
width = inWidth;
@@ -61,7 +61,7 @@ void image_c::CopyRaw(int inType, dword inWidth, dword inHeight, const byte* inD
6161

6262
void image_c::Free()
6363
{
64-
delete dat;
64+
delete[] dat;
6565
dat = NULL;
6666
}
6767

@@ -188,7 +188,7 @@ bool targa_c::Load(const char* fileName)
188188
int rlen = ((rlehdr & 0x7F) + 1) * comp;
189189
if (x + rlen > rowSize) {
190190
con->Warning("TGA '%s': invalid RLE coding (overlong row)", fileName);
191-
delete dat;
191+
delete[] dat;
192192
return true;
193193
}
194194
if (rlehdr & 0x80) {
@@ -225,7 +225,22 @@ bool targa_c::Load(const char* fileName)
225225

226226
bool targa_c::Save(const char* fileName)
227227
{
228-
return true;
228+
if (type != IMGTYPE_RGB && type != IMGTYPE_RGBA) {
229+
return true;
230+
}
231+
232+
// Open file
233+
fileOutputStream_c out;
234+
if (out.FileOpen(fileName, true)) {
235+
return true;
236+
}
237+
238+
auto rc = stbi_write_tga_to_func([](void* ctx, void* data, int size) {
239+
auto out = (fileOutputStream_c*)ctx;
240+
out->Write(data, size);
241+
}, &out, width, height, comp, dat);
242+
243+
return !rc;
229244
}
230245

231246
bool targa_c::ImageInfo(const char* fileName, imageInfo_s* info)
@@ -277,14 +292,14 @@ bool jpeg_c::Load(const char* fileName)
277292
return true;
278293
}
279294
int x, y, in_comp;
280-
if (!stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp)) {
295+
if (!stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp)) {
281296
return true;
282297
}
283298
if (in_comp != 1 && in_comp != 3) {
284299
con->Warning("JPEG '%s': unsupported component count '%d'", fileName, comp);
285300
return true;
286301
}
287-
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, in_comp);
302+
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, in_comp);
288303
if (!data) {
289304
stbi_image_free(data);
290305
return true;
@@ -335,7 +350,7 @@ bool jpeg_c::ImageInfo(const char* fileName, imageInfo_s* info)
335350
return true;
336351
}
337352
int x, y, comp;
338-
if (stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &comp)) {
353+
if (stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &comp)) {
339354
return true;
340355
}
341356

@@ -366,14 +381,14 @@ bool png_c::Load(const char* fileName)
366381
return true;
367382
}
368383
int x, y, in_comp;
369-
if (!stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp)) {
384+
if (!stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp)) {
370385
return true;
371386
}
372387
width = x;
373388
height = y;
374389
comp = (in_comp == 1 || in_comp == 3) ? 3 : 4;
375390
type = comp == 3 ? IMGTYPE_RGB : IMGTYPE_RGBA;
376-
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, comp);
391+
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, comp);
377392
if (!data) {
378393
stbi_image_free(data);
379394
return true;
@@ -420,7 +435,7 @@ bool png_c::ImageInfo(const char* fileName, imageInfo_s* info)
420435
return true;
421436
}
422437
int x, y, comp;
423-
if (stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &comp)) {
438+
if (stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &comp)) {
424439
return true;
425440
}
426441

@@ -449,7 +464,7 @@ bool gif_c::Load(const char* fileName)
449464
return true;
450465
}
451466
int x, y, in_comp;
452-
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, 4);
467+
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, 4);
453468
if (!data || in_comp != 4) {
454469
stbi_image_free(data);
455470
return true;

engine/core/core_video.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ class core_video_c: public core_IVideo, public conCmdHandler_c {
4646
sys_IMain* sys;
4747

4848
conVar_c* vid_mode;
49-
conVar_c* vid_display;
5049
conVar_c* vid_fullscreen;
5150
conVar_c* vid_resizable;
5251
conVar_c* vid_last;
@@ -69,7 +68,6 @@ core_video_c::core_video_c(sys_IMain* sysHnd)
6968
: conCmdHandler_c(sysHnd->con), sys(sysHnd)
7069
{
7170
vid_mode = sys->con->Cvar_Add("vid_mode", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFMODE, -1, VID_NUMMODES-1);
72-
vid_display = sys->con->Cvar_Add("vid_display", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFDISPLAY, -1, 15);
7371
vid_fullscreen = sys->con->Cvar_Add("vid_fullscreen", CV_ARCHIVE, CFG_VID_DEFFULLSCREEN);
7472
vid_resizable = sys->con->Cvar_Add("vid_resizable", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFRESIZABLE, 0, 3);
7573
vid_last = sys->con->Cvar_Add("vid_last", CV_ARCHIVE, "");
@@ -100,15 +98,13 @@ void core_video_c::Apply(bool shown)
10098
}
10199
}
102100
}
103-
set.display = vid_display->intVal;
104101
if (vid_mode->intVal >= 0) {
105102
set.mode[0] = (std::max)(vid_modeList[vid_mode->intVal][0], CFG_VID_MINWIDTH);
106103
set.mode[1] = (std::max)(vid_modeList[vid_mode->intVal][1], CFG_VID_MINHEIGHT);
107104
} else {
108105
set.mode[0] = 0;
109106
set.mode[1] = 0;
110107
}
111-
set.depth = 0;
112108
set.minSize[0] = CFG_VID_MINWIDTH;
113109
set.minSize[1] = CFG_VID_MINHEIGHT;
114110
sys->video->Apply(&set);

engine/render.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// Classes
99
// =======
1010

11+
// Renderer feature flags
12+
enum r_featureFlag_e {
13+
F_DPI_AWARE = 0x1, // App understands DPI, do not virtualize screen size/positions
14+
};
15+
1116
// Font alignment
1217
enum r_fontAlign_e {
1318
F_LEFT,
@@ -59,7 +64,7 @@ class r_IRenderer {
5964
static r_IRenderer* GetHandle(sys_IMain* sysHnd);
6065
static void FreeHandle(r_IRenderer* hnd);
6166

62-
virtual void Init() = 0;
67+
virtual void Init(r_featureFlag_e features) = 0;
6368
virtual void Shutdown() = 0;
6469

6570
virtual void BeginFrame() = 0;

engine/render/r_font.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
// Glyph parameters
1919
struct f_glyph_s {
20-
double tcLeft = 0.0;
21-
double tcRight = 0.0;
22-
double tcTop = 0.0;
23-
double tcBottom = 0.0;
20+
float tcLeft = 0.0;
21+
float tcRight = 0.0;
22+
float tcTop = 0.0;
23+
float tcBottom = 0.0;
2424
int width = 0;
2525
int spLeft = 0;
2626
int spRight = 0;
@@ -32,7 +32,7 @@ struct f_fontHeight_s {
3232
int height;
3333
int numGlyph;
3434
f_glyph_s glyphs[128];
35-
f_glyph_s defGlyph{0.0, 0.0, 0.0, 0.0, 0, 0, 0};
35+
f_glyph_s defGlyph{0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0};
3636

3737
f_glyph_s const& Glyph(char ch) const {
3838
if ((unsigned char)ch >= numGlyph) {
@@ -84,10 +84,10 @@ r_font_c::r_font_c(r_renderer_c* renderer, const char* fontName)
8484
// Add glyph
8585
if (fh->numGlyph >= 128) continue;
8686
f_glyph_s* glyph = &fh->glyphs[fh->numGlyph++];
87-
glyph->tcLeft = (double)x / fh->tex->fileWidth;
88-
glyph->tcRight = (double)(x + w) / fh->tex->fileWidth;
89-
glyph->tcTop = (double)y / fh->tex->fileHeight;
90-
glyph->tcBottom = (double)(y + fh->height) / fh->tex->fileHeight;
87+
glyph->tcLeft = (float)x / fh->tex->fileWidth;
88+
glyph->tcRight = (float)(x + w) / fh->tex->fileWidth;
89+
glyph->tcTop = (float)y / fh->tex->fileHeight;
90+
glyph->tcBottom = (float)(y + fh->height) / fh->tex->fileHeight;
9191
glyph->width = w;
9292
glyph->spLeft = sl;
9393
glyph->spRight = sr;
@@ -240,14 +240,14 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, const
240240

241241
// Find best height to use
242242
f_fontHeight_s *fh = fontHeights[height > maxHeight? (numFontHeight - 1) : fontHeightMap[height]];
243-
double scale = (double)height / fh->height;
243+
float scale = (float)height / fh->height;
244244

245245
// Calculate the string position
246-
double x = pos[X];
247-
double y = pos[Y];
246+
float x = pos[X];
247+
float y = pos[Y];
248248
if (align != F_LEFT) {
249249
// Calculate the real width of the string
250-
double width = StringWidthInternal(fh, str) * scale;
250+
float width = StringWidthInternal(fh, str) * scale;
251251
switch (align) {
252252
case F_CENTRE:
253253
x = floor((renderer->VirtualScreenWidth() - width) / 2.0f + pos[X]);
@@ -297,7 +297,7 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, const
297297
auto& glyph = fh->Glyph(*str++);
298298
x+= glyph.spLeft * scale;
299299
if (glyph.width) {
300-
double w = glyph.width * scale;
300+
float w = glyph.width * scale;
301301
if (x + w >= 0 && x < renderer->VirtualScreenWidth()) {
302302
renderer->curLayer->Quad(
303303
glyph.tcLeft, glyph.tcTop, x, y,

0 commit comments

Comments
 (0)