commit de acabar la jornada laboral

This commit is contained in:
2024-10-17 13:57:41 +02:00
parent db884cb422
commit 59de566c5b
24 changed files with 78 additions and 274 deletions

View File

@@ -16,9 +16,7 @@ std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, s
auto frame_height = 0;
auto max_tiles = 0;
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
std::ifstream file(file_path);
std::string line;
@@ -26,9 +24,7 @@ std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, s
if (file.good())
{
// Procesa el fichero linea a linea
#ifdef VERBOSE
std::cout << "Animation loaded: " << file_name << std::endl;
#endif
while (std::getline(file, line))
{
// Si la linea contiene el texto [animation] se realiza el proceso de carga de una animación
@@ -86,9 +82,7 @@ std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, s
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
}
} while (line != "[/animation]");
@@ -123,9 +117,7 @@ std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, s
else
{
#ifdef VERBOSE
std::cout << "Warning: file " << file_name.c_str() << "\n, unknown parameter \"" << line.substr(0, pos).c_str() << "\"" << std::endl;
#endif
}
// Normaliza valores
@@ -150,9 +142,7 @@ std::vector<Animation> loadAnimationFromFile(std::shared_ptr<Texture> texture, s
// El fichero no se puede abrir
else
{
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
#endif
}
return animations;
@@ -204,9 +194,7 @@ int AnimatedSprite::getIndex(const std::string &name)
return index;
}
}
#ifdef VERBOSE
std::cout << "** Warning: could not find \"" << name.c_str() << "\" animation" << std::endl;
#endif
return -1;
}
@@ -407,9 +395,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
else
{
#ifdef VERBOSE
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
#endif
success = false;
}
}
@@ -445,9 +431,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
else
{
#ifdef VERBOSE
std::cout << "Warning: unknown parameter " << line.substr(0, pos).c_str() << std::endl;
#endif
success = false;
}
@@ -471,7 +455,7 @@ bool AnimatedSprite::loadFromVector(std::vector<std::string> *source)
}
// Pone un valor por defecto
setPos((SDL_Rect){0, 0, frame_width, frame_height});
setPosition((SDL_Rect){0, 0, frame_width, frame_height});
return success;
}

View File

@@ -59,9 +59,7 @@ std::string Asset::get(const std::string &text) const
}
}
#ifdef VERBOSE
std::cout << "Warning: file " << text.c_str() << " not found" << std::endl;
#endif
return "";
}
@@ -70,12 +68,10 @@ bool Asset::check() const
{
bool success = true;
#ifdef VERBOSE
std::cout << "\n** Checking files" << std::endl;
std::cout << "Executable path is: " << executable_path_ << std::endl;
std::cout << "Sample filepath: " << file_list_.back().file << std::endl;
#endif
// Comprueba la lista de ficheros clasificandolos por tipo
for (int type = 0; type < static_cast<int>(AssetType::MAX_ASSET_TYPE); ++type)
@@ -94,9 +90,7 @@ bool Asset::check() const
// Si hay ficheros de ese tipo, comprueba si existen
if (any)
{
#ifdef VERBOSE
std::cout << "\n>> " << getTypeName(static_cast<AssetType>(type)).c_str() << " FILES" << std::endl;
#endif
for (const auto &f : file_list_)
{
@@ -108,10 +102,8 @@ bool Asset::check() const
}
}
// Resultado
#ifdef VERBOSE
// Resultado
std::cout << (success ? "\n** All files OK.\n" : "\n** A file is missing. Exiting.\n") << std::endl;
#endif
return success;
}
@@ -130,7 +122,6 @@ bool Asset::checkFile(const std::string &path) const
SDL_RWclose(file);
}
#ifdef VERBOSE
const std::string file_name = path.substr(path.find_last_of("\\/") + 1);
std::cout.setf(std::ios::left, std::ios::adjustfield);
std::cout << "Checking file: ";
@@ -138,7 +129,6 @@ bool Asset::checkFile(const std::string &path) const
std::cout.fill('.');
std::cout << file_name;
std::cout << (success ? " [OK]" : " [ERROR]") << std::endl;
#endif
return success;
}

