12SDL3Renderer::SDL3Renderer() =
default;
14SDL3Renderer::~SDL3Renderer() { Shutdown(); }
23bool SDL3Renderer::Initialize(SDL_Window* window) {
27 renderer_ = SDL_CreateRenderer(window,
nullptr);
29 if (renderer_ ==
nullptr) {
30 SDL_Log(
"SDL_CreateRenderer Error: %s", SDL_GetError());
35 SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
38 SDL_SetRenderVSync(renderer_, 1);
46void SDL3Renderer::Shutdown() {
48 SDL_DestroyRenderer(renderer_);
59TextureHandle SDL3Renderer::CreateTexture(
int width,
int height) {
63 static_cast<SDL_PixelFormat
>(SDL_PIXELFORMAT_RGBA8888),
64 static_cast<SDL_TextureAccess
>(SDL_TEXTUREACCESS_STREAMING), width,
74TextureHandle SDL3Renderer::CreateTextureWithFormat(
int width,
int height,
78 SDL_CreateTexture(renderer_,
79 static_cast<SDL_PixelFormat
>(format),
80 static_cast<SDL_TextureAccess
>(access), width, height));
90void SDL3Renderer::UpdateTexture(
TextureHandle texture,
const Bitmap& bitmap) {
91 SDL_Surface* surface = bitmap.surface();
94 if (!texture || !surface || surface->format == SDL_PIXELFORMAT_UNKNOWN) {
99 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
106 SDL_Surface* converted_surface =
107 SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
109 if (!converted_surface || !converted_surface->pixels) {
110 if (converted_surface) {
111 SDL_DestroySurface(converted_surface);
117 SDL_UpdateTexture(
static_cast<SDL_Texture*
>(texture),
nullptr,
118 converted_surface->pixels, converted_surface->pitch);
121 SDL_DestroySurface(converted_surface);
129 SDL_DestroyTexture(
static_cast<SDL_Texture*
>(texture));
136bool SDL3Renderer::LockTexture(
TextureHandle texture, SDL_Rect* rect,
137 void** pixels,
int* pitch) {
141 return SDL_LockTexture(
static_cast<SDL_Texture*
>(texture), rect, pixels,
149 SDL_UnlockTexture(
static_cast<SDL_Texture*
>(texture));
155void SDL3Renderer::Clear() { SDL_RenderClear(renderer_); }
160void SDL3Renderer::Present() { SDL_RenderPresent(renderer_); }
168void SDL3Renderer::RenderCopy(
TextureHandle texture,
const SDL_Rect* srcrect,
169 const SDL_Rect* dstrect) {
170 SDL_FRect src_frect, dst_frect;
171 SDL_FRect* src_ptr = ToFRect(srcrect, &src_frect);
172 SDL_FRect* dst_ptr = ToFRect(dstrect, &dst_frect);
176 SDL_RenderTexture(renderer_,
static_cast<SDL_Texture*
>(texture), src_ptr,
184 SDL_SetRenderTarget(renderer_,
static_cast<SDL_Texture*
>(texture));
190void SDL3Renderer::SetDrawColor(SDL_Color color) {
191 SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, color.a);
204SDL_FRect* SDL3Renderer::ToFRect(
const SDL_Rect* rect, SDL_FRect* frect) {
205 if (!rect || !frect) {
209 frect->x =
static_cast<float>(rect->x);
210 frect->y =
static_cast<float>(rect->y);
211 frect->w =
static_cast<float>(rect->w);
212 frect->h =
static_cast<float>(rect->h);
void * TextureHandle
An abstract handle representing a texture.