Files
jaildoctors_dilemma/source/room.cpp

168 lines
3.5 KiB
C++

#include "room.h"
#include <iostream>
#include <fstream>
#include <sstream>
// Constructor
Room::Room(std::string _file_path, SDL_Renderer *_renderer, Asset *_asset)
{
texture = new LTexture();
asset = _asset;
renderer = _renderer;
load(_file_path);
loadTextureFromFile(texture, asset->get(tileset), renderer);
}
// Destructor
Room::~Room()
{
texture->unload();
delete texture;
texture = nullptr;
}
// Carga una habitación desde un fichero
bool Room::load(std::string _file_path)
{
// Indicador de éxito en la carga
bool success = true;
std::string filename = _file_path.substr(_file_path.find_last_of("\\/") + 1);
std::string line;
std::ifstream file(_file_path);
// El fichero se puede abrir
if (file.good())
{
// Carga los datos
printf("Reading file %s\n", filename.c_str());
while (std::getline(file, line))
{
int pos = line.find("=");
if (!setVars(line.substr(0, pos), line.substr(pos + 1, line.length())))
{
printf("Warning: file %s, unknown parameter \"%s\"\n", filename.c_str(), line.substr(0, pos).c_str());
success = false;
}
}
// Cierra el fichero
printf("Closing file %s\n", filename.c_str());
file.close();
}
// El fichero no se puede abrir
else
{
printf("Warning: Unable to open %s file\n", filename.c_str());
success = false;
}
return success;
}
// Asigna variables a partir de dos cadenas
bool Room::setVars(std::string _var, std::string _value)
{
// Indicador de éxito en la asignación
bool success = true;
if (_var == "id")
{
id = _value;
}
else if (_var == "name")
{
name = _value;
}
else if (_var == "bg_color")
{
bg_color = _value;
}
else if (_var == "tileset")
{
tileset = _value;
}
else if (_var == "room_up")
{
room_up = _value;
}
else if (_var == "room_down")
{
room_down = _value;
}
else if (_var == "room_left")
{
room_left = _value;
}
else if (_var == "room_right")
{
room_right = _value;
}
else if (_var == "tilemap")
{
std::stringstream ss(_value);
std::string tmp;
while (getline(ss, tmp, ','))
{
// printf("text - %s\n",tmp.c_str());
tilemap.push_back(std::stoi(tmp));
// printf("int - %i\n",std::stoi(tmp));
}
}
else
{
success = false;
}
return success;
}
// Devuelve el nombre de la habitación
std::string Room::getName()
{
return name;
}
// Devuelve el color de la habitación
color_t Room::getBGColor()
{
color_t color = {0x00, 0x00, 0x00};
if (bg_color == "white")
{
color = {0xFF, 0xFF, 0xFF};
}
else if (bg_color == "red")
{
color = {0xFF, 0x00, 0x00};
}
else if (bg_color == "green")
{
color = {0x00, 0xFF, 0x00};
}
else if (bg_color == "blue")
{
color = {0x00, 0x00, 0xFF};
}
else if (bg_color == "yellow")
{
color = {0xFF, 0xFF, 0x00};
}
else if (bg_color == "cyan")
{
color = {0x00, 0xFF, 0xFF};
}
else if (bg_color == "purple")
{
color = {0xFF, 0x00, 0xFF};
}
return color;
}
// Dibuja la habitación en pantalla
void Room::draw()
{
int x = 0;
int y = 0;
SDL_Rect clip = {0, 0, 8, 8};
texture->render(renderer,x, y, &clip);
}