SDL_Rect Ptrs and reference fixes.

This commit is contained in:
2023-08-12 19:15:30 +02:00
parent 279a09256e
commit 74b8c36d82
6 changed files with 23 additions and 17 deletions
+3 -2
View File
@@ -8,7 +8,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
SDL_Rect UE::Texture::GetTextureRect(int i) SDL_Rect& UE::Texture::GetTextureRect(int i)
{ {
try try
{ {
@@ -64,6 +64,7 @@ void UE::Texture::InitTexture()
//Cargamos los Rects. //Cargamos los Rects.
json textureRects = jread["TextureRects"]; json textureRects = jread["TextureRects"];
_txRects.reserve(textureRects.size() + 1);
//Por defecto, el Rect 0 es la textura completa. //Por defecto, el Rect 0 es la textura completa.
AddRect(SDL_Rect{ 0, 0, txSizeX, txSizeY }); AddRect(SDL_Rect{ 0, 0, txSizeX, txSizeY });
@@ -117,7 +118,7 @@ int UE::Texture::CreateTexture()
return 0; return 0;
} }
void UE::Texture::AddRect(SDL_Rect& newRect) void UE::Texture::AddRect(const SDL_Rect& newRect)
{ {
_txRects.push_back(newRect); _txRects.push_back(newRect);
} }
+2 -2
View File
@@ -71,7 +71,7 @@ namespace UE
* @param i : index del rect. (El index 0 es la textura completa). * @param i : index del rect. (El index 0 es la textura completa).
* @return El SDL_Rect solicitado (o el por defecto si no está disponible). * @return El SDL_Rect solicitado (o el por defecto si no está disponible).
*/ */
SDL_Rect GetTextureRect(int i); SDL_Rect& GetTextureRect(int i);
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
@@ -92,7 +92,7 @@ namespace UE
* *
* @param newRect: New Rect to add. Lo enviamos como referencia para ahorrar copias. IDK if is better... * @param newRect: New Rect to add. Lo enviamos como referencia para ahorrar copias. IDK if is better...
*/ */
void AddRect(SDL_Rect& newRect); void AddRect(const SDL_Rect& newRect);
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
+6 -4
View File
@@ -37,13 +37,15 @@ namespace UE
void Sprite::Draw(Renderer* renderer) void Sprite::Draw(Renderer* renderer)
{ {
//Comprobar si es visible. SDL_Rect transformRect = GetTransformRect();
if (!renderer->IsObjectVisible(&GetTransformRect()))
//Comprobar si es visible. Esta comprobación ya la realiza el renderer, no la hagamos dos veces.
/*if (!renderer->IsObjectVisible(transformRect))
{ {
return; return;
} }*/
renderer->DrawTextureOnWorld(_tx, _txRectID, GetTransformRect()); renderer->DrawTextureOnWorld(_tx, _txRectID, transformRect);
} }
+2 -1
View File
@@ -49,7 +49,8 @@ void UE::TextBox::Draw(Renderer* renderer)
//SDL_Texture* tx = _textTexture.GetTexture(); //SDL_Texture* tx = _textTexture.GetTexture();
//renderer->QueueDrawCall(_renderLayer, dest, _textTexture); //renderer->QueueDrawCall(_renderLayer, dest, _textTexture);
renderer->DrawTextureOnScreen(_tx, 0, GetTransformRect()); SDL_Rect transformRect = GetTransformRect();
renderer->DrawTextureOnScreen(_tx, 0, transformRect);
//Actualizamos en cada frame el ptr a la textura que tiene que usar el drawable. //Actualizamos en cada frame el ptr a la textura que tiene que usar el drawable.
//no vaya a ser que se invalide por algo. //no vaya a ser que se invalide por algo.
+7 -5
View File
@@ -57,6 +57,8 @@ namespace UE
worldLayers.push_back(std::vector<Drawable*>()); worldLayers.push_back(std::vector<Drawable*>());
} }
//All right!
return 0;
} }
void Renderer::CleanRenderer() void Renderer::CleanRenderer()
@@ -185,12 +187,12 @@ namespace UE
} }
void Renderer::DrawTextureOnWorld(UE::Texture* tx, int txRectID, SDL_Rect worldRect) void Renderer::DrawTextureOnWorld(UE::Texture* tx, int txRectID, SDL_Rect& worldRect)
{ {
//TODO: Añadir seguridad. //TODO: Añadir seguridad.
//Si el objeto no es visible, no lo renderizamos. //Si el objeto no es visible, no lo renderizamos.
if (!IsObjectVisible(&worldRect)) if (!IsObjectVisible(worldRect))
{ {
return; return;
} }
@@ -204,7 +206,7 @@ namespace UE
} }
void Renderer::DrawTextureOnScreen(UE::Texture* tx, int txRectID, SDL_Rect screenRect) void Renderer::DrawTextureOnScreen(UE::Texture* tx, int txRectID, SDL_Rect& screenRect)
{ {
SDL_RenderCopy(main_Renderer, tx->GetTexture(), &tx->GetTextureRect(txRectID), &screenRect); SDL_RenderCopy(main_Renderer, tx->GetTexture(), &tx->GetTextureRect(txRectID), &screenRect);
} }
@@ -241,9 +243,9 @@ namespace UE
SetCamera(&defaultCam); SetCamera(&defaultCam);
} }
bool Renderer::IsObjectVisible(SDL_Rect* objectRect) bool Renderer::IsObjectVisible(SDL_Rect& objectRect)
{ {
return SDL_HasIntersection(objectRect, &activeCamRect); return SDL_HasIntersection(&objectRect, &activeCamRect);
} }
+3 -3
View File
@@ -96,10 +96,10 @@ namespace UE
* Renderizar una textura dado su rect en WorldCoords. * Renderizar una textura dado su rect en WorldCoords.
* Se ha de utilizar en las implementaciones del método Draw de la clase Drawable. * Se ha de utilizar en las implementaciones del método Draw de la clase Drawable.
*/ */
void DrawTextureOnWorld(UE::Texture* tx, int txRectID, SDL_Rect worldRect); void DrawTextureOnWorld(UE::Texture* tx, int txRectID, SDL_Rect& worldRect);
void DrawTextureOnScreen(UE::Texture* tx, int txRectID, SDL_Rect screenRect); void DrawTextureOnScreen(UE::Texture* tx, int txRectID, SDL_Rect& screenRect);
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
@@ -155,7 +155,7 @@ namespace UE
/* /*
* Check if an object is currently visible on Screen. * Check if an object is currently visible on Screen.
*/ */
bool IsObjectVisible(SDL_Rect* objectRect); bool IsObjectVisible(SDL_Rect& objectRect);
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////