yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
sdl3_renderer.cc
Go to the documentation of this file.
1#ifdef YAZE_USE_SDL3
2
4
5#include <SDL3/SDL.h>
6
8
9namespace yaze {
10namespace gfx {
11
12SDL3Renderer::SDL3Renderer() = default;
13
14SDL3Renderer::~SDL3Renderer() { Shutdown(); }
15
23bool SDL3Renderer::Initialize(SDL_Window* window) {
24 // Create an SDL3 renderer.
25 // SDL3 API: SDL_CreateRenderer(window, driver_name)
26 // Pass nullptr to let SDL choose the best available driver.
27 renderer_ = SDL_CreateRenderer(window, nullptr);
28
29 if (renderer_ == nullptr) {
30 SDL_Log("SDL_CreateRenderer Error: %s", SDL_GetError());
31 return false;
32 }
33
34 // Set blend mode for transparency support.
35 SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
36
37 // Enable vsync for smoother rendering.
38 SDL_SetRenderVSync(renderer_, 1);
39
40 return true;
41}
42
46void SDL3Renderer::Shutdown() {
47 if (renderer_) {
48 SDL_DestroyRenderer(renderer_);
49 renderer_ = nullptr;
50 }
51}
52
59TextureHandle SDL3Renderer::CreateTexture(int width, int height) {
60 // SDL3 texture creation is largely unchanged from SDL2.
61 return static_cast<TextureHandle>(SDL_CreateTexture(
62 renderer_,
63 static_cast<SDL_PixelFormat>(SDL_PIXELFORMAT_RGBA8888),
64 static_cast<SDL_TextureAccess>(SDL_TEXTUREACCESS_STREAMING), width,
65 height));
66}
67
74TextureHandle SDL3Renderer::CreateTextureWithFormat(int width, int height,
75 uint32_t format,
76 int access) {
77 return static_cast<TextureHandle>(
78 SDL_CreateTexture(renderer_,
79 static_cast<SDL_PixelFormat>(format),
80 static_cast<SDL_TextureAccess>(access), width, height));
81}
82
90void SDL3Renderer::UpdateTexture(TextureHandle texture, const Bitmap& bitmap) {
91 SDL_Surface* surface = bitmap.surface();
92
93 // Validate texture, surface, and surface format
94 if (!texture || !surface || surface->format == SDL_PIXELFORMAT_UNKNOWN) {
95 return;
96 }
97
98 // Validate surface has pixels
99 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
100 return;
101 }
102
103 // Convert the bitmap's surface to RGBA8888 format for compatibility with the
104 // texture.
105 // SDL3 API: SDL_ConvertSurface(surface, format) - no flags parameter
106 SDL_Surface* converted_surface =
107 SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
108
109 if (!converted_surface || !converted_surface->pixels) {
110 if (converted_surface) {
111 SDL_DestroySurface(converted_surface);
112 }
113 return;
114 }
115
116 // Update the texture with the pixels from the converted surface.
117 SDL_UpdateTexture(static_cast<SDL_Texture*>(texture), nullptr,
118 converted_surface->pixels, converted_surface->pitch);
119
120 // SDL3 uses SDL_DestroySurface instead of SDL_FreeSurface
121 SDL_DestroySurface(converted_surface);
122}
123
127void SDL3Renderer::DestroyTexture(TextureHandle texture) {
128 if (texture) {
129 SDL_DestroyTexture(static_cast<SDL_Texture*>(texture));
130 }
131}
132
136bool SDL3Renderer::LockTexture(TextureHandle texture, SDL_Rect* rect,
137 void** pixels, int* pitch) {
138 // SDL3 LockTexture now takes SDL_FRect*, but for simplicity we use the
139 // integer version when available. In SDL3, LockTexture still accepts
140 // SDL_Rect* for the region.
141 return SDL_LockTexture(static_cast<SDL_Texture*>(texture), rect, pixels,
142 pitch);
143}
144
148void SDL3Renderer::UnlockTexture(TextureHandle texture) {
149 SDL_UnlockTexture(static_cast<SDL_Texture*>(texture));
150}
151
155void SDL3Renderer::Clear() { SDL_RenderClear(renderer_); }
156
160void SDL3Renderer::Present() { SDL_RenderPresent(renderer_); }
161
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);
173
174 // SDL3 API: SDL_RenderTexture(renderer, texture, srcrect, dstrect)
175 // Both rectangles use SDL_FRect (float) in SDL3.
176 SDL_RenderTexture(renderer_, static_cast<SDL_Texture*>(texture), src_ptr,
177 dst_ptr);
178}
179
183void SDL3Renderer::SetRenderTarget(TextureHandle texture) {
184 SDL_SetRenderTarget(renderer_, static_cast<SDL_Texture*>(texture));
185}
186
190void SDL3Renderer::SetDrawColor(SDL_Color color) {
191 SDL_SetRenderDrawColor(renderer_, color.r, color.g, color.b, color.a);
192}
193
204SDL_FRect* SDL3Renderer::ToFRect(const SDL_Rect* rect, SDL_FRect* frect) {
205 if (!rect || !frect) {
206 return nullptr;
207 }
208
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);
213
214 return frect;
215}
216
217} // namespace gfx
218} // namespace yaze
219
220#endif // YAZE_USE_SDL3
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:27