- Canvis que vaig fer fa 2 messos, però no sé en què estava pensant, he de arreglar-ho.

This commit is contained in:
2023-02-13 11:50:37 +01:00
parent d230259e42
commit 908ffa6d55
5 changed files with 62 additions and 22 deletions

View File

@@ -4,13 +4,13 @@
#include "stb_image.h"
#include <iostream>
#include "systempalette.h"
#include "destsurface.h"
// Constructor
Texture::Texture(SDL_Renderer *renderer, SDL_Texture *texture, std::string path, bool verbose)
Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
{
// Copia punteros
this->renderer = renderer;
this->texture = texture;
this->path = path;
// Inicializa
@@ -56,6 +56,8 @@ bool Texture::loadFromFile(std::string path, bool verbose)
// Limpia
unload();
this->width = width;
this->height = height;
const int size = width*height;
this->pixels = new uint8_t[size];
for (int i = 0; i < size; ++i) this->pixels[i] = SystemPalette::getEntry(data[i]);
@@ -66,7 +68,7 @@ bool Texture::loadFromFile(std::string path, bool verbose)
}
// Crea una textura en blanco
bool Texture::createBlank(int width, int height, SDL_TextureAccess access)
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_TextureAccess access)
{
// Crea una textura sin inicializar
this->pixels = new uint8_t[width*height];
@@ -124,13 +126,24 @@ void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float
renderQuad.h = renderQuad.h * zoomH;
// Renderiza a pantalla
SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
//SDL_RenderCopyEx(renderer, texture, clip, &renderQuad, angle, center, flip);
uint8_t *destSurface = DestSurface::getPixels();
int canvasWidth = DestSurface::getWidth();
int y_inc = 1, y_ini = 0, y_fin = renderQuad.h-1; if (flip && SDL_FLIP_VERTICAL==SDL_FLIP_VERTICAL) { auto tmp=y_ini; y_ini=y_fin; y_fin=tmp; y_inc=-1; }
int x_inc = 1, x_ini = 0, x_fin = renderQuad.w-1; if (flip && SDL_FLIP_HORIZONTAL==SDL_FLIP_HORIZONTAL) { auto tmp=x_ini; x_ini=x_fin; x_fin=tmp; x_inc=-1; }
for (int yy=y_ini; yy <= y_fin; yy += y_inc) {
for (int xx=x_ini; xx <= x_fin; xx += x_inc) {
destSurface[ x+xx + y+yy * canvasWidth ] = pixels[ clip->x+xx + clip->y+yy * width ];
}
}
}
// Establece la textura como objetivo de renderizado
void Texture::setAsRenderTarget(SDL_Renderer *renderer)
{
SDL_SetRenderTarget(renderer, texture);
//SDL_SetRenderTarget(renderer, texture);
}
// Obtiene el ancho de la imagen
@@ -154,5 +167,5 @@ bool Texture::reLoad()
// Obtiene la textura
SDL_Texture *Texture::getSDLTexture()
{
return texture;
return nullptr; //texture;
}