Some Refactor, Redesign and comments.
Eliminamos la clase Game para mover la ejecución base al Main. More Stuff.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <json.hpp>
|
#include <json\json.hpp>
|
||||||
using json = nlohmann::ordered_json;
|
using json = nlohmann::ordered_json;
|
||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
@@ -39,7 +39,7 @@ namespace UE
|
|||||||
{"name", as._name},
|
{"name", as._name},
|
||||||
{"Path", as._path},
|
{"Path", as._path},
|
||||||
{"Type", as._type},
|
{"Type", as._type},
|
||||||
{"ID", as._ID}
|
//{"ID", as._ID}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ namespace UE
|
|||||||
j.at("name").get_to(as._name);
|
j.at("name").get_to(as._name);
|
||||||
j.at("Path").get_to(as._path);
|
j.at("Path").get_to(as._path);
|
||||||
j.at("Type").get_to(as._type);
|
j.at("Type").get_to(as._type);
|
||||||
j.at("ID").get_to(as._ID);
|
//j.at("ID").get_to(as._ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -9,21 +9,22 @@
|
|||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
{
|
{
|
||||||
int AssetManager::AssetManagerInit()
|
int AssetManager::InitAssetManager()
|
||||||
{
|
{
|
||||||
IMG_Init(IMG_INIT_PNG);
|
IMG_Init(IMG_INIT_PNG);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AssetManager::AssetManagerClean()
|
int AssetManager::CleanAssetManager()
|
||||||
{
|
{
|
||||||
for (auto i = _textures.begin(); i != _textures.end(); i++)
|
for (auto i = _textures.begin(); i != _textures.end(); i++)
|
||||||
{
|
{
|
||||||
SDL_DestroyTexture(i->second);
|
SDL_DestroyTexture(i->second);
|
||||||
}
|
}
|
||||||
//CPP se debería de encargar de ellos, pero bueno.
|
//CPP se debería de encargar de ellos, pero bueno.
|
||||||
_textures.clear();
|
|
||||||
_assets.clear();
|
_assets.clear();
|
||||||
|
_textures.clear();
|
||||||
|
_SpriteSheets.clear();
|
||||||
|
|
||||||
IMG_Quit();
|
IMG_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
@@ -31,12 +32,14 @@ namespace UE
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
int AssetManager::LoadAssetsFromFile(std::string fileToLoad)
|
|
||||||
|
void AssetManager::LoadAssetsFromFile(std::string filePath, LoadMethod loadMethod)
|
||||||
{
|
{
|
||||||
std::fstream file(fileToLoad.c_str());
|
std::fstream file(filePath.c_str());
|
||||||
|
|
||||||
//Load Asset data from JSON.
|
//Load Asset data from JSON.
|
||||||
json jread = json::parse(file);
|
json jread = json::parse(file);
|
||||||
|
|
||||||
json Textures = jread["Textures"];
|
json Textures = jread["Textures"];
|
||||||
json SpriteSheets = jread["SpriteSheets"];
|
json SpriteSheets = jread["SpriteSheets"];
|
||||||
json Sounds = jread["Sounds"];
|
json Sounds = jread["Sounds"];
|
||||||
@@ -50,31 +53,42 @@ namespace UE
|
|||||||
{
|
{
|
||||||
_assets.push_back(SpriteSheets.at(i));
|
_assets.push_back(SpriteSheets.at(i));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < Sounds.size(); i++) {
|
for (int i = 0; i < Sounds.size(); i++)
|
||||||
|
{
|
||||||
_assets.push_back(Sounds.at(i));
|
_assets.push_back(Sounds.at(i));
|
||||||
}
|
}
|
||||||
for (int i = 0; i < Others.size(); i++) {
|
for (int i = 0; i < Others.size(); i++)
|
||||||
|
{
|
||||||
_assets.push_back(Others.at(i));
|
_assets.push_back(Others.at(i));
|
||||||
}
|
}
|
||||||
file.close();
|
file.close();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int AssetManager::LoadAssetQueue()
|
void AssetManager::LoadAsset(Asset& as, LoadMethod loadMethod)
|
||||||
{
|
{
|
||||||
|
_assets.push_back(as);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssetManager::ProcessAssetQueue(int amount)
|
||||||
|
{
|
||||||
|
int cantidad = amount;
|
||||||
|
|
||||||
|
if (amount == -1 || cantidad >= _assets.size())
|
||||||
|
cantidad = _assets.size();
|
||||||
|
|
||||||
//Load Assets from files, depending on the type.
|
//Load Assets from files, depending on the type.
|
||||||
for (int i = 0; i < _assets.size(); i++)
|
for (int i = 0; i < cantidad; i++)
|
||||||
{
|
{
|
||||||
UE::Asset& as = _assets.at(i);
|
Asset& as = _assets.at(i);
|
||||||
switch (as._type)
|
switch (as._type)
|
||||||
{
|
{
|
||||||
case UE::AssetType::Texture:
|
case AssetType::Texture:
|
||||||
{
|
{
|
||||||
AddTexture(as._name, as._path);
|
AddTexture(as._name, as._path);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case UE::AssetType::SpriteSheet:
|
case AssetType::SpriteSheet:
|
||||||
{
|
{
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -82,20 +96,14 @@ namespace UE
|
|||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
std::cout << "Can't load Assets." << std::endl;
|
std::cout << "Can't load Assets." << std::endl;
|
||||||
return ERROR;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_assets.clear();
|
_assets.clear();
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AssetManager::AddAssetToQueue(Asset& as)
|
|
||||||
{
|
|
||||||
_assets.push_back(as);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void AssetManager::AddTexture(std::string name, std::string txPath)
|
void AssetManager::AddTexture(std::string name, std::string txPath)
|
||||||
@@ -111,11 +119,16 @@ namespace UE
|
|||||||
_textures.erase(name);
|
_textures.erase(name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SDL_Texture* AssetManager::getTexture(std::string TextureName)
|
SDL_Texture* AssetManager::getTexture(std::string TextureName)
|
||||||
{
|
{
|
||||||
return _textures.at(TextureName);
|
return _textures.at(TextureName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void AssetManager::AddSpriteSheet(std::string name, std::string spPath)
|
void AssetManager::AddSpriteSheet(std::string name, std::string spPath)
|
||||||
{
|
{
|
||||||
std::fstream file(spPath.c_str());
|
std::fstream file(spPath.c_str());
|
||||||
|
|||||||
@@ -20,45 +20,81 @@
|
|||||||
/// - Modificar las funciones de Carga para quitar la queue o implementar una que tenga sentido.
|
/// - 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
|
/// 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.
|
/// procesar toda la cola de golpe. Optimizaría la carga de assets.
|
||||||
|
/// - Terminar la implementación del tipo de carga (queue/now).
|
||||||
|
/// - Modificar los for en la carga de archivo, así como la estructura del archivo JSON. El tipo de Asset está repetido.
|
||||||
/// - Logging.
|
/// - Logging.
|
||||||
/// - ERROR HANDLING
|
/// - ERROR HANDLING
|
||||||
/// - More asset types.
|
/// - More asset types.
|
||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
{
|
{
|
||||||
|
enum class LoadMethod
|
||||||
|
{
|
||||||
|
//Añade los Assets a una cola, que será procesada poco a poco mediante ProcessAssetQueue().
|
||||||
|
Queue,
|
||||||
|
|
||||||
|
//Carga el Asset directamente sin pasar por la cola. Cargar muchos assets a la vez así puede detener
|
||||||
|
//el programa hasta que se termine la carga, dando una sensación de unresponsividad.
|
||||||
|
Now
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Gestiona todos assets primitivos, almacenando sus ptrs en un lugar seguro. */
|
/* Gestiona todos assets primitivos, almacenando sus ptrs en un lugar seguro. */
|
||||||
class AssetManager
|
class AssetManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/* Constructor. */
|
/* Constructor. */
|
||||||
AssetManager(SDL_Renderer* SDLrenderer,
|
AssetManager(SDL_Renderer* SDLrenderer)
|
||||||
std::string AssetPath = "")
|
|
||||||
: _mainRenderer(SDLrenderer) {}
|
: _mainRenderer(SDLrenderer) {}
|
||||||
|
|
||||||
|
|
||||||
/* Init AssetManager y todo lo necesario para funcionar correctamente.
|
/* Init AssetManager y todo lo necesario para funcionar correctamente.
|
||||||
- Inicia SDL_IMG.
|
- Inicia SDL_IMG.
|
||||||
*/
|
*/
|
||||||
int AssetManagerInit();
|
int InitAssetManager();
|
||||||
|
|
||||||
/* Clean AssetManager Data on exit
|
/* Clean AssetManager Data on exit
|
||||||
- Elimina todos los assets de sus contenedores.
|
- Elimina todos los assets de sus contenedores.
|
||||||
- Descarga SDL_IMG
|
- Descarga SDL_IMG
|
||||||
*/
|
*/
|
||||||
int AssetManagerClean();
|
int CleanAssetManager();
|
||||||
|
|
||||||
|
|
||||||
/* Carga de Assets */
|
/* Carga de Assets */
|
||||||
/* Se cargarán a una queue los asset struct desde un fichero JSON. */
|
|
||||||
int LoadAssetsFromFile(std::string fileToLoad); //Añade a la cola los assets de un archivo.
|
/**
|
||||||
void AddAssetToQueue(Asset& as); //Añade manualmente un asset a la cola de carga.
|
* Carga los datos de los Assets desde un fichero JSON según parámetros.
|
||||||
/* Se cargarán en memoria los assets de la queue en su correspondiente contenedor. */
|
*
|
||||||
int LoadAssetQueue(); //Procesa la queue de carga.
|
* \param filePath: Ubicación del archivo JSON.
|
||||||
|
* \param loadMethod: Forma de carga. Procesado al momento o añadido a la cola de carga.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void LoadAssetsFromFile(std::string filePath, LoadMethod loadMethod = LoadMethod::Queue);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Carga el Asset pasado por parámetro.
|
||||||
|
*
|
||||||
|
* \param as: Asset que cargar.
|
||||||
|
* \param loadMethod: Forma de carga. Procesado al momento o añadido a la cola de carga.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void LoadAsset(Asset& as, LoadMethod loadMethod = LoadMethod::Queue);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Procesa la cola de carga de Assets.
|
||||||
|
*
|
||||||
|
* \param Amount: Número de Assets de la cola a procesar. Si vale -1 se procesarán todos.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void ProcessAssetQueue(int amount = -1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Métodos para manejar las SDL_Texture* almacenados.
|
Métodos para manejar las SDL_Texture* almacenados.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//Crea una textura de SDL y la añade al mapa de texturas identificada por su nombre.
|
//Crea una textura de SDL y la añade al mapa de texturas identificada por su nombre.
|
||||||
void AddTexture(std::string name, std::string txPath);
|
void AddTexture(std::string name, std::string txPath);
|
||||||
|
|
||||||
@@ -90,7 +126,8 @@ namespace UE
|
|||||||
std::vector<UE::Asset> _assets = {};
|
std::vector<UE::Asset> _assets = {};
|
||||||
|
|
||||||
/* Almacenes de asssets */
|
/* Almacenes de asssets */
|
||||||
//Almacén de SDL_Texture* (ptrs).
|
|
||||||
|
//Almacén de SDL_Texture* (ptrs, pues lo gestiona SDL).
|
||||||
std::unordered_map<std::string, SDL_Texture*> _textures = {};
|
std::unordered_map<std::string, SDL_Texture*> _textures = {};
|
||||||
//Almacén de SpriteSheets.
|
//Almacén de SpriteSheets.
|
||||||
std::unordered_map<std::string, SpriteSheet> _SpriteSheets = {};
|
std::unordered_map<std::string, SpriteSheet> _SpriteSheets = {};
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#include "SpriteSheet.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void UE::SpriteSheet::CalculateRects()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < _textureAmount; i++)
|
||||||
|
{
|
||||||
|
int posX = _textureWidth * i;
|
||||||
|
int posY = 0; //Siempre vale 0 en una textura de 1 dimensión.
|
||||||
|
|
||||||
|
SDL_Rect rect = {posX, posY, _textureWidth, _textureHeight};
|
||||||
|
_txRects.push_back(rect);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,8 +3,8 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <json.hpp>
|
#include <json\json.hpp>
|
||||||
using json = nlohmann::ordered_json;
|
//using json = nlohmann::ordered_json;
|
||||||
|
|
||||||
|
|
||||||
namespace UE
|
namespace UE
|
||||||
@@ -31,6 +31,7 @@ namespace UE
|
|||||||
|
|
||||||
//Debe coincicidir con el nombre de la textura a la que se asocia.
|
//Debe coincicidir con el nombre de la textura a la que se asocia.
|
||||||
std::string _spriteSheetName = "";
|
std::string _spriteSheetName = "";
|
||||||
|
|
||||||
int _textureAmount = 1;
|
int _textureAmount = 1;
|
||||||
int _textureWidth = 16;
|
int _textureWidth = 16;
|
||||||
int _textureHeight = 16;
|
int _textureHeight = 16;
|
||||||
@@ -45,16 +46,16 @@ namespace UE
|
|||||||
{
|
{
|
||||||
j = nlohmann::ordered_json
|
j = nlohmann::ordered_json
|
||||||
{
|
{
|
||||||
{"TextureName", sp._spriteSheetName},
|
{"Name", sp._spriteSheetName},
|
||||||
{"TextureAmount", sp._textureAmount},
|
{"TextureAmount", sp._textureAmount},
|
||||||
{"TextureWidth", sp._textureWidth},
|
{"TextureWidth", sp._textureWidth},
|
||||||
{"TextureHeight", sp._textureHeight}
|
{"TextureHeight", sp._textureHeight}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void from_json(nlohmann::ordered_json& j, const SpriteSheet& sp)
|
inline void from_json(const nlohmann::ordered_json& j, SpriteSheet& sp)
|
||||||
{
|
{
|
||||||
j.at("TextureName").get_to(sp._spriteSheetName);
|
j.at("Name").get_to(sp._spriteSheetName);
|
||||||
j.at("TextureAmount").get_to(sp._textureAmount);
|
j.at("TextureAmount").get_to(sp._textureAmount);
|
||||||
j.at("TextureWidth").get_to(sp._textureWidth);
|
j.at("TextureWidth").get_to(sp._textureWidth);
|
||||||
j.at("TextureHeight").get_to(sp._textureHeight);
|
j.at("TextureHeight").get_to(sp._textureHeight);
|
||||||
|
|||||||
@@ -10,10 +10,6 @@ void UE::Sprite::SetTexturePtr(SDL_Texture* texture)
|
|||||||
_spriteTx = texture;
|
_spriteTx = texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Sprite::SetTexturePtr(std::string textureName, AssetManager* assetManager)
|
|
||||||
{
|
|
||||||
_spriteTx = assetManager->getTexture(textureName);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UE::Sprite::SetPosition(float x, float y)
|
void UE::Sprite::SetPosition(float x, float y)
|
||||||
{
|
{
|
||||||
@@ -36,6 +32,7 @@ void UE::Sprite::SetSize(glm::vec<2,float> newSize)
|
|||||||
_size = newSize;
|
_size = newSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UE::Sprite::SetLayer(int newLayer)
|
void UE::Sprite::SetLayer(int newLayer)
|
||||||
{
|
{
|
||||||
_layer = newLayer;
|
_layer = newLayer;
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
//#include <glm.hpp>
|
//#include <glm.hpp>
|
||||||
#include <vec2.hpp>
|
|
||||||
|
#include <glm\vec2.hpp>
|
||||||
|
|
||||||
|
|
||||||
#include "Render\Drawable.h"
|
#include "Render\Drawable.h"
|
||||||
@@ -40,17 +41,6 @@ namespace UE
|
|||||||
Los demás parámetros tienen un valor por defecto.
|
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,
|
Sprite(SDL_Texture* texture,
|
||||||
int renderLayer = 1,
|
int renderLayer = 1,
|
||||||
glm::vec2 position = glm::vec2(0, 0),
|
glm::vec2 position = glm::vec2(0, 0),
|
||||||
@@ -75,14 +65,13 @@ namespace UE
|
|||||||
//Lo llama el constructor.
|
//Lo llama el constructor.
|
||||||
// ¿Queremos darle acceso al usuario? ¿Lo movemos a private / protected?
|
// ¿Queremos darle acceso al usuario? ¿Lo movemos a private / protected?
|
||||||
void SetTexturePtr(SDL_Texture* texture);
|
void SetTexturePtr(SDL_Texture* texture);
|
||||||
void SetTexturePtr(std::string textureName, AssetManager* assetManager);
|
|
||||||
|
|
||||||
|
|
||||||
//Modifica la posición del Sprite.
|
//Modifica la posición del Sprite.
|
||||||
void SetPosition(float x, float y);
|
void SetPosition(float x, float y);
|
||||||
void SetPosition(glm::vec<2, float> newPosition);
|
void SetPosition(glm::vec<2, float> newPosition);
|
||||||
|
|
||||||
//Modifica la escala de sprite (en relación a la textura).
|
//Modifica la escala de sprite ¿(en relación a la textura)?.
|
||||||
void SetSize(float x, float y);
|
void SetSize(float x, float y);
|
||||||
void SetSize(glm::vec<2,float> newSize);
|
void SetSize(glm::vec<2,float> newSize);
|
||||||
|
|
||||||
@@ -97,7 +86,7 @@ namespace UE
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Texture* _spriteTx = nullptr;
|
SDL_Texture* _spriteTx = nullptr;
|
||||||
SDL_Rect* _txRect = NULL;
|
SDL_Rect* _txRect = nullptr;
|
||||||
|
|
||||||
glm::vec<2, float> _pos = glm::vec2(25, 25);
|
glm::vec<2, float> _pos = glm::vec2(25, 25);
|
||||||
glm::vec<2, float> _size = glm::vec2(100, 100);
|
glm::vec<2, float> _size = glm::vec2(100, 100);
|
||||||
|
|||||||
+22
-17
@@ -7,16 +7,27 @@
|
|||||||
|
|
||||||
int UE::Game::GameInit()
|
int UE::Game::GameInit()
|
||||||
{
|
{
|
||||||
|
|
||||||
renderer = new Renderer(640, 480);
|
renderer = new Renderer(640, 480);
|
||||||
renderer->RenderInit();
|
renderer->InitRenderer();
|
||||||
|
|
||||||
assetManager = new AssetManager(renderer->GetSDLRenderer());
|
assetManager = new AssetManager(renderer->GetSDLRenderer());
|
||||||
assetManager->AssetManagerInit();
|
assetManager->InitAssetManager();
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int UE::Game::GameLoad()
|
||||||
|
{
|
||||||
|
//Asset as1("bg1", "resources\\Sprites\\bc.png", AssetType::Texture);
|
||||||
|
//assetManager->AddAssetToQueue(as1);
|
||||||
|
assetManager->LoadAssetsFromFile("resources\\AssetList\\MainAssets.json");
|
||||||
|
assetManager->ProcessAssetQueue();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int UE::Game::GameLoop()
|
int UE::Game::GameLoop()
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -39,20 +50,13 @@ int UE::Game::GameLoop()
|
|||||||
DestR2.w = 128;
|
DestR2.w = 128;
|
||||||
DestR2.h = 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("bg1");
|
background = assetManager->getTexture("bg1");
|
||||||
test1 = assetManager->getTexture("test1");
|
test1 = assetManager->getTexture("test1");
|
||||||
|
|
||||||
glm::vec2 sp1_pos(48, 48);
|
glm::vec2 sp1_pos(48, 48);
|
||||||
glm::vec2 sp1_size(145, 145);
|
glm::vec2 sp1_size(145, 145);
|
||||||
Sprite sp1("test1", assetManager, 5, sp1_pos, sp1_size);
|
//Sprite sp1("test1", assetManager, 5, sp1_pos, sp1_size);
|
||||||
Sprite sp2("test1", assetManager, 7, sp1_pos);
|
//Sprite sp2("test1", assetManager, 7, sp1_pos);
|
||||||
Sprite sp3(test1);
|
Sprite sp3(test1);
|
||||||
|
|
||||||
|
|
||||||
@@ -85,11 +89,12 @@ int UE::Game::GameLoop()
|
|||||||
const Uint8* keyStates = SDL_GetKeyboardState(NULL);
|
const Uint8* keyStates = SDL_GetKeyboardState(NULL);
|
||||||
if (keyStates[SDL_SCANCODE_W])
|
if (keyStates[SDL_SCANCODE_W])
|
||||||
{
|
{
|
||||||
DestR2.y += 25 * deltaTime;
|
DestR2.y -= (25 * deltaTime);
|
||||||
}
|
}
|
||||||
if(keyStates[SDL_SCANCODE_S])
|
if(keyStates[SDL_SCANCODE_S])
|
||||||
{
|
{
|
||||||
DestR2.y -= (25 * deltaTime);
|
DestR2.y -= (50 * deltaTime);
|
||||||
|
//std::cout << "S PRESS" << std::endl;
|
||||||
}
|
}
|
||||||
if (keyStates[SDL_SCANCODE_ESCAPE])
|
if (keyStates[SDL_SCANCODE_ESCAPE])
|
||||||
{
|
{
|
||||||
@@ -101,8 +106,8 @@ int UE::Game::GameLoop()
|
|||||||
renderer->QueueDrawCall(6, DestR2, test1);
|
renderer->QueueDrawCall(6, DestR2, test1);
|
||||||
renderer->QueueDrawCall(1, DestR, background);
|
renderer->QueueDrawCall(1, DestR, background);
|
||||||
|
|
||||||
renderer->Render(sp1);
|
//renderer->Render(sp1);
|
||||||
sp2.Draw(renderer);
|
//sp2.Draw(renderer);
|
||||||
//sp3.Draw(renderer);
|
//sp3.Draw(renderer);
|
||||||
|
|
||||||
renderer->RenderFrame();
|
renderer->RenderFrame();
|
||||||
@@ -127,10 +132,10 @@ int UE::Game::GameLoop()
|
|||||||
int UE::Game::GameClean()
|
int UE::Game::GameClean()
|
||||||
{
|
{
|
||||||
|
|
||||||
assetManager->AssetManagerClean();
|
assetManager->CleanAssetManager();
|
||||||
delete assetManager;
|
delete assetManager;
|
||||||
|
|
||||||
renderer->RendererClean();
|
renderer->CleanRenderer();
|
||||||
delete renderer;
|
delete renderer;
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
//#include <iostream>
|
//#include <iostream>
|
||||||
|
|
||||||
/*LIB Includes*/
|
/*LIB Includes*/
|
||||||
#include <imgui.h>
|
#include <ImGui\imgui.h>
|
||||||
|
|
||||||
/*UE Includes*/
|
/*UE Includes*/
|
||||||
#include "Render\Renderer.h"
|
#include "Render\Renderer.h"
|
||||||
@@ -20,7 +20,7 @@ namespace UE
|
|||||||
{
|
{
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
#pragma region Singleton
|
#pragma region Singleton
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
#include "Renderer.h"
|
#include "Renderer.h"
|
||||||
|
|
||||||
|
namespace UE
|
||||||
|
{
|
||||||
|
//class Game;
|
||||||
|
|
||||||
//class UE::Game;
|
int Renderer::InitRenderer()
|
||||||
|
|
||||||
int UE::Renderer::RenderInit()
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||||
@@ -40,7 +41,7 @@ int UE::Renderer::RenderInit()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Renderer::RendererClean()
|
void Renderer::CleanRenderer()
|
||||||
{
|
{
|
||||||
renderQueue.empty();
|
renderQueue.empty();
|
||||||
|
|
||||||
@@ -50,21 +51,21 @@ void UE::Renderer::RendererClean()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void UE::Renderer::RenderFrame()
|
void Renderer::RenderFrame()
|
||||||
{
|
{
|
||||||
RenderClear();
|
StartRender();
|
||||||
RenderDraw();
|
RenderQueue();
|
||||||
RenderUpdate();
|
FinishRender();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UE::Renderer::RenderClear()
|
void Renderer::StartRender()
|
||||||
{
|
{
|
||||||
SDL_RenderClear(main_Renderer);
|
SDL_RenderClear(main_Renderer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Renderer::RenderDraw()
|
void Renderer::RenderQueue()
|
||||||
{
|
{
|
||||||
//Add items to their layer.
|
//Add items to their layer.
|
||||||
for (int i = 0; i < renderQueue.size(); i++)
|
for (int i = 0; i < renderQueue.size(); i++)
|
||||||
@@ -102,7 +103,7 @@ void UE::Renderer::RenderDraw()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UE::Renderer::RenderUpdate()
|
void Renderer::FinishRender()
|
||||||
{
|
{
|
||||||
//SDL_UpdateWindowSurface(window);
|
//SDL_UpdateWindowSurface(window);
|
||||||
SDL_RenderPresent(main_Renderer);
|
SDL_RenderPresent(main_Renderer);
|
||||||
@@ -112,32 +113,60 @@ void UE::Renderer::RenderUpdate()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void UE::Renderer::DrawObject(SDL_Rect* position, SDL_Rect* texture_pos, SDL_Texture* texture)
|
void Renderer::DrawObject(SDL_Rect* position, SDL_Rect* texture_pos, SDL_Texture* texture)
|
||||||
{
|
{
|
||||||
SDL_RenderCopy(main_Renderer, texture, texture_pos, position);
|
SDL_RenderCopy(main_Renderer, texture, texture_pos, position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void UE::Renderer::QueueDrawCall(DrawCall drawCall)
|
void Renderer::QueueDrawCall(DrawCall drawCall)
|
||||||
{
|
{
|
||||||
renderQueue.push_back(drawCall);
|
//Add item i to layer number i.layer.
|
||||||
|
if (drawCall.layer > LAYER_AMOUNT)
|
||||||
|
{
|
||||||
|
std::cout << "Layer out of range\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
renderLayers.at(drawCall.layer).push_back(drawCall);
|
||||||
|
|
||||||
|
//renderQueue.push_back(drawCall);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Rect & position, SDL_Texture* texture, SDL_Rect & texture_position)
|
void Renderer::QueueDrawCall(int layer, SDL_Rect & position, SDL_Texture* texture, SDL_Rect & texture_position)
|
||||||
{
|
{
|
||||||
DrawCall dc = DrawCall(layer, position, texture, texture_position, false);
|
DrawCall dc = DrawCall(layer, position, texture, texture_position, false);
|
||||||
renderQueue.push_back(dc);
|
|
||||||
|
//Add item i to layer number i.layer.
|
||||||
|
if (layer > LAYER_AMOUNT)
|
||||||
|
{
|
||||||
|
std::cout << "Layer out of range\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
renderLayers.at(layer).push_back(dc);
|
||||||
|
|
||||||
|
//renderQueue.push_back(dc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Renderer::QueueDrawCall(int layer, SDL_Rect & position, SDL_Texture* texture)
|
void Renderer::QueueDrawCall(int layer, SDL_Rect & position, SDL_Texture* texture)
|
||||||
{
|
{
|
||||||
//std::cout << "Drawable added to queue";
|
//std::cout << "Drawable added to queue";
|
||||||
DrawCall dc = DrawCall(layer, position, texture);
|
DrawCall dc = DrawCall(layer, position, texture);
|
||||||
renderQueue.push_back(dc);
|
|
||||||
|
if (layer >= LAYER_AMOUNT)
|
||||||
|
{
|
||||||
|
std::cout << "Layer out of range\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
renderLayers.at(layer).push_back(dc);
|
||||||
|
|
||||||
|
//renderQueue.push_back(dc);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UE::Renderer::Render(Drawable& drawable)
|
void Renderer::Render(Drawable& drawable)
|
||||||
{
|
{
|
||||||
drawable.Draw(this);
|
drawable.Draw(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -40,10 +40,10 @@ namespace UE
|
|||||||
- Crea el renderer.
|
- Crea el renderer.
|
||||||
- ...
|
- ...
|
||||||
*/
|
*/
|
||||||
int RenderInit();
|
int InitRenderer();
|
||||||
|
|
||||||
/* Limpia todo antes de salir. */
|
/* Limpia todo antes de salir. */
|
||||||
void RendererClean();
|
void CleanRenderer();
|
||||||
|
|
||||||
|
|
||||||
//Realizar RenderClear(), RenderDraw() y RenderUpdate();
|
//Realizar RenderClear(), RenderDraw() y RenderUpdate();
|
||||||
@@ -71,14 +71,14 @@ namespace UE
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
//Hace el SDL_RenderClear() para limpiar el buffer al principio de un nuevo frame.
|
//Hace el SDL_RenderClear() para limpiar el buffer al principio de un nuevo frame.
|
||||||
void RenderClear();
|
void StartRender();
|
||||||
|
|
||||||
//Ordena por layer todos los objetos de la renderQueue y los dibuja en orden.
|
//Ordena por layer todos los objetos de la renderQueue y los dibuja en orden.
|
||||||
//Se puede mover el proceso de ordenado a otra función, con el objeto de ir vaciando la Queue.
|
//Se puede mover el proceso de ordenado a otra función, con el objeto de ir vaciando la Queue.
|
||||||
void RenderDraw();
|
void RenderQueue();
|
||||||
|
|
||||||
//Hace el swap del render buffer (tras dibujar todos los objetos). Termina el renderizado.
|
//Hace el swap del render buffer (tras dibujar todos los objetos). Termina el renderizado.
|
||||||
void RenderUpdate();
|
void FinishRender();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
///
|
///
|
||||||
/// TODO:
|
/// TODO:
|
||||||
/// - Scene debería ser serializable y cargable desde archivos, al menos para las clases derivadas.
|
/// - Scene debería ser serializable y cargable desde archivos, al menos para las clases derivadas.
|
||||||
@@ -10,7 +12,8 @@
|
|||||||
namespace UE
|
namespace UE
|
||||||
{
|
{
|
||||||
|
|
||||||
/* Estructura básica de una escena. Almacena los datos mínimos.
|
/*
|
||||||
|
Estructura básica de una escena. Almacena los datos mínimos.
|
||||||
Se utilizará como clase base para las clases Scene específicas.
|
Se utilizará como clase base para las clases Scene específicas.
|
||||||
Esta clase será heredada por las escenas del usuario.
|
Esta clase será heredada por las escenas del usuario.
|
||||||
*/
|
*/
|
||||||
@@ -24,12 +27,16 @@ namespace UE
|
|||||||
void SceneInit(); //Te calienta y prepara la casa al llegar.
|
void SceneInit(); //Te calienta y prepara la casa al llegar.
|
||||||
void SceneClean(); //Te deja todo limpio y reluciente al salir.
|
void SceneClean(); //Te deja todo limpio y reluciente al salir.
|
||||||
|
|
||||||
|
|
||||||
/* Funciones que habrán de overlodear las escenas del usuario. */
|
/* Funciones que habrán de overlodear las escenas del usuario. */
|
||||||
virtual void SceneLoad();
|
virtual void SceneLoad();
|
||||||
virtual void SceneUpdate();
|
virtual void SceneUpdate();
|
||||||
virtual void SceneUnload();
|
virtual void SceneUnload();
|
||||||
|
|
||||||
|
|
||||||
|
void loadDataFromFile(std::string path);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int id = 0;
|
int id = 0;
|
||||||
//std::vector<Entity> _entidades;
|
//std::vector<Entity> _entidades;
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
|
|
||||||
|
#include "Render\Renderer.h"
|
||||||
|
#include "Assets\AssetManager.h"
|
||||||
+153
-5
@@ -1,11 +1,159 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <glm\glm.hpp>
|
||||||
|
|
||||||
|
#include "Render\Renderer.h"
|
||||||
|
#include "Drawables\Sprite.h"
|
||||||
#include "UberEngine.h"
|
#include "UberEngine.h"
|
||||||
|
|
||||||
|
|
||||||
|
static UE::Renderer* mainRenderer;
|
||||||
|
static UE::AssetManager* asManager;
|
||||||
|
|
||||||
|
static bool _quit = false;
|
||||||
|
|
||||||
|
void Init();
|
||||||
|
void Load();
|
||||||
|
void Loop();
|
||||||
|
void Clean();
|
||||||
|
|
||||||
int main(int argc, char* args[])
|
int main(int argc, char* args[])
|
||||||
{
|
{
|
||||||
static UE::Game& game = UE::Game::getInstance();
|
|
||||||
game.GameInit();
|
Init();
|
||||||
game.GameLoop();
|
Load();
|
||||||
game.GameClean();
|
Loop();
|
||||||
//exit = game.GameLoop();
|
Clean();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Init()
|
||||||
|
{
|
||||||
|
mainRenderer = new UE::Renderer(640, 480, 5);
|
||||||
|
mainRenderer->InitRenderer();
|
||||||
|
|
||||||
|
asManager = new UE::AssetManager(mainRenderer->GetSDLRenderer());
|
||||||
|
asManager->InitAssetManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Load()
|
||||||
|
{
|
||||||
|
asManager->LoadAssetsFromFile("resources\\AssetList\\MainAssets.json");
|
||||||
|
asManager->ProcessAssetQueue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Loop()
|
||||||
|
{
|
||||||
|
#pragma region TEMP_REDERING
|
||||||
|
|
||||||
|
//SDL_Surface* loaderSurface;
|
||||||
|
SDL_Texture* background;
|
||||||
|
SDL_Texture* test1;
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Rect DestR;
|
||||||
|
DestR.x = 0;
|
||||||
|
DestR.y = 0;
|
||||||
|
DestR.w = 640;
|
||||||
|
DestR.h = 480;
|
||||||
|
|
||||||
|
SDL_Rect DestR2;
|
||||||
|
DestR2.x = 48;
|
||||||
|
DestR2.y = 48;
|
||||||
|
DestR2.w = 128;
|
||||||
|
DestR2.h = 128;
|
||||||
|
|
||||||
|
background = asManager->getTexture("bg1");
|
||||||
|
test1 = asManager->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);
|
||||||
|
UE::Sprite sp3(test1);
|
||||||
|
|
||||||
|
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
|
#pragma region TEMP_FPS_Timer
|
||||||
|
|
||||||
|
Uint32 startTime = SDL_GetTicks();
|
||||||
|
Uint16 countedFrames = 0;
|
||||||
|
double deltaTime = 0;
|
||||||
|
const int FPS = 60;
|
||||||
|
const int MS_PER_FRAME = 1000 / FPS;
|
||||||
|
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
|
|
||||||
|
while (!_quit)
|
||||||
|
{
|
||||||
|
startTime = SDL_GetTicks();
|
||||||
|
//TEMP Event Handler:
|
||||||
|
SDL_Event eventHandler;
|
||||||
|
while (SDL_PollEvent(&eventHandler) != 0)
|
||||||
|
{
|
||||||
|
if (eventHandler.type == SDL_QUIT)
|
||||||
|
{
|
||||||
|
_quit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Uint8* keyStates = SDL_GetKeyboardState(NULL);
|
||||||
|
if (keyStates[SDL_SCANCODE_W])
|
||||||
|
{
|
||||||
|
DestR2.y -= (25 * deltaTime);
|
||||||
|
}
|
||||||
|
if (keyStates[SDL_SCANCODE_S])
|
||||||
|
{
|
||||||
|
DestR2.y -= (50 * deltaTime);
|
||||||
|
//std::cout << "S PRESS" << std::endl;
|
||||||
|
}
|
||||||
|
if (keyStates[SDL_SCANCODE_ESCAPE])
|
||||||
|
{
|
||||||
|
_quit = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//FRAME PHYSICS
|
||||||
|
//TICK ALL OBJECTS.
|
||||||
|
mainRenderer->QueueDrawCall(4, DestR2, test1);
|
||||||
|
mainRenderer->QueueDrawCall(1, DestR, background);
|
||||||
|
|
||||||
|
//renderer->Render(sp1);
|
||||||
|
//sp2.Draw(renderer);
|
||||||
|
sp3.Draw(mainRenderer);
|
||||||
|
|
||||||
|
mainRenderer->RenderFrame();
|
||||||
|
Uint32 frameTime = SDL_GetTicks() - startTime;
|
||||||
|
deltaTime = frameTime / 1000.f;
|
||||||
|
//Frames totales / segundos totales.
|
||||||
|
float avgFPS = countedFrames / (SDL_GetTicks() / 1000.f);
|
||||||
|
std::cout << "FPS: " << avgFPS << " / " << "dT: " << deltaTime << std::endl;
|
||||||
|
|
||||||
|
//FPS CAP
|
||||||
|
if (frameTime < MS_PER_FRAME)
|
||||||
|
{
|
||||||
|
SDL_Delay(MS_PER_FRAME - frameTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
++countedFrames;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Clean()
|
||||||
|
{
|
||||||
|
asManager->CleanAssetManager();
|
||||||
|
delete asManager;
|
||||||
|
|
||||||
|
mainRenderer->CleanRenderer();
|
||||||
|
delete mainRenderer;
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
+5
-5
@@ -24,11 +24,11 @@ project (""..GameName.."")
|
|||||||
"UberEngine/src/**.h", "UberEngine/src/**.cpp",
|
"UberEngine/src/**.h", "UberEngine/src/**.cpp",
|
||||||
"%{prj.name}/**.h", "%{prj.name}/**.cpp",
|
"%{prj.name}/**.h", "%{prj.name}/**.cpp",
|
||||||
|
|
||||||
"UberEngine/vendor/FastNoiseLite/**.h",
|
-- "UberEngine/vendor/FastNoiseLite/**.h",
|
||||||
"UberEngine/vendor/glm/**.hpp",
|
-- "UberEngine/vendor/glm/**.hpp",
|
||||||
"UberEngine/vendor/ImGui/**.h", "UberEngine/vendor/ImGui/**.cpp",
|
-- "UberEngine/vendor/ImGui/**.h", "UberEngine/vendor/ImGui/**.cpp",
|
||||||
"UberEngine/vendor/json/**.hpp",
|
-- "UberEngine/vendor/json/**.hpp",
|
||||||
"UberEngine/vendor/spdlog/include/**.h",
|
-- "UberEngine/vendor/spdlog/include/**.h",
|
||||||
}
|
}
|
||||||
|
|
||||||
includedirs
|
includedirs
|
||||||
|
|||||||
@@ -3,37 +3,29 @@
|
|||||||
{
|
{
|
||||||
"name": "bg1",
|
"name": "bg1",
|
||||||
"Path": "resources/Sprites/bc.png",
|
"Path": "resources/Sprites/bc.png",
|
||||||
"Type": 0,
|
"Type": 0
|
||||||
"ID": 0
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "test1",
|
"name": "test1",
|
||||||
"Path": "resources/Sprites/Test1.png",
|
"Path": "resources/Sprites/Test1.png",
|
||||||
"Type": 0,
|
"Type": 0
|
||||||
"ID": 1
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Player",
|
"name": "Player",
|
||||||
"Path": "resources/Sprites/Player.png",
|
"Path": "resources/Sprites/Player.png",
|
||||||
"Type": 0,
|
"Type": 0
|
||||||
"ID": 2
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Button",
|
"name": "Button",
|
||||||
"Path": "resources/Sprites/Button.png",
|
"Path": "resources/Sprites/Button.png",
|
||||||
"Type": 0,
|
"Type": 0
|
||||||
"ID": 3
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Tilemap",
|
"name": "Tilemap",
|
||||||
"Path": "resources/Sprites/Tilemap2.png",
|
"Path": "resources/Sprites/Tilemap2.png",
|
||||||
"Type": 0,
|
"Type": 0
|
||||||
"ID": 5
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"SpriteSheets": [
|
"SpriteSheets": [
|
||||||
{
|
|
||||||
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user