GUIApp.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2002-2007 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "GUIApp.h"
00022 
00023 #include <SDL.h>
00024 
00026 #include "Kernel.h"
00027 #include "FileSystem.h"
00028 #include "SettingManager.h"
00029 #include "ConfigFileManager.h"
00030 #include "ObjectManager.h"
00031 #include "GameInfo.h"
00032 #include "FontManager.h"
00033 #include "MemoryManager.h"
00034 
00035 #include "HIDManager.h"
00036 #include "Joystick.h"
00037 
00038 #include "RenderSurface.h"
00039 #include "Texture.h"
00040 #include "FixedWidthFont.h"
00041 #include "PaletteManager.h"
00042 #include "Palette.h"
00043 #include "GameData.h"
00044 #include "World.h"
00045 #include "Direction.h"
00046 #include "Game.h"
00047 #include "getObject.h"
00048 
00049 #include "SavegameWriter.h"
00050 #include "Savegame.h"
00051 #include <ctime>
00052 
00053 #include "Gump.h"
00054 #include "DesktopGump.h"
00055 #include "ConsoleGump.h"
00056 #include "GameMapGump.h"
00057 #include "InverterGump.h"
00058 #include "ScalerGump.h"
00059 #include "FastAreaVisGump.h"
00060 #include "MiniMapGump.h"
00061 #include "QuitGump.h"
00062 #include "MenuGump.h"
00063 #include "PentagramMenuGump.h"
00064 
00065 // For gump positioning... perhaps shouldn't do it this way....
00066 #include "BarkGump.h"
00067 #include "AskGump.h"
00068 #include "ModalGump.h"
00069 
00070 
00071 #include "QuickAvatarMoverProcess.h"
00072 #include "Actor.h"
00073 #include "ActorAnimProcess.h"
00074 #include "TargetedAnimProcess.h"
00075 #include "u8intrinsics.h"
00076 #include "remorseintrinsics.h"
00077 #include "Egg.h"
00078 #include "CurrentMap.h"
00079 #include "InverterProcess.h"
00080 #include "HealProcess.h"
00081 #include "SchedulerProcess.h"
00082 
00083 #include "EggHatcherProcess.h" // for a hack
00084 #include "UCProcess.h" // more hacking
00085 #include "GumpNotifyProcess.h" // guess
00086 #include "ActorBarkNotifyProcess.h" // guess
00087 #include "DelayProcess.h"
00088 #include "AvatarGravityProcess.h"
00089 #include "MissileProcess.h"
00090 #include "TeleportToEggProcess.h"
00091 #include "ItemFactory.h"
00092 #include "PathfinderProcess.h"
00093 #include "AvatarMoverProcess.h"
00094 #include "ResurrectionProcess.h"
00095 #include "SplitItemProcess.h"
00096 #include "ClearFeignDeathProcess.h"
00097 #include "LoiterProcess.h"
00098 #include "AvatarDeathProcess.h"
00099 #include "GrantPeaceProcess.h"
00100 #include "CombatProcess.h"
00101 #include "FireballProcess.h"
00102 #include "DestroyItemProcess.h"
00103 #include "AmbushProcess.h"
00104 #include "Pathfinder.h"
00105 
00106 #include "MovieGump.h"
00107 #include "ShapeViewerGump.h"
00108 
00109 #include "AudioMixer.h"
00110 
00111 #ifdef WIN32
00112 #include <windows.h>
00113 #endif
00114 
00115 #if defined(WIN32) && defined(I_AM_COLOURLESS_EXPERIMENTING_WITH_HW_CURSORS)
00116 #include "Shape.h"
00117 #include "ShapeFrame.h"
00118 #include "SoftRenderSurface.h"
00119 #include "SDL_syswm.h"
00120 
00121 struct HWMouseCursor {
00122         HICON hCursor;
00123 };
00124 
00125 #endif
00126 
00127 #include "XFormBlend.h"
00128 
00129 #include "MusicProcess.h"
00130 #include "AudioProcess.h"
00131 
00132 #include "util.h"
00133 
00134 using std::string;
00135 
00136 DEFINE_RUNTIME_CLASSTYPE_CODE(GUIApp,CoreApp);
00137 
00138 GUIApp::GUIApp(int argc, const char* const* argv)
00139         : CoreApp(argc, argv), save_count(0), game(0), kernel(0), objectmanager(0),
00140           hidmanager(0), ucmachine(0), screen(0), fullscreen(false), palettemanager(0), 
00141           gamedata(0), world(0), desktopGump(0), consoleGump(0), gameMapGump(0),
00142           avatarMoverProcess(0), runSDLInit(false),
00143           frameSkip(false), frameLimit(true), interpolate(false),
00144           animationRate(100), avatarInStasis(false), paintEditorItems(false),
00145           painting(false), showTouching(false), mouseX(0), mouseY(0),
00146           defMouse(0), flashingcursor(0), 
00147           mouseOverGump(0), dragging(DRAG_NOT), dragging_offsetX(0),
00148           dragging_offsetY(0), inversion(0), timeOffset(0), has_cheated(false),
00149           drawRenderStats(false), ttfoverrides(false), audiomixer(0)
00150 {
00151         application = this;
00152 
00153         for (int i = 0; i < MOUSE_LAST; ++i) {
00154                 mouseButton[i].downGump = 0;
00155                 mouseButton[i].lastDown = 0;
00156                 mouseButton[i].state = MBS_HANDLED;
00157         }
00158 
00159         con.AddConsoleCommand("quit", ConCmd_quit);
00160         con.AddConsoleCommand("GUIApp::quit", ConCmd_quit);
00161         con.AddConsoleCommand("QuitGump::verifyQuit", QuitGump::ConCmd_verifyQuit);
00162         con.AddConsoleCommand("ShapeViewerGump::U8ShapeViewer", ShapeViewerGump::ConCmd_U8ShapeViewer);
00163         con.AddConsoleCommand("MenuGump::showMenu", MenuGump::ConCmd_showMenu);
00164         con.AddConsoleCommand("GUIApp::drawRenderStats", ConCmd_drawRenderStats);
00165         con.AddConsoleCommand("GUIApp::engineStats", ConCmd_engineStats);
00166 
00167         con.AddConsoleCommand("GUIApp::changeGame",ConCmd_changeGame);
00168         con.AddConsoleCommand("GUIApp::listGames",ConCmd_listGames);
00169 
00170         con.AddConsoleCommand("GUIApp::setVideoMode",ConCmd_setVideoMode);
00171         con.AddConsoleCommand("GUIApp::toggleFullscreen",ConCmd_toggleFullscreen);
00172 
00173         con.AddConsoleCommand("GUIApp::toggleAvatarInStasis",ConCmd_toggleAvatarInStasis);
00174         con.AddConsoleCommand("GUIApp::togglePaintEditorItems",ConCmd_togglePaintEditorItems);
00175         con.AddConsoleCommand("GUIApp::toggleShowTouchingItems",ConCmd_toggleShowTouchingItems);
00176 
00177         con.AddConsoleCommand("GUIApp::closeItemGumps",ConCmd_closeItemGumps);
00178 
00179         con.AddConsoleCommand("HIDManager::bind", HIDManager::ConCmd_bind);
00180         con.AddConsoleCommand("HIDManager::unbind", HIDManager::ConCmd_unbind);
00181         con.AddConsoleCommand("HIDManager::listbinds",
00182                                                   HIDManager::ConCmd_listbinds);
00183         con.AddConsoleCommand("HIDManager::save", HIDManager::ConCmd_save);
00184         con.AddConsoleCommand("Kernel::processTypes", Kernel::ConCmd_processTypes);
00185         con.AddConsoleCommand("Kernel::processInfo", Kernel::ConCmd_processInfo);
00186         con.AddConsoleCommand("Kernel::listProcesses",
00187                                                   Kernel::ConCmd_listProcesses);
00188         con.AddConsoleCommand("Kernel::toggleFrameByFrame",
00189                                                   Kernel::ConCmd_toggleFrameByFrame);
00190         con.AddConsoleCommand("Kernel::advanceFrame", Kernel::ConCmd_advanceFrame);
00191         con.AddConsoleCommand("ObjectManager::objectTypes",
00192                                                   ObjectManager::ConCmd_objectTypes);
00193         con.AddConsoleCommand("ObjectManager::objectInfo",
00194                                                   ObjectManager::ConCmd_objectInfo);
00195         con.AddConsoleCommand("MemoryManager::MemInfo",
00196                                                   MemoryManager::ConCmd_MemInfo);
00197         con.AddConsoleCommand("MemoryManager::test",
00198                                                   MemoryManager::ConCmd_test);
00199 
00200         con.AddConsoleCommand("QuickAvatarMoverProcess::startMoveUp",
00201                                                   QuickAvatarMoverProcess::ConCmd_startMoveUp);
00202         con.AddConsoleCommand("QuickAvatarMoverProcess::startMoveDown",
00203                                                   QuickAvatarMoverProcess::ConCmd_startMoveDown);
00204         con.AddConsoleCommand("QuickAvatarMoverProcess::startMoveLeft",
00205                                                   QuickAvatarMoverProcess::ConCmd_startMoveLeft);
00206         con.AddConsoleCommand("QuickAvatarMoverProcess::startMoveRight",
00207                                                   QuickAvatarMoverProcess::ConCmd_startMoveRight);
00208         con.AddConsoleCommand("QuickAvatarMoverProcess::startAscend",
00209                                                   QuickAvatarMoverProcess::ConCmd_startAscend);
00210         con.AddConsoleCommand("QuickAvatarMoverProcess::startDescend",
00211                                                   QuickAvatarMoverProcess::ConCmd_startDescend);
00212         con.AddConsoleCommand("QuickAvatarMoverProcess::stopMoveUp",
00213                                                   QuickAvatarMoverProcess::ConCmd_stopMoveUp);
00214         con.AddConsoleCommand("QuickAvatarMoverProcess::stopMoveDown",
00215                                                   QuickAvatarMoverProcess::ConCmd_stopMoveDown);
00216         con.AddConsoleCommand("QuickAvatarMoverProcess::stopMoveLeft",
00217                                                   QuickAvatarMoverProcess::ConCmd_stopMoveLeft);
00218         con.AddConsoleCommand("QuickAvatarMoverProcess::stopMoveRight",
00219                                                   QuickAvatarMoverProcess::ConCmd_stopMoveRight);
00220         con.AddConsoleCommand("QuickAvatarMoverProcess::stopAscend",
00221                                                   QuickAvatarMoverProcess::ConCmd_stopAscend);
00222         con.AddConsoleCommand("QuickAvatarMoverProcess::stopDescend",
00223                                                   QuickAvatarMoverProcess::ConCmd_stopDescend);
00224         con.AddConsoleCommand("QuickAvatarMoverProcess::toggleQuarterSpeed",
00225                                                   QuickAvatarMoverProcess::ConCmd_toggleQuarterSpeed);
00226         con.AddConsoleCommand("QuickAvatarMoverProcess::toggleClipping",
00227                                                   QuickAvatarMoverProcess::ConCmd_toggleClipping);
00228 
00229         con.AddConsoleCommand("GameMapGump::toggleHighlightItems",
00230                                                   GameMapGump::ConCmd_toggleHighlightItems);
00231         con.AddConsoleCommand("GameMapGump::dumpMap",
00232                                                   GameMapGump::ConCmd_dumpMap);
00233 
00234         con.AddConsoleCommand("AudioProcess::listSFX", AudioProcess::ConCmd_listSFX);
00235         con.AddConsoleCommand("AudioProcess::playSFX", AudioProcess::ConCmd_playSFX);
00236         con.AddConsoleCommand("AudioProcess::stopSFX", AudioProcess::ConCmd_stopSFX);
00237 
00238         // Game related console commands are now added in startupGame
00239 }
00240 
00241 GUIApp::~GUIApp()
00242 {
00243         shutdown();
00244 
00245         con.RemoveConsoleCommand(GUIApp::ConCmd_quit);
00246         con.RemoveConsoleCommand(QuitGump::ConCmd_verifyQuit);
00247         con.RemoveConsoleCommand(ShapeViewerGump::ConCmd_U8ShapeViewer);
00248         con.RemoveConsoleCommand(MenuGump::ConCmd_showMenu);
00249         con.RemoveConsoleCommand(GUIApp::ConCmd_drawRenderStats);
00250         con.RemoveConsoleCommand(GUIApp::ConCmd_engineStats);
00251 
00252         con.RemoveConsoleCommand(GUIApp::ConCmd_changeGame);
00253         con.RemoveConsoleCommand(GUIApp::ConCmd_listGames);
00254 
00255         con.RemoveConsoleCommand(GUIApp::ConCmd_setVideoMode);
00256         con.RemoveConsoleCommand(GUIApp::ConCmd_toggleFullscreen);
00257 
00258         con.RemoveConsoleCommand(GUIApp::ConCmd_toggleAvatarInStasis);
00259         con.RemoveConsoleCommand(GUIApp::ConCmd_togglePaintEditorItems);
00260         con.RemoveConsoleCommand(GUIApp::ConCmd_toggleShowTouchingItems);
00261 
00262         con.RemoveConsoleCommand(GUIApp::ConCmd_closeItemGumps);
00263 
00264         con.RemoveConsoleCommand(HIDManager::ConCmd_bind);
00265         con.RemoveConsoleCommand(HIDManager::ConCmd_unbind);
00266         con.RemoveConsoleCommand(HIDManager::ConCmd_listbinds);
00267         con.RemoveConsoleCommand(HIDManager::ConCmd_save);
00268         con.RemoveConsoleCommand(Kernel::ConCmd_processTypes);
00269         con.RemoveConsoleCommand(Kernel::ConCmd_processInfo);
00270         con.RemoveConsoleCommand(Kernel::ConCmd_listProcesses);
00271         con.RemoveConsoleCommand(Kernel::ConCmd_toggleFrameByFrame);
00272         con.RemoveConsoleCommand(Kernel::ConCmd_advanceFrame);
00273         con.RemoveConsoleCommand(ObjectManager::ConCmd_objectTypes);
00274         con.RemoveConsoleCommand(ObjectManager::ConCmd_objectInfo);
00275         con.RemoveConsoleCommand(MemoryManager::ConCmd_MemInfo);
00276         con.RemoveConsoleCommand(MemoryManager::ConCmd_test);
00277 
00278         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startMoveUp);
00279         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startMoveDown);
00280         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startMoveLeft);
00281         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startMoveRight);
00282         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startAscend);
00283         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_startDescend);
00284         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopMoveUp);
00285         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopMoveDown);
00286         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopMoveLeft);
00287         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopMoveRight);
00288         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopAscend);
00289         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_stopDescend);
00290         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_toggleQuarterSpeed);
00291         con.RemoveConsoleCommand(QuickAvatarMoverProcess::ConCmd_toggleClipping);
00292 
00293         con.RemoveConsoleCommand(GameMapGump::ConCmd_toggleHighlightItems);
00294         con.RemoveConsoleCommand(GameMapGump::ConCmd_dumpMap);
00295 
00296         con.RemoveConsoleCommand(AudioProcess::ConCmd_listSFX);
00297         con.RemoveConsoleCommand(AudioProcess::ConCmd_stopSFX);
00298         con.RemoveConsoleCommand(AudioProcess::ConCmd_playSFX);
00299 
00300         // Game related console commands are now removed in shutdownGame
00301 
00302         FORGET_OBJECT(kernel);
00303         FORGET_OBJECT(defMouse);
00304         FORGET_OBJECT(objectmanager);
00305         FORGET_OBJECT(hidmanager);
00306         FORGET_OBJECT(audiomixer);
00307         FORGET_OBJECT(ucmachine);
00308         FORGET_OBJECT(palettemanager);
00309         FORGET_OBJECT(gamedata);
00310         FORGET_OBJECT(world);
00311         FORGET_OBJECT(ucmachine);
00312         FORGET_OBJECT(fontmanager);
00313         FORGET_OBJECT(screen);
00314 }
00315 
00316 // Init sdl
00317 void GUIApp::SDLInit()
00318 {
00319         con.Print(MM_INFO, "Initialising SDL...\n");
00320         SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
00321         atexit(SDL_Quit);
00322 }
00323 
00324 void GUIApp::startup()
00325 {
00326         SDLInit();
00327 
00328         // Set the console to auto paint, till we have finished initing
00329         con.SetAutoPaint(conAutoPaint);
00330 
00331         pout << "-- Initializing Pentagram -- " << std::endl;
00332 
00333         // parent's startup first
00334         CoreApp::startup();
00335 
00336         bool dataoverride;
00337         if (!settingman->get("dataoverride", dataoverride,
00338                                                  SettingManager::DOM_GLOBAL))
00339                 dataoverride = false;
00340         filesystem->initBuiltinData(dataoverride);
00341 
00342         kernel = new Kernel();
00343 
00345         kernel->addProcessLoader("DelayProcess",
00346                                                          ProcessLoader<DelayProcess>::load);
00347         kernel->addProcessLoader("GravityProcess",
00348                                                          ProcessLoader<GravityProcess>::load);
00349         kernel->addProcessLoader("AvatarGravityProcess",
00350                                                          ProcessLoader<AvatarGravityProcess>::load);
00351         kernel->addProcessLoader("PaletteFaderProcess",
00352                                                          ProcessLoader<PaletteFaderProcess>::load);
00353         kernel->addProcessLoader("TeleportToEggProcess",
00354                                                          ProcessLoader<TeleportToEggProcess>::load);
00355         kernel->addProcessLoader("ActorAnimProcess",
00356                                                          ProcessLoader<ActorAnimProcess>::load);
00357         kernel->addProcessLoader("TargetedAnimProcess",
00358                                                          ProcessLoader<TargetedAnimProcess>::load);
00359         kernel->addProcessLoader("AvatarMoverProcess",
00360                                                          ProcessLoader<AvatarMoverProcess>::load);
00361         kernel->addProcessLoader("QuickAvatarMoverProcess",
00362                                                          ProcessLoader<QuickAvatarMoverProcess>::load);
00363         kernel->addProcessLoader("PathfinderProcess",
00364                                                          ProcessLoader<PathfinderProcess>::load);
00365         kernel->addProcessLoader("SpriteProcess",
00366                                                          ProcessLoader<SpriteProcess>::load);
00367         kernel->addProcessLoader("MissileProcess",
00368                                                          ProcessLoader<MissileProcess>::load);
00369         kernel->addProcessLoader("CameraProcess",
00370                                                          ProcessLoader<CameraProcess>::load);
00371         kernel->addProcessLoader("MusicProcess",
00372                                                          ProcessLoader<MusicProcess>::load);
00373         kernel->addProcessLoader("AudioProcess",
00374                                                          ProcessLoader<AudioProcess>::load);
00375         kernel->addProcessLoader("EggHatcherProcess",
00376                                                          ProcessLoader<EggHatcherProcess>::load);
00377         kernel->addProcessLoader("UCProcess",
00378                                                          ProcessLoader<UCProcess>::load);
00379         kernel->addProcessLoader("GumpNotifyProcess",
00380                                                          ProcessLoader<GumpNotifyProcess>::load);
00381         kernel->addProcessLoader("ResurrectionProcess",
00382                                                          ProcessLoader<ResurrectionProcess>::load);
00383         kernel->addProcessLoader("DeleteActorProcess",
00384                                                          ProcessLoader<DestroyItemProcess>::load);      // YES, this is intentional
00385         kernel->addProcessLoader("DestroyItemProcess",
00386                                                          ProcessLoader<DestroyItemProcess>::load);
00387         kernel->addProcessLoader("SplitItemProcess",
00388                                                          ProcessLoader<SplitItemProcess>::load);
00389         kernel->addProcessLoader("ClearFeignDeathProcess",
00390                                                          ProcessLoader<ClearFeignDeathProcess>::load);
00391         kernel->addProcessLoader("LoiterProcess",
00392                                                          ProcessLoader<LoiterProcess>::load);
00393         kernel->addProcessLoader("AvatarDeathProcess",
00394                                                          ProcessLoader<AvatarDeathProcess>::load);
00395         kernel->addProcessLoader("GrantPeaceProcess",
00396                                                          ProcessLoader<GrantPeaceProcess>::load);
00397         kernel->addProcessLoader("CombatProcess",
00398                                                          ProcessLoader<CombatProcess>::load);
00399         kernel->addProcessLoader("FireballProcess",
00400                                                          ProcessLoader<FireballProcess>::load);
00401         kernel->addProcessLoader("HealProcess",
00402                                                          ProcessLoader<HealProcess>::load);
00403         kernel->addProcessLoader("SchedulerProcess",
00404                                                          ProcessLoader<SchedulerProcess>::load);
00405         kernel->addProcessLoader("InverterProcess",
00406                                                          ProcessLoader<InverterProcess>::load);
00407         kernel->addProcessLoader("ActorBarkNotifyProcess",
00408                                                          ProcessLoader<ActorBarkNotifyProcess>::load);
00409         kernel->addProcessLoader("JoystickCursorProcess",
00410                                                          ProcessLoader<JoystickCursorProcess>::load);
00411         kernel->addProcessLoader("AmbushProcess",
00412                                                          ProcessLoader<AmbushProcess>::load);
00413 
00414         objectmanager = new ObjectManager();
00415 
00416         GraphicSysInit();
00417 
00418         SDL_ShowCursor(SDL_DISABLE);
00419         SDL_GetMouseState(&mouseX, &mouseY);
00420 
00421         hidmanager = new HIDManager();
00422 
00423         // Audio Mixer
00424         audiomixer = new Pentagram::AudioMixer(22050,true,8);
00425 
00426         pout << "-- Pentagram Initialized -- " << std::endl << std::endl;
00427 
00428         // We Attempt to startup game
00429         setupGameList();
00430         GameInfo* info = getDefaultGame();
00431         if (setupGame(info))
00432                 startupGame();
00433         else
00434                 startupPentagramMenu();
00435 
00436         // Unset the console auto paint, since we have finished initing
00437         con.SetAutoPaint(0);
00438 
00439 //      pout << "Paint Initial display" << std::endl;
00440         paint();
00441 }
00442 
00443 void GUIApp::startupGame()
00444 {
00445         con.SetAutoPaint(conAutoPaint);
00446 
00447         pout  << std::endl << "-- Initializing Game: " << gameinfo->name << " --" << std::endl;
00448 
00449         GraphicSysInit();
00450 
00451         // set window title to current game
00452         std::string title = "Pentagram - ";
00453         title += getGameInfo()->getGameTitle();
00454         SDL_WM_SetCaption(title.c_str(), "");
00455 
00456         // Generic Commands
00457         con.AddConsoleCommand("GUIApp::saveGame", ConCmd_saveGame);
00458         con.AddConsoleCommand("GUIApp::loadGame", ConCmd_loadGame);
00459         con.AddConsoleCommand("GUIApp::newGame", ConCmd_newGame);
00460 #ifdef DEBUG
00461         con.AddConsoleCommand("Pathfinder::visualDebug",
00462                                                   Pathfinder::ConCmd_visualDebug);
00463 #endif
00464 
00465         // U8 Game commands
00466         con.AddConsoleCommand("MainActor::teleport", MainActor::ConCmd_teleport);
00467         con.AddConsoleCommand("MainActor::mark", MainActor::ConCmd_mark);
00468         con.AddConsoleCommand("MainActor::recall", MainActor::ConCmd_recall);
00469         con.AddConsoleCommand("MainActor::listmarks", MainActor::ConCmd_listmarks);
00470         con.AddConsoleCommand("Cheat::maxstats", MainActor::ConCmd_maxstats);
00471         con.AddConsoleCommand("Cheat::heal", MainActor::ConCmd_heal);
00472         con.AddConsoleCommand("Cheat::toggleInvincibility", MainActor::ConCmd_toggleInvincibility);
00473         con.AddConsoleCommand("MainActor::name", MainActor::ConCmd_name);
00474         con.AddConsoleCommand("MovieGump::play", MovieGump::ConCmd_play);
00475         con.AddConsoleCommand("MusicProcess::playMusic", MusicProcess::ConCmd_playMusic);
00476         con.AddConsoleCommand("InverterProcess::invertScreen",
00477                                                   InverterProcess::ConCmd_invertScreen);
00478         con.AddConsoleCommand("FastAreaVisGump::toggle",
00479                                                   FastAreaVisGump::ConCmd_toggle);
00480         con.AddConsoleCommand("MiniMapGump::toggle",
00481                                                   MiniMapGump::ConCmd_toggle);
00482         con.AddConsoleCommand("MainActor::useBackpack",
00483                                                   MainActor::ConCmd_useBackpack);
00484         con.AddConsoleCommand("MainActor::useInventory",
00485                                                   MainActor::ConCmd_useInventory);
00486         con.AddConsoleCommand("MainActor::useRecall",
00487                                                   MainActor::ConCmd_useRecall);
00488         con.AddConsoleCommand("MainActor::useBedroll",
00489                                                   MainActor::ConCmd_useBedroll);
00490         con.AddConsoleCommand("MainActor::useKeyring",
00491                                                   MainActor::ConCmd_useKeyring);
00492         con.AddConsoleCommand("MainActor::toggleCombat",
00493                                                   MainActor::ConCmd_toggleCombat);
00494 
00495         gamedata = new GameData();
00496 
00497         std::string bindingsfile;
00498         if (GAME_IS_U8) {
00499                 bindingsfile = "@data/u8bindings.ini";
00500         } else if (GAME_IS_REMORSE) {
00501                 bindingsfile = "@data/remorsebindings.ini";
00502         }
00503         if (!bindingsfile.empty()) {
00504                 // system-wide config
00505                 if (configfileman->readConfigFile(bindingsfile,
00506                                                                                   "bindings", true))
00507                         con.Printf(MM_INFO, "%s... Ok\n", bindingsfile.c_str());
00508                 else
00509                         con.Printf(MM_MINOR_WARN, "%s... Failed\n", bindingsfile.c_str());
00510         }
00511 
00512         hidmanager->loadBindings();
00513         
00514         if (GAME_IS_U8) {
00515                 ucmachine = new UCMachine(U8Intrinsics, 256);
00516         } else if (GAME_IS_REMORSE) {
00517                 ucmachine = new UCMachine(RemorseIntrinsics, 308);
00518         } else {
00519                 CANT_HAPPEN_MSG("Invalid game type.");
00520         }
00521 
00522         inBetweenFrame = 0;
00523         lerpFactor = 256;
00524 
00525         // Initialize world
00526         world = new World();
00527         world->initMaps();
00528 
00529         game = Game::createGame(getGameInfo());
00530 
00531         settingman->setDefault("ttf", false);
00532         settingman->get("ttf", ttfoverrides);
00533 
00534         game->loadFiles();
00535         gamedata->setupFontOverrides();
00536 
00537         // Unset the console auto paint (can't have it from here on)
00538         con.SetAutoPaint(0);
00539 
00540         // Create Midi Driver for Ultima 8
00541         if (getGameInfo()->type == GameInfo::GAME_U8) 
00542                 audiomixer->openMidiOutput();
00543 
00544 #if 1
00545         newGame();
00546 #else
00547         loadGame("@save/quicksave");
00548 #endif
00549 
00550         consoleGump->HideConsole();
00551 
00552         pout << "-- Game Initialized --" << std::endl << std::endl;
00553 }
00554 
00555 void GUIApp::startupPentagramMenu()
00556 {
00557         con.SetAutoPaint(conAutoPaint);
00558 
00559         pout << std::endl << "-- Initializing Pentagram Menu -- " << std::endl;
00560 
00561         setupGame(getGameInfo("pentagram"));
00562         assert(gameinfo);
00563 
00564         GraphicSysInit();
00565 
00566         // Unset the console auto paint, since we have finished initing
00567         con.SetAutoPaint(0);
00568         consoleGump->HideConsole();
00569 
00570         Pentagram::Rect dims;
00571         desktopGump->GetDims(dims);
00572 
00573         Gump* menugump = new PentagramMenuGump(0,0,dims.w,dims.h);
00574         menugump->InitGump(0, true);
00575 }
00576 
00577 void GUIApp::shutdown()
00578 {
00579         shutdownGame(false);
00580 }
00581 
00582 void GUIApp::shutdownGame(bool reloading)
00583 {
00584         pout << "-- Shutting down Game -- " << std::endl;
00585 
00586         // Save config here....
00587 
00588         SDL_WM_SetCaption("Pentagram", "");
00589 
00590         textmodes.clear();
00591 
00592         // reset mouse cursor
00593         while (!cursors.empty()) cursors.pop();
00594         pushMouseCursor();
00595 
00596         if (audiomixer) {
00597                 audiomixer->closeMidiOutput();
00598                 audiomixer->reset();
00599         }
00600 
00601         FORGET_OBJECT(world);
00602         objectmanager->reset();
00603         FORGET_OBJECT(ucmachine);
00604         kernel->reset();
00605         palettemanager->reset();
00606         fontmanager->resetGameFonts();
00607 
00608         FORGET_OBJECT(game);
00609         FORGET_OBJECT(gamedata);
00610 
00611         desktopGump = 0;
00612         consoleGump = 0;
00613         gameMapGump = 0;
00614         scalerGump = 0;
00615         inverterGump = 0;
00616 
00617         timeOffset = -(sint32)Kernel::get_instance()->getFrameNum();
00618         inversion = 0;
00619         save_count = 0;
00620         has_cheated = false;
00621 
00622         // Generic Game 
00623         con.RemoveConsoleCommand(GUIApp::ConCmd_saveGame);
00624         con.RemoveConsoleCommand(GUIApp::ConCmd_loadGame);
00625         con.RemoveConsoleCommand(GUIApp::ConCmd_newGame);
00626 #ifdef DEBUG
00627         con.RemoveConsoleCommand(Pathfinder::ConCmd_visualDebug);
00628 #endif
00629 
00630         // U8 Only kind of
00631         con.RemoveConsoleCommand(MainActor::ConCmd_teleport);
00632         con.RemoveConsoleCommand(MainActor::ConCmd_mark);
00633         con.RemoveConsoleCommand(MainActor::ConCmd_recall);
00634         con.RemoveConsoleCommand(MainActor::ConCmd_listmarks);
00635         con.RemoveConsoleCommand(MainActor::ConCmd_maxstats);
00636         con.RemoveConsoleCommand(MainActor::ConCmd_heal);
00637         con.RemoveConsoleCommand(MainActor::ConCmd_toggleInvincibility);
00638         con.RemoveConsoleCommand(MainActor::ConCmd_name);
00639         con.RemoveConsoleCommand(MovieGump::ConCmd_play);
00640         con.RemoveConsoleCommand(MusicProcess::ConCmd_playMusic);
00641         con.RemoveConsoleCommand(InverterProcess::ConCmd_invertScreen);
00642         con.RemoveConsoleCommand(FastAreaVisGump::ConCmd_toggle);
00643         con.RemoveConsoleCommand(MiniMapGump::ConCmd_toggle);
00644         con.RemoveConsoleCommand(MainActor::ConCmd_useBackpack);
00645         con.RemoveConsoleCommand(MainActor::ConCmd_useInventory);
00646         con.RemoveConsoleCommand(MainActor::ConCmd_useRecall);
00647         con.RemoveConsoleCommand(MainActor::ConCmd_useBedroll);
00648         con.RemoveConsoleCommand(MainActor::ConCmd_useKeyring);
00649         con.RemoveConsoleCommand(MainActor::ConCmd_toggleCombat);
00650 
00651 
00652         // Kill Game
00653         CoreApp::killGame();
00654 
00655         pout << "-- Game Shutdown -- " << std::endl;
00656 
00657         if (reloading) {
00658                 Pentagram::Rect dims;
00659                 screen->GetSurfaceDims(dims);
00660 
00661                 con.Print(MM_INFO, "Creating Desktop...\n");
00662                 desktopGump = new DesktopGump(0,0, dims.w, dims.h);
00663                 desktopGump->InitGump(0);
00664                 desktopGump->MakeFocus();
00665 
00666                 con.Print(MM_INFO, "Creating Graphics Console...\n");
00667                 consoleGump = new ConsoleGump(0, 0, dims.w, dims.h);
00668                 consoleGump->InitGump(0);
00669 
00670                 enterTextMode(consoleGump);
00671         }
00672 }
00673 
00674 void GUIApp::changeGame(Pentagram::istring newgame)
00675 {
00676         change_gamename = newgame;
00677 }
00678 
00679 void GUIApp::DeclareArgs()
00680 {
00681         // parent's arguments first
00682         CoreApp::DeclareArgs();
00683 
00684         // anything else?
00685 }
00686 
00687 void GUIApp::run()
00688 {
00689         isRunning = true;
00690 
00691         sint32 next_ticks = SDL_GetTicks()*3;   // Next time is right now!
00692         
00693         // Ok, the theory is that if this is set to true then we must do a repaint
00694         // At the moment only it's ignored
00695         bool repaint;
00696 
00697         SDL_Event event;
00698         while (isRunning) {
00699                 inBetweenFrame = true;  // Will get set false if it's not an inBetweenFrame
00700 
00701                 if (!frameLimit) {
00702                         repaint = false;
00703                         
00704                         if (kernel->runProcesses()) repaint = true;
00705                         desktopGump->Run(kernel->getFrameNum());
00706                         inBetweenFrame = false;
00707                         next_ticks = animationRate + SDL_GetTicks()*3;
00708                         lerpFactor = 256;
00709                 }
00710                 else 
00711                 {
00712                         sint32 ticks = SDL_GetTicks()*3;
00713                         sint32 diff = next_ticks - ticks;
00714                         repaint = false;
00715 
00716                         while (diff < 0) {
00717                                 next_ticks += animationRate;
00718                                 if (kernel->runProcesses()) repaint = true;
00719                                 desktopGump->Run(kernel->getFrameNum());
00720 #if 0
00721                                 perr << "--------------------------------------" << std::endl;
00722                                 perr << "NEW FRAME" << std::endl;
00723                                 perr << "--------------------------------------" << std::endl;
00724 #endif
00725                                 inBetweenFrame = false;
00726 
00727                                 ticks = SDL_GetTicks()*3;
00728 
00729                                 // If frame skipping is off, we will only recalc next
00730                                 // ticks IF the frames are taking up 'way' too much time. 
00731                                 if (!frameSkip && diff <= -animationRate*2) next_ticks = animationRate + ticks;
00732 
00733                                 diff = next_ticks - ticks;
00734                                 if (!frameSkip) break;
00735                         }
00736 
00737                         // Calculate the lerp_factor
00738                         lerpFactor = ((animationRate-diff)*256)/animationRate;
00739                         //pout << "lerpFactor: " << lerpFactor << " framenum: " << framenum << std::endl;
00740                         if (!interpolate || kernel->isPaused() || lerpFactor > 256)
00741                                 lerpFactor = 256;
00742                 }
00743 
00744 
00745                 repaint = true;
00746 
00747                 // get & handle all events in queue
00748                 while (