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 =
59 SDL_CreateTexture(renderer_.get(), SDL_PIXELFORMAT_RGBA8888,
60 SDL_TEXTUREACCESS_STREAMING, width, height);
61
62 if (texture) {
63 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
64 }
65
66 return static_cast<TextureHandle>(texture);
67}
68
70 SDL_Texture* texture =
71 SDL_CreateTexture(renderer_.get(), SDL_PIXELFORMAT_RGBA8888,
72 SDL_TEXTUREACCESS_TARGET, width, height);
73
74 if (texture) {
75 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
76 }
77
78 return static_cast<TextureHandle>(texture);
79}
80
86 uint32_t format,
87 int access) {
88 return static_cast<TextureHandle>(
89 SDL_CreateTexture(renderer_.get(), format, access, width, height));
90}
91
97void SDL2Renderer::UpdateTexture(TextureHandle texture, const Bitmap& bitmap) {
98 SDL_Surface* surface = bitmap.surface();
99
100 // Validate texture, surface, and surface format
101 if (!texture || !surface || !surface->format) {
102 return;
103 }
104
105 // Validate surface has pixels
106 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
107 return;
108 }
109
110 // Convert the bitmap's surface to RGBA8888 format for compatibility with the
111 // texture.
112 auto converted_surface =
113 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>(
114 platform::ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA8888));
115
116 if (!converted_surface || !converted_surface->pixels) {
117 return;
118 }
119
120 // Update the texture with the pixels from the converted surface.
121 SDL_UpdateTexture(static_cast<SDL_Texture*>(texture), nullptr,
122 converted_surface->pixels, converted_surface->pitch);
123}
124
129 if (texture) {
130 SDL_DestroyTexture(static_cast<SDL_Texture*>(texture));
131 }
132}
133
134bool SDL2Renderer::LockTexture(TextureHandle texture, SDL_Rect* rect,
135 void** pixels, int* pitch) {
136 return SDL_LockTexture(static_cast<SDL_Texture*>(texture), rect, pixels,
137 pitch) == 0;
138}
139
141 SDL_UnlockTexture(static_cast<SDL_Texture*>(texture));
142}
143
148 SDL_RenderClear(renderer_.get());
149}
150
155 SDL_RenderPresent(renderer_.get());
156}
157
161void SDL2Renderer::RenderCopy(TextureHandle texture, const SDL_Rect* srcrect,
162 const SDL_Rect* dstrect) {
163 platform::RenderTexture(renderer_.get(), static_cast<SDL_Texture*>(texture),
164 srcrect, dstrect);
165}
166
171 SDL_SetRenderTarget(renderer_.get(), static_cast<SDL_Texture*>(texture));
172}
173
177void SDL2Renderer::SetDrawColor(SDL_Color color) {
178 SDL_SetRenderDrawColor(renderer_.get(), color.r, color.g, color.b, color.a);
179}
180
181} // namespace gfx
182} // 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 ...
TextureHandle CreateRenderTargetTexture(int width, int height) override
Creates a texture intended to be used as a render target.
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:47
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.