2012-06-22 14:31:55 +00:00
|
|
|
// * This file is part of the COLOBOT source code
|
|
|
|
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
|
|
|
|
// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
|
|
|
|
// *
|
|
|
|
// * This program is free software: you can redistribute it and/or modify
|
|
|
|
// * it under the terms of the GNU General Public License as published by
|
|
|
|
// * the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// * (at your option) any later version.
|
|
|
|
// *
|
|
|
|
// * This program is distributed in the hope that it will be useful,
|
|
|
|
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// * GNU General Public License for more details.
|
|
|
|
// *
|
|
|
|
// * You should have received a copy of the GNU General Public License
|
|
|
|
// * along with this program. If not, see http://www.gnu.org/licenses/.
|
|
|
|
|
|
|
|
// app.cpp
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
#include "app/system.h"
|
2012-07-04 17:56:22 +00:00
|
|
|
#include "common/logger.h"
|
2012-06-25 17:59:17 +00:00
|
|
|
#include "common/iman.h"
|
2012-07-22 20:05:12 +00:00
|
|
|
#include "common/image.h"
|
2012-06-30 23:37:30 +00:00
|
|
|
#include "graphics/opengl/gldevice.h"
|
2012-09-15 20:19:32 +00:00
|
|
|
#include "object/robotmain.h"
|
2012-06-22 14:31:55 +00:00
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
|
|
|
|
#include <SDL/SDL.h>
|
|
|
|
#include <SDL/SDL_image.h>
|
|
|
|
|
2012-09-13 21:28:06 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libintl.h>
|
2012-06-30 08:16:52 +00:00
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
template<> CApplication* CSingleton<CApplication>::mInstance = nullptr;
|
2012-07-22 20:05:12 +00:00
|
|
|
|
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
//! Interval of timer called to update joystick state
|
|
|
|
const int JOYSTICK_TIMER_INTERVAL = 1000/30;
|
|
|
|
|
|
|
|
//! Function called by the timer
|
|
|
|
Uint32 JoystickTimerCallback(Uint32 interval, void *);
|
|
|
|
|
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
/**
|
2012-06-26 20:50:55 +00:00
|
|
|
* \struct ApplicationPrivate
|
|
|
|
* \brief Private data of CApplication class
|
2012-06-25 17:59:17 +00:00
|
|
|
*
|
|
|
|
* Contains SDL-specific variables that should not be visible outside application module.
|
|
|
|
*/
|
|
|
|
struct ApplicationPrivate
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
//! Display surface
|
|
|
|
SDL_Surface *surface;
|
|
|
|
//! Currently handled event
|
|
|
|
SDL_Event currentEvent;
|
|
|
|
//! Joystick
|
|
|
|
SDL_Joystick *joystick;
|
2012-06-30 10:26:40 +00:00
|
|
|
//! Id of joystick timer
|
|
|
|
SDL_TimerID joystickTimer;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
ApplicationPrivate()
|
|
|
|
{
|
|
|
|
memset(¤tEvent, 0, sizeof(SDL_Event));
|
2012-09-09 15:51:10 +00:00
|
|
|
surface = nullptr;
|
|
|
|
joystick = nullptr;
|
2012-06-30 10:26:40 +00:00
|
|
|
joystickTimer = 0;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
CApplication::CApplication()
|
|
|
|
{
|
2012-07-29 13:09:53 +00:00
|
|
|
m_private = new ApplicationPrivate();
|
|
|
|
m_iMan = new CInstanceManager();
|
2012-06-29 22:12:04 +00:00
|
|
|
m_eventQueue = new CEventQueue(m_iMan);
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
m_engine = nullptr;
|
|
|
|
m_device = nullptr;
|
|
|
|
m_robotMain = nullptr;
|
|
|
|
m_sound = nullptr;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
m_exitCode = 0;
|
|
|
|
m_active = false;
|
|
|
|
m_debugMode = false;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
m_windowTitle = "COLOBOT";
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
m_simulationSuspended = false;
|
|
|
|
|
|
|
|
m_simulationSpeed = 1.0f;
|
|
|
|
|
|
|
|
m_realAbsTimeBase = 0LL;
|
|
|
|
m_realAbsTime = 0LL;
|
|
|
|
m_realRelTime = 0LL;
|
|
|
|
|
|
|
|
m_absTimeBase = 0LL;
|
|
|
|
m_exactAbsTime = 0LL;
|
|
|
|
m_exactRelTime = 0LL;
|
|
|
|
|
|
|
|
m_absTime = 0.0f;
|
|
|
|
m_relTime = 0.0f;
|
|
|
|
|
|
|
|
m_baseTimeStamp = CreateTimeStamp();
|
|
|
|
m_curTimeStamp = CreateTimeStamp();
|
|
|
|
m_lastTimeStamp = CreateTimeStamp();
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
m_joystickEnabled = false;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
m_kmodState = 0;
|
|
|
|
m_mouseButtonsState = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < TRKEY_MAX; ++i)
|
|
|
|
m_trackedKeysState[i] = false;
|
|
|
|
|
|
|
|
m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
|
|
|
|
m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
m_dataPath = "./data";
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
m_language = LANG_ENGLISH;
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
SetDefaultInputBindings();
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CApplication::~CApplication()
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
delete m_private;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_private = nullptr;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
delete m_eventQueue;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_eventQueue = nullptr;
|
2012-06-29 22:12:04 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
delete m_iMan;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_iMan = nullptr;
|
2012-09-12 21:43:04 +00:00
|
|
|
|
|
|
|
DestroyTimeStamp(m_baseTimeStamp);
|
|
|
|
DestroyTimeStamp(m_curTimeStamp);
|
|
|
|
DestroyTimeStamp(m_lastTimeStamp);
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 21:41:53 +00:00
|
|
|
ParseArgsStatus CApplication::ParseArguments(int argc, char *argv[])
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
bool waitDataDir = false;
|
2012-08-31 18:55:16 +00:00
|
|
|
bool waitLogLevel = false;
|
2012-09-13 21:28:06 +00:00
|
|
|
bool waitLanguage = false;
|
2012-07-22 20:05:12 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
{
|
|
|
|
std::string arg = argv[i];
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
if (waitDataDir)
|
|
|
|
{
|
|
|
|
waitDataDir = false;
|
|
|
|
m_dataPath = arg;
|
2012-09-13 21:28:06 +00:00
|
|
|
GetLogger()->Info("Using custom data dir: '%s'\n", m_dataPath.c_str());
|
2012-08-03 21:23:13 +00:00
|
|
|
continue;
|
2012-07-22 20:05:12 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 18:55:16 +00:00
|
|
|
if (waitLogLevel)
|
|
|
|
{
|
|
|
|
waitLogLevel = false;
|
|
|
|
if (arg == "trace")
|
|
|
|
GetLogger()->SetLogLevel(LOG_TRACE);
|
|
|
|
else if (arg == "debug")
|
|
|
|
GetLogger()->SetLogLevel(LOG_DEBUG);
|
|
|
|
else if (arg == "info")
|
|
|
|
GetLogger()->SetLogLevel(LOG_INFO);
|
|
|
|
else if (arg == "warn")
|
|
|
|
GetLogger()->SetLogLevel(LOG_WARN);
|
|
|
|
else if (arg == "error")
|
|
|
|
GetLogger()->SetLogLevel(LOG_ERROR);
|
|
|
|
else if (arg == "none")
|
|
|
|
GetLogger()->SetLogLevel(LOG_NONE);
|
|
|
|
else
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_FAIL;
|
2012-08-31 18:55:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-09-13 21:28:06 +00:00
|
|
|
if (waitLanguage)
|
|
|
|
{
|
|
|
|
waitLanguage = false;
|
|
|
|
if (arg == "en")
|
|
|
|
m_language = LANG_ENGLISH;
|
|
|
|
else if (arg == "de")
|
|
|
|
m_language = LANG_GERMAN;
|
|
|
|
else if (arg == "fr")
|
|
|
|
m_language = LANG_FRENCH;
|
|
|
|
else if (arg == "pl")
|
|
|
|
m_language = LANG_POLISH;
|
|
|
|
else
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_FAIL;
|
2012-09-13 21:28:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
if (arg == "-debug")
|
|
|
|
{
|
|
|
|
SetDebugMode(true);
|
|
|
|
}
|
2012-08-31 18:55:16 +00:00
|
|
|
else if (arg == "-loglevel")
|
|
|
|
{
|
|
|
|
waitLogLevel = true;
|
|
|
|
}
|
2012-07-22 20:05:12 +00:00
|
|
|
else if (arg == "-datadir")
|
|
|
|
{
|
|
|
|
waitDataDir = true;
|
|
|
|
}
|
2012-09-13 21:28:06 +00:00
|
|
|
else if (arg == "-language")
|
|
|
|
{
|
|
|
|
waitLanguage = true;
|
|
|
|
}
|
|
|
|
else if (arg == "-help")
|
|
|
|
{
|
2012-09-17 21:41:53 +00:00
|
|
|
GetLogger()->Message("\n");
|
|
|
|
GetLogger()->Message("COLOBOT GOLD pre-alpha\n");
|
2012-09-13 21:28:06 +00:00
|
|
|
GetLogger()->Message("\n");
|
|
|
|
GetLogger()->Message("List of available options:\n");
|
|
|
|
GetLogger()->Message(" -help this help\n");
|
|
|
|
GetLogger()->Message(" -datadir path set custom data directory path\n");
|
|
|
|
GetLogger()->Message(" -debug enable debug mode (more info printed in logs)\n");
|
|
|
|
GetLogger()->Message(" -loglevel level set log level to level (one of: trace, debug, info, warn, error, none)\n");
|
|
|
|
GetLogger()->Message(" -language lang set language (one of: en, de, fr, pl)\n");
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_HELP;
|
2012-09-13 21:28:06 +00:00
|
|
|
}
|
2012-07-22 20:05:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
m_exitCode = 1;
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_FAIL;
|
2012-07-22 20:05:12 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-31 18:55:16 +00:00
|
|
|
// Args not given?
|
2012-09-13 21:28:06 +00:00
|
|
|
if (waitDataDir || waitLogLevel || waitLanguage)
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_FAIL;
|
2012-07-22 20:05:12 +00:00
|
|
|
|
2012-09-17 21:41:53 +00:00
|
|
|
return PARSE_ARGS_OK;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::Create()
|
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
GetLogger()->Info("Creating CApplication\n");
|
|
|
|
|
2012-09-13 21:28:06 +00:00
|
|
|
// I know, a primitive way to check for dir, but works
|
|
|
|
std::string readmePath = m_dataPath + "/README.txt";
|
|
|
|
std::ifstream testReadme;
|
|
|
|
testReadme.open(readmePath.c_str(), std::ios_base::in);
|
|
|
|
if (!testReadme.good())
|
|
|
|
{
|
|
|
|
GetLogger()->Error("Could not open test file in data dir: '%s'\n", readmePath.c_str());
|
|
|
|
m_errorMessage = std::string("Could not read from data directory:\n") +
|
|
|
|
std::string("'") + m_dataPath + std::string("'\n") +
|
|
|
|
std::string("Please check your installation, or supply a valid data directory by -datadir option.");
|
|
|
|
m_exitCode = 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gettext initialization */
|
|
|
|
|
2012-09-18 22:04:21 +00:00
|
|
|
m_locale = "LANGUAGE=";
|
2012-09-13 21:28:06 +00:00
|
|
|
std::string locale = "C";
|
|
|
|
switch (m_language)
|
|
|
|
{
|
|
|
|
case LANG_ENGLISH:
|
|
|
|
locale = "en_US.utf8";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LANG_GERMAN:
|
|
|
|
locale = "de_DE.utf8";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LANG_FRENCH:
|
|
|
|
locale = "fr_FR.utf8";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LANG_POLISH:
|
|
|
|
locale = "pl_PL.utf8";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-18 22:04:21 +00:00
|
|
|
m_locale += locale;
|
|
|
|
putenv(m_locale.c_str());
|
2012-09-13 21:28:06 +00:00
|
|
|
setlocale(LC_ALL, locale.c_str());
|
|
|
|
|
|
|
|
std::string trPath = m_dataPath + std::string("/i18n");
|
|
|
|
bindtextdomain("colobot", trPath.c_str());
|
|
|
|
bind_textdomain_codeset("colobot", "UTF-8");
|
|
|
|
textdomain("colobot");
|
|
|
|
|
|
|
|
GetLogger()->Debug("Testing gettext translation: '%s'\n", gettext("Colobot rules!"));
|
2012-07-22 20:05:12 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
// Temporarily -- only in windowed mode
|
2012-07-29 13:09:53 +00:00
|
|
|
m_deviceConfig.fullScreen = false;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-15 20:19:32 +00:00
|
|
|
// Create the sound instance.
|
|
|
|
m_sound = new CSoundInterface();
|
2012-06-25 17:59:17 +00:00
|
|
|
|
|
|
|
|
2012-08-13 21:09:30 +00:00
|
|
|
std::string standardInfoMessage =
|
|
|
|
"\nPlease see the console output or log file\n"
|
|
|
|
"to get more information on the source of error";
|
2012-06-29 22:12:04 +00:00
|
|
|
|
|
|
|
/* SDL initialization sequence */
|
|
|
|
|
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
Uint32 initFlags = SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_TIMER;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
if (SDL_Init(initFlags) < 0)
|
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("SDL initialization error:\n") +
|
|
|
|
std::string(SDL_GetError());
|
|
|
|
GetLogger()->Error(m_errorMessage.c_str());
|
2012-07-04 17:56:22 +00:00
|
|
|
m_exitCode = 2;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0)
|
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("SDL_Image initialization error:\n") +
|
|
|
|
std::string(IMG_GetError());
|
|
|
|
GetLogger()->Error(m_errorMessage.c_str());
|
2012-07-04 17:56:22 +00:00
|
|
|
m_exitCode = 3;
|
2012-06-26 20:23:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
if (! CreateVideoSurface())
|
|
|
|
return false; // dialog is in function
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->surface == nullptr)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("SDL error while setting video mode:\n") +
|
|
|
|
std::string(SDL_GetError());
|
|
|
|
GetLogger()->Error(m_errorMessage.c_str());
|
|
|
|
m_exitCode = 4;
|
2012-06-26 20:23:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
SDL_WM_SetCaption(m_windowTitle.c_str(), m_windowTitle.c_str());
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
// Enable translating key codes of key press events to unicode chars
|
2012-06-26 20:23:05 +00:00
|
|
|
SDL_EnableUNICODE(1);
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
// Don't generate joystick events
|
|
|
|
SDL_JoystickEventState(SDL_IGNORE);
|
2012-06-25 17:59:17 +00:00
|
|
|
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
// For now, enable joystick for testing
|
|
|
|
SetJoystickEnabled(true);
|
2012-06-25 17:59:17 +00:00
|
|
|
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
// The video is ready, we can create and initalize the graphics device
|
2012-07-29 13:09:53 +00:00
|
|
|
m_device = new Gfx::CGLDevice(m_deviceConfig);
|
2012-07-01 20:59:22 +00:00
|
|
|
if (! m_device->Create() )
|
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("Error in CDevice::Create()\n") + standardInfoMessage;
|
|
|
|
m_exitCode = 5;
|
2012-07-01 20:59:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
// Create the 3D engine
|
|
|
|
m_engine = new Gfx::CEngine(m_iMan, this);
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
m_engine->SetDevice(m_device);
|
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
if (! m_engine->Create() )
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("Error in CEngine::Init()\n") + standardInfoMessage;
|
|
|
|
m_exitCode = 6;
|
2012-07-01 20:59:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-15 20:19:32 +00:00
|
|
|
// Create the robot application.
|
|
|
|
m_robotMain = new CRobotMain(m_iMan, this);
|
|
|
|
|
2012-09-16 08:38:08 +00:00
|
|
|
m_robotMain->ChangePhase(PHASE_WELCOME1);
|
|
|
|
|
2012-08-13 21:09:30 +00:00
|
|
|
GetLogger()->Info("CApplication created successfully\n");
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::CreateVideoSurface()
|
|
|
|
{
|
|
|
|
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
|
2012-09-09 15:51:10 +00:00
|
|
|
if (videoInfo == nullptr)
|
2012-07-29 13:09:53 +00:00
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
m_errorMessage = std::string("SDL error while getting video info:\n ") +
|
|
|
|
std::string(SDL_GetError());
|
|
|
|
GetLogger()->Error(m_errorMessage.c_str());
|
|
|
|
m_exitCode = 7;
|
2012-07-29 13:09:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;
|
|
|
|
|
|
|
|
// Use hardware surface if available
|
|
|
|
if (videoInfo->hw_available)
|
|
|
|
videoFlags |= SDL_HWSURFACE;
|
|
|
|
else
|
|
|
|
videoFlags |= SDL_SWSURFACE;
|
|
|
|
|
|
|
|
// Enable hardware blit if available
|
|
|
|
if (videoInfo->blit_hw)
|
|
|
|
videoFlags |= SDL_HWACCEL;
|
|
|
|
|
|
|
|
if (m_deviceConfig.fullScreen)
|
|
|
|
videoFlags |= SDL_FULLSCREEN;
|
|
|
|
|
|
|
|
if (m_deviceConfig.resizeable)
|
|
|
|
videoFlags |= SDL_RESIZABLE;
|
|
|
|
|
|
|
|
// Set OpenGL attributes
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, m_deviceConfig.redSize);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, m_deviceConfig.greenSize);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, m_deviceConfig.blueSize);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, m_deviceConfig.alphaSize);
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, m_deviceConfig.depthSize);
|
|
|
|
|
|
|
|
if (m_deviceConfig.doubleBuf)
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
|
|
|
|
/* If hardware acceleration specifically requested, this will force the hw accel
|
|
|
|
and fail with error if not available */
|
|
|
|
if (m_deviceConfig.hardwareAccel)
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-08-11 15:17:04 +00:00
|
|
|
m_private->surface = SDL_SetVideoMode(m_deviceConfig.size.x, m_deviceConfig.size.y,
|
2012-07-29 13:09:53 +00:00
|
|
|
m_deviceConfig.bpp, videoFlags);
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
return true;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::Destroy()
|
|
|
|
{
|
2012-09-15 20:19:32 +00:00
|
|
|
if (m_robotMain != nullptr)
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
|
|
|
delete m_robotMain;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_robotMain = nullptr;
|
2012-07-01 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_sound != nullptr)
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
|
|
|
delete m_sound;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_sound = nullptr;
|
2012-09-15 20:19:32 +00:00
|
|
|
}
|
2012-07-01 20:59:22 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_engine != nullptr)
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
2012-08-03 21:23:13 +00:00
|
|
|
m_engine->Destroy();
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
|
|
delete m_engine;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_engine = nullptr;
|
2012-07-01 20:59:22 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_device != nullptr)
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
2012-08-03 21:23:13 +00:00
|
|
|
m_device->Destroy();
|
2012-07-01 20:59:22 +00:00
|
|
|
|
|
|
|
delete m_device;
|
2012-09-09 15:51:10 +00:00
|
|
|
m_device = nullptr;
|
2012-07-01 20:59:22 +00:00
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->joystick != nullptr)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
SDL_JoystickClose(m_private->joystick);
|
2012-09-09 15:51:10 +00:00
|
|
|
m_private->joystick = nullptr;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->surface != nullptr)
|
2012-07-01 20:59:22 +00:00
|
|
|
{
|
|
|
|
SDL_FreeSurface(m_private->surface);
|
2012-09-09 15:51:10 +00:00
|
|
|
m_private->surface = nullptr;
|
2012-07-01 20:59:22 +00:00
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
IMG_Quit();
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
SDL_Quit();
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
bool CApplication::ChangeVideoConfig(const Gfx::GLDeviceConfig &newConfig)
|
|
|
|
{
|
|
|
|
static bool restore = false;
|
|
|
|
|
|
|
|
m_lastDeviceConfig = m_deviceConfig;
|
|
|
|
m_deviceConfig = newConfig;
|
|
|
|
|
|
|
|
|
|
|
|
SDL_FreeSurface(m_private->surface);
|
|
|
|
|
|
|
|
if (! CreateVideoSurface())
|
|
|
|
{
|
|
|
|
// Fatal error, so post the quit event
|
|
|
|
m_eventQueue->AddEvent(Event(EVENT_QUIT));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->surface == nullptr)
|
2012-07-29 13:09:53 +00:00
|
|
|
{
|
|
|
|
if (! restore)
|
|
|
|
{
|
2012-08-13 21:09:30 +00:00
|
|
|
std::string error = std::string("SDL error while setting video mode:\n") +
|
2012-07-29 13:09:53 +00:00
|
|
|
std::string(SDL_GetError()) + std::string("\n") +
|
2012-08-13 21:09:30 +00:00
|
|
|
std::string("Previous mode will be restored");
|
|
|
|
GetLogger()->Error(error.c_str());
|
|
|
|
SystemDialog( SDT_ERROR, "COLOBT - Error", error);
|
2012-07-29 13:09:53 +00:00
|
|
|
|
|
|
|
restore = true;
|
|
|
|
ChangeVideoConfig(m_lastDeviceConfig);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
restore = false;
|
|
|
|
|
2012-08-13 21:09:30 +00:00
|
|
|
std::string error = std::string("SDL error while restoring previous video mode:\n") +
|
|
|
|
std::string(SDL_GetError());
|
|
|
|
GetLogger()->Error(error.c_str());
|
|
|
|
SystemDialog( SDT_ERROR, "COLOBT - Fatal Error", error);
|
2012-07-29 13:09:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Fatal error, so post the quit event
|
|
|
|
m_eventQueue->AddEvent(Event(EVENT_QUIT));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
( static_cast<Gfx::CGLDevice*>(m_device) )->ConfigChanged(m_deviceConfig);
|
|
|
|
|
|
|
|
m_engine->ResetAfterDeviceChanged();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
bool CApplication::OpenJoystick()
|
|
|
|
{
|
2012-07-29 13:09:53 +00:00
|
|
|
if ( (m_joystick.index < 0) || (m_joystick.index >= SDL_NumJoysticks()) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_private->joystick = SDL_JoystickOpen(m_joystick.index);
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->joystick == nullptr)
|
2012-06-29 22:12:04 +00:00
|
|
|
return false;
|
2012-06-30 08:16:52 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
m_joystick.axisCount = SDL_JoystickNumAxes(m_private->joystick);
|
|
|
|
m_joystick.buttonCount = SDL_JoystickNumButtons(m_private->joystick);
|
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
// Create the vectors with joystick axis & button states to exactly the required size
|
2012-07-29 13:09:53 +00:00
|
|
|
m_joyAxeState = std::vector<int>(m_joystick.axisCount, 0);
|
|
|
|
m_joyButtonState = std::vector<bool>(m_joystick.buttonCount, false);
|
2012-06-30 10:26:40 +00:00
|
|
|
|
|
|
|
// Create a timer for polling joystick state
|
2012-09-09 15:51:10 +00:00
|
|
|
m_private->joystickTimer = SDL_AddTimer(JOYSTICK_TIMER_INTERVAL, JoystickTimerCallback, nullptr);
|
2012-06-30 10:26:40 +00:00
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
return true;
|
2012-06-29 22:12:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::CloseJoystick()
|
|
|
|
{
|
2012-06-30 10:26:40 +00:00
|
|
|
// Timer will remove itself automatically
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
SDL_JoystickClose(m_private->joystick);
|
2012-09-09 15:51:10 +00:00
|
|
|
m_private->joystick = nullptr;
|
2012-06-30 10:26:40 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick)
|
|
|
|
{
|
|
|
|
if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) )
|
|
|
|
return false;
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_private->joystick != nullptr)
|
2012-07-29 13:09:53 +00:00
|
|
|
CloseJoystick();
|
|
|
|
|
|
|
|
return OpenJoystick();
|
|
|
|
}
|
|
|
|
|
2012-06-30 10:26:40 +00:00
|
|
|
Uint32 JoystickTimerCallback(Uint32 interval, void *)
|
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
CApplication *app = CApplication::GetInstancePointer();
|
2012-09-09 15:51:10 +00:00
|
|
|
if ((app == nullptr) || (! app->GetJoystickEnabled()))
|
2012-06-30 10:26:40 +00:00
|
|
|
return 0; // don't run the timer again
|
|
|
|
|
|
|
|
app->UpdateJoystick();
|
|
|
|
|
|
|
|
return interval; // run for the same interval again
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Updates the state info in CApplication and on change, creates SDL events and pushes them to SDL event queue.
|
|
|
|
This way, the events get handled properly in the main event loop and besides, SDL_PushEvent() ensures thread-safety. */
|
|
|
|
void CApplication::UpdateJoystick()
|
|
|
|
{
|
|
|
|
if (! m_joystickEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDL_JoystickUpdate();
|
|
|
|
|
2012-07-30 20:59:18 +00:00
|
|
|
for (int axis = 0; axis < static_cast<int>( m_joyAxeState.size() ); ++axis)
|
2012-06-30 10:26:40 +00:00
|
|
|
{
|
|
|
|
int newValue = SDL_JoystickGetAxis(m_private->joystick, axis);
|
|
|
|
|
|
|
|
if (m_joyAxeState[axis] != newValue)
|
|
|
|
{
|
|
|
|
m_joyAxeState[axis] = newValue;
|
|
|
|
|
|
|
|
SDL_Event joyAxisEvent;
|
|
|
|
|
|
|
|
joyAxisEvent.jaxis.type = SDL_JOYAXISMOTION;
|
|
|
|
joyAxisEvent.jaxis.which = 0;
|
|
|
|
joyAxisEvent.jaxis.axis = axis;
|
|
|
|
joyAxisEvent.jaxis.value = newValue;
|
|
|
|
|
|
|
|
SDL_PushEvent(&joyAxisEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 20:59:18 +00:00
|
|
|
for (int button = 0; button < static_cast<int>( m_joyButtonState.size() ); ++button)
|
2012-06-30 10:26:40 +00:00
|
|
|
{
|
|
|
|
bool newValue = SDL_JoystickGetButton(m_private->joystick, button) == 1;
|
|
|
|
|
|
|
|
if (m_joyButtonState[button] != newValue)
|
|
|
|
{
|
|
|
|
m_joyButtonState[button] = newValue;
|
|
|
|
|
|
|
|
SDL_Event joyButtonEvent;
|
|
|
|
|
|
|
|
if (newValue)
|
|
|
|
{
|
|
|
|
joyButtonEvent.jbutton.type = SDL_JOYBUTTONDOWN;
|
|
|
|
joyButtonEvent.jbutton.state = SDL_PRESSED;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
joyButtonEvent.jbutton.type = SDL_JOYBUTTONUP;
|
|
|
|
joyButtonEvent.jbutton.state = SDL_RELEASED;
|
|
|
|
}
|
|
|
|
joyButtonEvent.jbutton.which = 0;
|
|
|
|
joyButtonEvent.jbutton.button = button;
|
|
|
|
|
|
|
|
SDL_PushEvent(&joyButtonEvent);
|
|
|
|
}
|
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
}
|
|
|
|
|
2012-08-12 08:45:04 +00:00
|
|
|
void CApplication::UpdateMouse()
|
|
|
|
{
|
|
|
|
Math::IntPoint pos;
|
|
|
|
SDL_GetMouseState(&pos.x, &pos.y);
|
|
|
|
m_systemMousePos = m_engine->WindowToInterfaceCoords(pos);
|
|
|
|
m_engine->SetMousePos(m_systemMousePos);
|
|
|
|
}
|
|
|
|
|
2012-06-25 17:59:17 +00:00
|
|
|
int CApplication::Run()
|
|
|
|
{
|
2012-06-26 20:23:05 +00:00
|
|
|
m_active = true;
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
GetCurrentTimeStamp(m_baseTimeStamp);
|
|
|
|
GetCurrentTimeStamp(m_lastTimeStamp);
|
|
|
|
GetCurrentTimeStamp(m_curTimeStamp);
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
while (true)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
// To be sure no old event remains
|
|
|
|
m_private->currentEvent.type = SDL_NOEVENT;
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
// Call SDL_PumpEvents() only once here
|
|
|
|
// (SDL_PeepEvents() doesn't call it)
|
2012-07-23 19:41:27 +00:00
|
|
|
if (m_active)
|
|
|
|
SDL_PumpEvents();
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
bool haveEvent = true;
|
|
|
|
while (haveEvent)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
haveEvent = false;
|
|
|
|
|
2012-07-23 19:41:27 +00:00
|
|
|
int count = 0;
|
|
|
|
// Use SDL_PeepEvents() if the app is active, so we can use idle time to
|
2012-07-29 13:09:53 +00:00
|
|
|
// render the scene. Else, use SDL_WaitEvent() to avoid eating CPU time.
|
2012-07-22 20:05:12 +00:00
|
|
|
if (m_active)
|
|
|
|
count = SDL_PeepEvents(&m_private->currentEvent, 1, SDL_GETEVENT, SDL_ALLEVENTS);
|
|
|
|
else
|
2012-07-29 13:09:53 +00:00
|
|
|
count = SDL_WaitEvent(&m_private->currentEvent);
|
2012-07-22 20:05:12 +00:00
|
|
|
|
|
|
|
// If received an event
|
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
haveEvent = true;
|
|
|
|
|
|
|
|
Event event = ParseEvent();
|
|
|
|
|
|
|
|
if (event.type == EVENT_QUIT)
|
|
|
|
goto end; // exit the loop
|
|
|
|
|
|
|
|
if (event.type != EVENT_NULL)
|
|
|
|
{
|
|
|
|
bool passOn = ProcessEvent(event);
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (m_engine != nullptr && passOn)
|
2012-07-22 20:05:12 +00:00
|
|
|
passOn = m_engine->ProcessEvent(event);
|
|
|
|
|
|
|
|
if (passOn)
|
|
|
|
m_eventQueue->AddEvent(event);
|
|
|
|
}
|
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
// Enter game update & frame rendering only if active
|
2012-07-29 13:09:53 +00:00
|
|
|
if (m_active)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
Event event;
|
2012-06-29 22:12:04 +00:00
|
|
|
while (m_eventQueue->GetEvent(event))
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
if (event.type == EVENT_QUIT)
|
2012-06-26 20:23:05 +00:00
|
|
|
goto end; // exit both loops
|
2012-07-22 20:05:12 +00:00
|
|
|
|
|
|
|
bool passOn = true;
|
|
|
|
|
|
|
|
// Skip system events (they have been processed earlier)
|
|
|
|
if (! event.systemEvent)
|
|
|
|
{
|
|
|
|
passOn = ProcessEvent(event);
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
if (passOn && m_engine != nullptr)
|
2012-07-22 20:05:12 +00:00
|
|
|
passOn = m_engine->ProcessEvent(event);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-15 20:19:32 +00:00
|
|
|
if (passOn && m_robotMain != nullptr)
|
|
|
|
m_robotMain->EventProcess(event);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-12 08:45:04 +00:00
|
|
|
/* Update mouse position explicitly right before rendering
|
|
|
|
* because mouse events are usually way behind */
|
|
|
|
UpdateMouse();
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
// Update game and render a frame during idle time (no messages are waiting)
|
2012-08-10 21:31:42 +00:00
|
|
|
Render();
|
2012-09-12 21:43:04 +00:00
|
|
|
|
|
|
|
// Update simulation state
|
|
|
|
StepSimulation();
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
|
|
|
|
end:
|
2012-06-26 20:23:05 +00:00
|
|
|
Destroy();
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-26 20:23:05 +00:00
|
|
|
return m_exitCode;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-04 17:56:22 +00:00
|
|
|
int CApplication::GetExitCode()
|
|
|
|
{
|
|
|
|
return m_exitCode;
|
|
|
|
}
|
|
|
|
|
2012-08-13 21:09:30 +00:00
|
|
|
const std::string& CApplication::GetErrorMessage()
|
|
|
|
{
|
|
|
|
return m_errorMessage;
|
|
|
|
}
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
//! Translates SDL press state to PressState
|
|
|
|
PressState TranslatePressState(unsigned char state)
|
|
|
|
{
|
|
|
|
if (state == SDL_PRESSED)
|
|
|
|
return STATE_PRESSED;
|
|
|
|
else
|
|
|
|
return STATE_RELEASED;
|
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
/** The SDL event parsed is stored internally.
|
|
|
|
If event is not available or is not understood, returned event is of type EVENT_NULL. */
|
|
|
|
Event CApplication::ParseEvent()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
Event event;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
event.systemEvent = true;
|
|
|
|
|
|
|
|
if (m_private->currentEvent.type == SDL_QUIT)
|
|
|
|
{
|
|
|
|
event.type = EVENT_QUIT;
|
|
|
|
}
|
|
|
|
else if ( (m_private->currentEvent.type == SDL_KEYDOWN) ||
|
2012-06-29 22:12:04 +00:00
|
|
|
(m_private->currentEvent.type == SDL_KEYUP) )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
if (m_private->currentEvent.type == SDL_KEYDOWN)
|
|
|
|
event.type = EVENT_KEY_DOWN;
|
|
|
|
else
|
|
|
|
event.type = EVENT_KEY_UP;
|
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
event.key.key = m_private->currentEvent.key.keysym.sym;
|
|
|
|
event.key.mod = m_private->currentEvent.key.keysym.mod;
|
|
|
|
event.key.state = TranslatePressState(m_private->currentEvent.key.state);
|
|
|
|
event.key.unicode = m_private->currentEvent.key.keysym.unicode;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
else if ( (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN) ||
|
|
|
|
(m_private->currentEvent.type == SDL_MOUSEBUTTONUP) )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
if (m_private->currentEvent.type == SDL_MOUSEBUTTONDOWN)
|
|
|
|
event.type = EVENT_MOUSE_BUTTON_DOWN;
|
|
|
|
else
|
|
|
|
event.type = EVENT_MOUSE_BUTTON_UP;
|
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
event.mouseButton.button = m_private->currentEvent.button.button;
|
|
|
|
event.mouseButton.state = TranslatePressState(m_private->currentEvent.button.state);
|
2012-08-03 21:23:13 +00:00
|
|
|
event.mouseButton.pos = m_engine->WindowToInterfaceCoords(
|
|
|
|
Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y));
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
|
|
|
else if (m_private->currentEvent.type == SDL_MOUSEMOTION)
|
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
event.type = EVENT_MOUSE_MOVE;
|
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
event.mouseMove.state = TranslatePressState(m_private->currentEvent.button.state);
|
2012-08-03 21:23:13 +00:00
|
|
|
event.mouseMove.pos = m_engine->WindowToInterfaceCoords(
|
|
|
|
Math::IntPoint(m_private->currentEvent.button.x, m_private->currentEvent.button.y));
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
else if (m_private->currentEvent.type == SDL_JOYAXISMOTION)
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
event.type = EVENT_JOY_AXIS;
|
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
event.joyAxis.axis = m_private->currentEvent.jaxis.axis;
|
|
|
|
event.joyAxis.value = m_private->currentEvent.jaxis.value;
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
else if ( (m_private->currentEvent.type == SDL_JOYBUTTONDOWN) ||
|
|
|
|
(m_private->currentEvent.type == SDL_JOYBUTTONUP) )
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
if (m_private->currentEvent.type == SDL_JOYBUTTONDOWN)
|
|
|
|
event.type = EVENT_JOY_BUTTON_DOWN;
|
|
|
|
else
|
|
|
|
event.type = EVENT_JOY_BUTTON_UP;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-06-30 08:16:52 +00:00
|
|
|
event.joyButton.button = m_private->currentEvent.jbutton.button;
|
|
|
|
event.joyButton.state = TranslatePressState(m_private->currentEvent.jbutton.state);
|
2012-06-26 20:23:05 +00:00
|
|
|
}
|
2012-07-29 13:09:53 +00:00
|
|
|
else if (m_private->currentEvent.type == SDL_ACTIVEEVENT)
|
|
|
|
{
|
|
|
|
event.type = EVENT_ACTIVE;
|
|
|
|
|
|
|
|
if (m_private->currentEvent.active.type & SDL_APPINPUTFOCUS)
|
|
|
|
event.active.flags |= ACTIVE_INPUT;
|
|
|
|
if (m_private->currentEvent.active.type & SDL_APPMOUSEFOCUS)
|
|
|
|
event.active.flags |= ACTIVE_MOUSE;
|
|
|
|
if (m_private->currentEvent.active.type & SDL_APPACTIVE)
|
|
|
|
event.active.flags |= ACTIVE_APP;
|
|
|
|
|
|
|
|
event.active.gain = m_private->currentEvent.active.gain == 1;
|
|
|
|
}
|
2012-06-29 22:12:04 +00:00
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
return event;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
/** Processes incoming events. It is the first function called after an event is captures.
|
|
|
|
Function returns \c true if the event is to be passed on to other processing functions
|
|
|
|
or \c false if not. */
|
|
|
|
bool CApplication::ProcessEvent(const Event &event)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-07-04 17:56:22 +00:00
|
|
|
CLogger *l = GetLogger();
|
2012-07-29 13:09:53 +00:00
|
|
|
|
|
|
|
if (event.type == EVENT_ACTIVE)
|
|
|
|
{
|
|
|
|
if (m_debugMode)
|
2012-09-13 21:28:06 +00:00
|
|
|
l->Info("Focus change: active = %s\n", event.active.gain ? "true" : "false");
|
2012-09-12 21:43:04 +00:00
|
|
|
|
2012-09-16 08:38:08 +00:00
|
|
|
/*if (m_active != event.active.gain)
|
2012-09-13 21:28:06 +00:00
|
|
|
{
|
|
|
|
m_active = event.active.gain;
|
|
|
|
|
|
|
|
if (m_active)
|
|
|
|
ResumeSimulation();
|
|
|
|
else
|
|
|
|
SuspendSimulation();
|
2012-09-16 08:38:08 +00:00
|
|
|
}*/
|
2012-09-12 21:43:04 +00:00
|
|
|
}
|
|
|
|
else if (event.type == EVENT_KEY_DOWN)
|
|
|
|
{
|
|
|
|
m_kmodState = event.key.mod;
|
|
|
|
|
|
|
|
if ((m_kmodState & KEY_MOD(SHIFT)) != 0)
|
|
|
|
m_trackedKeysState[TRKEY_SHIFT] = true;
|
|
|
|
else if ((m_kmodState & KEY_MOD(CTRL)) != 0)
|
|
|
|
m_trackedKeysState[TRKEY_CONTROL] = true;
|
|
|
|
else if (event.key.key == KEY(KP8))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_UP] = true;
|
|
|
|
else if (event.key.key == KEY(KP2))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_DOWN] = true;
|
|
|
|
else if (event.key.key == KEY(KP4))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_LEFT] = true;
|
|
|
|
else if (event.key.key == KEY(KP6))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_RIGHT] = true;
|
|
|
|
else if (event.key.key == KEY(KP_PLUS))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_PLUS] = true;
|
|
|
|
else if (event.key.key == KEY(KP_MINUS))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_MINUS] = true;
|
|
|
|
else if (event.key.key == KEY(PAGEUP))
|
|
|
|
m_trackedKeysState[TRKEY_PAGE_UP] = true;
|
|
|
|
else if (event.key.key == KEY(PAGEDOWN))
|
|
|
|
m_trackedKeysState[TRKEY_PAGE_DOWN] = true;
|
|
|
|
}
|
|
|
|
else if (event.type == EVENT_KEY_UP)
|
|
|
|
{
|
|
|
|
m_kmodState = event.key.mod;
|
|
|
|
|
|
|
|
if ((m_kmodState & KEY_MOD(SHIFT)) != 0)
|
|
|
|
m_trackedKeysState[TRKEY_SHIFT] = false;
|
|
|
|
else if ((m_kmodState & KEY_MOD(CTRL)) != 0)
|
|
|
|
m_trackedKeysState[TRKEY_CONTROL] = false;
|
|
|
|
else if (event.key.key == KEY(KP8))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_UP] = false;
|
|
|
|
else if (event.key.key == KEY(KP2))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_DOWN] = false;
|
|
|
|
else if (event.key.key == KEY(KP4))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_LEFT] = false;
|
|
|
|
else if (event.key.key == KEY(KP6))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_RIGHT] = false;
|
|
|
|
else if (event.key.key == KEY(KP_PLUS))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_PLUS] = false;
|
|
|
|
else if (event.key.key == KEY(KP_MINUS))
|
|
|
|
m_trackedKeysState[TRKEY_NUM_MINUS] = false;
|
|
|
|
else if (event.key.key == KEY(PAGEUP))
|
|
|
|
m_trackedKeysState[TRKEY_PAGE_UP] = false;
|
|
|
|
else if (event.key.key == KEY(PAGEDOWN))
|
|
|
|
m_trackedKeysState[TRKEY_PAGE_DOWN] = false;
|
|
|
|
}
|
|
|
|
else if (event.type == EVENT_MOUSE_BUTTON_DOWN)
|
|
|
|
{
|
|
|
|
m_mouseButtonsState |= 1 << event.mouseButton.button;
|
|
|
|
}
|
|
|
|
else if (event.type == EVENT_MOUSE_BUTTON_UP)
|
|
|
|
{
|
|
|
|
m_mouseButtonsState &= ~(1 << event.mouseButton.button);
|
2012-07-29 13:09:53 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
// Print the events in debug mode to test the code
|
|
|
|
if (m_debugMode)
|
|
|
|
{
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case EVENT_KEY_DOWN:
|
|
|
|
case EVENT_KEY_UP:
|
2012-07-04 17:56:22 +00:00
|
|
|
l->Info("EVENT_KEY_%s:\n", (event.type == EVENT_KEY_DOWN) ? "DOWN" : "UP");
|
|
|
|
l->Info(" key = %4x\n", event.key.key);
|
|
|
|
l->Info(" state = %s\n", (event.key.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED");
|
|
|
|
l->Info(" mod = %4x\n", event.key.mod);
|
|
|
|
l->Info(" unicode = %4x\n", event.key.unicode);
|
2012-06-29 22:12:04 +00:00
|
|
|
break;
|
|
|
|
case EVENT_MOUSE_MOVE:
|
2012-07-04 17:56:22 +00:00
|
|
|
l->Info("EVENT_MOUSE_MOVE:\n");
|
|
|
|
l->Info(" state = %s\n", (event.mouseMove.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED");
|
|
|
|
l->Info(" pos = (%f, %f)\n", event.mouseMove.pos.x, event.mouseMove.pos.y);
|
2012-06-29 22:12:04 +00:00
|
|
|
break;
|
|
|
|
case EVENT_MOUSE_BUTTON_DOWN:
|
|
|
|
case EVENT_MOUSE_BUTTON_UP:
|
2012-07-04 17:56:22 +00:00
|
|
|
l->Info("EVENT_MOUSE_BUTTON_%s:\n", (event.type == EVENT_MOUSE_BUTTON_DOWN) ? "DOWN" : "UP");
|
|
|
|
l->Info(" button = %d\n", event.mouseButton.button);
|
|
|
|
l->Info(" state = %s\n", (event.mouseButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED");
|
|
|
|
l->Info(" pos = (%f, %f)\n", event.mouseButton.pos.x, event.mouseButton.pos.y);
|
2012-06-29 22:12:04 +00:00
|
|
|
break;
|
|
|
|
case EVENT_JOY_AXIS:
|
2012-07-04 17:56:22 +00:00
|
|
|
l->Info("EVENT_JOY_AXIS:\n");
|
|
|
|
l->Info(" axis = %d\n", event.joyAxis.axis);
|
|
|
|
l->Info(" value = %d\n", event.joyAxis.value);
|
2012-06-29 22:12:04 +00:00
|
|
|
break;
|
|
|
|
case EVENT_JOY_BUTTON_DOWN:
|
|
|
|
case EVENT_JOY_BUTTON_UP:
|
2012-07-04 17:56:22 +00:00
|
|
|
l->Info("EVENT_JOY_BUTTON_%s:\n", (event.type == EVENT_JOY_BUTTON_DOWN) ? "DOWN" : "UP");
|
|
|
|
l->Info(" button = %d\n", event.joyButton.button);
|
|
|
|
l->Info(" state = %s\n", (event.joyButton.state == STATE_PRESSED) ? "STATE_PRESSED" : "STATE_RELEASED");
|
2012-06-29 22:12:04 +00:00
|
|
|
break;
|
2012-07-29 13:09:53 +00:00
|
|
|
case EVENT_ACTIVE:
|
|
|
|
l->Info("EVENT_ACTIVE:\n");
|
|
|
|
l->Info(" flags = 0x%x\n", event.active.flags);
|
|
|
|
l->Info(" gain = %s\n", event.active.gain ? "true" : "false");
|
|
|
|
break;
|
2012-06-29 22:12:04 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-22 20:05:12 +00:00
|
|
|
|
|
|
|
// By default, pass on all events
|
|
|
|
return true;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-10 21:31:42 +00:00
|
|
|
/** Renders the frame and swaps buffers as necessary */
|
|
|
|
void CApplication::Render()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-08-10 21:31:42 +00:00
|
|
|
m_engine->Render();
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
if (m_deviceConfig.doubleBuf)
|
2012-06-26 20:23:05 +00:00
|
|
|
SDL_GL_SwapBuffers();
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
void CApplication::SuspendSimulation()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-09-12 21:43:04 +00:00
|
|
|
m_simulationSuspended = true;
|
|
|
|
GetLogger()->Info("Suspend simulation\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::ResumeSimulation()
|
|
|
|
{
|
|
|
|
m_simulationSuspended = false;
|
|
|
|
|
|
|
|
GetCurrentTimeStamp(m_baseTimeStamp);
|
|
|
|
CopyTimeStamp(m_curTimeStamp, m_baseTimeStamp);
|
|
|
|
m_realAbsTimeBase = m_realAbsTime;
|
|
|
|
m_absTimeBase = m_exactAbsTime;
|
|
|
|
|
|
|
|
GetLogger()->Info("Resume simulation\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::GetSimulationSuspended()
|
|
|
|
{
|
|
|
|
return m_simulationSuspended;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::SetSimulationSpeed(float speed)
|
|
|
|
{
|
|
|
|
m_simulationSpeed = speed;
|
|
|
|
|
|
|
|
GetCurrentTimeStamp(m_baseTimeStamp);
|
|
|
|
m_realAbsTimeBase = m_realAbsTime;
|
|
|
|
m_absTimeBase = m_exactAbsTime;
|
|
|
|
|
|
|
|
GetLogger()->Info("Simulation speed = %.2f\n", speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::StepSimulation()
|
|
|
|
{
|
|
|
|
if (m_simulationSuspended)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp);
|
|
|
|
GetCurrentTimeStamp(m_curTimeStamp);
|
|
|
|
|
|
|
|
long long absDiff = TimeStampExactDiff(m_baseTimeStamp, m_curTimeStamp);
|
|
|
|
m_realAbsTime = m_realAbsTimeBase + absDiff;
|
|
|
|
// m_baseTimeStamp is updated on simulation speed change, so this is OK
|
|
|
|
m_exactAbsTime = m_absTimeBase + m_simulationSpeed * absDiff;
|
|
|
|
m_absTime = (m_absTimeBase + m_simulationSpeed * absDiff) / 1e9f;
|
|
|
|
|
|
|
|
m_realRelTime = TimeStampExactDiff(m_lastTimeStamp, m_curTimeStamp);
|
|
|
|
m_exactRelTime = m_simulationSpeed * m_realRelTime;
|
|
|
|
m_relTime = (m_simulationSpeed * m_realRelTime) / 1e9f;
|
|
|
|
|
|
|
|
|
|
|
|
m_engine->FrameUpdate();
|
2012-09-15 20:19:32 +00:00
|
|
|
m_sound->FrameMove(m_relTime);
|
2012-09-12 21:43:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
Event frameEvent(EVENT_FRAME);
|
|
|
|
frameEvent.rTime = m_relTime;
|
|
|
|
m_eventQueue->AddEvent(frameEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
float CApplication::GetSimulationSpeed()
|
|
|
|
{
|
|
|
|
return m_simulationSpeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CApplication::GetAbsTime()
|
|
|
|
{
|
|
|
|
return m_absTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long CApplication::GetExactAbsTime()
|
|
|
|
{
|
|
|
|
return m_exactAbsTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long CApplication::GetRealAbsTime()
|
|
|
|
{
|
|
|
|
return m_realAbsTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
float CApplication::GetRelTime()
|
|
|
|
{
|
|
|
|
return m_relTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long CApplication::GetExactRelTime()
|
|
|
|
{
|
|
|
|
return m_exactRelTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long CApplication::GetRealRelTime()
|
|
|
|
{
|
|
|
|
return m_realRelTime;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:23:13 +00:00
|
|
|
Gfx::GLDeviceConfig CApplication::GetVideoConfig()
|
|
|
|
{
|
|
|
|
return m_deviceConfig;
|
|
|
|
}
|
|
|
|
|
2012-08-11 15:17:04 +00:00
|
|
|
VideoQueryResult CApplication::GetVideoResolutionList(std::vector<Math::IntPoint> &resolutions,
|
2012-07-29 13:09:53 +00:00
|
|
|
bool fullScreen, bool resizeable)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-07-29 13:09:53 +00:00
|
|
|
resolutions.clear();
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
|
2012-09-09 15:51:10 +00:00
|
|
|
if (videoInfo == nullptr)
|
2012-07-29 13:09:53 +00:00
|
|
|
return VIDEO_QUERY_ERROR;
|
|
|
|
|
|
|
|
Uint32 videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;
|
|
|
|
|
|
|
|
// Use hardware surface if available
|
|
|
|
if (videoInfo->hw_available)
|
|
|
|
videoFlags |= SDL_HWSURFACE;
|
|
|
|
else
|
|
|
|
videoFlags |= SDL_SWSURFACE;
|
|
|
|
|
|
|
|
// Enable hardware blit if available
|
|
|
|
if (videoInfo->blit_hw)
|
|
|
|
videoFlags |= SDL_HWACCEL;
|
|
|
|
|
|
|
|
if (resizeable)
|
|
|
|
videoFlags |= SDL_RESIZABLE;
|
|
|
|
|
|
|
|
if (fullScreen)
|
|
|
|
videoFlags |= SDL_FULLSCREEN;
|
|
|
|
|
|
|
|
|
|
|
|
SDL_Rect **modes = SDL_ListModes(NULL, videoFlags);
|
|
|
|
|
2012-07-30 20:59:18 +00:00
|
|
|
if (modes == reinterpret_cast<SDL_Rect **>(0) )
|
2012-07-29 13:09:53 +00:00
|
|
|
return VIDEO_QUERY_NONE; // no modes available
|
|
|
|
|
2012-07-30 20:59:18 +00:00
|
|
|
if (modes == reinterpret_cast<SDL_Rect **>(-1) )
|
2012-07-29 13:09:53 +00:00
|
|
|
return VIDEO_QUERY_ALL; // all resolutions are possible
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; modes[i] != NULL; ++i)
|
2012-08-11 15:17:04 +00:00
|
|
|
resolutions.push_back(Math::IntPoint(modes[i]->w, modes[i]->h));
|
2012-07-29 13:09:53 +00:00
|
|
|
|
|
|
|
return VIDEO_QUERY_OK;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::SetDebugMode(bool mode)
|
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
m_debugMode = mode;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
bool CApplication::GetDebugMode()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
return m_debugMode;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
void CApplication::SetDefaultInputBindings()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < KEYRANK_MAX; i++)
|
|
|
|
m_inputBindings[i].Reset();
|
|
|
|
|
|
|
|
m_inputBindings[KEYRANK_LEFT ].key = KEY(LEFT);
|
|
|
|
m_inputBindings[KEYRANK_RIGHT ].key = KEY(RIGHT);
|
|
|
|
m_inputBindings[KEYRANK_UP ].key = KEY(UP);
|
|
|
|
m_inputBindings[KEYRANK_DOWN ].key = KEY(DOWN);
|
|
|
|
m_inputBindings[KEYRANK_GUP ].kmod = KEY_MOD(SHIFT);
|
|
|
|
m_inputBindings[KEYRANK_GDOWN ].kmod = KEY_MOD(CTRL);
|
|
|
|
m_inputBindings[KEYRANK_CAMERA ].key = KEY(SPACE);
|
|
|
|
m_inputBindings[KEYRANK_CAMERA ].joy = 2;
|
|
|
|
m_inputBindings[KEYRANK_DESEL ].key = KEY(KP0);
|
|
|
|
m_inputBindings[KEYRANK_DESEL ].kmod = 6;
|
|
|
|
m_inputBindings[KEYRANK_ACTION ].key = KEY(RETURN);
|
|
|
|
m_inputBindings[KEYRANK_ACTION ].joy = 1;
|
|
|
|
m_inputBindings[KEYRANK_NEAR ].key = KEY(KP_PLUS);
|
|
|
|
m_inputBindings[KEYRANK_NEAR ].joy = 5;
|
|
|
|
m_inputBindings[KEYRANK_AWAY ].key = KEY(KP_MINUS);
|
|
|
|
m_inputBindings[KEYRANK_AWAY ].joy = 4;
|
|
|
|
m_inputBindings[KEYRANK_NEXT ].key = KEY(TAB);
|
|
|
|
m_inputBindings[KEYRANK_NEXT ].joy = 3;
|
|
|
|
m_inputBindings[KEYRANK_HUMAN ].key = KEY(HOME);
|
|
|
|
m_inputBindings[KEYRANK_HUMAN ].joy = 7;
|
|
|
|
m_inputBindings[KEYRANK_QUIT ].key = KEY(ESCAPE);
|
|
|
|
m_inputBindings[KEYRANK_HELP ].key = KEY(F1);
|
|
|
|
m_inputBindings[KEYRANK_PROG ].key = KEY(F2);
|
|
|
|
m_inputBindings[KEYRANK_CBOT ].key = KEY(F3);
|
|
|
|
m_inputBindings[KEYRANK_VISIT ].key = KEY(KP_PERIOD);
|
|
|
|
m_inputBindings[KEYRANK_SPEED10].key = KEY(F4);
|
|
|
|
m_inputBindings[KEYRANK_SPEED15].key = KEY(F5);
|
|
|
|
m_inputBindings[KEYRANK_SPEED20].key = KEY(F6);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CApplication::GetKmods()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-09-12 21:43:04 +00:00
|
|
|
return m_kmodState;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
bool CApplication::GetKmodState(int kmod)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-09-12 21:43:04 +00:00
|
|
|
return (m_kmodState & kmod) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::GetTrackedKeyState(TrackedKey key)
|
|
|
|
{
|
|
|
|
return m_trackedKeysState[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::GetMouseButtonState(int index)
|
|
|
|
{
|
|
|
|
return (m_mouseButtonsState & (1<<index)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::ResetKeyStates()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < TRKEY_MAX; ++i)
|
|
|
|
m_trackedKeysState[i] = false;
|
|
|
|
|
|
|
|
m_kmodState = 0;
|
|
|
|
m_keyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
|
|
|
|
m_joyMotion = Math::Vector(0.0f, 0.0f, 0.0f);
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
void CApplication::SetInputBinding(InputSlot slot, const InputBinding& binding)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-09-12 21:43:04 +00:00
|
|
|
m_inputBindings[slot] = binding;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-09-12 21:43:04 +00:00
|
|
|
const InputBinding& CApplication::GetInputBinding(InputSlot slot)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-09-12 21:43:04 +00:00
|
|
|
return m_inputBindings[slot];
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
void CApplication::SetGrabInput(bool grab)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
SDL_WM_GrabInput(grab ? SDL_GRAB_ON : SDL_GRAB_OFF);
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
bool CApplication::GetGrabInput()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-07-22 20:05:12 +00:00
|
|
|
int result = SDL_WM_GrabInput(SDL_GRAB_QUERY);
|
|
|
|
return result == SDL_GRAB_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::SetSystemMouseVisible(bool visible)
|
|
|
|
{
|
|
|
|
SDL_ShowCursor(visible ? SDL_ENABLE : SDL_DISABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CApplication::GetSystemMouseVisibile()
|
|
|
|
{
|
|
|
|
int result = SDL_ShowCursor(SDL_QUERY);
|
|
|
|
return result == SDL_ENABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::SetSystemMousePos(Math::Point pos)
|
|
|
|
{
|
2012-08-03 21:23:13 +00:00
|
|
|
Math::IntPoint windowPos = m_engine->InterfaceToWindowCoords(pos);
|
2012-07-22 20:05:12 +00:00
|
|
|
SDL_WarpMouse(windowPos.x, windowPos.y);
|
|
|
|
m_systemMousePos = pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Math::Point CApplication::GetSystemMousePos()
|
|
|
|
{
|
|
|
|
return m_systemMousePos;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-29 13:09:53 +00:00
|
|
|
std::vector<JoystickDevice> CApplication::GetJoystickList()
|
|
|
|
{
|
|
|
|
std::vector<JoystickDevice> result;
|
|
|
|
|
|
|
|
int count = SDL_NumJoysticks();
|
|
|
|
|
|
|
|
for (int index = 0; index < count; ++index)
|
|
|
|
{
|
|
|
|
JoystickDevice device;
|
|
|
|
device.index = index;
|
|
|
|
device.name = SDL_JoystickName(index);
|
|
|
|
result.push_back(device);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
JoystickDevice CApplication::GetJoystick()
|
|
|
|
{
|
|
|
|
return m_joystick;
|
|
|
|
}
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
void CApplication::SetJoystickEnabled(bool enable)
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
m_joystickEnabled = enable;
|
2012-06-25 17:59:17 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
if (m_joystickEnabled)
|
|
|
|
{
|
|
|
|
if (! OpenJoystick())
|
|
|
|
{
|
|
|
|
m_joystickEnabled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CloseJoystick();
|
|
|
|
}
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 20:59:22 +00:00
|
|
|
bool CApplication::GetJoystickEnabled()
|
2012-06-25 17:59:17 +00:00
|
|
|
{
|
2012-06-29 22:12:04 +00:00
|
|
|
return m_joystickEnabled;
|
2012-06-25 17:59:17 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 20:05:12 +00:00
|
|
|
std::string CApplication::GetDataFilePath(const std::string& dirName, const std::string& fileName)
|
|
|
|
{
|
|
|
|
return m_dataPath + "/" + dirName + "/" + fileName;
|
|
|
|
}
|
2012-09-09 15:51:10 +00:00
|
|
|
|
|
|
|
Language CApplication::GetLanguage()
|
|
|
|
{
|
|
|
|
return m_language;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CApplication::SetLanguage(Language language)
|
|
|
|
{
|
|
|
|
m_language = language;
|
|
|
|
}
|