Files
thepool/source/main.cpp

191 lines
5.8 KiB
C++

#include "jgame.h"
#include "jdraw.h"
#include "jfile.h"
#include "jinput.h"
#include "editor.h"
#include "console.h"
#include "string.h"
#include "config.h"
#include <SDL2/SDL.h>
#include "actor.h"
#include "room.h"
#include "m_game.h"
#include "m_menu.h"
#include "m_logo.h"
#include "m_ingame.h"
#include "m_gameover.h"
#include "m_catslife.h"
#include "m_menu_tecles.h"
#include "m_menu_audio.h"
#include "m_editor_map.h"
#include "m_editor_templates.h"
#define M_LOGO 0
#define M_MENU 1
#define M_GAME 2
#define M_INGAME 3
#define M_GAMEOVER 4
#define M_CATSLIFE 5
#define M_MENU_TECLES 6
#define M_MENU_AUDIO 7
#define M_EDITOR_MAP 8
#define M_EDITOR_TEMPLATES 9
int current_module = M_LOGO;
int zoom = 3;
bool fullscreen = false;
char tmp[100];
void loadConfig()
{
file::setConfigFolder("thepool");
if (strcmp(file::getConfigValue("music").c_str(), "no")==0) config::setMusic(false);
const char *so = file::getConfigValue("sound").c_str();
if (strcmp(so, "basic")==0)
config::setSound(SOUND_BASIC);
else if (strcmp(so, "none")==0)
config::setSound(SOUND_NONE);
std::string txt_zoom = file::getConfigValue("zoom");
if (txt_zoom!="") zoom = SDL_atoi(txt_zoom.c_str());
std::string txt_fullscreen = file::getConfigValue("fullscreen");
if (txt_fullscreen=="yes") fullscreen = true;
static const char* nomtecles[6] = {"keyup", "keydown", "keyleft", "keyright", "keyjump", "keypick"};
for (int i=0; i<6; ++i)
{
const int value = SDL_atoi( file::getConfigValue(nomtecles[i]).c_str() );
if (value != 0) config::defineKey(i, value);
}
}
void game::init()
{
if (game::getParams(1) && strcmp(game::getParams(1), "editor")==0) editor::setDevMode();
if (editor::isDevMode())
draw::init("The Pool", 520, 240, zoom);
else {
loadConfig();
draw::init("The Pool", 320, 240, zoom, fullscreen);
console::init();
}
draw::loadPalette("test.gif");
if (editor::isDevMode()) {
current_module = M_GAME;
modules::game::init();
} else
modules::logo::init();
}
bool game::loop()
{
if (input::keyPressed(SDL_SCANCODE_F1)) {
draw::decZoom();
zoom = draw::getZoom();
file::setConfigValue("zoom", SDL_itoa(zoom, tmp, 10));
}
if (input::keyPressed(SDL_SCANCODE_F2)) {
draw::incZoom();
zoom = draw::getZoom();
file::setConfigValue("zoom", SDL_itoa(zoom, tmp, 10));
}
if (input::keyPressed(SDL_SCANCODE_F3)) {
draw::toggleFullscreen();
fullscreen = draw::getFullscreen();
file::setConfigValue("fullscreen", fullscreen?"yes":"no");
}
if (input::keyPressed(SDL_SCANCODE_F12)) {
draw::reloadAll();
}
int option;
switch(current_module)
{
case M_LOGO:
if (!modules::logo::loop()) { modules::menu::init(); current_module = M_MENU; }
break;
case M_MENU:
option = modules::menu::loop();
if (option != OPTION_NONE) {
if (option == OPTION_EIXIR) return false;
if (option == OPTION_JUGAR) { modules::game::init(); current_module = M_GAME; }
if (option == OPTION_TECLES) { modules::menu_tecles::init(); current_module = M_MENU_TECLES; }
if (option == OPTION_AUDIO) { modules::menu_audio::init(); current_module = M_MENU_AUDIO; }
}
break;
case M_GAME:
option = modules::game::loop();
if (option!=GAME_NONE) {
if (option==GAME_MENU) {
if (editor::isDevMode()) {
return false;
} else {
modules::ingame::init(); current_module = M_INGAME;
}
} else if (option==GAME_DEAD) {
if (actor::stats::catsLife()) {
modules::catslife::init(); current_module = M_CATSLIFE;
} else {
modules::gameover::init(); current_module = M_GAMEOVER;
}
} else if (option==GAME_EDITOR_MAP) {
modules::editor_map::init(); current_module = M_EDITOR_MAP;
} else if (option==GAME_EDITOR_TEMPLATES) {
modules::editor_templates::init(); current_module = M_EDITOR_TEMPLATES;
}
}
break;
case M_EDITOR_MAP:
if (!modules::editor_map::loop()) {
current_module = M_GAME;
}
break;
case M_EDITOR_TEMPLATES:
if (!modules::editor_templates::loop()) {
current_module = M_GAME;
}
break;
case M_INGAME:
option = modules::ingame::loop();
if (option != INGAME_NONE) {
if (option == INGAME_EIXIR) { modules::menu::init(); current_module = M_MENU; }
if (option == INGAME_CONTINUAR) { current_module = M_GAME; }
}
break;
case M_GAMEOVER:
if (!modules::gameover::loop()) {
modules::menu::init(); current_module = M_MENU;
}
break;
case M_CATSLIFE:
if (!modules::catslife::loop()) {
actor::hero::init(false);
actor::hero::setLives(1);
room::reload();
current_module = M_GAME;
}
break;
case M_MENU_TECLES:
if (modules::menu_tecles::loop() == MENU_TECLES_TORNAR) {
modules::menu::init(); current_module = M_MENU;
}
break;
case M_MENU_AUDIO:
if (modules::menu_audio::loop() == MENU_AUDIO_TORNAR) {
modules::menu::init(); current_module = M_MENU;
}
break;
};
return true;
}