forked from Green-Sky/tomato
add sdl bmp image loader
This commit is contained in:
parent
42b3866753
commit
c0b57c30bd
@ -1,22 +1,26 @@
|
||||
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
|
||||
|
||||
add_executable(tomato
|
||||
main.cpp
|
||||
icon.rc
|
||||
./main.cpp
|
||||
./icon.rc
|
||||
|
||||
screen.hpp
|
||||
start_screen.hpp
|
||||
start_screen.cpp
|
||||
main_screen.hpp
|
||||
main_screen.cpp
|
||||
./screen.hpp
|
||||
./start_screen.hpp
|
||||
./start_screen.cpp
|
||||
./main_screen.hpp
|
||||
./main_screen.cpp
|
||||
|
||||
./tox_client.hpp
|
||||
./tox_client.cpp
|
||||
./auto_dirty.hpp
|
||||
./auto_dirty.cpp
|
||||
|
||||
theme.hpp
|
||||
texture_uploader.hpp
|
||||
./image_loader.hpp
|
||||
./image_loader_sdl_bmp.hpp
|
||||
./image_loader_sdl_bmp.cpp
|
||||
|
||||
./theme.hpp
|
||||
./texture_uploader.hpp
|
||||
./sdlrenderer_texture_uploader.hpp
|
||||
./sdlrenderer_texture_uploader.cpp
|
||||
./sdl_clipboard_utils.hpp
|
||||
|
26
src/image_loader.hpp
Normal file
26
src/image_loader.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
struct ImageLoaderI {
|
||||
virtual ~ImageLoaderI(void) {}
|
||||
|
||||
struct ImageInfo {
|
||||
uint32_t width {0};
|
||||
uint32_t height {0};
|
||||
};
|
||||
virtual ImageInfo loadInfoFromMemory(const uint8_t* data, uint64_t data_size) = 0;
|
||||
|
||||
struct ImageResult {
|
||||
uint32_t width {0};
|
||||
uint32_t height {0};
|
||||
struct Frame {
|
||||
int32_t ms {0};
|
||||
std::vector<uint8_t> data;
|
||||
};
|
||||
std::vector<Frame> frames;
|
||||
};
|
||||
virtual ImageResult loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) = 0;
|
||||
};
|
||||
|
53
src/image_loader_sdl_bmp.cpp
Normal file
53
src/image_loader_sdl_bmp.cpp
Normal file
@ -0,0 +1,53 @@
|
||||
#include "./image_loader_sdl_bmp.hpp"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
ImageLoaderSDLBMP::ImageInfo ImageLoaderSDLBMP::loadInfoFromMemory(const uint8_t* data, uint64_t data_size) {
|
||||
ImageInfo res;
|
||||
|
||||
auto* rw_ctx = SDL_RWFromConstMem(data, data_size);
|
||||
|
||||
SDL_Surface* surf = SDL_LoadBMP_RW(rw_ctx, SDL_TRUE);
|
||||
if (surf == nullptr) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res.width = surf->w;
|
||||
res.height = surf->h;
|
||||
|
||||
SDL_DestroySurface(surf);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
ImageLoaderSDLBMP::ImageResult ImageLoaderSDLBMP::loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) {
|
||||
ImageResult res;
|
||||
|
||||
auto* rw_ctx = SDL_RWFromConstMem(data, data_size);
|
||||
|
||||
SDL_Surface* surf = SDL_LoadBMP_RW(rw_ctx, SDL_TRUE);
|
||||
if (surf == nullptr) {
|
||||
return res;
|
||||
}
|
||||
|
||||
SDL_Surface* conv_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_RGBA32);
|
||||
SDL_DestroySurface(surf);
|
||||
if (conv_surf == nullptr) {
|
||||
return res;
|
||||
}
|
||||
|
||||
res.width = surf->w;
|
||||
res.height = surf->h;
|
||||
|
||||
SDL_LockSurface(conv_surf);
|
||||
|
||||
auto& new_frame = res.frames.emplace_back();
|
||||
new_frame.ms = 0;
|
||||
new_frame.data.insert(new_frame.data.cbegin(), (const uint8_t*)conv_surf->pixels, ((const uint8_t*)conv_surf->pixels) + (surf->w*surf->h*4));
|
||||
|
||||
SDL_UnlockSurface(conv_surf);
|
||||
SDL_DestroySurface(conv_surf);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
9
src/image_loader_sdl_bmp.hpp
Normal file
9
src/image_loader_sdl_bmp.hpp
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "./image_loader.hpp"
|
||||
|
||||
struct ImageLoaderSDLBMP : public ImageLoaderI {
|
||||
ImageInfo loadInfoFromMemory(const uint8_t* data, uint64_t data_size) override;
|
||||
ImageResult loadFromMemoryRGBA(const uint8_t* data, uint64_t data_size) override;
|
||||
};
|
||||
|
@ -13,8 +13,7 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi
|
||||
(void*)data,
|
||||
width, height,
|
||||
4*width,
|
||||
//SDL_PIXELFORMAT_RGBA8888
|
||||
SDL_PIXELFORMAT_ABGR8888 // little endian
|
||||
SDL_PIXELFORMAT_RGBA32 // auto big/little
|
||||
);
|
||||
assert(surf); // TODO: add error reporting
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user