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#include "absl/strings/str_format.h"
3#include "app/gfx/bitmap.h"
4
5namespace yaze {
6namespace gfx {
7
9
13
18bool SDL2Renderer::Initialize(SDL_Window* window) {
19 // Create an SDL2 renderer with hardware acceleration.
20 renderer_ = std::unique_ptr<SDL_Renderer, util::SDL_Deleter>(
21 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED));
22
23 if (renderer_ == nullptr) {
24 // Log an error if renderer creation fails.
25 printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
26 return false;
27 }
28
29 // Set the blend mode to allow for transparency.
30 SDL_SetRenderDrawBlendMode(renderer_.get(), SDL_BLENDMODE_BLEND);
31 return true;
32}
33
39 renderer_.reset();
40}
41
47 // The TextureHandle is a void*, so we cast the SDL_Texture* to it.
48 return static_cast<TextureHandle>(
49 SDL_CreateTexture(renderer_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, width, height)
50 );
51}
52
57TextureHandle SDL2Renderer::CreateTextureWithFormat(int width, int height, uint32_t format, int access) {
58 return static_cast<TextureHandle>(
59 SDL_CreateTexture(renderer_.get(), format, access, width, height)
60 );
61}
62
67void SDL2Renderer::UpdateTexture(TextureHandle texture, const Bitmap& bitmap) {
68 SDL_Surface* surface = bitmap.surface();
69
70 // Validate texture, surface, and surface format
71 if (!texture || !surface || !surface->format) {
72 return;
73 }
74
75 // Validate surface has pixels
76 if (!surface->pixels || surface->w <= 0 || surface->h <= 0) {
77 return;
78 }
79
80 // Convert the bitmap's surface to RGBA8888 format for compatibility with the texture.
81 auto converted_surface = std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>(
82 SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA8888, 0));
83
84 if (!converted_surface || !converted_surface->pixels) {
85 return;
86 }
87
88 // Update the texture with the pixels from the converted surface.
89 SDL_UpdateTexture(static_cast<SDL_Texture*>(texture), nullptr, converted_surface->pixels, converted_surface->pitch);
90}
91
96 if (texture) {
97 SDL_DestroyTexture(static_cast<SDL_Texture*>(texture));
98 }
99}
100
101bool SDL2Renderer::LockTexture(TextureHandle texture, SDL_Rect* rect, void** pixels, int* pitch) {
102 return SDL_LockTexture(static_cast<SDL_Texture*>(texture), rect, pixels, pitch) == 0;
103}
104
106 SDL_UnlockTexture(static_cast<SDL_Texture*>(texture));
107}
108
113 SDL_RenderClear(renderer_.get());
114}
115
120 SDL_RenderPresent(renderer_.get());
121}
122
126void SDL2Renderer::RenderCopy(TextureHandle texture, const SDL_Rect* srcrect, const SDL_Rect* dstrect) {
127 SDL_RenderCopy(renderer_.get(), static_cast<SDL_Texture*>(texture), srcrect, dstrect);
128}
129
134 SDL_SetRenderTarget(renderer_.get(), static_cast<SDL_Texture*>(texture));
135}
136
140void SDL2Renderer::SetDrawColor(SDL_Color color) {
141 SDL_SetRenderDrawColor(renderer_.get(), color.r, color.g, color.b, color.a);
142}
143
144} // namespace gfx
145} // namespace yaze
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:66
SDL_Surface * surface() const
Definition bitmap.h:259
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:24
Main namespace for the application.