View File

@@ -81,8 +81,8 @@ Background::Background(SDL_Renderer *renderer)
bottom_clouds_sprite_b_->setVelX(-bottom_clouds_speed);
bottom_clouds_sprite_b_->setSpriteClip(0, 0, bottom_clouds_texture_->getWidth(), bottom_clouds_texture_->getHeight());
buildings_sprite_->setPosY(base_ - buildings_sprite_->getHeight());
grass_sprite_->setPosY(base_ - grass_sprite_->getHeight());
buildings_sprite_->setY(base_ - buildings_sprite_->getHeight());
grass_sprite_->setY(base_ - grass_sprite_->getHeight());
}
// Crea la textura para componer el fondo

View File

@@ -306,7 +306,7 @@ void Balloon::render()
if (kind_ == POWER_BALL && !isBeingCreated())
{
auto sp = std::make_unique<Sprite>(sprite_->getTexture(), sprite_->getPos());
auto sp = std::make_unique<Sprite>(sprite_->getTexture(), sprite_->getPosition());
sp->setSpriteClip(BALLOON_WIDTH_4, 0, BALLOON_WIDTH_4, BALLOON_WIDTH_4);
sp->render();
}

View File

@@ -57,8 +57,8 @@ BulletMoveStatus Bullet::move()
return BulletMoveStatus::OUT;
}
sprite_->setPosX(pos_x_);
sprite_->setPosY(pos_y_);
sprite_->setX(pos_x_);
sprite_->setY(pos_y_);
shiftColliders();
return BulletMoveStatus::OK;

View File

