Skip to content

Commit

Permalink
Merge pull request #44 from zao/feat/render-evolve
Browse files Browse the repository at this point in the history
Evolve renderer backend and API surface
  • Loading branch information
Wires77 authored Mar 22, 2024
2 parents 166d251 + 84e98fb commit b8503a5
Show file tree
Hide file tree
Showing 14 changed files with 855 additions and 367 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
[submodule "dep/imgui"]
path = dep/imgui
url = https://github.com/ocornut/imgui.git
[submodule "dep/glm"]
path = dep/glm
url = https://github.com/g-truc/glm.git
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ include(${PROJECT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake)

set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ".")
include(InstallRequiredSystemLibraries)
add_subdirectory(dep/glm)

set(SIMPLEGRAPHIC_SOURCES
"config.h"
"dep/stb/stb_image.h"
"dep/stb/stb_image_resize.h"
"dep/stb/stb_image_write.h"
"engine/common/common.cpp"
"engine/common.h"
"engine/common/console.cpp"
"engine/common/console.h"
"engine/common/keylist.h"
Expand Down Expand Up @@ -137,6 +139,7 @@ target_compile_definitions(imgui PUBLIC
target_include_directories(imgui PUBLIC
dep/imgui
dep/imgui/backends
dep/imgui/misc/cpp
)

target_link_libraries(imgui PUBLIC
Expand Down Expand Up @@ -181,6 +184,7 @@ target_link_libraries(SimpleGraphic
unofficial::angle::libGLESv2
fmt::fmt
glfw
glm::glm
imgui
LuaJIT::LuaJIT
re2::re2
Expand Down
1 change: 1 addition & 0 deletions dep/glm
Submodule glm added at 47585f
37 changes: 26 additions & 11 deletions engine/core/core_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ image_c::~image_c()

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

void image_c::Free()
{
delete dat;
delete[] dat;
dat = NULL;
}

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

bool targa_c::Save(const char* fileName)
{
return true;
if (type != IMGTYPE_RGB && type != IMGTYPE_RGBA) {
return true;
}

// Open file
fileOutputStream_c out;
if (out.FileOpen(fileName, true)) {
return true;
}

auto rc = stbi_write_tga_to_func([](void* ctx, void* data, int size) {
auto out = (fileOutputStream_c*)ctx;
out->Write(data, size);
}, &out, width, height, comp, dat);

return !rc;
}

bool targa_c::ImageInfo(const char* fileName, imageInfo_s* info)
Expand Down Expand Up @@ -277,14 +292,14 @@ bool jpeg_c::Load(const char* fileName)
return true;
}
int x, y, in_comp;
if (!stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp)) {
if (!stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp)) {
return true;
}
if (in_comp != 1 && in_comp != 3) {
con->Warning("JPEG '%s': unsupported component count '%d'", fileName, comp);
return true;
}
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, in_comp);
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, in_comp);
if (!data) {
stbi_image_free(data);
return true;
Expand Down Expand Up @@ -335,7 +350,7 @@ bool jpeg_c::ImageInfo(const char* fileName, imageInfo_s* info)
return true;
}
int x, y, comp;
if (stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &comp)) {
if (stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &comp)) {
return true;
}

Expand Down Expand Up @@ -366,14 +381,14 @@ bool png_c::Load(const char* fileName)
return true;
}
int x, y, in_comp;
if (!stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp)) {
if (!stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp)) {
return true;
}
width = x;
height = y;
comp = (in_comp == 1 || in_comp == 3) ? 3 : 4;
type = comp == 3 ? IMGTYPE_RGB : IMGTYPE_RGBA;
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, comp);
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, comp);
if (!data) {
stbi_image_free(data);
return true;
Expand Down Expand Up @@ -420,7 +435,7 @@ bool png_c::ImageInfo(const char* fileName, imageInfo_s* info)
return true;
}
int x, y, comp;
if (stbi_info_from_memory(fileData.data(), fileData.size(), &x, &y, &comp)) {
if (stbi_info_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &comp)) {
return true;
}

