diff --git a/source/screen.cpp b/source/screen.cpp index 85b964c..09880dc 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -52,7 +52,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) info_resolution_ = std::to_string(DM.w) + " X " + std::to_string(DM.h) + " AT " + std::to_string(DM.refresh_rate) + " HZ"; // Ajusta los tamaños - adjustGameCanvasRect(); + game_surface_dstrect_ = {options.video.border.width, options.video.border.height, options.game.width, options.game.height}; adjustWindowSize(); current_palette_ = findPalette(options.video.palette); @@ -139,7 +139,7 @@ void Screen::clearRenderer(Color color) // Prepara para empezar a dibujar en la textura de juego void Screen::start() { - SDL_SetRenderTarget(renderer_, game_texture_); + //SDL_SetRenderTarget(renderer_, game_texture_); setRendererSurface(nullptr); } @@ -170,7 +170,6 @@ void Screen::setVideoMode(int mode) // Configura el modo de pantalla y ajusta la ventana SDL_SetWindowFullscreen(window_, options.video.mode); adjustWindowSize(); - adjustGameCanvasRect(); adjustRenderLogicalSize(); // Reinicia los shaders @@ -286,16 +285,6 @@ void Screen::adjustWindowSize() } } -// Ajusta game_canvas_rect_ -void Screen::adjustGameCanvasRect() -{ - game_rect_ = { - options.video.border.enabled ? options.video.border.width : 0, - options.video.border.enabled ? options.video.border.height : 0, - options.game.width, - options.game.height}; -} - // Ajusta el tamaño lógico del renderizador void Screen::adjustRenderLogicalSize() { SDL_RenderSetLogicalSize(renderer_, window_width_, window_height_); } @@ -395,9 +384,8 @@ void Screen::surfaceToTexture() // Si está el borde activo, vuelca gameCanvas sobre borderCanvas if (options.video.border.enabled) { - setRendererSurface(border_surface_); - game_surface_->render(options.video.border.width, options.video.border.height); border_surface_->copyToTexture(renderer_, border_texture_); + game_surface_->copyToTexture(renderer_, border_texture_, nullptr, &game_surface_dstrect_); } else { diff --git a/source/screen.h b/source/screen.h index c48facd..1250037 100644 --- a/source/screen.h +++ b/source/screen.h @@ -70,7 +70,7 @@ private: // Variables int window_width_; // Ancho de la pantalla o ventana int window_height_; // Alto de la pantalla o ventana - SDL_Rect game_rect_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana + SDL_Rect game_surface_dstrect_; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana Uint8 border_color_; // Color del borde añadido a la textura de juego para rellenar la pantalla std::vector palettes_; // Listado de los ficheros de paletta disponibles Uint8 current_palette_ = 0; // Indice para el vector de paletas @@ -90,9 +90,6 @@ private: // Calcula el tamaño de la ventana void adjustWindowSize(); - // Ajusta game_canvas_rect_ - void adjustGameCanvasRect(); - // Ajusta el tamaño lógico del renderizador void adjustRenderLogicalSize(); diff --git a/source/surface.cpp b/source/surface.cpp index 2c80637..f945d0e 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -179,7 +179,7 @@ void Surface::setColor(int index, Uint32 color) void Surface::clear(Uint8 color) { const size_t total_pixels = surface_data_->width * surface_data_->height; - Uint8* data_ptr = surface_data_->data.get(); // Explicitly get a typed pointer + Uint8* data_ptr = surface_data_->data.get(); std::fill(data_ptr, data_ptr + total_pixels, color); } @@ -523,6 +523,50 @@ void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture) } } +// Vuelca la superficie a una textura +void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcRect, SDL_Rect *destRect) +{ + if (!renderer || !texture || !surface_data_) + { + throw std::runtime_error("Renderer or texture is null."); + } + + if (surface_data_->width <= 0 || surface_data_->height <= 0 || !surface_data_->data.get()) + { + throw std::runtime_error("Invalid surface dimensions or data."); + } + + Uint32 *pixels = nullptr; + int pitch = 0; + + if (SDL_LockTexture(texture, destRect, reinterpret_cast(&pixels), &pitch) != 0) + { + throw std::runtime_error("Failed to lock texture: " + std::string(SDL_GetError())); + } + + int row_stride = pitch / sizeof(Uint32); + + for (int y = 0; y < surface_data_->height; ++y) + { + for (int x = 0; x < surface_data_->width; ++x) + { + int texture_index = y * row_stride + x; + int surface_index = y * surface_data_->width + x; + + pixels[texture_index] = palette_[surface_data_->data.get()[surface_index]]; + } + } + + SDL_UnlockTexture(texture); + + // Renderiza la textura con los rectángulos especificados + if (SDL_RenderCopy(renderer, texture, srcRect, destRect) != 0) + { + throw std::runtime_error("Failed to copy texture to renderer: " + std::string(SDL_GetError())); + } +} + + // Realiza un efecto de fundido en la paleta principal bool Surface::fadePalette() { diff --git a/source/surface.h b/source/surface.h index f019079..4f2391b 100644 --- a/source/surface.h +++ b/source/surface.h @@ -87,6 +87,7 @@ public: // Vuelca la SurfaceData a una textura void copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture); + void copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Rect *srcRect, SDL_Rect *destRect); // Realiza un efecto de fundido en las paletas bool fadePalette();