eliminat el punter al renderer de 50.000 llocs

This commit is contained in:
2024-07-28 10:45:14 +02:00
parent 7501b4936f
commit 2948684ad3
58 changed files with 492 additions and 401 deletions

View File

@@ -146,11 +146,10 @@ animatedSprite_t loadAnimationFromFile(Texture *texture, std::string filePath, b
}
// Constructor
AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::string file, std::vector<std::string> *buffer)
AnimatedSprite::AnimatedSprite(Texture *texture, std::string file, std::vector<std::string> *buffer)
{
// Copia los punteros
setTexture(texture);
setRenderer(renderer);
// Carga las animaciones
if (file != "")
@@ -174,11 +173,10 @@ AnimatedSprite::AnimatedSprite(Texture *texture, SDL_Renderer *renderer, std::st
}
// Constructor
AnimatedSprite::AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation)
AnimatedSprite::AnimatedSprite(animatedSprite_t *animation)
{
// Copia los punteros
setTexture(animation->texture);
setRenderer(renderer);
// Inicializa variables
currentAnimation = 0;

View File

@@ -37,8 +37,8 @@ private:
public:
// Constructor
AnimatedSprite(Texture *texture = nullptr, SDL_Renderer *renderer = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(SDL_Renderer *renderer, animatedSprite_t *animation);
AnimatedSprite(Texture *texture = nullptr, std::string file = "", std::vector<std::string> *buffer = nullptr);
AnimatedSprite(animatedSprite_t *animation);
// Destructor
~AnimatedSprite();

View File

@@ -1,11 +1,10 @@
#include "movingsprite.h"
// Constructor
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture, SDL_Renderer *renderer)
MovingSprite::MovingSprite(float x, float y, int w, int h, float velx, float vely, float accelx, float accely, Texture *texture)
{
// Copia los punteros
this->texture = texture;
this->renderer = renderer;
// Establece el alto y el ancho del sprite
this->w = w;
@@ -96,7 +95,7 @@ void MovingSprite::render()
{
if (enabled)
{
texture->render(renderer, (int)x, (int)y, &spriteClip, zoomW, zoomH, angle, center, currentFlip);
texture->render((int)x, (int)y, &spriteClip, zoomW, zoomH, angle, center, currentFlip);
}
}

View File

@@ -32,7 +32,7 @@ protected:
public:
// Constructor
MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
MovingSprite(float x = 0, float y = 0, int w = 0, int h = 0, float velx = 0, float vely = 0, float accelx = 0, float accely = 0, Texture *texture = nullptr);
// Mueve el sprite
void move();

View File

@@ -1,11 +1,10 @@
#include "smartsprite.h"
// Constructor
SmartSprite::SmartSprite(Texture *texture, SDL_Renderer *renderer)
SmartSprite::SmartSprite(Texture *texture)
{
// Copia punteros
setTexture(texture);
setRenderer(renderer);
// Inicializa el objeto
init();

View File

@@ -25,7 +25,7 @@ private:
public:
// Constructor
SmartSprite(Texture *texture, SDL_Renderer *renderer);
SmartSprite(Texture *texture);
// Inicializa el objeto
void init();

View File

@@ -1,7 +1,7 @@
#include "sprite.h"
// Constructor
Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *renderer)
Sprite::Sprite(int x, int y, int w, int h, Texture *texture)
{
// Establece la posición X,Y del sprite
this->x = x;
@@ -11,9 +11,6 @@ Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *rende
this->w = w;
this->h = h;
// Establece el puntero al renderizador de la ventana
this->renderer = renderer;
// Establece la textura donde están los gráficos para el sprite
this->texture = texture;
@@ -24,7 +21,7 @@ Sprite::Sprite(int x, int y, int w, int h, Texture *texture, SDL_Renderer *rende
enabled = true;
}
Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer)
Sprite::Sprite(SDL_Rect rect, Texture *texture)
{
// Establece la posición X,Y del sprite
x = rect.x;
@@ -34,9 +31,6 @@ Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer)
w = rect.w;
h = rect.h;
// Establece el puntero al renderizador de la ventana
this->renderer = renderer;
// Establece la textura donde están los gráficos para el sprite
this->texture = texture;
@@ -51,7 +45,6 @@ Sprite::Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer)
Sprite::~Sprite()
{
texture = nullptr;
renderer = nullptr;
}
// Muestra el sprite por pantalla
@@ -59,7 +52,7 @@ void Sprite::render()
{
if (enabled)
{
texture->render(renderer, x, y, &spriteClip);
texture->render(x, y, &spriteClip);
}
}
@@ -88,10 +81,10 @@ int Sprite::getHeight()
}
// Establece la posición del objeto
void Sprite::setPos(SDL_Rect rect)
void Sprite::setPos(SDL_Point p)
{
this->x = rect.x;
this->y = rect.y;
this->x = p.x;
this->y = p.y;
}
// Establece el valor de la variable
@@ -148,18 +141,6 @@ void Sprite::setTexture(Texture *texture)
this->texture = texture;
}
// Obten el valor de la variable
SDL_Renderer *Sprite::getRenderer()
{
return renderer;
}
// Establece el valor de la variable
void Sprite::setRenderer(SDL_Renderer *renderer)
{
this->renderer = renderer;
}
// Establece el valor de la variable
void Sprite::setEnabled(bool value)
{

View File

@@ -12,7 +12,6 @@ protected:
int w; // Ancho del sprite
int h; // Alto del sprite
SDL_Renderer *renderer; // Puntero al renderizador de la ventana
Texture *texture; // Textura donde estan todos los dibujos del sprite
SDL_Rect spriteClip; // Rectangulo de origen de la textura que se dibujará en pantalla
@@ -20,8 +19,8 @@ protected:
public:
// Constructor
Sprite(int x = 0, int y = 0, int w = 0, int h = 0, Texture *texture = nullptr, SDL_Renderer *renderer = nullptr);
Sprite(SDL_Rect rect, Texture *texture, SDL_Renderer *renderer);
Sprite(int x = 0, int y = 0, int w = 0, int h = 0, Texture *texture = nullptr);
Sprite(SDL_Rect rect, Texture *texture);
// Destructor
~Sprite();
@@ -42,7 +41,7 @@ public:
int getHeight();
// Establece la posición del objeto
void setPos(SDL_Rect rect);
void setPos(SDL_Point p);
// Establece el valor de la variable
void setPosX(int x);
@@ -77,12 +76,6 @@ public:
// Establece el valor de la variable
void setTexture(Texture *texture);
// Obten el valor de la variable
SDL_Renderer *getRenderer();
// Establece el valor de la variable
void setRenderer(SDL_Renderer *renderer);
// Establece el valor de la variable
void setEnabled(bool value);

View File

@@ -94,7 +94,7 @@ Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
// Crea los objetos
texture = new Texture(renderer, bitmapFile);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture);
// Inicializa variables
fixedWidth = false;
@@ -118,7 +118,7 @@ Text::Text(std::string textFile, Texture *texture, SDL_Renderer *renderer)
// Crea los objetos
this->texture = nullptr;
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture);
// Inicializa variables
fixedWidth = false;
@@ -139,7 +139,7 @@ Text::Text(textFile_t *textFile, Texture *texture, SDL_Renderer *renderer)
// Crea los objetos
this->texture = nullptr;
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture, renderer);
sprite = new Sprite({0, 0, boxWidth, boxHeight}, texture);
// Inicializa variables
fixedWidth = false;

