231 lines
5.8 KiB
C++
231 lines
5.8 KiB
C++
#include "demo.h"
|
|
#include <SDL2/SDL_events.h> // for SDL_PollEvent, SDL_Event
|
|
#include <SDL2/SDL_rect.h> // for SDL_Rect
|
|
#include <SDL2/SDL_timer.h> // for SDL_GetTicks
|
|
#include <iostream> // for basic_ostream, operator<<, cout, endl
|
|
#include "asset.h" // for Asset
|
|
#include "debug.h" // for Debug
|
|
#include "defines.h" // for BLOCK, PLAY_AREA_WIDTH, GAMECANVAS_CENT...
|
|
#include "global_events.h" // for check
|
|
#include "global_inputs.h" // for check
|
|
#include "input.h" // for Input
|
|
#include "item_tracker.h" // for ItemTracker
|
|
#include "options.h" // for Options, options, OptionsVideo, Section...
|
|
#include "resource.h" // for Resource
|
|
#include "room.h" // for Room
|
|
#include "screen.h" // for Screen
|
|
#include "text.h" // for Text, TEXT_CENTER, TEXT_COLOR
|
|
#include "utils.h" // for Color, stringToColor, colorAreEqual
|
|
|
|
// Constructor
|
|
Demo::Demo()
|
|
: screen(Screen::get()),
|
|
renderer(Screen::get()->getRenderer()),
|
|
resource(Resource::get()),
|
|
asset(Asset::get()),
|
|
input(Input::get()),
|
|
debug(Debug::get())
|
|
{
|
|
// Inicia algunas variables
|
|
board.iniClock = SDL_GetTicks();
|
|
rooms.push_back("04.room");
|
|
rooms.push_back("54.room");
|
|
rooms.push_back("20.room");
|
|
rooms.push_back("09.room");
|
|
rooms.push_back("05.room");
|
|
rooms.push_back("11.room");
|
|
rooms.push_back("31.room");
|
|
rooms.push_back("44.room");
|
|
|
|
roomIndex = 0;
|
|
currentRoom = rooms[roomIndex];
|
|
|
|
// Crea los objetos
|
|
itemTracker = std::make_shared<ItemTracker>();
|
|
scoreboard = std::make_shared<Scoreboard>(&board);
|
|
room = std::make_shared<Room>(resource->getRoom(currentRoom), itemTracker, &board.items, false);
|
|
text = resource->getText("smb2");
|
|
|
|
// Inicializa el resto de variables
|
|
counter = 0;
|
|
roomTime = 400;
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
board.lives = 9;
|
|
board.items = 0;
|
|
board.rooms = 1;
|
|
board.jail_is_open = false;
|
|
board.music = true;
|
|
setScoreBoardColor();
|
|
|
|
options.section.section = Section::DEMO;
|
|
options.section.subsection = Subsection::NONE;
|
|
}
|
|
|
|
// Comprueba los eventos de la cola
|
|
void Demo::checkEvents()
|
|
{
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
globalEvents::check(event);
|
|
}
|
|
}
|
|
|
|
// Comprueba las entradas
|
|
void Demo::checkInput()
|
|
{
|
|
globalInputs::check();
|
|
}
|
|
|
|
// Bucle para el juego
|
|
void Demo::run()
|
|
{
|
|
while (options.section.section == Section::DEMO)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
}
|
|
|
|
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
|
void Demo::update()
|
|
{
|
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
|
if (SDL_GetTicks() - ticks > ticksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks = SDL_GetTicks();
|
|
|
|
// Comprueba las entradas
|
|
checkInput();
|
|
|
|
// Actualiza los objetos
|
|
room->update();
|
|
scoreboard->update();
|
|
screen->updateFX();
|
|
checkRoomChange();
|
|
|
|
screen->update();
|
|
}
|
|
}
|
|
|
|
// Pinta los objetos en pantalla
|
|
void Demo::render()
|
|
{
|
|
// Prepara para dibujar el frame
|
|
screen->start();
|
|
|
|
// Dibuja los elementos del juego en orden
|
|
room->renderMap();
|
|
room->renderEnemies();
|
|
room->renderItems();
|
|
renderRoomName();
|
|
scoreboard->render();
|
|
screen->renderFX();
|
|
|
|
// Actualiza la pantalla
|
|
screen->render();
|
|
}
|
|
|
|
// Escribe el nombre de la pantalla
|
|
void Demo::renderRoomName()
|
|
{
|
|
// Texto en el centro de la pantalla
|
|
SDL_Rect rect = {0, 16 * BLOCK, PLAY_AREA_WIDTH, BLOCK * 2};
|
|
Color color = stringToColor(options.video.palette, "white");
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
|
|
text->writeDX(TEXT_CENTER | TEXT_COLOR, GAMECANVAS_CENTER_X, 16 * 8 + 4, room->getName(), 1, room->getBGColor());
|
|
}
|
|
|
|
// Recarga todas las texturas
|
|
void Demo::reLoadTextures()
|
|
{
|
|
if (options.console)
|
|
{
|
|
std::cout << "** RELOAD REQUESTED" << std::endl;
|
|
}
|
|
room->reLoadTexture();
|
|
scoreboard->reLoadTexture();
|
|
text->reLoadTexture();
|
|
}
|
|
|
|
// Cambia la paleta
|
|
void Demo::switchPalette()
|
|
{
|
|
// Modifica la variable
|
|
if (options.video.palette == Palette::ZXSPECTRUM)
|
|
{
|
|
options.video.palette = Palette::ZXARNE;
|
|
}
|
|
else
|
|
{
|
|
options.video.palette = Palette::ZXSPECTRUM;
|
|
}
|
|
|
|
room->reLoadPalette();
|
|
scoreboard->reLoadPalette();
|
|
|
|
// Pone el color del marcador en función del color del borde de la habitación
|
|
setScoreBoardColor();
|
|
}
|
|
|
|
// Cambia de habitación
|
|
bool Demo::changeRoom(std::string file)
|
|
{
|
|
// En las habitaciones los limites tienen la cadena del fichero o un 0 en caso de no limitar con nada
|
|
if (file != "0")
|
|
{
|
|
// Verifica que exista el fichero que se va a cargar
|
|
if (asset->get(file) != "")
|
|
{
|
|
// Crea un objeto habitación a partir del fichero
|
|
room = std::make_shared<Room>(resource->getRoom(file), itemTracker, &board.items, false);
|
|
|
|
// Pone el color del marcador en función del color del borde de la habitación
|
|
setScoreBoardColor();
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
// Comprueba si se ha de cambiar de habitación
|
|
void Demo::checkRoomChange()
|
|
{
|
|
counter++;
|
|
if (counter == roomTime)
|
|
{
|
|
counter = 0;
|
|
roomIndex++;
|
|
if (roomIndex == (int)rooms.size())
|
|
{
|
|
options.section.section = Section::LOGO;
|
|
options.section.subsection = Subsection::LOGO_TO_TITLE;
|
|
}
|
|
else
|
|
{
|
|
changeRoom(rooms[roomIndex]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Pone el color del marcador en función del color del borde de la habitación
|
|
void Demo::setScoreBoardColor()
|
|
{
|
|
// Obtiene el color del borde
|
|
const Color color = room->getBorderColor();
|
|
|
|
// Si el color es negro lo cambia a blanco
|
|
const Color black_color = stringToColor(options.video.palette, "black");
|
|
board.color = colorAreEqual(color, black_color) ? stringToColor(options.video.palette, "white") : color;
|
|
|
|
// Si el color es negro brillante lo cambia a blanco
|
|
const Color bright_blac_color = stringToColor(options.video.palette, "bright_black");
|
|
board.color = colorAreEqual(color, bright_blac_color) ? stringToColor(options.video.palette, "white") : color;
|
|
} |