Primera implementación Sprite + renderer changes
This commit is contained in:
@@ -12,7 +12,7 @@ namespace UE
|
||||
int AssetManager::AssetManagerInit()
|
||||
{
|
||||
IMG_Init(IMG_INIT_PNG);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AssetManager::AssetManagerClean()
|
||||
@@ -21,7 +21,9 @@ namespace UE
|
||||
{
|
||||
SDL_DestroyTexture(i->second);
|
||||
}
|
||||
|
||||
//CPP se debería de encargar de ellos, pero bueno.
|
||||
_textures.clear();
|
||||
_assets.clear();
|
||||
|
||||
IMG_Quit();
|
||||
return 0;
|
||||
@@ -50,7 +52,7 @@ namespace UE
|
||||
_assets.push_back(Others.at(i));
|
||||
}
|
||||
file.close();
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AssetManager::LoadAssetQueue()
|
||||
@@ -77,7 +79,7 @@ namespace UE
|
||||
}
|
||||
_assets.clear();
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void AssetManager::AddAssetToQueue(Asset& as)
|
||||
@@ -95,8 +97,9 @@ namespace UE
|
||||
}
|
||||
int AssetManager::RemoveTexture(std::string name)
|
||||
{
|
||||
SDL_DestroyTexture(_textures.at(name));
|
||||
_textures.erase(name);
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
SDL_Texture* AssetManager::getTexture(std::string TextureName)
|
||||
{
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
|
||||
///
|
||||
/// TODO:
|
||||
/// - Modificar las funciones de Carga para quitar la queue o implementar una que tenga sentido.
|
||||
/// Por ejemplo, se puede crear una cola (std::deque) y cargar un asset cada gameLoop en vez de
|
||||
/// procesar toda la cola de golpe. Optimizaría la carga de assets.
|
||||
/// - Logging.
|
||||
/// - ERROR HANDLING
|
||||
/// - Cambiar el _assetPath para permitir cargar desde tantos ficheros como se quiera (AssetLoadFromFile).
|
||||
/// - More asset types.
|
||||
|
||||
namespace UE
|
||||
@@ -46,10 +48,10 @@ namespace UE
|
||||
|
||||
/* Carga de Assets */
|
||||
/* Se cargarán a una queue los asset struct desde un fichero JSON. */
|
||||
/* Se cargarán en memoria los assets de la queue en su correspondiente contenedor. */
|
||||
int LoadAssetsFromFile(std::string fileToLoad); //Añade a la cola los assets de un archivo.
|
||||
int LoadAssetQueue(); //Carga en memoria los assets de la cola.
|
||||
void AddAssetToQueue(Asset& as); //Añade manualmente un asset a la cola de carga.
|
||||
/* Se cargarán en memoria los assets de la queue en su correspondiente contenedor. */
|
||||
int LoadAssetQueue(); //Procesa la queue de carga.
|
||||
|
||||
|
||||
/* Métodos para manejar las SDL_Texture* almacenados. */
|
||||
@@ -70,6 +72,7 @@ namespace UE
|
||||
/* Almacenes de asssets */
|
||||
std::unordered_map<std::string, SDL_Texture*> _textures = {};
|
||||
|
||||
//Necesario para crear las SDL_Texture a partir de SDL_Surface.
|
||||
SDL_Renderer* _mainRenderer = NULL;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "Sprite.h"
|
||||
#include "Render\Renderer.h"
|
||||
#include "Assets\AssetManager.h"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void UE::Sprite::SetTexturePtr(SDL_Texture* texture)
|
||||
{
|
||||
_spriteTx = texture;
|
||||
}
|
||||
|
||||
void UE::Sprite::SetTexturePtr(std::string textureName, AssetManager* assetManager)
|
||||
{
|
||||
_spriteTx = assetManager->getTexture(textureName);
|
||||
}
|
||||
|
||||
void UE::Sprite::SetPosition(float x, float y)
|
||||
{
|
||||
_pos = glm::vec2(x, y);
|
||||
}
|
||||
|
||||
void UE::Sprite::SetPosition(glm::vec<2, float> newPosition)
|
||||
{
|
||||
_pos = newPosition;
|
||||
}
|
||||
|
||||
|
||||
void UE::Sprite::SetSize(float x, float y)
|
||||
{
|
||||
_size = glm::vec2(x, y);
|
||||
}
|
||||
|
||||
void UE::Sprite::SetSize(glm::vec<2,float> newSize)
|
||||
{
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
void UE::Sprite::SetLayer(int newLayer)
|
||||
{
|
||||
_layer = newLayer;
|
||||
}
|
||||
|
||||
|
||||
void UE::Sprite::Draw(Renderer* renderer)
|
||||
{
|
||||
SDL_Rect destination;
|
||||
destination.x = _pos.x;
|
||||
destination.y = _pos.y;
|
||||
|
||||
destination.w = _size.x;
|
||||
destination.h = _size.y;
|
||||
|
||||
//std::cout << "Sprite Rendered to: " << destination.x << "-" << destination.y << std::endl;
|
||||
|
||||
renderer->QueueDrawCall(_layer, _spriteTx, destination);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <SDL.h>
|
||||
//#include <glm.hpp>
|
||||
#include <vec2.hpp>
|
||||
|
||||
|
||||
#include "Render\Drawable.h"
|
||||
|
||||
|
||||
namespace UE
|
||||
{
|
||||
class Renderer;
|
||||
class AssetManager;
|
||||
|
||||
class Sprite : public Drawable
|
||||
{
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
// --------------
|
||||
// Constructores.
|
||||
// --------------
|
||||
*/
|
||||
|
||||
/*
|
||||
Se establecen todos los parámetos.
|
||||
La textura es obligatoria, y puede darse:
|
||||
- En std::string nombre y assetManager* -> Va a buscarse al assetManager especificado.
|
||||
- En SDL_Texture* -> Sin más, se entiende.
|
||||
En ambos casos se llama al método SetTexturePtr.
|
||||
Los demás parámetros tienen un valor por defecto.
|
||||
*/
|
||||
|
||||
Sprite(std::string texture_Name, AssetManager* assetManager, int renderLayer = 1,
|
||||
glm::vec2 position = glm::vec2(0, 0), glm::vec2 size = glm::vec2(100, 100))
|
||||
: _layer(renderLayer), _pos(position), _size(size)
|
||||
{
|
||||
SetTexturePtr(texture_Name, assetManager);
|
||||
}
|
||||
|
||||
Sprite(SDL_Texture* texture, int renderLayer = 1,
|
||||
glm::vec2 position = glm::vec2(0, 0), glm::vec2 size = glm::vec2(100, 100))
|
||||
: _layer(renderLayer), _pos(position), _size(size)
|
||||
{
|
||||
SetTexturePtr(texture);
|
||||
}
|
||||
|
||||
~Sprite() {}
|
||||
|
||||
|
||||
/*
|
||||
//-----------
|
||||
// Setters
|
||||
//-----------
|
||||
*/
|
||||
|
||||
//Establece la textura a _spriteTx.
|
||||
//Lo llama el constructor.
|
||||
// ¿Queremos darle acceso al usuario? ¿Lo movemos a private / protected?
|
||||
void SetTexturePtr(SDL_Texture* texture);
|
||||
void SetTexturePtr(std::string textureName, AssetManager* assetManager);
|
||||
|
||||
|
||||
//Modifica la posición del Sprite.
|
||||
void SetPosition(float x, float y);
|
||||
void SetPosition(glm::vec<2, float> newPosition);
|
||||
|
||||
//Modifica la escala de sprite (en relación a la textura).
|
||||
void SetSize(float x, float y);
|
||||
void SetSize(glm::vec<2,float> newSize);
|
||||
|
||||
//Set rendering layer.
|
||||
void SetLayer(int newLayer);
|
||||
|
||||
|
||||
|
||||
//Implementa el Draw de Drawable.
|
||||
void Draw(Renderer* renderer) override;
|
||||
|
||||
|
||||
private:
|
||||
SDL_Texture* _spriteTx = nullptr;
|
||||
|
||||
glm::vec<2, float> _pos = glm::vec2(25, 25);
|
||||
glm::vec<2, float> _size = glm::vec2(100, 100);
|
||||
|
||||
int _layer = 1;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
+38
-12
@@ -1,9 +1,11 @@
|
||||
#include "Game.h"
|
||||
#include "Render/Renderer.h"
|
||||
#include "Render\Renderer.h"
|
||||
|
||||
#include "Drawables\Sprite.h"
|
||||
|
||||
int UE::Game::GameInit()
|
||||
{
|
||||
renderer = new Renderer();
|
||||
renderer = new Renderer(640,480);
|
||||
renderer->RenderInit();
|
||||
|
||||
assetManager = new AssetManager(renderer->GetSDLRenderer());
|
||||
@@ -18,34 +20,58 @@ int UE::Game::GameLoop()
|
||||
|
||||
#pragma region TEMP_REDERING
|
||||
|
||||
SDL_Surface* loaderSurface;
|
||||
//SDL_Surface* loaderSurface;
|
||||
SDL_Texture* background;
|
||||
SDL_Texture* test1;
|
||||
|
||||
|
||||
SDL_Rect DestR;
|
||||
DestR.x = 640 / 2 - 16 / 2;
|
||||
DestR.y = 580 / 2 - 16 / 2;
|
||||
DestR.w = 16;
|
||||
DestR.h = 16;
|
||||
DestR.x = 0;
|
||||
DestR.y = 0;
|
||||
DestR.w = 640;
|
||||
DestR.h = 480;
|
||||
|
||||
Asset as1("test1", "resources\\Sprites\\bc.png", AssetType::Texture);
|
||||
assetManager->AddAssetToQueue(as1);
|
||||
SDL_Rect DestR2;
|
||||
DestR2.x = 48;
|
||||
DestR2.y = 48;
|
||||
DestR2.w = 128;
|
||||
DestR2.h = 128;
|
||||
|
||||
|
||||
//Asset as1("bg1", "resources\\Sprites\\bc.png", AssetType::Texture);
|
||||
//assetManager->AddAssetToQueue(as1);
|
||||
assetManager->LoadAssetsFromFile("resources\\AssetList\\MainAssets.json");
|
||||
assetManager->LoadAssetQueue();
|
||||
|
||||
|
||||
background = assetManager->getTexture("test1");
|
||||
background = assetManager->getTexture("bg1");
|
||||
test1 = assetManager->getTexture("test1");
|
||||
|
||||
glm::vec2 sp1_pos(48, 48);
|
||||
glm::vec2 sp1_size(145, 145);
|
||||
Sprite sp1("test1", assetManager, 5, sp1_pos, sp1_size);
|
||||
Sprite sp2("test1", assetManager, 7, sp1_pos);
|
||||
Sprite sp3(test1);
|
||||
|
||||
|
||||
#pragma endregion
|
||||
|
||||
while (true)
|
||||
{
|
||||
|
||||
SDL_RenderClear(renderer->GetSDLRenderer());
|
||||
//FRAME PHYSICS
|
||||
//TICK ALL OBJECTS.
|
||||
renderer->QueueDrawCall(1, background, &DestR);
|
||||
renderer->QueueDrawCall(6, test1, DestR2);
|
||||
renderer->QueueDrawCall(1, background, DestR);
|
||||
|
||||
renderer->Render(sp1);
|
||||
sp2.Draw(renderer);
|
||||
//sp3.Draw(renderer);
|
||||
|
||||
renderer->RenderDraw();
|
||||
renderer->RenderUpdate();
|
||||
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace UE
|
||||
|
||||
class Drawable
|
||||
{
|
||||
class DrawCall;
|
||||
|
||||
public:
|
||||
Drawable() {}
|
||||
|
||||
@@ -34,7 +34,7 @@ int UE::Renderer::RenderInit()
|
||||
|
||||
|
||||
//Init layers.
|
||||
for (int i = 0; i < layers; i++)
|
||||
for (int i = 0; i < LAYER_AMOUNT; i++)
|
||||
{
|
||||
renderLayers.push_back(std::vector<DrawCall>());
|
||||
}
|
||||
@@ -48,9 +48,9 @@ void UE::Renderer::RenderDraw()
|
||||
DrawCall item = renderQueue.at(i);
|
||||
|
||||
//Add item i to layer number i.layer.
|
||||
if (item.layer > layers)
|
||||
if (item.layer > LAYER_AMOUNT)
|
||||
{
|
||||
std::cout << "Layer out of range";
|
||||
std::cout << "Layer out of range\n";
|
||||
return;
|
||||
}
|
||||
renderLayers.at(item.layer).push_back(item);
|
||||
@@ -66,7 +66,11 @@ void UE::Renderer::RenderDraw()
|
||||
for (int k = 0; k < layer.size(); k++)
|
||||
{
|
||||
DrawCall item = layer.at(k);
|
||||
SDL_RenderCopy(main_Renderer, item.tx, item.tx_rect, item.pos_rect);
|
||||
if (!item.fullTexture)
|
||||
{
|
||||
SDL_RenderCopy(main_Renderer, item.tx, &item.tx_rect, &item.pos_rect);
|
||||
}
|
||||
SDL_RenderCopy(main_Renderer, item.tx, NULL, &item.pos_rect);
|
||||
}
|
||||
layer.clear();
|
||||
}
|
||||
@@ -100,15 +104,16 @@ void UE::Renderer::QueueDrawCall(DrawCall drawCall)
|
||||
renderQueue.push_back(drawCall);
|
||||
}
|
||||
|
||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect* texture_position, SDL_Rect* position)
|
||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect & texture_position, SDL_Rect & position)
|
||||
{
|
||||
DrawCall dc = DrawCall(layer, texture, texture_position, position);
|
||||
DrawCall dc = DrawCall(layer, position, texture, texture_position, false);
|
||||
renderQueue.push_back(dc);
|
||||
}
|
||||
|
||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect* position)
|
||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect & position)
|
||||
{
|
||||
DrawCall dc = DrawCall(layer, texture, NULL, position);
|
||||
//std::cout << "Drawable added to queue";
|
||||
DrawCall dc = DrawCall(layer, position, texture);
|
||||
renderQueue.push_back(dc);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,8 @@ namespace UE
|
||||
|
||||
public:
|
||||
/* Constructor */
|
||||
Renderer()
|
||||
Renderer(int windowWidth, int windowHeight, int renderingLayers = 10)
|
||||
: SCREEN_WIDTH(windowWidth), SCREEN_HEIGHT(windowHeight), LAYER_AMOUNT(renderingLayers)
|
||||
{
|
||||
printf("Renderer Created");
|
||||
}
|
||||
@@ -46,8 +47,8 @@ namespace UE
|
||||
|
||||
//Añade un DrawCall al renderQueue.
|
||||
void QueueDrawCall(DrawCall drawCall);
|
||||
void QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect* texture_position, SDL_Rect* position);
|
||||
void QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect* position);
|
||||
void QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect& texture_position, SDL_Rect& position);
|
||||
void QueueDrawCall(int layer, SDL_Texture* texture, SDL_Rect & position);
|
||||
|
||||
//Le pasamos el este renderer al Drawable para que haga lo que quiera.
|
||||
//Como si quiere usar el SDL_Renderer, es libre.
|
||||
@@ -57,6 +58,7 @@ namespace UE
|
||||
SDL_Renderer* GetSDLRenderer() { return main_Renderer; }
|
||||
|
||||
private:
|
||||
/* Objetos de SDL para el rendering. */
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Surface* screenSurface = NULL;
|
||||
SDL_Renderer* main_Renderer = NULL;
|
||||
@@ -65,7 +67,9 @@ namespace UE
|
||||
//Se podría sustituir por un std::queue o std::deque si fuera necesario.
|
||||
std::vector<DrawCall> renderQueue;
|
||||
|
||||
const int layers = 10;
|
||||
//Las capas en las que se organizan los diferentes objetos.
|
||||
//Permite dibujar unos sprites sobre otros.
|
||||
const int LAYER_AMOUNT = 10;
|
||||
std::vector<std::vector<DrawCall>> renderLayers;
|
||||
|
||||
|
||||
@@ -75,20 +79,25 @@ namespace UE
|
||||
};
|
||||
|
||||
/*
|
||||
Contiene los valores necesarios para realizar el renderizado.
|
||||
Es el resultado de las funciones Draw de los objetos.
|
||||
Contiene los valores necesarios para realizar el renderizado con SDL_RenderCopy();
|
||||
Se almacena en la renderQueue y luego son ordenados por las renderLayers.
|
||||
*/
|
||||
struct DrawCall
|
||||
{
|
||||
DrawCall(int layer, SDL_Texture* texture, SDL_Rect* texture_position, SDL_Rect* position)
|
||||
: layer(layer), tx(texture), tx_rect(texture_position), pos_rect(position) {}
|
||||
DrawCall(int layer, SDL_Rect position, SDL_Texture* texture, SDL_Rect texture_position = SDL_Rect(), bool renderFullTexture = true)
|
||||
: layer(layer), fullTexture(renderFullTexture), tx(texture), tx_rect(texture_position), pos_rect(position)
|
||||
{}
|
||||
|
||||
int layer;
|
||||
/*Es necesario saber si es la textura completa o sólo una parte.
|
||||
//De tal forma, el renderer puede pasar un NULL o el tx_rect al SDL_RenderCopy().
|
||||
*/
|
||||
bool fullTexture;
|
||||
|
||||
SDL_Texture* tx;
|
||||
SDL_Rect* tx_rect;
|
||||
SDL_Rect tx_rect;
|
||||
|
||||
SDL_Rect* pos_rect;
|
||||
SDL_Rect pos_rect;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Drawable.h"
|
||||
|
||||
|
||||
namespace UE
|
||||
{
|
||||
|
||||
class Sprite : public Drawable
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
+5
-4
@@ -33,13 +33,14 @@ project (""..GameName.."")
|
||||
|
||||
includedirs
|
||||
{
|
||||
"UberEngine/vendor/FastNoiseLite", --Header Only
|
||||
"UberEngine/vendor/glm",
|
||||
"UberEngine/vendor/ImGui",
|
||||
"UberEngine/vendor/json", --Header-Only
|
||||
-- "UberEngine/vendor/FastNoiseLite", --Header Only
|
||||
-- "UberEngine/vendor/glm",
|
||||
-- "UberEngine/vendor/ImGui",
|
||||
-- "UberEngine/vendor/json", --Header-Only
|
||||
"UberEngine/vendor/SDL/include",
|
||||
"UberEngine/vendor/SDL2_image/include",
|
||||
"UberEngine/vendor/spdlog/include",
|
||||
"UberEngine/vendor",
|
||||
"UberEngine/src"
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"Textures": [
|
||||
{
|
||||
"name": "bg1",
|
||||
"Path": "resources/Sprites/bc.png",
|
||||
"Type": 0,
|
||||
"ID": 0
|
||||
},
|
||||
{
|
||||
"name": "test1",
|
||||
"Path": "resources/Sprites/Test1.png",
|
||||
"Type": 0,
|
||||
"ID": 1
|
||||
},
|
||||
{
|
||||
"name": "Player",
|
||||
"Path": "resources/Sprites/Player.png",
|
||||
"Type": 0,
|
||||
"ID": 2
|
||||
},
|
||||
{
|
||||
"name": "Button",
|
||||
"Path": "resources/Sprites/Button.png",
|
||||
"Type": 0,
|
||||
"ID": 3
|
||||
},
|
||||
{
|
||||
"name": "Tilemap",
|
||||
"Path": "resources/Sprites/Tilemap2.png",
|
||||
"Type": 0,
|
||||
"ID": 5
|
||||
}
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 474 B |
Binary file not shown.
|
After Width: | Height: | Size: 599 B |
Binary file not shown.
|
After Width: | Height: | Size: 630 B |
Binary file not shown.
|
After Width: | Height: | Size: 170 B |
Binary file not shown.
|
After Width: | Height: | Size: 236 B |
Reference in New Issue
Block a user