@@ -55,9 +55,11 @@ Director::Director(int argc, const char *argv[])
section::name = section::Name::LOGO;
#endif
#ifndef VERBOSE
// Deshabilita todos los std::cout
//std::ostream null_stream(nullptr);
//std::streambuf *orig_buf = std::cout.rdbuf(null_stream.rdbuf());
std::ostream null_stream(nullptr);
std::streambuf *orig_buf = std::cout.rdbuf(null_stream.rdbuf());
#endif
// Comprueba los parametros del programa
checkProgramArguments(argc, argv);
@@ -252,9 +254,7 @@ bool Director::initSDL()
// Inicializa SDL
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
#ifdef VERBOSE
std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl;
#endif
success = false;
}
else
@@ -262,7 +262,6 @@ bool Director::initSDL()
// Inicia el generador de numeros aleatorios
std::srand(static_cast<unsigned int>(SDL_GetTicks()));
#ifdef VERBOSE
// Muestra información de la pantalla
/*std::cout << "\nDisplay modes list:" << std::endl;
for (int i = 0; i < SDL_GetNumDisplayModes(0); ++i)
@@ -276,30 +275,23 @@ bool Director::initSDL()
SDL_GetCurrentDisplayMode(0, &DM);
std::cout << "\nCurrent display mode: " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl;
std::cout << "Window resolution : " + std::to_string(param.game.width) + "x" + std::to_string(param.game.height) + " x" + std::to_string(options.video.window.size) << std::endl;
#endif
// Establece el filtro de la textura
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(static_cast<int>(options.video.filter)).c_str()))
{
#ifdef VERBOSE
std::cout << "Warning: texture filtering not enabled!\n";
#endif
}
#ifndef NO_SHADERS
if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"))
{
#ifdef VERBOSE
std::cout << "Warning: opengl not enabled!\n";
#endif // VERBOSE
}
#endif // NO_SHADERS
// Crea la ventana
window_ = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options.video.window.size, param.game.height * options.video.window.size, SDL_WINDOW_HIDDEN);
if (!window_)
{
#ifdef VERBOSE
std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
#endif
success = false;
}
else
@@ -318,9 +310,7 @@ bool Director::initSDL()
if (!renderer_)
{
#ifdef VERBOSE
std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl;
#endif
success = false;
}
else
@@ -338,9 +328,7 @@ bool Director::initSDL()
}
}
#ifdef VERBOSE
std::cout << std::endl;
#endif
return success;
}

View File

@@ -300,9 +300,7 @@ void Game::init(int player_id)
// Carga los recursos necesarios para la sección 'Game'
void Game::loadMedia()
{
#ifdef VERBOSE
std::cout << "\n** LOADING RESOURCES FOR GAME SECTION" << std::endl;
#endif
// Limpia
{
@@ -492,10 +490,8 @@ void Game::loadMedia()
coffee_machine_sound_ = JA_LoadSound(asset_->get("title.wav").c_str());
}
#ifdef VERBOSE
std::cout << "** RESOURCES FOR GAME SECTION LOADED\n"
<< std::endl;
#endif
}
// Libera los recursos previamente cargados
@@ -537,15 +533,11 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
{
// Indicador de éxito en la carga
auto success = true;
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
auto file = SDL_RWFromFile(file_path.c_str(), "r+b");
if (!file)
{ // El fichero no existe
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << file_name.c_str() << " file" << std::endl;
#endif
// Creamos el fichero para escritura
file = SDL_RWFromFile(file_path.c_str(), "w+b");
@@ -553,9 +545,7 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
// Si ha creado el fichero
if (file)
{
#ifdef VERBOSE
std::cout << "New file (" << file_name.c_str() << ") created!" << std::endl;
#endif
// Inicializas los datos y los guarda en el fichero
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
@@ -576,9 +566,7 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
}
else
{ // Si no puede crear el fichero
#ifdef VERBOSE
std::cout << "Error: Unable to create file " << file_name.c_str() << std::endl;
#endif
success = false;
}
}
@@ -586,9 +574,7 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
else
{
// Mensaje de proceder a la carga de los datos
#ifdef VERBOSE
std::cout << "Reading file: " << file_name.c_str() << std::endl;
#endif
// Lee todos los datos del fichero y los deja en el destino
for (int i = 0; i < TOTAL_DEMO_DATA; ++i)
@@ -610,9 +596,7 @@ bool Game::loadDemoFile(const std::string &file_path, DemoKeys (*data_file)[TOTA
bool Game::saveDemoFile(const std::string &file_path)
{
auto success = true;
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif // VERBOSE
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
if (file)
@@ -623,18 +607,14 @@ bool Game::saveDemoFile(const std::string &file_path)
SDL_RWwrite(file, &demo.dataFile[0][i], sizeof(DemoKeys), 1);
}
#ifdef VERBOSE
std::cout << "Writing file " << file_name.c_str() << std::endl;
#endif // VERBOSE
// Cierra el fichero
SDL_RWclose(file);
}
else
{
#ifdef VERBOSE
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
#endif // VERBOSE
}
return success;
@@ -1420,7 +1400,7 @@ void Game::createItemScoreSprite(int x, int y, std::shared_ptr<Texture> texture)
// Inicializa
smart_sprites_.back()->setPos({0, 0, texture->getWidth(), texture->getHeight()});
smart_sprites_.back()->setSpriteClip(smart_sprites_.back()->getPos());
smart_sprites_.back()->setSpriteClip(smart_sprites_.back()->getPosition());
smart_sprites_.back()->setPosX(x);
smart_sprites_.back()->setPosY(y);
smart_sprites_.back()->setDestX(x);
@@ -2439,9 +2419,7 @@ void Game::loadAnimations(std::string filePath, std::vector<std::string> *buffer
if (file)
{
#ifdef VERBOSE
std::cout << "Animation loaded: " << filePath.substr(filePath.find_last_of("\\/") + 1).c_str() << std::endl;
#endif
while (std::getline(file, line))
{
buffer->push_back(line);

View File

@@ -396,11 +396,9 @@ bool Input::discoverGameControllers()
if (SDL_GameControllerAddMappingsFromFile(game_controller_db_path_.c_str()) < 0)
{
#ifdef VERBOSE
{
std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
}
#endif
}
num_joysticks_ = SDL_NumJoysticks();
@@ -418,12 +416,10 @@ bool Input::discoverGameControllers()
}
}
#ifdef VERBOSE
{
std::cout << "\nChecking for game controllers...\n";
std::cout << num_joysticks_ << " joysticks found, " << num_gamepads_ << " are gamepads\n";
}
#endif
if (num_gamepads_ > 0)
{
@@ -437,20 +433,16 @@ bool Input::discoverGameControllers()
{
connected_controllers_.push_back(pad);
const std::string name = SDL_GameControllerNameForIndex(i);
#ifdef VERBOSE
{
std::cout << name << std::endl;
}
#endif
controller_names_.push_back(name);
}
else
{
#ifdef VERBOSE
{
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
#endif
}
}

View File

@@ -99,7 +99,7 @@ void Instructions::iniSprites()
for (int i = 0; i < (int)item_textures_.size(); ++i)
{
auto sprite = std::make_unique<Sprite>(item_textures_[i], 0, 0, param.game.item_size, param.game.item_size);
sprite->setPos((SDL_Point){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)});
sprite->setPosition((SDL_Point){sprite_pos_.x, sprite_pos_.y + ((param.game.item_size + item_space_) * i)});
sprites_.push_back(std::move(sprite));
}
}

View File

@@ -31,7 +31,7 @@ Logo::Logo()
ticks_ = 0;
dest_.x = param.game.game_area.center_x - jail_texture_->getWidth() / 2;
dest_.y = param.game.game_area.center_y - jail_texture_->getHeight() / 2;
since_sprite_->setPosY(dest_.y + jail_texture_->getHeight() + 5);
since_sprite_->setY(dest_.y + jail_texture_->getHeight() + 5);
since_sprite_->setSpriteClip(0, 0, since_texture_->getWidth(), since_texture_->getHeight());
since_texture_->setColor(0x00, 0x00, 0x00); // Esto en linux no hace nada ??
@@ -41,8 +41,8 @@ Logo::Logo()
auto temp = std::make_unique<Sprite>(jail_texture_, 0, i, jail_texture_->getWidth(), 1);
temp->setSpriteClip(0, i, jail_texture_->getWidth(), 1);
const int posX = (i % 2 == 0) ? param.game.width + (i * 3) : -jail_texture_->getWidth() - (i * 3);
temp->setPosX(posX);
temp->setPosY(dest_.y + i);
temp->setX(posX);
temp->setY(dest_.y + i);
jail_sprite_.push_back(std::move(temp));
}
@@ -115,22 +115,22 @@ void Logo::updateJAILGAMES()
{
for (int i = 0; i < (int)jail_sprite_.size(); ++i)
{
if (jail_sprite_[i]->getPosX() != dest_.x)
if (jail_sprite_[i]->getX() != dest_.x)
{
if (i % 2 == 0)
{
jail_sprite_[i]->incPosX(-SPEED);
if (jail_sprite_[i]->getPosX() < dest_.x)
jail_sprite_[i]->incX(-SPEED);
if (jail_sprite_[i]->getX() < dest_.x)
{
jail_sprite_[i]->setPosX(dest_.x);
jail_sprite_[i]->setX(dest_.x);
}
}
else
{
jail_sprite_[i]->incPosX(SPEED);
if (jail_sprite_[i]->getPosX() > dest_.x)
jail_sprite_[i]->incX(SPEED);
if (jail_sprite_[i]->getX() > dest_.x)
{
jail_sprite_[i]->setPosX(dest_.x);
jail_sprite_[i]->setX(dest_.x);
}
}
}

View File

@@ -66,10 +66,8 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
if (file)
{
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
std::cout << "Reading file: " << file_name.c_str() << std::endl;
#endif
for (int i = 0; i < (int)table_->size(); ++i)
{
int nameSize = 0;
@@ -115,9 +113,7 @@ bool ManageHiScoreTable::loadFromFile(const std::string &file_path)
// Guarda la tabla en un fichero
bool ManageHiScoreTable::saveToFile(const std::string &file_path)
{
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
auto success = true;
auto file = SDL_RWFromFile(file_path.c_str(), "w+b");
@@ -132,17 +128,13 @@ bool ManageHiScoreTable::saveToFile(const std::string &file_path)
SDL_RWwrite(file, table_->at(i).name.c_str(), nameSize, 1);
}
#ifdef VERBOSE
std::cout << "Writing file: " << file_name.c_str() << std::endl;
#endif
// Cierra el fichero
SDL_RWclose(file);
}
else
{
#ifdef VERBOSE
std::cout << "Error: Unable to save " << file_name.c_str() << " file! " << SDL_GetError() << std::endl;
#endif
}
return success;
}

View File

@@ -4,6 +4,8 @@
// Constructor
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos, Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip)
: Sprite(texture, pos),
x_(pos.x),
y_(pos.y),
rotate_(rotate),
zoom_w_(zoom_w),
zoom_h_(zoom_h),
@@ -11,6 +13,8 @@ MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos, Rotat
MovingSprite::MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos)
: Sprite(texture, pos),
x_(pos.x),
y_(pos.y),
rotate_({false, 0, 0, 0.0f, 0.0f, nullptr}),
zoom_w_(1.0f),
zoom_h_(1.0f),
@@ -59,6 +63,8 @@ void MovingSprite::move()
vx_ += ax_;
vy_ += ay_;
syncPos();
}
// Actualiza las variables internas del objeto
@@ -243,8 +249,7 @@ void MovingSprite::setPos(float x, float y)
x_ = x;
y_ = y;
pos_.x = (int)x;
pos_.y = (int)y;
syncPos();
}
// Establece el valor de la variable
@@ -284,3 +289,10 @@ void MovingSprite::setAccelY(float value)
{
ay_ = value;
}
// Sincroniza la posición
void MovingSprite::syncPos()
{
pos_.x = (int)x_;
pos_.y = (int)y_;
}

View File

@@ -45,6 +45,9 @@ protected:
// Rota el sprite
void rotate();
// Sincroniza la posición
void syncPos();
public:
// Constructor
MovingSprite(std::shared_ptr<Texture> texture, SDL_Rect pos, MovingSprite::Rotate rotate, float zoom_w, float zoom_h, SDL_RendererFlip flip);

View File

@@ -145,7 +145,7 @@ void Notifier::update()
}
}
notifications_[i].sprite->setPos(notifications_[i].rect);
notifications_[i].sprite->setPosition(notifications_[i].rect);
}
clearFinishedNotifications();
@@ -278,7 +278,7 @@ void Notifier::showText(std::string text1, std::string text2, int icon, std::str
if (has_icons_ && icon >= 0 && num_texts == 2)
{
auto sp = std::make_unique<Sprite>(icon_texture_, (SDL_Rect){0, 0, icon_size, icon_size});
sp->setPos({padding_in_h, padding_in_v, icon_size, icon_size});
sp->setPosition({padding_in_h, padding_in_v, icon_size, icon_size});
sp->setSpriteClip({icon_size * (icon % 10), icon_size * (icon / 10), icon_size, icon_size});
sp->render();
}

View File

@@ -186,7 +186,7 @@ auto OnScreenHelp::getLargestStringSize() -> int const
void OnScreenHelp::renderButton(Sprite *sprite, Text *text, const SDL_Rect &buttonClip, const SDL_Rect &buttonPos, int textId)
{
sprite->setSpriteClip(buttonClip);
sprite->setPos(buttonPos);
sprite->setPosition(buttonPos);
sprite->render();
text->write(buttonPos.x + DESP.x, buttonPos.y + DESP.y, lang::getText(textId));
}

View File

@@ -86,18 +86,14 @@ bool loadOptionsFile(std::string file_path)
bool success = true;
// Variables para manejar el fichero
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
std::ifstream file(file_path);
// Si el fichero se puede abrir
if (file.good())
{
// Procesa el fichero linea a linea
#ifdef VERBOSE
std::cout << "Reading file: " << file_name << std::endl;
#endif
std::string line;
while (std::getline(file, line))
{
@@ -109,10 +105,8 @@ bool loadOptionsFile(std::string file_path)
// Procesa las dos subcadenas
if (!setOptions(line.substr(0, pos), line.substr(pos + 1, line.length())))
{
#ifdef VERBOSE
std::cout << "Warning: file " << file_name << std::endl;
std::cout << "Unknown parameter " << line.substr(0, pos).c_str() << std::endl;
#endif
success = false;
}
}
@@ -152,22 +146,16 @@ bool loadOptionsFile(std::string file_path)
// Guarda el fichero de configuración
bool saveOptionsFile(std::string file_path)
{
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
#endif
std::ofstream file(file_path);
if (!file.good())
{
#ifdef VERBOSE
std::cout << file_name << " can't be opened" << std::endl;
#endif
return false;
}
#ifdef VERBOSE
std::cout << "Writing file: " << file_name << std::endl;
#endif
// Opciones de video
const auto value_video_mode_winow = std::to_string(static_cast<int>(ScreenVideoMode::WINDOW));

View File

@@ -65,109 +65,6 @@ void initParam()
param.notification.color.b = 48;
}
// Establece valores para los parametros a partir de un fichero de texto
/*void loadParamsFromFile(std::string file_path)
{
// Pone valores por defecto a las variables
initParam();
// Variables para manejar el fichero
std::ifstream file(file_path);
std::string line;
std::string param1;
std::string param2;
// Si el fichero se puede abrir
if (file.good())
{
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
std::cout << "Reading file: " << file_name << std::endl;
#endif
// Procesa cada linea del fichero
while (std::getline(file, line))
{
// Reinicia variables
param1.clear();
param2.clear();
// Elimina los comentarios al final de una linea
{
// line = line.substr(0, line.find("#"));
auto pos = line.find("#");
if (pos != std::string::npos)
{
line.resize(pos);
}
}
// Ignora los espacios en blanco
int pos = 0;
while (pos < (int)line.size() && (int)line[pos] <= 32)
{
pos++;
}
// Si no ha llegado al final de la linea, es que hay algo escrito (no es una linea vacía)
if (pos < (int)line.size())
{
// Elimina el espacio en blanco
line = line.substr(pos, std::string::npos);
// Se queda con todo lo que no sean espacios en blanco
pos = 1;
while (pos <= (int)line.size() && (int)line[pos] > 32)
{
pos++;
}
// Si llega al final de la linea, falta la segunda palabra
if (pos < (int)line.size())
{
// Se queda con la primera palabra
param1 = line.substr(0, pos);
// Y recorta la linea
line = line.substr(pos, std::string::npos);
// Ignora los espacios en blanco
pos = 0;
while (pos <= (int)line.size() && (int)line[pos] <= 32)
{
pos++;
}
// Si llega al final de la linea, falta la segunda palabra
if (pos < (int)line.size())
{
// Elimina el espacio en blanco
line = line.substr(pos, std::string::npos);
// Ignora los espacios en blanco
pos = 1;
while (pos <= (int)line.size() && (int)line[pos] > 32)
{
pos++;
}
// Se queda con la segunda palabra
param2 = line.substr(0, pos);
}
}
}
setParams(param1, param2);
}
// Cierra el fichero
file.close();
}
#ifdef VERBOSE
else
std::cout << "Failed to load file: " << file_path << std::endl;
#endif
precalculateZones();
}*/
void loadParamsFromFile(const std::string &file_path)
{
// Inicializa los parámetros con valores por defecto
@@ -177,16 +74,12 @@ void loadParamsFromFile(const std::string &file_path)
std::ifstream file(file_path);
if (!file.is_open())
{
#ifdef VERBOSE
std::cerr << "Error: No se pudo abrir el archivo " << file_path << std::endl;
#endif
throw std::runtime_error("No se pudo abrir el archivo: " + file_path);
}
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1);
std::cout << "Reading file: " << file_name << std::endl;
#endif
std::string line, param1, param2;
while (std::getline(file, line))

