Añadidas las librerias asset y screen
This commit is contained in:
118
source/asset.cpp
Normal file
118
source/asset.cpp
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
#include "asset.h"
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Asset::Asset(std::string path)
|
||||||
|
{
|
||||||
|
mExecutablePath = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Asset::~Asset()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Añade un elemento a la lista
|
||||||
|
void Asset::add(std::string file, enum assetType type, bool required)
|
||||||
|
{
|
||||||
|
item_t temp;
|
||||||
|
temp.file = mExecutablePath + "/.." + file;
|
||||||
|
temp.type = type;
|
||||||
|
temp.required = required;
|
||||||
|
mFileList.push_back(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el fichero de un elemento de la lista a partir de una cadena
|
||||||
|
std::string Asset::get(std::string text)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < mFileList.size(); i++)
|
||||||
|
if (mFileList[i].file.find(text) != std::string::npos)
|
||||||
|
return mFileList[i].file;
|
||||||
|
|
||||||
|
printf("Warning: file %s not found\n", text.c_str());
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba que existen todos los elementos
|
||||||
|
bool Asset::check()
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// Comprueba la lista de ficheros clasificandolos por tipo
|
||||||
|
for (int type = 0; type < maxAssetType; type++)
|
||||||
|
{
|
||||||
|
printf("\n>> %s FILES\n", getTypeName(type).c_str());
|
||||||
|
|
||||||
|
for (int i = 0; i < mFileList.size(); i++)
|
||||||
|
if ((mFileList[i].required) && (mFileList[i].type == type))
|
||||||
|
success &= checkFile(mFileList[i].file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resultado
|
||||||
|
if (success)
|
||||||
|
printf("\n** All files OK.\n\n");
|
||||||
|
else
|
||||||
|
printf("\n** A file is missing. Exiting.\n\n");
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comprueba que existe un fichero
|
||||||
|
bool Asset::checkFile(std::string path)
|
||||||
|
{
|
||||||
|
bool success = true;
|
||||||
|
|
||||||
|
// Comprueba si existe el fichero
|
||||||
|
const std::string filename = path.substr(path.find_last_of("\\/") + 1);
|
||||||
|
SDL_RWops *file = SDL_RWFromFile(path.c_str(), "r+b");
|
||||||
|
|
||||||
|
if (file != NULL)
|
||||||
|
{
|
||||||
|
printf("Checking file %-20s [OK]\n", filename.c_str());
|
||||||
|
SDL_RWclose(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Devuelve el nombre del tipo de recurso
|
||||||
|
std::string Asset::getTypeName(int type)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case bitmap:
|
||||||
|
return "BITMAP";
|
||||||
|
break;
|
||||||
|
case music:
|
||||||
|
return "MUSIC";
|
||||||
|
break;
|
||||||
|
case sound:
|
||||||
|
return "SOUND";
|
||||||
|
break;
|
||||||
|
case font:
|
||||||
|
return "FONT";
|
||||||
|
break;
|
||||||
|
case lang:
|
||||||
|
return "LANG";
|
||||||
|
break;
|
||||||
|
case data:
|
||||||
|
return "DATA";
|
||||||
|
break;
|
||||||
|
case room:
|
||||||
|
return "ROOM";
|
||||||
|
break;
|
||||||
|
case enemy:
|
||||||
|
return "ENEMY";
|
||||||
|
break;
|
||||||
|
case item:
|
||||||
|
return "ITEM";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return "ERROR";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
61
source/asset.h
Normal file
61
source/asset.h
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "ifdefs.h"
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#ifndef ASSET_H
|
||||||
|
#define ASSET_H
|
||||||
|
|
||||||
|
enum assetType
|
||||||
|
{
|
||||||
|
bitmap,
|
||||||
|
music,
|
||||||
|
sound,
|
||||||
|
font,
|
||||||
|
lang,
|
||||||
|
data,
|
||||||
|
room,
|
||||||
|
enemy,
|
||||||
|
item,
|
||||||
|
maxAssetType
|
||||||
|
};
|
||||||
|
|
||||||
|
// Clase Asset
|
||||||
|
class Asset
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
// Estructura para definir un item
|
||||||
|
struct item_t
|
||||||
|
{
|
||||||
|
std::string file; // Ruta del fichero desde la raiz del directorio
|
||||||
|
enum assetType type; // Indica el tipo de recurso
|
||||||
|
bool required; // Indica si es un fichero que debe de existir
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<item_t> mFileList;
|
||||||
|
std::string mExecutablePath;
|
||||||
|
|
||||||
|
// Comprueba que existe un fichero
|
||||||
|
bool checkFile(std::string path);
|
||||||
|
|
||||||
|
// Devuelve el nombre del tipo de recurso
|
||||||
|
std::string getTypeName(int type);
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Asset(std::string path);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Asset();
|
||||||
|
|
||||||
|
// Añade un elemento a la lista
|
||||||
|
void add(std::string file, enum assetType type, bool required = true);
|
||||||
|
|
||||||
|
// Devuelve un elemento de la lista a partir de una cadena
|
||||||
|
std::string get(std::string text);
|
||||||
|
|
||||||
|
// Comprueba que existen todos los elementos
|
||||||
|
bool check();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
63
source/screen.cpp
Normal file
63
source/screen.cpp
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include "screen.h"
|
||||||
|
#include "const.h"
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||||
|
{
|
||||||
|
// Inicializa variables
|
||||||
|
mWindow = window;
|
||||||
|
mRenderer = renderer;
|
||||||
|
|
||||||
|
mScreenWidth = SCREEN_WIDTH;
|
||||||
|
mScreenHeight = SCREEN_HEIGHT;
|
||||||
|
mGameCanvasWidth = GAMECANVAS_WIDTH;
|
||||||
|
mGameCanvasHeight = GAMECANVAS_HEIGHT;
|
||||||
|
mGameCanvasPosX = (SCREEN_WIDTH - GAMECANVAS_WIDTH) / 2;
|
||||||
|
mGameCanvasPosY = (SCREEN_HEIGHT - GAMECANVAS_HEIGHT) / 2;
|
||||||
|
|
||||||
|
mBorderColor = {0x27, 0x27, 0x36};
|
||||||
|
|
||||||
|
// Crea la textura donde se dibujan los graficos del juego
|
||||||
|
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||||
|
if (mGameCanvas == NULL)
|
||||||
|
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
Screen::~Screen()
|
||||||
|
{
|
||||||
|
mRenderer = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limpia la pantalla
|
||||||
|
void Screen::clean(color_t color)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
|
||||||
|
SDL_RenderClear(mRenderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepara para empezar a dibujar en la textura de juego
|
||||||
|
void Screen::start()
|
||||||
|
{
|
||||||
|
SDL_SetRenderTarget(mRenderer, mGameCanvas);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vuelca el contenido del renderizador en pantalla
|
||||||
|
void Screen::blit()
|
||||||
|
{
|
||||||
|
// Vuelve a dejar el renderizador en modo normal
|
||||||
|
SDL_SetRenderTarget(mRenderer, NULL);
|
||||||
|
|
||||||
|
// Borra el contenido previo
|
||||||
|
SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF);
|
||||||
|
SDL_RenderClear(mRenderer);
|
||||||
|
|
||||||
|
// Rectangulo de destino donde se dibujarà la textura con el juego
|
||||||
|
SDL_Rect dest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth, mGameCanvasHeight};
|
||||||
|
|
||||||
|
// Copia la textura de juego en el renderizador en la posición adecuada
|
||||||
|
SDL_RenderCopy(mRenderer, mGameCanvas, NULL, &dest);
|
||||||
|
|
||||||
|
// Muestra por pantalla el renderizador
|
||||||
|
SDL_RenderPresent(mRenderer);
|
||||||
|
}
|
||||||
42
source/screen.h
Normal file
42
source/screen.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "ifdefs.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#ifndef SCREEN_H
|
||||||
|
#define SCREEN_H
|
||||||
|
|
||||||
|
// Clase Screen
|
||||||
|
class Screen
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
SDL_Window *mWindow; // Ventana de la aplicación
|
||||||
|
SDL_Renderer *mRenderer; // El renderizador de la ventana
|
||||||
|
SDL_Texture *mGameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
|
||||||
|
|
||||||
|
int mScreenWidth; // Ancho de la pantalla
|
||||||
|
int mScreenHeight; // Alto de la pantalla
|
||||||
|
int mGameCanvasWidth; // Ancho de la textura donde se dibuja el juego
|
||||||
|
int mGameCanvasHeight; // Alto de la textura donde se dibuja el juego
|
||||||
|
int mGameCanvasPosX; // Posicion en el eje X donde se dibujará la textura del juego dentro de la pantalla
|
||||||
|
int mGameCanvasPosY; // Posicion en el eje Y donde se dibujará la textura del juego dentro de la pantalla
|
||||||
|
|
||||||
|
color_t mBorderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Screen(SDL_Window *windows, SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
// Destructor
|
||||||
|
~Screen();
|
||||||
|
|
||||||
|
// Limpia la pantalla
|
||||||
|
void clean(color_t color);
|
||||||
|
|
||||||
|
// Prepara para empezar a dibujar en la textura de juego
|
||||||
|
void start();
|
||||||
|
|
||||||
|
// Vuelca el contenido del renderizador en pantalla
|
||||||
|
void blit();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user