yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
arena.cc
Go to the documentation of this file.
1#include "app/gfx/arena.h"
2
3#include <SDL.h>
4
6
7namespace yaze {
8namespace gfx {
9
11 static Arena instance;
12 return instance;
13}
14
16 layer1_buffer_.fill(0);
17 layer2_buffer_.fill(0);
18}
19
21 textures_.clear();
22 surfaces_.clear();
23}
24
25SDL_Texture* Arena::AllocateTexture(SDL_Renderer* renderer, int width,
26 int height) {
27 if (!renderer) {
28 SDL_Log("Invalid renderer passed to AllocateTexture");
29 return nullptr;
30 }
31
32 if (width <= 0 || height <= 0) {
33 SDL_Log("Invalid texture dimensions: width=%d, height=%d", width, height);
34 return nullptr;
35 }
36
37 SDL_Texture* texture =
38 SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888,
39 SDL_TEXTUREACCESS_STREAMING, width, height);
40 if (!texture) {
41 SDL_Log("Failed to create texture: %s", SDL_GetError());
42 return nullptr;
43 }
44
45 textures_[texture] =
46 std::unique_ptr<SDL_Texture, core::SDL_Texture_Deleter>(texture);
47 return texture;
48}
49
50void Arena::FreeTexture(SDL_Texture* texture) {
51 if (!texture) return;
52
53 auto it = textures_.find(texture);
54 if (it != textures_.end()) {
55 textures_.erase(it);
56 }
57}
58
59void Arena::UpdateTexture(SDL_Texture* texture, SDL_Surface* surface) {
60 if (!texture || !surface) {
61 SDL_Log("Invalid texture or surface passed to UpdateTexture");
62 return;
63 }
64
65 if (surface->pixels == nullptr) {
66 SDL_Log("Surface pixels are nullptr");
67 return;
68 }
69
70 auto converted_surface =
71 std::unique_ptr<SDL_Surface, core::SDL_Surface_Deleter>(
72 SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA8888, 0),
74
75 if (!converted_surface) {
76 SDL_Log("SDL_ConvertSurfaceFormat failed: %s", SDL_GetError());
77 return;
78 }
79
80 void* pixels;
81 int pitch;
82 if (SDL_LockTexture(texture, nullptr, &pixels, &pitch) != 0) {
83 SDL_Log("SDL_LockTexture failed: %s", SDL_GetError());
84 return;
85 }
86
87 memcpy(pixels, converted_surface->pixels,
88 converted_surface->h * converted_surface->pitch);
89
90 SDL_UnlockTexture(texture);
91}
92
93SDL_Surface* Arena::AllocateSurface(int width, int height, int depth,
94 int format) {
95 SDL_Surface* surface =
96 SDL_CreateRGBSurfaceWithFormat(0, width, height, depth, format);
97 if (!surface) {
98 SDL_Log("Failed to create surface: %s", SDL_GetError());
99 return nullptr;
100 }
101
102 surfaces_[surface] =
103 std::unique_ptr<SDL_Surface, core::SDL_Surface_Deleter>(surface);
104 return surface;
105}
106
107void Arena::FreeSurface(SDL_Surface* surface) {
108 if (!surface) return;
109
110 auto it = surfaces_.find(surface);
111 if (it != surfaces_.end()) {
112 surfaces_.erase(it);
113 }
114}
115
116} // namespace gfx
117} // namespace yaze
SDL_Texture * AllocateTexture(SDL_Renderer *renderer, int width, int height)
Definition arena.cc:25
SDL_Surface * AllocateSurface(int width, int height, int depth, int format)
Definition arena.cc:93
std::array< uint16_t, kTotalTiles > layer2_buffer_
Definition arena.h:48
std::unordered_map< SDL_Texture *, std::unique_ptr< SDL_Texture, core::SDL_Texture_Deleter > > textures_
Definition arena.h:54
std::unordered_map< SDL_Surface *, std::unique_ptr< SDL_Surface, core::SDL_Surface_Deleter > > surfaces_
Definition arena.h:58
void FreeSurface(SDL_Surface *surface)
Definition arena.cc:107
void FreeTexture(SDL_Texture *texture)
Definition arena.cc:50
std::array< uint16_t, kTotalTiles > layer1_buffer_
Definition arena.h:47
void UpdateTexture(SDL_Texture *texture, SDL_Surface *surface)
Definition arena.cc:59
static Arena & Get()
Definition arena.cc:10
Contains classes for handling graphical data.
Definition arena.cc:8
Main namespace for the application.
Definition controller.cc:18