View File

@@ -66,7 +66,7 @@ Scoreboard::Scoreboard(SDL_Renderer *renderer)
// Recalcula las anclas de los elementos
recalculateAnchors();
power_meter_sprite_->setPos({slot4_2_.x - 20, slot4_2_.y, 40, 7});
power_meter_sprite_->setPosition({slot4_2_.x - 20, slot4_2_.y, 40, 7});
// Crea la textura de fondo
background_ = nullptr;
@@ -418,8 +418,8 @@ void Scoreboard::recalculateAnchors()
// Recoloca los sprites
if (power_meter_sprite_)
{
power_meter_sprite_->setPosX(slot4_2_.x - 20);
power_meter_sprite_->setPosY(slot4_2_.y);
power_meter_sprite_->setX(slot4_2_.x - 20);
power_meter_sprite_->setY(slot4_2_.y);
}
}

View File

@@ -23,13 +23,13 @@ void Sprite::render()
}
// Obten el valor de la variable
int Sprite::getPosX() const
int Sprite::getX() const
{
return pos_.x;
}
// Obten el valor de la variable
int Sprite::getPosY() const
int Sprite::getY() const
{
return pos_.y;
}
@@ -47,33 +47,33 @@ int Sprite::getHeight() const
}
// Establece la posición del objeto
void Sprite::setPos(int x, int y)
void Sprite::setPosition(int x, int y)
{
pos_.x = x;
pos_.y = y;
}
// Establece la posición del objeto
void Sprite::setPos(SDL_Point p)
void Sprite::setPosition(SDL_Point p)
{
pos_.x = p.x;
pos_.y = p.y;
}
// Establece la posición del objeto
void Sprite::setPos(SDL_Rect r)
void Sprite::setPosition(SDL_Rect r)
{
pos_ = r;
}
// Establece el valor de la variable
void Sprite::setPosX(int x)
void Sprite::setX(int x)
{
pos_.x = x;
}
// Establece el valor de la variable
void Sprite::setPosY(int y)
void Sprite::setY(int y)
{
pos_.y = y;
}
@@ -121,19 +121,19 @@ void Sprite::setTexture(std::shared_ptr<Texture> texture)
}
// Devuelve el rectangulo donde está el sprite
SDL_Rect Sprite::getPos() const
SDL_Rect Sprite::getPosition() const
{
return pos_;
}
// Incrementa el valor de la variable
void Sprite::incPosX(int value)
void Sprite::incX(int value)
{
pos_.x += value;
}
// Incrementa el valor de la variable
void Sprite::incPosY(int value)
void Sprite::incY(int value)
{
pos_.y += value;
}

