From e76e56e025d1421c1fa9a35590ccd9192b42a943 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 21 Jan 2024 13:58:22 +0100 Subject: [PATCH] larger font hack + linear texture filter for images --- src/main.cpp | 17 +++++++++++++++++ src/sdlrenderer_texture_uploader.cpp | 9 ++++++++- src/sdlrenderer_texture_uploader.hpp | 2 +- src/texture_uploader.hpp | 8 ++++++-- src/tox_avatar_loader.cpp | 2 +- 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f08b97a..4b76d3b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,6 +58,23 @@ int main(int argc, char** argv) { //ImGui::StyleColorsDark(); setThemeGreen(); + { + ImGui::GetIO().Fonts->ClearFonts(); + ImFontConfig fontcfg; + + // upsampling to int looks almost ok + const float font_size_scale = 1.3f; + const float font_oversample = 4.f; + + // default font is pixel perfect at 13 + fontcfg.SizePixels = 13.f * font_size_scale; + fontcfg.RasterizerDensity = font_oversample/font_size_scale; + // normally density would be set to dpi scale of the display + + ImGui::GetIO().Fonts->AddFontDefault(&fontcfg); + ImGui::GetIO().Fonts->Build(); + } + ImGui_ImplSDL3_InitForSDLRenderer(window.get(), renderer.get()); auto imgui_sdl_scope = std::async(std::launch::deferred, &ImGui_ImplSDL3_Shutdown); ImGui_ImplSDLRenderer3_Init(renderer.get()); diff --git a/src/sdlrenderer_texture_uploader.cpp b/src/sdlrenderer_texture_uploader.cpp index d114a9c..24346d7 100644 --- a/src/sdlrenderer_texture_uploader.cpp +++ b/src/sdlrenderer_texture_uploader.cpp @@ -7,7 +7,7 @@ SDLRendererTextureUploader::SDLRendererTextureUploader(SDL_Renderer* renderer_) { } -uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height) { +uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter) { // TODO: test if pitch is 4 or 4*width SDL_Surface* surf = SDL_CreateSurfaceFrom( (void*)data, @@ -17,6 +17,13 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi ); assert(surf); // TODO: add error reporting + // TODO: this touches global state, reset? + if (filter == NEAREST) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); + } else if (filter == LINEAR) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + } + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); assert(tex); // TODO: add error reporting diff --git a/src/sdlrenderer_texture_uploader.hpp b/src/sdlrenderer_texture_uploader.hpp index e007d33..64bc390 100644 --- a/src/sdlrenderer_texture_uploader.hpp +++ b/src/sdlrenderer_texture_uploader.hpp @@ -10,7 +10,7 @@ struct SDLRendererTextureUploader : public TextureUploaderI { SDLRendererTextureUploader(SDL_Renderer* renderer_); ~SDLRendererTextureUploader(void) = default; - uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height) override; + uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter) override; void destroy(uint64_t tex_id) override; }; diff --git a/src/texture_uploader.hpp b/src/texture_uploader.hpp index fb31e47..3fcc024 100644 --- a/src/texture_uploader.hpp +++ b/src/texture_uploader.hpp @@ -5,10 +5,14 @@ struct TextureUploaderI { static constexpr const char* version {"1"}; + enum Filter { + NEAREST, + LINEAR, + }; + virtual ~TextureUploaderI(void) {} - //virtual uint64_t uploadRGBA(const uint8_t* data, uint64_t data_size) = 0; - virtual uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height) = 0; + virtual uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter = LINEAR) = 0; virtual void destroy(uint64_t tex_id) = 0; }; diff --git a/src/tox_avatar_loader.cpp b/src/tox_avatar_loader.cpp index 1e52322..3aa7a8c 100644 --- a/src/tox_avatar_loader.cpp +++ b/src/tox_avatar_loader.cpp @@ -203,7 +203,7 @@ std::optional ToxAvatarLoader::load(TextureUploaderI& tu, Contact3 new_entry.timestamp_last_rendered = Message::getTimeMS(); new_entry.current_texture = 0; - const auto n_t = tu.uploadRGBA(pixels.data(), 5, 5); + const auto n_t = tu.uploadRGBA(pixels.data(), 5, 5, TextureUploaderI::NEAREST); new_entry.textures.push_back(n_t); new_entry.frame_duration.push_back(250);