diff --git a/source/game.cpp b/source/game.cpp index c571aa3..7263d71 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -37,7 +37,7 @@ Game::Game() cheevos_(Cheevos::get()) { // Inicia algunas variables - std::make_shared(Screen::get()->getGameSurface(), "test.gif"); + test_surface_ = std::make_shared(Screen::get()->getSurface(), "test.gif"); board_ = std::make_shared(); board_->ini_clock = SDL_GetTicks(); #ifdef DEBUG @@ -260,7 +260,6 @@ void Game::update() // Pinta los objetos en pantalla void Game::render() { - // Prepara para dibujar el frame screen_->start(); test_surface_->render(0, 0, 10, 10, 64, 64); diff --git a/source/screen.cpp b/source/screen.cpp index dc973f0..b6fa531 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -52,7 +52,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) SDL_RenderSetIntegerScale(renderer_, options.video.integer_scale ? SDL_TRUE : SDL_FALSE); // Crea la textura donde se vuelcan las surfaces - surface_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); + surface_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, options.game.width, options.game.height); if (surface_texture_ == nullptr) { if (options.console) @@ -60,6 +60,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) std::cout << "surface_texture_ could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } } + //SDL_SetTextureBlendMode(surface_texture_, SDL_BLENDMODE_BLEND); // Crea la textura donde se dibujan los graficos del juego game_texture_ = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, options.game.width, options.game.height); @@ -128,7 +129,9 @@ void Screen::render() // Renderiza sobre gameCanvas los overlays renderNotifications(); - surface_->copyToTexture(renderer_, game_texture_); + fillTextureWithColor(renderer_, surface_texture_, 0xFF, 0x00, 0xFF, 0xFF); + surface_->copyToTexture(renderer_, surface_texture_); + SDL_RenderCopy(renderer_, surface_texture_, nullptr, nullptr); // Si está el borde activo, vuelca gameCanvas sobre borderCanvas if (options.video.border.enabled) diff --git a/source/screen.h b/source/screen.h index 4383d72..92ed283 100644 --- a/source/screen.h +++ b/source/screen.h @@ -29,7 +29,7 @@ private: // Objetos y punteros SDL_Window *window_; // Ventana de la aplicación SDL_Renderer *renderer_; // El renderizador de la ventana - SDL_Texture *surface_texture_; // Textura donde se dibuja el juego + SDL_Texture *surface_texture_; // Textura donde se dibuja el juego SDL_Texture *game_texture_; // Textura donde se dibuja el juego SDL_Texture *border_texture_; // Textura donde se dibuja el borde del juego std::shared_ptr surface_; // Objeto para trabajar con surfaces @@ -135,7 +135,7 @@ public: // Getters SDL_Renderer *getRenderer() { return renderer_; } - std::shared_ptr getGameSurface() { return surface_->getSurface(); } + std::shared_ptr getSurface() { return surface_->getSurface(); } SDL_Texture *getGameTexture() { return game_texture_; }; SDL_Texture *getBorderTexture() { return border_texture_; } }; \ No newline at end of file diff --git a/source/surface.cpp b/source/surface.cpp index 8fff6c9..1460175 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -18,7 +18,9 @@ Surface::Surface(std::shared_ptr surface_dest, int w, int h) Surface::Surface(std::shared_ptr surface_dest, std::string file_path) : surface_dest_(surface_dest), surface_(std::make_shared(loadSurface(Asset::get()->get(file_path)))), - transparent_color_(0) {} + transparent_color_(0) { + std::cout << "surface loaded: "<< surface_->width << "x" << surface_->height << std::endl; + } Surface::~Surface() {} @@ -84,6 +86,12 @@ void Surface::loadPalette(const std::string &file_path) // Copiar los datos de la paleta al std::array std::copy(pal.get(), pal.get() + palette_.size(), palette_.begin()); + + for (auto p : palette_) + { + std::cout << std::hex << p << " "; + } + std::cout << std::endl; } // Establece un color en la paleta @@ -125,6 +133,11 @@ Uint8 Surface::getPixel(int x, int y) // Copia una región de la superficie de origen a la de destino void Surface::render(int dx, int dy, int sx, int sy, int w, int h) { + if (!surface_ || !surface_dest_) + { + throw std::runtime_error("Surface source or destination is null."); + } + // Limitar la región para evitar accesos fuera de rango w = std::min(w, surface_->width - sx); h = std::min(h, surface_->height - sy); diff --git a/source/utils.cpp b/source/utils.cpp index debbb27..e65df22 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -536,4 +536,23 @@ void playMusic(const std::string &music_path) { JA_PlayMusic(Resource::get()->getMusic(music_path)); } -} \ No newline at end of file +} + +// Rellena una textura de un color +void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + // Guardar el render target actual + SDL_Texture* previous_target = SDL_GetRenderTarget(renderer); + + // Establecer la textura como el render target + SDL_SetRenderTarget(renderer, texture); + + // Establecer el color deseado + SDL_SetRenderDrawColor(renderer, r, g, b, a); + + // Pintar toda el área + SDL_RenderClear(renderer); + + // Restaurar el render target previo + SDL_SetRenderTarget(renderer, previous_target); +} diff --git a/source/utils.h b/source/utils.h index 7c22790..ff8b933 100644 --- a/source/utils.h +++ b/source/utils.h @@ -4,6 +4,9 @@ #include // for Uint8 #include // for string #include +#include // for SDL_Renderer +#include // for SDL_Texture + // Tipos de paleta enum class Palette : int @@ -129,4 +132,7 @@ void printWithDots(const std::string &text1, const std::string &text2, const std bool stringInVector(const std::vector &vec, const std::string &str); // Hace sonar la música -void playMusic(const std::string &music_path); \ No newline at end of file +void playMusic(const std::string &music_path); + +// Rellena una textura de un color +void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a); \ No newline at end of file