refactor file from messages to objects (#27)

* part 1 move files from messages to objects tomato - did not touch chat_gui yet, but image loaders and other stuff
* part 1.1
* part 2, mostly chatgui - also ngcft1 behind the scenes
* part 3 - port over rest, except for avatar_manager, which is effectivly disabled
* fix surface missused causing bmp loader to crash
* fixing small issues and small forward refactor
This commit is contained in:
Erik Scholz
2024-07-31 18:10:52 +02:00
committed by GitHub
parent f27d178b78
commit 2abf09ac06
17 changed files with 621 additions and 348 deletions

View File

@ -3,6 +3,7 @@
#include <SDL3/SDL.h>
#include <iostream>
#include <cassert>
ImageLoaderSDLBMP::ImageInfo ImageLoaderSDLBMP::loadInfoFromMemory(const uint8_t* data, uint64_t data_size) {
ImageInfo res;
@ -14,6 +15,9 @@ ImageLoaderSDLBMP::ImageInfo ImageLoaderSDLBMP::loadInfoFromMemory(const uint8_t
return res;
}
assert(surf->w >= 0);
assert(surf->h >= 0);
res.width = surf->w;
res.height = surf->h;
res.file_ext = "bmp";
@ -24,32 +28,48 @@ ImageLoaderSDLBMP::ImageInfo ImageLoaderSDLBMP::loadInfoFromMemory(const uint8_t
}
ImageLoaderSDLBMP::ImageResult ImageLoaderSDLBMP::loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) {
ImageResult res;
auto* ios = SDL_IOFromConstMem(data, data_size);
SDL_Surface* surf = SDL_LoadBMP_IO(ios, SDL_TRUE);
if (surf == nullptr) {
return res;
return {};
}
SDL_Surface* conv_surf = SDL_ConvertSurface(surf, SDL_PIXELFORMAT_RGBA32);
SDL_DestroySurface(surf);
if (conv_surf == nullptr) {
return res;
return {};
}
res.width = surf->w;
res.height = surf->h;
res.file_ext = "bmp";
assert(conv_surf->w >= 0);
assert(conv_surf->h >= 0);
SDL_LockSurface(conv_surf);
if (conv_surf->w > 16*1024 || conv_surf->h > 10*1024) {
std::cerr << "IL_SDLBMP error: image too large\n";
return {};
}
ImageResult res;
if (SDL_MUSTLOCK(conv_surf)) {
if (SDL_LockSurface(conv_surf) < 0) {
std::cerr << "IL_SDLBMP error: " << SDL_GetError() << "\n";
SDL_DestroySurface(conv_surf);
return {};
}
}
res.width = conv_surf->w;
res.height = conv_surf->h;
res.file_ext = "bmp";
auto& new_frame = res.frames.emplace_back();
new_frame.ms = 0;
new_frame.data = {(const uint8_t*)conv_surf->pixels, ((const uint8_t*)conv_surf->pixels) + (surf->w*surf->h*4)};
new_frame.data = {(const uint8_t*)conv_surf->pixels, ((const uint8_t*)conv_surf->pixels) + (conv_surf->w*conv_surf->h*4)};
SDL_UnlockSurface(conv_surf);
if (SDL_MUSTLOCK(conv_surf)) {
SDL_UnlockSurface(conv_surf);
}
SDL_DestroySurface(conv_surf);
std::cout << "IL_SDLBMP: loaded img " << res.width << "x" << res.height << "\n";