2023-07-26 20:09:57 +02:00
|
|
|
#include "./sdlrenderer_texture_uploader.hpp"
|
|
|
|
|
|
|
|
#include <cassert>
|
2024-03-08 22:04:58 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2023-07-26 20:09:57 +02:00
|
|
|
|
2024-03-09 16:26:35 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-07-26 20:09:57 +02:00
|
|
|
SDLRendererTextureUploader::SDLRendererTextureUploader(SDL_Renderer* renderer_) :
|
|
|
|
renderer(renderer_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-03-08 22:04:58 +01:00
|
|
|
uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter, Access access) {
|
2023-07-26 20:09:57 +02:00
|
|
|
// TODO: test if pitch is 4 or 4*width
|
|
|
|
SDL_Surface* surf = SDL_CreateSurfaceFrom(
|
|
|
|
(void*)data,
|
|
|
|
width, height,
|
|
|
|
4*width,
|
2023-07-30 16:00:55 +02:00
|
|
|
SDL_PIXELFORMAT_RGBA32 // auto big/little
|
2023-07-26 20:09:57 +02:00
|
|
|
);
|
|
|
|
assert(surf); // TODO: add error reporting
|
|
|
|
|
2024-03-09 16:26:35 +01:00
|
|
|
SDL_Texture* tex = SDL_CreateTexture(
|
|
|
|
renderer,
|
|
|
|
surf->format->format,
|
|
|
|
access == Access::STREAMING ? SDL_TEXTUREACCESS_STREAMING : SDL_TEXTUREACCESS_STATIC,
|
|
|
|
surf->w, surf->h
|
|
|
|
);
|
2024-01-21 20:20:32 +01:00
|
|
|
assert(tex); // TODO: add error reporting
|
2024-03-09 16:26:35 +01:00
|
|
|
// TODO: error reporting
|
|
|
|
SDL_UpdateTexture(tex, nullptr, surf->pixels, surf->pitch);
|
2024-01-21 20:20:32 +01:00
|
|
|
|
2024-01-21 13:58:22 +01:00
|
|
|
if (filter == NEAREST) {
|
2024-01-21 20:20:32 +01:00
|
|
|
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_NEAREST);
|
2024-01-21 13:58:22 +01:00
|
|
|
} else if (filter == LINEAR) {
|
2024-01-21 20:20:32 +01:00
|
|
|
SDL_SetTextureScaleMode(tex, SDL_SCALEMODE_LINEAR);
|
2024-01-21 13:58:22 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 20:09:57 +02:00
|
|
|
SDL_DestroySurface(surf);
|
|
|
|
|
|
|
|
return reinterpret_cast<uint64_t>(tex);
|
|
|
|
}
|
|
|
|
|
2024-03-08 22:04:58 +01:00
|
|
|
bool SDLRendererTextureUploader::updateRGBA(uint64_t tex_id, const uint8_t* data, size_t size) {
|
|
|
|
auto* texture = static_cast<SDL_Texture*>(reinterpret_cast<void*>(tex_id));
|
|
|
|
if (texture == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* pixels = nullptr;
|
|
|
|
int pitch = 0;
|
|
|
|
|
|
|
|
if (SDL_LockTexture(texture, nullptr, (void**)&pixels, &pitch) != 0) {
|
2024-03-09 16:26:35 +01:00
|
|
|
std::cerr << "SDLRTU error: failed locking texture '" << SDL_GetError() << "'\n";
|
2024-03-08 22:04:58 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::memcpy(pixels, data, size);
|
|
|
|
|
|
|
|
SDL_UnlockTexture(texture);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-26 20:09:57 +02:00
|
|
|
void SDLRendererTextureUploader::destroy(uint64_t tex_id) {
|
|
|
|
SDL_DestroyTexture(static_cast<SDL_Texture*>(reinterpret_cast<void*>(tex_id)));
|
|
|
|
}
|
|
|
|
|