yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
sdl2_renderer.cc
Go to the documentation of this file.
2
3#include "absl/strings/str_format.h"
6
7namespace yaze {
8namespace gfx {
9
11
15
21bool SDL2Renderer::Initialize(SDL_Window* window) {
22 // Create an SDL2 renderer with hardware acceleration.
23 renderer_ = std::unique_ptr<SDL_Renderer, util::SDL_Deleter>(
25
26 if (renderer_ == nullptr) {
27 // Log an error if renderer creation fails.
28 printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
29 return false;
30 }
31
32 // SDL3 sets vsync separately; this is a no-op on SDL2.
34
35 // Set the blend mode to allow for transparency.
36 SDL_SetRenderDrawBlendMode(renderer_.get(), SDL_BLENDMODE_BLEND);
37 return true;
38}
39
46 renderer_.reset();
47}
48
55 // The TextureHandle is a void*, so we cast the SDL_Texture* to it.
56 // SDL2's SDL_CreateTexture takes Uint32 for format
57 // SDL2's SDL_CreateTexture takes Uint32 for format
58 SDL_Texture* texture = SDL_CreateTexture(
59 renderer_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING,
60 width, height);
61
62 if (texture) {
63 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
64 }
65
66 return static_cast<TextureHandle>(texture);
67}
68
74 uint32_t format,
75 int access) {
76 return static_cast<TextureHandle>(
77 SDL_CreateTexture(renderer_.get(), format, access, width, height));
78}
79
85void SDL2Renderer::UpdateTexture(TextureHandle texture, const Bitmap& bitmap) {
86 SDL_Surface* surface = bitmap.surface();
87
88 // Validate texture, surface, and surface format
89 if (!texture || !surface || !surface->format) {
90 return;
91 }
92
93 // Validate surface has pixels
94 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
95 return;
96 }
97
98 // Convert the bitmap's surface to RGBA8888 format for compatibility with the
99 // texture.
100 auto converted_surface =
101 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>(
102 platform::ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA8888));
103
104 if (!converted_surface || !converted_surface->pixels) {
105 return;
106 }
107
108 // Update the texture with the pixels from the converted surface.
109 SDL_UpdateTexture(static_cast<SDL_Texture*>(texture), nullptr,
110 converted_surface->pixels, converted_surface->pitch);
111}
112
117 if (texture) {
118 SDL_DestroyTexture(static_cast<SDL_Texture*>(texture));
119 }
120}
121
122bool SDL2Renderer::LockTexture(TextureHandle texture, SDL_Rect* rect,
123 void** pixels, int* pitch) {
124 return SDL_LockTexture(static_cast<SDL_Texture*>(texture), rect, pixels,
125 pitch) == 0;
126}
127
129 SDL_UnlockTexture(static_cast<SDL_Texture*>(texture));
130}
131
136 SDL_RenderClear(renderer_.get());
137}
138
143 SDL_RenderPresent(renderer_.get());
144}
145
149void SDL2Renderer::RenderCopy(TextureHandle texture, const SDL_Rect* srcrect,
150 const SDL_Rect* dstrect) {
152 static_cast<SDL_Texture*>(texture), srcrect, dstrect);
153}
154
159 SDL_SetRenderTarget(renderer_.get(), static_cast<SDL_Texture*>(texture));
160}
161
165void SDL2Renderer::SetDrawColor(SDL_Color color) {
166 SDL_SetRenderDrawColor(renderer_.get(), color.r, color.g, color.b, color.a);
167}
168
169} // namespace gfx
170} // namespace yaze
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
SDL_Surface * surface() const
Definition bitmap.h:379
void SetDrawColor(SDL_Color color) override
Sets the draw color.
std::unique_ptr< SDL_Renderer, util::SDL_Deleter > renderer_
bool Initialize(SDL_Window *window) override
Initializes the SDL2 renderer. This function creates an accelerated SDL2 renderer and attaches it to ...
void DestroyTexture(TextureHandle texture) override
Destroys an SDL_Texture.
void UpdateTexture(TextureHandle texture, const Bitmap &bitmap) override
Updates an SDL_Texture with data from a Bitmap. This involves converting the bitmap's surface to the ...
void SetRenderTarget(TextureHandle texture) override
Sets the render target.
TextureHandle CreateTextureWithFormat(int width, int height, uint32_t format, int access) override
Creates an SDL_Texture with a specific pixel format and access pattern. This is useful for specialize...
TextureHandle CreateTexture(int width, int height) override
Creates an SDL_Texture. The texture is created with streaming access, which is suitable for textures ...
bool LockTexture(TextureHandle texture, SDL_Rect *rect, void **pixels, int *pitch) override
void UnlockTexture(TextureHandle texture) override
void Present() override
Presents the rendered frame to the screen.
void Clear() override
Clears the screen with the current draw color.
void Shutdown() override
Shuts down the renderer. The underlying SDL_Renderer is managed by a unique_ptr, so its destruction i...
void RenderCopy(TextureHandle texture, const SDL_Rect *srcrect, const SDL_Rect *dstrect) override
Copies a texture to the render target.
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:27
SDL_Surface * ConvertSurfaceFormat(SDL_Surface *surface, uint32_t format, uint32_t flags=0)
Convert a surface to a specific pixel format.
Definition sdl_compat.h:361
SDL_Renderer * CreateRenderer(SDL_Window *window)
Create a renderer with default settings.
Definition sdl_compat.h:275
void SetRenderVSync(SDL_Renderer *renderer, int interval)
Set vertical sync for the renderer.
Definition sdl_compat.h:289
bool RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *srcrect, const SDL_Rect *dstrect)
Render a texture to the current render target.
Definition sdl_compat.h:307
SDL2/SDL3 compatibility layer.