Expand Down Expand Up @@ -449,7 +464,7 @@ bool gif_c::Load(const char* fileName)
return true;
}
int x, y, in_comp;
stbi_uc* data = stbi_load_from_memory(fileData.data(), fileData.size(), &x, &y, &in_comp, 4);
stbi_uc* data = stbi_load_from_memory(fileData.data(), (int)fileData.size(), &x, &y, &in_comp, 4);
if (!data || in_comp != 4) {
stbi_image_free(data);
return true;
Expand Down
4 changes: 0 additions & 4 deletions engine/core/core_video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class core_video_c: public core_IVideo, public conCmdHandler_c {
sys_IMain* sys;

conVar_c* vid_mode;
conVar_c* vid_display;
conVar_c* vid_fullscreen;
conVar_c* vid_resizable;
conVar_c* vid_last;
Expand All @@ -69,7 +68,6 @@ core_video_c::core_video_c(sys_IMain* sysHnd)
: conCmdHandler_c(sysHnd->con), sys(sysHnd)
{
vid_mode = sys->con->Cvar_Add("vid_mode", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFMODE, -1, VID_NUMMODES-1);
vid_display = sys->con->Cvar_Add("vid_display", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFDISPLAY, -1, 15);
vid_fullscreen = sys->con->Cvar_Add("vid_fullscreen", CV_ARCHIVE, CFG_VID_DEFFULLSCREEN);
vid_resizable = sys->con->Cvar_Add("vid_resizable", CV_ARCHIVE|CV_CLAMP, CFG_VID_DEFRESIZABLE, 0, 3);
vid_last = sys->con->Cvar_Add("vid_last", CV_ARCHIVE, "");
Expand Down Expand Up @@ -100,15 +98,13 @@ void core_video_c::Apply(bool shown)
}
}
}
set.display = vid_display->intVal;
if (vid_mode->intVal >= 0) {
set.mode[0] = (std::max)(vid_modeList[vid_mode->intVal][0], CFG_VID_MINWIDTH);
set.mode[1] = (std::max)(vid_modeList[vid_mode->intVal][1], CFG_VID_MINHEIGHT);
} else {
set.mode[0] = 0;
set.mode[1] = 0;
}
set.depth = 0;
set.minSize[0] = CFG_VID_MINWIDTH;
set.minSize[1] = CFG_VID_MINHEIGHT;
sys->video->Apply(&set);
Expand Down
7 changes: 6 additions & 1 deletion engine/render.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
// Classes
// =======

// Renderer feature flags
enum r_featureFlag_e {
F_DPI_AWARE = 0x1, // App understands DPI, do not virtualize screen size/positions
};

// Font alignment
enum r_fontAlign_e {
F_LEFT,
Expand Down Expand Up @@ -59,7 +64,7 @@ class r_IRenderer {
static r_IRenderer* GetHandle(sys_IMain* sysHnd);
static void FreeHandle(r_IRenderer* hnd);

virtual void Init() = 0;
virtual void Init(r_featureFlag_e features) = 0;
virtual void Shutdown() = 0;

virtual void BeginFrame() = 0;
Expand Down
28 changes: 14 additions & 14 deletions engine/render/r_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

// Glyph parameters
struct f_glyph_s {
double tcLeft = 0.0;
double tcRight = 0.0;
double tcTop = 0.0;
double tcBottom = 0.0;
float tcLeft = 0.0;
float tcRight = 0.0;
float tcTop = 0.0;
float tcBottom = 0.0;
int width = 0;
int spLeft = 0;
int spRight = 0;
Expand All @@ -32,7 +32,7 @@ struct f_fontHeight_s {
int height;
int numGlyph;
f_glyph_s glyphs[128];
f_glyph_s defGlyph{0.0, 0.0, 0.0, 0.0, 0, 0, 0};
f_glyph_s defGlyph{0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0};

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

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

// Calculate the string position
double x = pos[X];
double y = pos[Y];
float x = pos[X];
float y = pos[Y];
if (align != F_LEFT) {
// Calculate the real width of the string
double width = StringWidthInternal(fh, str) * scale;
float width = StringWidthInternal(fh, str) * scale;
switch (align) {
case F_CENTRE:
x = floor((renderer->VirtualScreenWidth() - width) / 2.0f + pos[X]);
Expand Down Expand Up @@ -297,7 +297,7 @@ void r_font_c::DrawTextLine(scp_t pos, int align, int height, col4_t col, const
auto& glyph = fh->Glyph(*str++);
x+= glyph.spLeft * scale;
if (glyph.width) {
double w = glyph.width * scale;
float w = glyph.width * scale;
if (x + w >= 0 && x < renderer->VirtualScreenWidth()) {
renderer->curLayer->Quad(
glyph.tcLeft, glyph.tcTop, x, y,
Expand Down
Loading

0 comments on commit b8503a5

Please sign in to comment.