12SDL3Renderer::SDL3Renderer() =
default;
14SDL3Renderer::~SDL3Renderer() {
25bool SDL3Renderer::Initialize(SDL_Window* window) {
29 renderer_ = SDL_CreateRenderer(window,
nullptr);
31 if (renderer_ ==
nullptr) {
32 SDL_Log(
"SDL_CreateRenderer Error: %s", SDL_GetError());
37 SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
40 SDL_SetRenderVSync(renderer_, 1);
48void SDL3Renderer::Shutdown() {
50 SDL_DestroyRenderer(renderer_);
61TextureHandle SDL3Renderer::CreateTexture(
int width,
int height) {
64 renderer_,
static_cast<SDL_PixelFormat
>(SDL_PIXELFORMAT_RGBA8888),
65 static_cast<SDL_TextureAccess
>(SDL_TEXTUREACCESS_STREAMING), width,
69TextureHandle SDL3Renderer::CreateRenderTargetTexture(
int width,
int height) {
71 renderer_,
static_cast<SDL_PixelFormat
>(SDL_PIXELFORMAT_RGBA8888),
72 static_cast<SDL_TextureAccess
>(SDL_TEXTUREACCESS_TARGET), width, height));
81TextureHandle SDL3Renderer::CreateTextureWithFormat(
int width,
int height,
85 SDL_CreateTexture(renderer_,
static_cast<SDL_PixelFormat
>(format),
86 static_cast<SDL_TextureAccess
>(access), width, height));
96void SDL3Renderer::UpdateTexture(
TextureHandle texture,
const Bitmap& bitmap) {
97 SDL_Surface* surface = bitmap.surface();
100 if (!texture || !surface || surface->format == SDL_PIXELFORMAT_UNKNOWN) {
105 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
112 SDL_Surface* converted_surface =
113 SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
115 if (!converted_surface || !converted_surface->pixels) {
116 if (converted_surface) {
117 SDL_DestroySurface(converted_surface);
123 SDL_UpdateTexture(
static_cast<SDL_Texture*
>(texture),
nullptr,
124 converted_surface->pixels, converted_surface->pitch);
127 SDL_DestroySurface(converted_surface);
135 SDL_DestroyTexture(
static_cast<SDL_Texture*
>(texture));
142bool SDL3Renderer::LockTexture(
TextureHandle texture, SDL_Rect* rect,
143 void** pixels,
int* pitch) {
147 return SDL_LockTexture(
static_cast<SDL_Texture*
>(texture), rect, pixels,
155 SDL_UnlockTexture(
static_cast<SDL_Texture*
>(texture));
161void SDL3Renderer::Clear() {
162 SDL_RenderClear(renderer_);
168void SDL3Renderer::Present() {
169 SDL_RenderPresent(renderer_);
178void SDL3Renderer::RenderCopy(
TextureHandle texture,
const SDL_Rect* srcrect,
179 const SDL_Rect* dstrect) {
180 SDL_FRect src_frect, dst_frect;
181 SDL_FRect* src_ptr = ToFRect(srcrect, &src_frect);
182 SDL_FRect* dst_ptr = ToFRect(dstrect, &dst_frect);
186 SDL_RenderTexture(renderer_,
static_cast<SDL_Texture*
>(texture), src_ptr,
194 SDL_SetRenderTarget(renderer_,
static_cast<SDL_Texture*
>(texture));
200void SDL3Renderer::SetDrawColor(SDL_Color color) {
201 SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, color.a);
214SDL_FRect* SDL3Renderer::ToFRect(
const SDL_Rect* rect, SDL_FRect* frect) {
215 if (!rect || !frect) {
219 frect->x =
static_cast<float>(rect->x);
220 frect->y =
static_cast<float>(rect->y);
221 frect->w =
static_cast<float>(rect->w);
222 frect->h =
static_cast<float>(rect->h);
void * TextureHandle
An abstract handle representing a texture.