View File

@@ -26,28 +26,28 @@ public:
virtual void render();
// Obten el valor de la variable
int getPosX() const;
int getPosY() const;
int getX() const;
int getY() const;
int getWidth() const;
int getHeight() const;
// Devuelve el rectangulo donde está el sprite
SDL_Rect getPos() const;
SDL_Rect getPosition() const;
// Establece el valor de la variable
void setPosX(int x);
void setPosY(int y);
void setX(int x);
void setY(int y);
void setWidth(int w);
void setHeight(int h);
// Establece la posición del objeto
void setPos(int x, int y);
void setPos(SDL_Point p);
void setPos(SDL_Rect r);
void setPosition(int x, int y);
void setPosition(SDL_Point p);
void setPosition(SDL_Rect r);
// Incrementa el valor de la variable
void incPosX(int value);
void incPosY(int value);
void incX(int value);
void incY(int value);
// Obten el valor de la variable
SDL_Rect getSpriteClip() const;

View File

@@ -1,10 +1,10 @@
#include "text.h"
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <fstream> // for basic_ostream, basic_ifstream, basic_istream
#include <iostream> // for cout
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
#include "utils.h" // for Color
#include <SDL2/SDL_rect.h> // for SDL_Rect
#include <fstream> // for basic_ostream, basic_ifstream, basic_istream
#include <iostream> // for cout
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
#include "utils.h" // for Color
// Llena una estructuta TextFile desde un fichero
TextFile LoadTextFile(std::string file_path)
@@ -22,9 +22,7 @@ TextFile LoadTextFile(std::string file_path)
}
// Abre el fichero para leer los valores
#ifdef VERBOSE
const std::string file_name = file_path.substr(file_path.find_last_of("\\/") + 1).c_str();
#endif
std::ifstream file(file_path);
if (file.is_open() && file.good())
@@ -56,19 +54,15 @@ TextFile LoadTextFile(std::string file_path)
line_read++;
};
// Cierra el fichero
#ifdef VERBOSE
// Cierra el fichero
std::cout << "Text loaded: " << file_name << std::endl;
#endif
file.close();
}
// El fichero no se puede abrir
else
{
#ifdef VERBOSE
std::cout << "Warning: Unable to open " << file_name << " file" << std::endl;
#endif
}
// Establece las coordenadas para cada caracter ascii de la cadena y su ancho
@@ -158,14 +152,14 @@ void Text::write(int x, int y, const std::string &text, int kerning, int lenght)
lenght = text.length();
}
sprite_->setPosY(y);
sprite_->setY(y);
const auto width = sprite_->getWidth();
const auto height = sprite_->getHeight();
for (int i = 0; i < lenght; ++i)
{
const auto index = static_cast<int>(text[i]);
sprite_->setSpriteClip(offset_[index].x, offset_[index].y, width, height);
sprite_->setPosX(x + shift);
sprite_->setX(x + shift);
sprite_->render();
shift += fixed_width_ ? box_width_ : (offset_[int(text[i])].w + kerning);
}

