From 082c4febdfcf02c4a39196c71775fdc7d481e9e9 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 26 Jul 2023 20:09:57 +0200 Subject: [PATCH] more setup --- src/CMakeLists.txt | 5 ++++ src/main.cpp | 5 ++-- src/main_screen.cpp | 44 ++++++++++++++++++++++++++-- src/main_screen.hpp | 21 +++++++++++-- src/sdlrenderer_texture_uploader.cpp | 32 ++++++++++++++++++++ src/sdlrenderer_texture_uploader.hpp | 16 ++++++++++ src/start_screen.cpp | 4 +-- src/start_screen.hpp | 10 ++++++- src/texture_uploader.hpp | 13 ++++++++ 9 files changed, 141 insertions(+), 9 deletions(-) create mode 100644 src/sdlrenderer_texture_uploader.cpp create mode 100644 src/sdlrenderer_texture_uploader.hpp create mode 100644 src/texture_uploader.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 66bef13c..23f8bd0b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,6 +12,11 @@ add_executable(tomato tox_client.hpp tox_client.cpp + + theme.hpp + texture_uploader.hpp + ./sdlrenderer_texture_uploader.hpp + ./sdlrenderer_texture_uploader.cpp ) target_compile_features(tomato PUBLIC cxx_std_17) diff --git a/src/main.cpp b/src/main.cpp index d4472bdd..42a1a1d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include "./theme.hpp" +#include "./sdlrenderer_texture_uploader.hpp" #include "./start_screen.hpp" @@ -33,7 +34,7 @@ int main(int argc, char** argv) { } std::unique_ptr renderer { - SDL_CreateRenderer(window.get(), nullptr, 0), + SDL_CreateRenderer(window.get(), nullptr, SDL_RENDERER_PRESENTVSYNC), &SDL_DestroyRenderer }; if (!renderer) { @@ -52,7 +53,7 @@ int main(int argc, char** argv) { ImGui_ImplSDLRenderer3_Init(renderer.get()); auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown); - std::unique_ptr screen = std::make_unique(); + std::unique_ptr screen = std::make_unique(renderer.get()); bool quit = false; while (!quit) { diff --git a/src/main_screen.cpp b/src/main_screen.cpp index 9cc65c22..c4cf0770 100644 --- a/src/main_screen.cpp +++ b/src/main_screen.cpp @@ -2,9 +2,19 @@ #include +#include + #include -MainScreen::MainScreen(std::string save_path) : rmm(cr), tc(save_path) { +MainScreen::MainScreen(SDL_Renderer* renderer_, std::string save_path) : + renderer(renderer_), + rmm(cr), + tc(save_path), + tcm(cr, tc, tc), + tmm(rmm, cr, tcm, tc, tc), + ttm(rmm, cr, tcm, tc, tc), + sdlrtu(renderer_) +{ tel.subscribeAll(tc); conf.set("tox", "save_file_path", save_path); @@ -33,12 +43,15 @@ MainScreen::MainScreen(std::string save_path) : rmm(cr), tc(save_path) { // graphics g_provideInstance("ImGuiContext", "host", ImGui::GetCurrentContext()); - //g_provideInstance("TextureUploaderI", "host", &ogltu); + g_provideInstance("TextureUploaderI", "host", &sdlrtu); } conf.dump(); } +MainScreen::~MainScreen(void) { +} + Screen* MainScreen::poll(bool& quit) { auto new_time = std::chrono::high_resolution_clock::now(); const float time_delta {std::chrono::duration(new_time - last_time).count()}; @@ -54,6 +67,33 @@ Screen* MainScreen::poll(bool& quit) { quit = !open; } + { // texture tests + const size_t width = 8; + const size_t height = 8; +#define W 0xff, 0xff, 0xff, 0xff +#define B 0x00, 0x00, 0x00, 0xff +#define P 0xff, 0x00, 0xff, 0xff + static uint8_t raw_pixel[width*height*4] { + P, W, W, W, W, W, W, P, + W, W, W, W, W, W, W, W, + W, W, W, W, W, W, W, W, + W, W, W, B, B, W, W, W, + W, W, W, B, B, W, W, W, + W, W, W, W, W, W, W, W, + W, W, W, W, W, W, W, W, + P, W, W, W, W, W, W, P, + }; + + static uint64_t texture = sdlrtu.uploadRGBA(raw_pixel, width, height); + + if (ImGui::Begin("test texture")) { + ImGui::Text("test texture windoajsdf"); + + ImGui::Image(reinterpret_cast(texture), {width*10, height*10}); + } + ImGui::End(); + } + return nullptr; } diff --git a/src/main_screen.hpp b/src/main_screen.hpp index a0d83282..22f8f25b 100644 --- a/src/main_screen.hpp +++ b/src/main_screen.hpp @@ -8,13 +8,26 @@ #include #include +#include +#include +#include + #include "./tox_client.hpp" +#include "./sdlrenderer_texture_uploader.hpp" + #include #include #include +// fwd +extern "C" { + struct SDL_Renderer; +} // C + struct MainScreen final : public Screen { + SDL_Renderer* renderer; + std::chrono::high_resolution_clock::time_point last_time = std::chrono::high_resolution_clock::now(); SimpleConfigModel conf; @@ -25,12 +38,16 @@ struct MainScreen final : public Screen { ToxEventLogger tel{std::cout}; ToxClient tc; + ToxContactModel2 tcm; + ToxMessageManager tmm; + ToxTransferManager ttm; + SDLRendererTextureUploader sdlrtu; //OpenGLTextureUploader ogltu; - MainScreen(std::string save_path); - ~MainScreen(void) = default; + MainScreen(SDL_Renderer* renderer_, std::string save_path); + ~MainScreen(void); // return nullptr if not next // sets bool quit to true if exit diff --git a/src/sdlrenderer_texture_uploader.cpp b/src/sdlrenderer_texture_uploader.cpp new file mode 100644 index 00000000..6f9684fd --- /dev/null +++ b/src/sdlrenderer_texture_uploader.cpp @@ -0,0 +1,32 @@ +#include "./sdlrenderer_texture_uploader.hpp" + +#include + +SDLRendererTextureUploader::SDLRendererTextureUploader(SDL_Renderer* renderer_) : + renderer(renderer_) +{ +} + +uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height) { + // TODO: test if pitch is 4 or 4*width + SDL_Surface* surf = SDL_CreateSurfaceFrom( + (void*)data, + width, height, + 4*width, + //SDL_PIXELFORMAT_RGBA8888 + SDL_PIXELFORMAT_ABGR8888 // little endian + ); + assert(surf); // TODO: add error reporting + + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); + assert(tex); // TODO: add error reporting + + SDL_DestroySurface(surf); + + return reinterpret_cast(tex); +} + +void SDLRendererTextureUploader::destroy(uint64_t tex_id) { + SDL_DestroyTexture(static_cast(reinterpret_cast(tex_id))); +} + diff --git a/src/sdlrenderer_texture_uploader.hpp b/src/sdlrenderer_texture_uploader.hpp new file mode 100644 index 00000000..e007d33b --- /dev/null +++ b/src/sdlrenderer_texture_uploader.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "./texture_uploader.hpp" + +#include + +struct SDLRendererTextureUploader : public TextureUploaderI { + SDL_Renderer* renderer; + SDLRendererTextureUploader(void) = delete; + SDLRendererTextureUploader(SDL_Renderer* renderer_); + ~SDLRendererTextureUploader(void) = default; + + uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height) override; + void destroy(uint64_t tex_id) override; +}; + diff --git a/src/start_screen.cpp b/src/start_screen.cpp index 61fa765b..3fbcaa13 100644 --- a/src/start_screen.cpp +++ b/src/start_screen.cpp @@ -4,7 +4,7 @@ #include -StartScreen::StartScreen(void) { +StartScreen::StartScreen(SDL_Renderer* renderer) : _renderer(renderer) { } Screen* StartScreen::poll(bool&) { @@ -18,7 +18,7 @@ Screen* StartScreen::poll(bool&) { // | +------+ +-------- // +---------------------------- - auto new_screen = std::make_unique("tomato.tox"); + auto new_screen = std::make_unique(_renderer, "tomato.tox"); return new_screen.release(); } diff --git a/src/start_screen.hpp b/src/start_screen.hpp index 632feace..c36e3402 100644 --- a/src/start_screen.hpp +++ b/src/start_screen.hpp @@ -2,8 +2,16 @@ #include "./screen.hpp" +// fwd +extern "C" { + struct SDL_Renderer; +} // C + struct StartScreen final : public Screen { - StartScreen(void); + SDL_Renderer* _renderer; + + StartScreen(void) = delete; + StartScreen(SDL_Renderer* renderer); ~StartScreen(void) = default; // return nullptr if not next diff --git a/src/texture_uploader.hpp b/src/texture_uploader.hpp new file mode 100644 index 00000000..4e16ea05 --- /dev/null +++ b/src/texture_uploader.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +struct TextureUploaderI { + 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 void destroy(uint64_t tex_id) = 0; +}; +