View File

@@ -29,7 +29,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
// .png
if (extension == "png")
{
loadFromFile(path, renderer, verbose);
loadFromFile(path, verbose);
}
// .gif
@@ -38,7 +38,7 @@ Texture::Texture(SDL_Renderer *renderer, std::string path, bool verbose)
surface = loadSurface(path.c_str());
addPalette(path.c_str());
setPaletteColor(0, 0, 0x00000000);
createBlank(renderer, width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
createBlank(width, height, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
flipSurface();
}
@@ -53,7 +53,7 @@ Texture::~Texture()
}
// Carga una imagen desde un fichero
bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose)
bool Texture::loadFromFile(std::string path, bool verbose)
{
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
int req_format = STBI_rgb_alpha;
@@ -131,7 +131,7 @@ bool Texture::loadFromFile(std::string path, SDL_Renderer *renderer, bool verbos
}
// Crea una textura en blanco
bool Texture::createBlank(SDL_Renderer *renderer, int width, int height, SDL_PixelFormatEnum format, SDL_TextureAccess access)
bool Texture::createBlank(int width, int height, SDL_PixelFormatEnum format, SDL_TextureAccess access)
{
// Crea una textura sin inicializar
texture = SDL_CreateTexture(renderer, format, access, width, height);
@@ -187,7 +187,7 @@ void Texture::setAlpha(Uint8 alpha)
}
// Renderiza la textura en un punto específico
void Texture::render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
void Texture::render(int x, int y, SDL_Rect *clip, float zoomW, float zoomH, double angle, SDL_Point *center, SDL_RendererFlip flip)
{
// Establece el destino de renderizado en la pantalla
SDL_Rect renderQuad = {x, y, width, height};
@@ -373,4 +373,10 @@ void Texture::setPalette(int palette)
paletteIndex = palette;
flipSurface();
}
}
// Obtiene el renderizador
SDL_Renderer *Texture::getRenderer()
{
return renderer;
}

View File

@@ -52,10 +52,10 @@ public:
~Texture();
// Carga una imagen desde un fichero
bool loadFromFile(std::string path, SDL_Renderer *renderer, bool verbose = false);
bool loadFromFile(std::string path, bool verbose = false);
// Crea una textura en blanco
bool createBlank(SDL_Renderer *renderer, int width, int height, SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGBA8888, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
bool createBlank(int width, int height, SDL_PixelFormatEnum format = SDL_PIXELFORMAT_RGBA8888, SDL_TextureAccess = SDL_TEXTUREACCESS_STREAMING);
// Libera la memoria de la textura
void unload();
@@ -70,7 +70,7 @@ public:
void setAlpha(Uint8 alpha);
// Renderiza la textura en un punto específico
void render(SDL_Renderer *renderer, int x, int y, SDL_Rect *clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE);
void render(int x, int y, SDL_Rect *clip = nullptr, float zoomW = 1, float zoomH = 1, double angle = 0.0, SDL_Point *center = nullptr, SDL_RendererFlip flip = SDL_FLIP_NONE);
// Establece la textura como objetivo de renderizado
void setAsRenderTarget(SDL_Renderer *renderer);
@@ -95,4 +95,7 @@ public:
// Cambia la paleta de la textura
void setPalette(int palette);
// Obtiene el renderizador
SDL_Renderer *getRenderer();
};