View File

@@ -61,17 +61,13 @@ bool Texture::loadFromFile(const std::string &path)
unsigned char *data = stbi_load(path.c_str(), &width, &height, &orig_format, req_format);
if (!data)
{
#ifdef VERBOSE
std::cout << "Loading image failed: " << stbi_failure_reason() << std::endl;
#endif
exit(1);
}
else
{
#ifdef VERBOSE
const std::string file_name = path.substr(path.find_last_of("\\/") + 1);
std::cout << "Image loaded: " << file_name << std::endl;
#endif
}
int depth, pitch;
@@ -99,9 +95,7 @@ bool Texture::loadFromFile(const std::string &path)
auto loadedSurface = SDL_CreateRGBSurfaceWithFormatFrom(static_cast<void *>(data), width, height, depth, pitch, pixel_format);
if (loadedSurface == nullptr)
{
#ifdef VERBOSE
std::cout << "Unable to load image " << path << std::endl;
#endif
}
else
{
@@ -109,9 +103,7 @@ bool Texture::loadFromFile(const std::string &path)
newTexture = SDL_CreateTextureFromSurface(renderer_, loadedSurface);
if (newTexture == nullptr)
{
#ifdef VERBOSE
std::cout << "Unable to create texture from " << path << "! SDL Error: " << SDL_GetError() << std::endl;
#endif
}
else
{
@@ -137,9 +129,7 @@ bool Texture::createBlank(int width, int height, SDL_PixelFormatEnum format, SDL
texture_ = SDL_CreateTexture(renderer_, format, access, width, height);
if (!texture_)
{
#ifdef VERBOSE
std::cout << "Unable to create blank texture! SDL Error: " << SDL_GetError() << std::endl;
#endif
}
else
{

View File

@@ -74,8 +74,8 @@ void Tiledbg::fillTexture()
{
for (int j = 0; j < j_max; ++j)
{
tile->setPosX(i * tile_width_);
tile->setPosY(j * tile_height_);
tile->setX(i * tile_width_);
tile->setY(j * tile_height_);
tile->render();
}
}

View File

@@ -162,7 +162,7 @@ void Title::render()
// Mini logo
const int pos1 = (param.game.height / 5 * 4) + BLOCK;
const int pos2 = pos1 + mini_logo_sprite_->getHeight() + 3;
mini_logo_sprite_->setPosY(pos1);
mini_logo_sprite_->setY(pos1);
mini_logo_sprite_->render();
// Texto con el copyright