2010-05-20 02:55:02 +02:00
|
|
|
/* Copyright (C) 2010 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
This module drives the game when running without Atlas (our integrated
|
|
|
|
map editor). It receives input and OS messages via SDL and feeds them
|
|
|
|
into the input dispatcher, where they are passed on to the game GUI and
|
|
|
|
simulation.
|
|
|
|
It also contains main(), which either runs the above controller or
|
|
|
|
that of Atlas depending on commandline parameters.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2006-06-02 05:56:24 +02:00
|
|
|
// not for any PCH effort, but instead for the (common) definitions
|
|
|
|
// included there.
|
2010-03-28 20:21:48 +02:00
|
|
|
#define MINIMAL_PCH 2
|
2006-06-02 05:56:24 +02:00
|
|
|
#include "lib/precompiled.h"
|
2004-06-03 20:38:14 +02:00
|
|
|
|
2010-03-28 20:21:48 +02:00
|
|
|
#include "lib/debug.h"
|
|
|
|
#include "lib/lib_errors.h"
|
|
|
|
#include "lib/secure_crt.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "lib/frequency_filter.h"
|
2005-08-15 01:34:37 +02:00
|
|
|
#include "lib/input.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "lib/ogl.h"
|
2005-06-16 23:50:20 +02:00
|
|
|
#include "lib/timer.h"
|
2007-05-27 01:29:20 +02:00
|
|
|
#include "lib/external_libraries/sdl.h"
|
2006-04-10 23:49:12 +02:00
|
|
|
#include "lib/res/sound/snd_mgr.h"
|
2004-06-02 22:39:21 +02:00
|
|
|
|
2010-09-18 20:21:00 +02:00
|
|
|
#include "ps/ArchiveBuilder.h"
|
2004-06-02 22:39:21 +02:00
|
|
|
#include "ps/CConsole.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "ps/Filesystem.h"
|
2004-07-27 23:00:53 +02:00
|
|
|
#include "ps/Game.h"
|
2005-10-31 19:36:36 +01:00
|
|
|
#include "ps/Globals.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "ps/Hotkey.h"
|
|
|
|
#include "ps/Loader.h"
|
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "ps/Pyrogenesis.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "ps/Replay.h"
|
2011-02-16 21:40:15 +01:00
|
|
|
#include "ps/UserReport.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "ps/Util.h"
|
2010-06-03 21:07:59 +02:00
|
|
|
#include "ps/VideoMode.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "ps/GameSetup/GameSetup.h"
|
|
|
|
#include "ps/GameSetup/Atlas.h"
|
|
|
|
#include "ps/GameSetup/Config.h"
|
|
|
|
#include "ps/GameSetup/CmdLineArgs.h"
|
2010-08-07 00:16:05 +02:00
|
|
|
#include "ps/GameSetup/Paths.h"
|
2009-12-03 21:17:22 +01:00
|
|
|
#include "ps/XML/Xeromyces.h"
|
2008-06-16 20:19:35 +02:00
|
|
|
#include "network/NetClient.h"
|
|
|
|
#include "network/NetServer.h"
|
|
|
|
#include "network/NetSession.h"
|
2007-01-08 02:56:46 +01:00
|
|
|
#include "graphics/Camera.h"
|
2006-03-21 21:55:45 +01:00
|
|
|
#include "graphics/GameView.h"
|
2010-09-10 23:02:10 +02:00
|
|
|
#include "graphics/TextureManager.h"
|
|
|
|
#include "gui/GUIManager.h"
|
|
|
|
#include "renderer/Renderer.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "scripting/ScriptingHost.h"
|
2010-01-09 20:20:14 +01:00
|
|
|
#include "simulation2/Simulation2.h"
|
2004-09-19 17:58:13 +02:00
|
|
|
|
2006-10-08 05:28:22 +02:00
|
|
|
extern bool g_GameRestarted;
|
2005-06-16 23:50:20 +02:00
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
void kill_mainloop();
|
2005-06-16 23:50:20 +02:00
|
|
|
|
2010-06-03 23:15:45 +02:00
|
|
|
// to avoid redundant and/or recursive resizing, we save the new
|
|
|
|
// size after VIDEORESIZE messages and only update the video mode
|
|
|
|
// once per frame.
|
|
|
|
// these values are the latest resize message, and reset to 0 once we've
|
|
|
|
// updated the video mode
|
|
|
|
static int g_ResizedW;
|
|
|
|
static int g_ResizedH;
|
2004-06-02 22:39:21 +02:00
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
// main app message handler
|
2006-08-26 23:52:18 +02:00
|
|
|
static InReaction MainInputHandler(const SDL_Event_* ev)
|
2004-05-24 22:25:48 +02:00
|
|
|
{
|
2006-08-26 23:52:18 +02:00
|
|
|
switch(ev->ev.type)
|
2004-05-24 22:25:48 +02:00
|
|
|
{
|
2010-12-05 06:17:28 +01:00
|
|
|
case SDL_ACTIVEEVENT:
|
|
|
|
if (ev->ev.active.state == SDL_APPMOUSEFOCUS)
|
|
|
|
{
|
|
|
|
// Tell renderer not to render cursor if mouse focus is lost
|
|
|
|
// this restores system cursor, until/if focus is regained
|
|
|
|
if (!ev->ev.active.gain)
|
|
|
|
RenderCursor(false);
|
|
|
|
else
|
|
|
|
RenderCursor(true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-10-12 06:27:55 +02:00
|
|
|
case SDL_QUIT:
|
|
|
|
kill_mainloop();
|
|
|
|
break;
|
|
|
|
|
2010-06-03 21:07:59 +02:00
|
|
|
case SDL_VIDEORESIZE:
|
2010-06-03 23:15:45 +02:00
|
|
|
g_ResizedW = ev->ev.resize.w;
|
|
|
|
g_ResizedH = ev->ev.resize.h;
|
2010-06-03 21:07:59 +02:00
|
|
|
break;
|
|
|
|
|
2004-07-21 18:34:07 +02:00
|
|
|
case SDL_HOTKEYDOWN:
|
2010-10-23 04:37:00 +02:00
|
|
|
std::string hotkey = static_cast<const char*>(ev->ev.user.data1);
|
|
|
|
if (hotkey == "exit")
|
2004-05-24 22:25:48 +02:00
|
|
|
{
|
2005-08-15 01:34:37 +02:00
|
|
|
kill_mainloop();
|
2005-10-20 19:44:56 +02:00
|
|
|
return IN_HANDLED;
|
2010-10-23 04:37:00 +02:00
|
|
|
}
|
|
|
|
else if (hotkey == "screenshot")
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
WriteScreenshot(L".png");
|
2005-10-20 19:44:56 +02:00
|
|
|
return IN_HANDLED;
|
2010-10-23 04:37:00 +02:00
|
|
|
}
|
|
|
|
else if (hotkey == "bigscreenshot")
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
WriteBigScreenshot(L".bmp", 10);
|
2005-10-24 03:53:03 +02:00
|
|
|
return IN_HANDLED;
|
2010-10-23 04:37:00 +02:00
|
|
|
}
|
|
|
|
else if (hotkey == "togglefullscreen")
|
|
|
|
{
|
2010-06-03 21:07:59 +02:00
|
|
|
g_VideoMode.ToggleFullscreen();
|
|
|
|
return IN_HANDLED;
|
2004-05-24 22:25:48 +02:00
|
|
|
}
|
2005-10-13 20:05:55 +02:00
|
|
|
break;
|
2004-05-24 22:25:48 +02:00
|
|
|
}
|
|
|
|
|
2005-10-20 19:44:56 +02:00
|
|
|
return IN_PASS;
|
2004-05-24 22:25:48 +02:00
|
|
|
}
|
|
|
|
|
2005-06-16 23:50:20 +02:00
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
// dispatch all pending events to the various receivers.
|
|
|
|
static void PumpEvents()
|
2004-06-21 16:14:12 +02:00
|
|
|
{
|
2007-06-15 19:03:26 +02:00
|
|
|
PROFILE( "dispatch events" );
|
2005-08-15 01:34:37 +02:00
|
|
|
in_dispatch_recorded_events();
|
2004-07-24 21:38:12 +02:00
|
|
|
|
2006-08-26 23:52:18 +02:00
|
|
|
SDL_Event_ ev;
|
|
|
|
while(SDL_PollEvent(&ev.ev))
|
|
|
|
in_dispatch_event(&ev);
|
2004-12-08 16:34:15 +01:00
|
|
|
}
|
|
|
|
|
2005-06-16 23:50:20 +02:00
|
|
|
|
2006-03-01 23:31:11 +01:00
|
|
|
// return indication of whether archive is currently being built; this is
|
|
|
|
// used to prevent reloading during that time (see call site).
|
|
|
|
static bool ProgressiveBuildArchive()
|
|
|
|
{
|
2009-12-03 21:17:22 +01:00
|
|
|
ONCE(g_GUI->SendEventToAll("archivebuildercomplete"));
|
2007-12-20 21:21:45 +01:00
|
|
|
return false;
|
|
|
|
#if 0
|
2006-03-07 06:44:34 +01:00
|
|
|
int ret = vfs_opt_auto_build("../logs/trace.txt", "mods/official/official%02d.zip", "mods/official/mini%02d.zip");
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret == INFO::ALL_COMPLETE)
|
2006-03-01 23:31:11 +01:00
|
|
|
{
|
|
|
|
// nothing to do; will return false below
|
|
|
|
}
|
|
|
|
else if(ret < 0)
|
2008-01-19 12:33:11 +01:00
|
|
|
DEBUG_DISPLAY_ERROR(L"Archive build failed");
|
2006-09-22 15:19:40 +02:00
|
|
|
else if(ret == INFO::OK)
|
2006-03-01 23:31:11 +01:00
|
|
|
g_GUI.SendEventToAll("archivebuildercomplete");
|
|
|
|
// in progress
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int percent = (int)ret;
|
|
|
|
g_ScriptingHost.SetGlobal("g_ArchiveBuilderProgress", INT_TO_JSVAL(percent));
|
|
|
|
g_GUI.SendEventToAll("archivebuilderprogress");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2007-12-20 21:21:45 +01:00
|
|
|
#endif
|
2006-03-01 23:31:11 +01:00
|
|
|
}
|
|
|
|
|
2005-03-22 03:17:55 +01:00
|
|
|
|
|
|
|
static int ProgressiveLoad()
|
|
|
|
{
|
|
|
|
wchar_t description[100];
|
|
|
|
int progress_percent;
|
2005-12-12 20:19:30 +01:00
|
|
|
LibError ret = LDR_ProgressiveLoad(10e-3, description, ARRAY_SIZE(description), &progress_percent);
|
2005-03-22 03:17:55 +01:00
|
|
|
switch(ret)
|
|
|
|
{
|
2005-08-15 01:34:37 +02:00
|
|
|
// no load active => no-op (skip code below)
|
2006-09-22 15:19:40 +02:00
|
|
|
case INFO::OK:
|
2005-05-11 06:39:21 +02:00
|
|
|
return 0;
|
2005-08-15 01:34:37 +02:00
|
|
|
// current task didn't complete. we only care about this insofar as the
|
|
|
|
// load process is therefore not yet finished.
|
2006-09-22 15:19:40 +02:00
|
|
|
case ERR::TIMED_OUT:
|
2005-03-22 03:17:55 +01:00
|
|
|
break;
|
2005-08-15 01:34:37 +02:00
|
|
|
// just finished loading
|
2006-09-22 15:19:40 +02:00
|
|
|
case INFO::ALL_COMPLETE:
|
2005-03-22 03:17:55 +01:00
|
|
|
g_Game->ReallyStartGame();
|
|
|
|
wcscpy_s(description, ARRAY_SIZE(description), L"Game is starting..");
|
2005-08-15 01:34:37 +02:00
|
|
|
// LDR_ProgressiveLoad returns L""; set to valid text to
|
|
|
|
// avoid problems in converting to JSString
|
2005-03-22 03:17:55 +01:00
|
|
|
break;
|
2005-08-15 01:34:37 +02:00
|
|
|
// error!
|
2005-03-22 03:17:55 +01:00
|
|
|
default:
|
|
|
|
CHECK_ERR(ret);
|
2006-09-22 15:19:40 +02:00
|
|
|
// can't do this above due to legit ERR::TIMED_OUT
|
2005-03-22 03:17:55 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-08-14 17:23:59 +02:00
|
|
|
GUI_DisplayLoadProgress(progress_percent, description);
|
2005-03-22 03:17:55 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-03-01 23:31:11 +01:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
static void RendererIncrementalLoad()
|
|
|
|
{
|
|
|
|
const double maxTime = 0.1f;
|
|
|
|
|
|
|
|
double startTime = timer_Time();
|
|
|
|
bool more;
|
|
|
|
do {
|
|
|
|
more = g_Renderer.GetTextureManager().MakeProgress();
|
|
|
|
}
|
|
|
|
while (more && timer_Time() - startTime < maxTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-21 18:26:34 +02:00
|
|
|
static bool quit = false; // break out of main loop
|
|
|
|
|
2004-07-12 18:50:46 +02:00
|
|
|
static void Frame()
|
|
|
|
{
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2005-06-25 04:21:26 +02:00
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
// get elapsed time
|
2008-01-07 21:03:19 +01:00
|
|
|
const double time = timer_Time();
|
2007-12-20 21:21:45 +01:00
|
|
|
g_frequencyFilter->Update(time);
|
2005-10-31 19:36:36 +01:00
|
|
|
// .. old method - "exact" but contains jumps
|
2005-10-18 01:35:16 +02:00
|
|
|
#if 0
|
2004-07-12 18:50:46 +02:00
|
|
|
static double last_time;
|
2008-01-07 21:03:19 +01:00
|
|
|
const double time = timer_Time();
|
2004-07-12 18:50:46 +02:00
|
|
|
const float TimeSinceLastFrame = (float)(time-last_time);
|
|
|
|
last_time = time;
|
2005-10-18 01:35:16 +02:00
|
|
|
ONCE(return); // first call: set last_time and return
|
2005-10-31 19:36:36 +01:00
|
|
|
|
|
|
|
// .. new method - filtered and more smooth, but errors may accumulate
|
2005-10-18 01:35:16 +02:00
|
|
|
#else
|
2007-12-20 21:21:45 +01:00
|
|
|
const float TimeSinceLastFrame = 1.0 / g_frequencyFilter->SmoothedFrequency();
|
2005-10-18 01:35:16 +02:00
|
|
|
#endif
|
2008-07-05 10:27:33 +02:00
|
|
|
debug_assert(TimeSinceLastFrame > 0.0f);
|
2004-05-24 22:25:48 +02:00
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
// decide if update/render is necessary
|
2007-06-04 09:41:05 +02:00
|
|
|
bool need_render = !g_app_minimized;
|
|
|
|
bool need_update = true;
|
|
|
|
|
|
|
|
// If we are not running a multiplayer game, disable updates when the game is
|
|
|
|
// minimized or out of focus and relinquish the CPU a bit, in order to make
|
|
|
|
// debugging easier.
|
2010-06-30 23:41:04 +02:00
|
|
|
if( !g_NetClient && !g_app_has_focus )
|
2005-10-31 19:36:36 +01:00
|
|
|
{
|
2011-02-21 03:16:12 +01:00
|
|
|
PROFILE("non-focus delay");
|
2005-10-31 19:36:36 +01:00
|
|
|
need_update = false;
|
|
|
|
// don't use SDL_WaitEvent: don't want the main loop to freeze until app focus is restored
|
|
|
|
SDL_Delay(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: throttling: limit update and render frequency to the minimum.
|
|
|
|
// this is mostly relevant for "inactive" state, so that other windows
|
|
|
|
// get enough CPU time, but it's always nice for power+thermal management.
|
|
|
|
|
2006-03-01 23:31:11 +01:00
|
|
|
bool is_building_archive; // must come before PROFILE_START's {
|
|
|
|
PROFILE_START("build archive");
|
|
|
|
is_building_archive = ProgressiveBuildArchive();
|
|
|
|
PROFILE_END( "build archive");
|
|
|
|
|
|
|
|
// this scans for changed files/directories and reloads them, thus
|
|
|
|
// allowing hotloading (changes are immediately assimilated in-game).
|
|
|
|
// must not be done during archive building because it changes the
|
|
|
|
// archive file each iteration, but keeps it locked; reloading
|
|
|
|
// would trigger a warning because the file can't be opened.
|
|
|
|
if(!is_building_archive)
|
|
|
|
{
|
|
|
|
PROFILE_START("reload changed files");
|
2009-11-06 12:50:04 +01:00
|
|
|
ReloadChangedFiles();
|
2006-03-01 23:31:11 +01:00
|
|
|
PROFILE_END( "reload changed files");
|
|
|
|
}
|
2004-05-24 22:25:48 +02:00
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_START("progressive load");
|
2005-03-22 03:17:55 +01:00
|
|
|
ProgressiveLoad();
|
2009-12-03 21:10:22 +01:00
|
|
|
PROFILE_END("progressive load");
|
2004-07-15 21:12:54 +02:00
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
PROFILE_START("renderer incremental load");
|
|
|
|
RendererIncrementalLoad();
|
|
|
|
PROFILE_END("renderer incremental load");
|
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_START("input");
|
2005-08-15 01:34:37 +02:00
|
|
|
PumpEvents();
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_END("input");
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2010-08-21 18:26:34 +02:00
|
|
|
// if the user quit by closing the window, the GL context will be broken and
|
|
|
|
// may crash when we call Render() on some drivers, so leave this loop
|
|
|
|
// before rendering
|
|
|
|
if (quit)
|
|
|
|
return;
|
|
|
|
|
2010-06-03 23:15:45 +02:00
|
|
|
// respond to pumped resize events
|
|
|
|
if (g_ResizedW || g_ResizedH)
|
|
|
|
{
|
|
|
|
g_VideoMode.ResizeWindow(g_ResizedW, g_ResizedH);
|
|
|
|
g_ResizedW = g_ResizedH = 0;
|
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
PROFILE_START("network poll");
|
|
|
|
if (g_NetClient)
|
|
|
|
g_NetClient->Poll();
|
|
|
|
PROFILE_END("network poll");
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2005-10-12 06:27:55 +02:00
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_START("gui tick");
|
2009-12-03 21:17:22 +01:00
|
|
|
g_GUI->TickObjects();
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_END("gui tick");
|
2005-08-09 18:03:36 +02:00
|
|
|
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2005-10-12 06:27:55 +02:00
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
if (g_Game && g_Game->IsGameStarted() && need_update)
|
2004-08-05 15:07:51 +02:00
|
|
|
{
|
|
|
|
g_Game->Update(TimeSinceLastFrame);
|
2005-03-22 03:17:55 +01:00
|
|
|
|
2010-05-20 02:55:02 +02:00
|
|
|
PROFILE_START( "camera update" );
|
|
|
|
g_Game->GetView()->Update(float(TimeSinceLastFrame));
|
|
|
|
PROFILE_END( "camera update" );
|
2004-07-21 18:34:07 +02:00
|
|
|
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_START( "sound update" );
|
2004-10-15 15:29:04 +02:00
|
|
|
CCamera* camera = g_Game->GetView()->GetCamera();
|
|
|
|
CMatrix3D& orientation = camera->m_Orientation;
|
2004-10-16 22:55:56 +02:00
|
|
|
float* pos = &orientation._data[12];
|
|
|
|
float* dir = &orientation._data[8];
|
|
|
|
float* up = &orientation._data[4];
|
2008-06-21 22:06:30 +02:00
|
|
|
// HACK: otherwise sound effects are L/R flipped. No idea what else
|
|
|
|
// is going wrong, because the listener and camera are supposed to
|
|
|
|
// coincide in position and orientation.
|
|
|
|
float down[3] = { -up[0], -up[1], -up[2] };
|
|
|
|
if(snd_update(pos, dir, down) < 0)
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"snd_update failed\n");
|
2005-03-30 18:14:19 +02:00
|
|
|
PROFILE_END( "sound update" );
|
2004-08-05 15:07:51 +02:00
|
|
|
}
|
2004-10-21 01:16:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(snd_update(0, 0, 0) < 0)
|
2009-11-03 22:46:35 +01:00
|
|
|
debug_printf(L"snd_update (pos=0 version) failed\n");
|
2004-10-21 01:16:38 +02:00
|
|
|
}
|
2005-03-30 18:14:19 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
// Immediately flush any messages produced by simulation code
|
|
|
|
PROFILE_START("network flush");
|
|
|
|
if (g_NetClient)
|
|
|
|
g_NetClient->Flush();
|
|
|
|
PROFILE_END("network flush");
|
|
|
|
|
2011-02-16 21:40:15 +01:00
|
|
|
g_UserReporter.Update();
|
|
|
|
|
2004-07-20 21:30:35 +02:00
|
|
|
g_Console->Update(TimeSinceLastFrame);
|
|
|
|
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_START("render");
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2005-10-31 19:36:36 +01:00
|
|
|
if(need_render)
|
2004-07-12 18:50:46 +02:00
|
|
|
{
|
|
|
|
Render();
|
2005-03-31 00:33:10 +02:00
|
|
|
PROFILE_START( "swap buffers" );
|
2004-12-28 12:16:05 +01:00
|
|
|
SDL_GL_SwapBuffers();
|
2005-03-31 00:33:10 +02:00
|
|
|
PROFILE_END( "swap buffers" );
|
2004-07-12 18:50:46 +02:00
|
|
|
}
|
2007-05-02 14:07:08 +02:00
|
|
|
ogl_WarnIfError();
|
2005-10-31 19:36:36 +01:00
|
|
|
PROFILE_END("render");
|
2005-03-30 18:14:19 +02:00
|
|
|
|
|
|
|
g_Profiler.Frame();
|
|
|
|
|
2006-10-08 05:28:22 +02:00
|
|
|
g_GameRestarted = false;
|
2005-08-15 01:34:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MainControllerInit()
|
|
|
|
{
|
2005-10-19 08:11:21 +02:00
|
|
|
// add additional input handlers only needed by this controller:
|
2005-08-15 01:34:37 +02:00
|
|
|
|
2005-10-19 08:11:21 +02:00
|
|
|
// must be registered after gui_handler. Should mayhap even be last.
|
2005-08-15 01:34:37 +02:00
|
|
|
in_add_handler(MainInputHandler);
|
2004-07-12 18:50:46 +02:00
|
|
|
}
|
2004-05-24 22:25:48 +02:00
|
|
|
|
2004-07-15 21:12:54 +02:00
|
|
|
|
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
static void MainControllerShutdown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-13 20:05:55 +02:00
|
|
|
// stop the main loop and trigger orderly shutdown. called from several
|
|
|
|
// places: the event handler (SDL_QUIT and hotkey) and JS exitProgram.
|
2005-08-15 01:34:37 +02:00
|
|
|
void kill_mainloop()
|
|
|
|
{
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-07 23:47:31 +02:00
|
|
|
static bool restart_in_atlas = false;
|
|
|
|
// called by game code to indicate main() should restart in Atlas mode
|
|
|
|
// instead of terminating
|
|
|
|
void restart_mainloop_in_atlas()
|
|
|
|
{
|
|
|
|
quit = true;
|
|
|
|
restart_in_atlas = true;
|
|
|
|
}
|
|
|
|
|
2007-05-05 19:41:20 +02:00
|
|
|
// moved into a helper function to ensure args is destroyed before
|
|
|
|
// exit(), which may result in a memory leak.
|
2008-07-17 19:00:00 +02:00
|
|
|
static void RunGameOrAtlas(int argc, const char* argv[])
|
2004-07-12 18:50:46 +02:00
|
|
|
{
|
2007-05-04 19:30:32 +02:00
|
|
|
CmdLineArgs args(argc, argv);
|
2005-01-25 19:56:43 +01:00
|
|
|
|
2009-03-24 01:55:35 +01:00
|
|
|
// We need to initialise libxml2 in the main thread before
|
|
|
|
// any thread uses it. So initialise it here before we
|
|
|
|
// might run Atlas.
|
|
|
|
CXeromyces::Startup();
|
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
// run Atlas (if requested via args)
|
2010-07-07 23:47:31 +02:00
|
|
|
bool ran_atlas = ATLAS_RunIfOnCmdLine(args, false);
|
2007-05-05 19:41:20 +02:00
|
|
|
// Atlas handles the whole init/shutdown/etc sequence by itself;
|
|
|
|
// when we get here, it has exited and we're done.
|
2007-05-04 19:30:32 +02:00
|
|
|
if(ran_atlas)
|
2007-05-05 19:41:20 +02:00
|
|
|
return;
|
2006-04-10 23:49:12 +02:00
|
|
|
|
2010-08-07 00:16:05 +02:00
|
|
|
// run non-visual simulation replay if requested
|
|
|
|
if (args.Has("replay"))
|
|
|
|
{
|
2010-09-03 11:50:16 +02:00
|
|
|
snd_disable(true);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
|
|
|
Paths paths(args);
|
|
|
|
g_VFS = CreateVfs(20 * MiB);
|
|
|
|
g_VFS->Mount(L"cache/", paths.Cache(), VFS_MOUNT_ARCHIVABLE);
|
2011-03-23 14:36:20 +01:00
|
|
|
g_VFS->Mount(L"", paths.RData()/"mods/public", VFS_MOUNT_MUST_EXIST);
|
2010-08-07 00:16:05 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
CReplayPlayer replay;
|
|
|
|
replay.Load(args.Get("replay"));
|
|
|
|
replay.Replay();
|
|
|
|
}
|
|
|
|
|
|
|
|
g_VFS.reset();
|
|
|
|
|
|
|
|
CXeromyces::Terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-18 20:21:00 +02:00
|
|
|
// run in archive-building mode if requested
|
|
|
|
if (args.Has("archivebuild"))
|
|
|
|
{
|
|
|
|
Paths paths(args);
|
|
|
|
|
2011-03-23 14:36:20 +01:00
|
|
|
OsPath mod(args.Get("archivebuild"));
|
|
|
|
OsPath zip;
|
2010-09-18 20:21:00 +02:00
|
|
|
if (args.Has("archivebuild-output"))
|
2011-03-23 14:36:20 +01:00
|
|
|
zip = args.Get("archivebuild-output");
|
2010-09-18 20:21:00 +02:00
|
|
|
else
|
2011-03-23 14:36:20 +01:00
|
|
|
zip = mod.Filename().ChangeExtension(L".zip");
|
2010-09-18 20:21:00 +02:00
|
|
|
|
|
|
|
CArchiveBuilder builder(mod, paths.Cache());
|
|
|
|
builder.Build(zip);
|
|
|
|
|
|
|
|
CXeromyces::Terminate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-07-05 10:27:33 +02:00
|
|
|
const double res = timer_Resolution();
|
|
|
|
g_frequencyFilter = CreateFrequencyFilter(res, 30.0);
|
|
|
|
|
2007-05-04 19:30:32 +02:00
|
|
|
// run the game
|
|
|
|
Init(args, 0);
|
2010-10-22 01:44:54 +02:00
|
|
|
InitGraphics(args, 0);
|
2007-05-04 19:30:32 +02:00
|
|
|
MainControllerInit();
|
|
|
|
while(!quit)
|
|
|
|
Frame();
|
|
|
|
Shutdown(0);
|
2007-06-03 17:37:26 +02:00
|
|
|
ScriptingHost::FinalShutdown(); // this can't go in Shutdown() because that could be called multiple times per process, so stick it here instead
|
2007-05-04 19:30:32 +02:00
|
|
|
MainControllerShutdown();
|
2009-03-24 01:55:35 +01:00
|
|
|
|
2010-07-07 23:47:31 +02:00
|
|
|
if (restart_in_atlas)
|
|
|
|
{
|
|
|
|
ATLAS_RunIfOnCmdLine(args, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-03-24 01:55:35 +01:00
|
|
|
// Shut down libxml2 (done here to match the Startup call)
|
|
|
|
CXeromyces::Terminate();
|
2007-05-05 19:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2007-05-27 01:29:20 +02:00
|
|
|
EarlyInit(); // must come at beginning of main
|
2007-05-05 19:41:20 +02:00
|
|
|
|
2008-07-17 19:00:00 +02:00
|
|
|
RunGameOrAtlas(argc, const_cast<const char**>(argv));
|
2004-07-10 16:14:36 +02:00
|
|
|
|
2010-05-14 23:15:33 +02:00
|
|
|
return EXIT_SUCCESS;
|
2004-05-24 22:25:48 +02:00
|
|
|
}
|