diff --git a/source/surface.cpp b/source/surface.cpp index 7d2144d5..5b7798bb 100644 --- a/source/surface.cpp +++ b/source/surface.cpp @@ -155,21 +155,49 @@ void Surface::render(int dx, int dy, int sx, int sy, int w, int h) } } -/* -// Vuelca la superficie a una textura -void Surface::flip(SDL_Renderer *renderer, SDL_Texture *texture) +// Copia una región de la superficie de origen a la de destino +void Surface::render(int x, int y, SDL_Rect *clip, SDL_RendererFlip flip) { - Uint32 *pixels; - int pitch; - SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch); - for (int i = 0; i < surface_.width * surface_.height; ++i) + if (!surface_ || !surface_dest_) { - pixels[i] = palette_[surface_.data[i]]; + throw std::runtime_error("Surface source or destination is null."); + } + + // Determina la región de origen (clip) a renderizar + int sx = (clip) ? clip->x : 0; + int sy = (clip) ? clip->y : 0; + int w = (clip) ? clip->w : surface_->width; + int h = (clip) ? clip->h : surface_->height; + + // Limitar la región para evitar accesos fuera de rango + w = std::min(w, surface_->width - sx); + h = std::min(h, surface_->height - sy); + w = std::min(w, surface_dest_->width - x); + h = std::min(h, surface_dest_->height - y); + + // Renderiza píxel por píxel aplicando el flip si es necesario + for (int iy = 0; iy < h; ++iy) + { + for (int ix = 0; ix < w; ++ix) + { + // Coordenadas de origen + int src_x = (flip == SDL_FLIP_HORIZONTAL) ? (sx + w - 1 - ix) : (sx + ix); + int src_y = (flip == SDL_FLIP_VERTICAL) ? (sy + h - 1 - iy) : (sy + iy); + + // Coordenadas de destino + int dest_x = x + ix; + int dest_y = y + iy; + + // Copia el píxel si no es transparente + Uint8 color = surface_->data[src_x + src_y * surface_->width]; + if (color != transparent_color_) // Opcional: Ignorar píxeles transparentes + { + surface_dest_->data[dest_x + dest_y * surface_dest_->width] = color; + } } - SDL_UnlockTexture(texture); - SDL_RenderCopy(renderer, texture, nullptr, nullptr); - } - */ + } +} + // Vuelca la superficie a una textura void Surface::copyToTexture(SDL_Renderer *renderer, SDL_Texture *texture) diff --git a/source/surface.h b/source/surface.h index 14aada51..74f6097b 100644 --- a/source/surface.h +++ b/source/surface.h @@ -85,8 +85,9 @@ public: // Carga una paleta desde un archivo void loadPalette(const std::string &file_path); - // Copia una región de la superficie de origen a la de destino + // Copia una región de la superficie de origen a la superficie de destino void render(int dx, int dy, int sx, int sy, int w, int h); + void render(int x, int y, SDL_Rect *clip = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE); // Establece un color en la paleta void setColor(int index, Uint32 color); @@ -102,7 +103,7 @@ public: // Getters std::shared_ptr getSurface() const { return surface_; } - // std::shared_ptr getSurfaceDest() const { return surface_dest_; } - // std::array getPalette() const { return palette_; } int getTransparentColor() const { return transparent_color_; } + int getWidth() const { return surface_->width; } + int getHeight() const { return surface_->height; } };