more setup

This commit is contained in:
Green Sky 2023-07-26 20:09:57 +02:00
parent a848a01527
commit 082c4febdf
No known key found for this signature in database
9 changed files with 141 additions and 9 deletions

View File

@ -12,6 +12,11 @@ add_executable(tomato
tox_client.hpp tox_client.hpp
tox_client.cpp tox_client.cpp
theme.hpp
texture_uploader.hpp
./sdlrenderer_texture_uploader.hpp
./sdlrenderer_texture_uploader.cpp
) )
target_compile_features(tomato PUBLIC cxx_std_17) target_compile_features(tomato PUBLIC cxx_std_17)

View File

@ -6,6 +6,7 @@
#include <imgui/backends/imgui_impl_sdlrenderer3.h> #include <imgui/backends/imgui_impl_sdlrenderer3.h>
#include "./theme.hpp" #include "./theme.hpp"
#include "./sdlrenderer_texture_uploader.hpp"
#include "./start_screen.hpp" #include "./start_screen.hpp"
@ -33,7 +34,7 @@ int main(int argc, char** argv) {
} }
std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer { std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)> renderer {
SDL_CreateRenderer(window.get(), nullptr, 0), SDL_CreateRenderer(window.get(), nullptr, SDL_RENDERER_PRESENTVSYNC),
&SDL_DestroyRenderer &SDL_DestroyRenderer
}; };
if (!renderer) { if (!renderer) {
@ -52,7 +53,7 @@ int main(int argc, char** argv) {
ImGui_ImplSDLRenderer3_Init(renderer.get()); ImGui_ImplSDLRenderer3_Init(renderer.get());
auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown); auto imgui_sdlrenderer_scope = std::async(std::launch::deferred, &ImGui_ImplSDLRenderer3_Shutdown);
std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(); std::unique_ptr<Screen> screen = std::make_unique<StartScreen>(renderer.get());
bool quit = false; bool quit = false;
while (!quit) { while (!quit) {

View File

@ -2,9 +2,19 @@
#include <imgui/imgui.h> #include <imgui/imgui.h>
#include <SDL3/SDL.h>
#include <memory> #include <memory>
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); tel.subscribeAll(tc);
conf.set("tox", "save_file_path", save_path); conf.set("tox", "save_file_path", save_path);
@ -33,12 +43,15 @@ MainScreen::MainScreen(std::string save_path) : rmm(cr), tc(save_path) {
// graphics // graphics
g_provideInstance("ImGuiContext", "host", ImGui::GetCurrentContext()); g_provideInstance("ImGuiContext", "host", ImGui::GetCurrentContext());
//g_provideInstance<TextureUploaderI>("TextureUploaderI", "host", &ogltu); g_provideInstance<TextureUploaderI>("TextureUploaderI", "host", &sdlrtu);
} }
conf.dump(); conf.dump();
} }
MainScreen::~MainScreen(void) {
}
Screen* MainScreen::poll(bool& quit) { Screen* MainScreen::poll(bool& quit) {
auto new_time = std::chrono::high_resolution_clock::now(); auto new_time = std::chrono::high_resolution_clock::now();
const float time_delta {std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time).count()}; const float time_delta {std::chrono::duration<float, std::chrono::seconds::period>(new_time - last_time).count()};
@ -54,6 +67,33 @@ Screen* MainScreen::poll(bool& quit) {
quit = !open; 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<void*>(texture), {width*10, height*10});
}
ImGui::End();
}
return nullptr; return nullptr;
} }

View File

@ -8,13 +8,26 @@
#include <solanaceae/plugin/plugin_manager.hpp> #include <solanaceae/plugin/plugin_manager.hpp>
#include <solanaceae/toxcore/tox_event_logger.hpp> #include <solanaceae/toxcore/tox_event_logger.hpp>
#include <solanaceae/tox_contacts/tox_contact_model2.hpp>
#include <solanaceae/tox_messages/tox_message_manager.hpp>
#include <solanaceae/tox_messages/tox_transfer_manager.hpp>
#include "./tox_client.hpp" #include "./tox_client.hpp"
#include "./sdlrenderer_texture_uploader.hpp"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
// fwd
extern "C" {
struct SDL_Renderer;
} // C
struct MainScreen final : public Screen { struct MainScreen final : public Screen {
SDL_Renderer* renderer;
std::chrono::high_resolution_clock::time_point last_time = std::chrono::high_resolution_clock::now(); std::chrono::high_resolution_clock::time_point last_time = std::chrono::high_resolution_clock::now();
SimpleConfigModel conf; SimpleConfigModel conf;
@ -25,12 +38,16 @@ struct MainScreen final : public Screen {
ToxEventLogger tel{std::cout}; ToxEventLogger tel{std::cout};
ToxClient tc; ToxClient tc;
ToxContactModel2 tcm;
ToxMessageManager tmm;
ToxTransferManager ttm;
SDLRendererTextureUploader sdlrtu;
//OpenGLTextureUploader ogltu; //OpenGLTextureUploader ogltu;
MainScreen(std::string save_path); MainScreen(SDL_Renderer* renderer_, std::string save_path);
~MainScreen(void) = default; ~MainScreen(void);
// return nullptr if not next // return nullptr if not next
// sets bool quit to true if exit // sets bool quit to true if exit

View File

@ -0,0 +1,32 @@
#include "./sdlrenderer_texture_uploader.hpp"
#include <cassert>
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<uint64_t>(tex);
}
void SDLRendererTextureUploader::destroy(uint64_t tex_id) {
SDL_DestroyTexture(static_cast<SDL_Texture*>(reinterpret_cast<void*>(tex_id)));
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "./texture_uploader.hpp"
#include <SDL3/SDL.h>
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;
};

View File

@ -4,7 +4,7 @@
#include <memory> #include <memory>
StartScreen::StartScreen(void) { StartScreen::StartScreen(SDL_Renderer* renderer) : _renderer(renderer) {
} }
Screen* StartScreen::poll(bool&) { Screen* StartScreen::poll(bool&) {
@ -18,7 +18,7 @@ Screen* StartScreen::poll(bool&) {
// | +------+ +-------- // | +------+ +--------
// +---------------------------- // +----------------------------
auto new_screen = std::make_unique<MainScreen>("tomato.tox"); auto new_screen = std::make_unique<MainScreen>(_renderer, "tomato.tox");
return new_screen.release(); return new_screen.release();
} }

View File

@ -2,8 +2,16 @@
#include "./screen.hpp" #include "./screen.hpp"
// fwd
extern "C" {
struct SDL_Renderer;
} // C
struct StartScreen final : public Screen { struct StartScreen final : public Screen {
StartScreen(void); SDL_Renderer* _renderer;
StartScreen(void) = delete;
StartScreen(SDL_Renderer* renderer);
~StartScreen(void) = default; ~StartScreen(void) = default;
// return nullptr if not next // return nullptr if not next

13
src/texture_uploader.hpp Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <cstdint>
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;
};