add updating to textures

This commit is contained in:
Green Sky 2024-03-08 22:04:58 +01:00
parent 6709dba0e7
commit a3d193516c
No known key found for this signature in database
3 changed files with 47 additions and 4 deletions

View File

@ -1,13 +1,15 @@
#include "./sdlrenderer_texture_uploader.hpp" #include "./sdlrenderer_texture_uploader.hpp"
#include <cassert> #include <cassert>
#include <cstdint>
#include <cstring>
SDLRendererTextureUploader::SDLRendererTextureUploader(SDL_Renderer* renderer_) : SDLRendererTextureUploader::SDLRendererTextureUploader(SDL_Renderer* renderer_) :
renderer(renderer_) renderer(renderer_)
{ {
} }
uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter) { uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter, Access access) {
// TODO: test if pitch is 4 or 4*width // TODO: test if pitch is 4 or 4*width
SDL_Surface* surf = SDL_CreateSurfaceFrom( SDL_Surface* surf = SDL_CreateSurfaceFrom(
(void*)data, (void*)data,
@ -17,6 +19,14 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi
); );
assert(surf); // TODO: add error reporting assert(surf); // TODO: add error reporting
// hacky hint usage
if (access == Access::STREAMING) {
SDL_SetHint("SDL_TextureAccess", "SDL_TEXTUREACCESS_STREAMING");
} else {
SDL_SetHint("SDL_TextureAccess", "SDL_TEXTUREACCESS_STATIC");
}
// TODO: cleanup hints after
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf); SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
assert(tex); // TODO: add error reporting assert(tex); // TODO: add error reporting
@ -31,6 +41,27 @@ uint64_t SDLRendererTextureUploader::uploadRGBA(const uint8_t* data, uint32_t wi
return reinterpret_cast<uint64_t>(tex); return reinterpret_cast<uint64_t>(tex);
} }
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) {
// TODO: error
return false;
}
std::memcpy(pixels, data, size);
SDL_UnlockTexture(texture);
return true;
}
void SDLRendererTextureUploader::destroy(uint64_t tex_id) { void SDLRendererTextureUploader::destroy(uint64_t tex_id) {
SDL_DestroyTexture(static_cast<SDL_Texture*>(reinterpret_cast<void*>(tex_id))); SDL_DestroyTexture(static_cast<SDL_Texture*>(reinterpret_cast<void*>(tex_id)));
} }

View File

@ -10,7 +10,8 @@ struct SDLRendererTextureUploader : public TextureUploaderI {
SDLRendererTextureUploader(SDL_Renderer* renderer_); SDLRendererTextureUploader(SDL_Renderer* renderer_);
~SDLRendererTextureUploader(void) = default; ~SDLRendererTextureUploader(void) = default;
uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter) override; uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter, Access access) override;
bool updateRGBA(uint64_t tex_id, const uint8_t* data, size_t size) override;
void destroy(uint64_t tex_id) override; void destroy(uint64_t tex_id) override;
}; };

View File

@ -1,18 +1,29 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include <cstddef>
struct TextureUploaderI { struct TextureUploaderI {
static constexpr const char* version {"1"}; static constexpr const char* version {"2"};
enum Filter { enum Filter {
NEAREST, NEAREST,
LINEAR, LINEAR,
}; };
enum Access {
STATIC,
STREAMING,
// target?
};
virtual ~TextureUploaderI(void) {} virtual ~TextureUploaderI(void) {}
virtual uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter = LINEAR) = 0; virtual uint64_t uploadRGBA(const uint8_t* data, uint32_t width, uint32_t height, Filter filter = LINEAR, Access access = STATIC) = 0;
// keeps width height filter
// TODO: wh instead of size?
virtual bool updateRGBA(uint64_t tex_id, const uint8_t* data, size_t size) = 0;
virtual void destroy(uint64_t tex_id) = 0; virtual void destroy(uint64_t tex_id) = 0;
}; };