1
0
forked from 0ad/0ad
0ad/source/tools/atlas/GameInterface/GameLoop.cpp
Ykkrosh 549150fe38 Atlas: simple camera control and terrain editing.
Terrain: added terrain-editing code to CTerrain, for better
encapsulation or something.
Console: simplified screen resizing.
Game/etc: removed some unnecessary header file inclusions.

This was SVN commit r2459.
2005-07-03 16:25:48 +00:00

179 lines
4.4 KiB
C++

#include "precompiled.h"
#include "GameLoop.h"
#include "MessagePasserImpl.h"
#include "Messages.h"
#include "handlers/MessageHandler.h"
#include "InputProcessor.h"
#include "lib/sdl.h"
#include "lib/ogl.h"
#include "lib/timer.h"
#include "ps/CLogger.h"
#ifdef NDEBUG
#pragma comment(lib, "AtlasUI")
#else
#pragma comment(lib, "AtlasUI_d")
#endif
using namespace AtlasMessage;
extern __declspec(dllimport) void Atlas_StartWindow(wchar_t* type);
extern __declspec(dllimport) void Atlas_SetMessagePasser(MessagePasser<mCommand>*, MessagePasser<mInput>*);
extern void Render_();
extern "C" { __declspec(dllimport) int __stdcall SwapBuffers(void*); }
// HACK (and not exactly portable)
//
// (Er, actually that's what most of this file is. Oh well.)
static MessagePasserImpl<mCommand> msgPasser_Command;
static MessagePasserImpl<mInput> msgPasser_Input;
static InputProcessor g_Input;
static GameLoopState state;
GameLoopState* g_GameLoop = &state;
static void* LaunchWindow(void*)
{
Atlas_StartWindow(L"ScenarioEditor");
return NULL;
}
void BeginAtlas(int argc, char** argv)
{
// Pass our message handler to Atlas
Atlas_SetMessagePasser(&msgPasser_Command, &msgPasser_Input);
// Create a new thread, and launch the Atlas window inside that thread
pthread_t gameThread;
pthread_create(&gameThread, NULL, LaunchWindow, NULL);
state.argc = argc;
state.argv = argv;
state.running = true;
state.rendering = false;
state.currentDC = NULL;
while (state.running)
{
bool recent_activity = false;
//////////////////////////////////////////////////////////////////////////
// (TODO: Work out why these things have to be in this order (to avoid
// jumps when starting to move, etc)
// Calculate frame length
{
const double time = get_time();
static double last_time = time;
const float length = (float)(time-last_time);
last_time = time;
assert(length >= 0.0f);
// TODO: filter out big jumps, e.g. when having done a lot of slow
// processing in the last frame
state.frameLength = length;
}
if (g_Input.ProcessInput(&state))
recent_activity = true;
//////////////////////////////////////////////////////////////////////////
// if (!(in interactive-tool mode))
{
mCommand* msg;
while (msg = msgPasser_Command.Retrieve())
{
recent_activity = true;
std::string name (msg->GetType());
if (name == "CommandString")
{
// Allow some laziness: For commands that don't need any data other
// than their name, we just use CommandString (and then need to
// construct a reference to the appropriate handler for the
// given string)
name += "_";
name += static_cast<mCommandString*>(msg)->name;
// use 'static_cast' when casting messages, to make it clear
// that it's slightly dangerous - we have to just assume that
// GetType is correct, since we can't use proper RTTI
}
handlers::const_iterator it = GetHandlers().find(name);
if (it != GetHandlers().end())
{
it->second(msg);
}
else
{
debug_warn("Unrecognised message");
// TODO: CLogger might not be initialised
LOG(ERROR, "atlas", "Unrecognised message (%s)", name.c_str());
}
delete msg;
}
}
// Exit, if desired
if (! state.running)
break;
// Now do the same (roughly), for input events:
{
mInput* msg;
while (msg = msgPasser_Input.Retrieve())
{
recent_activity = true;
std::string name (msg->GetType());
handlers::const_iterator it = GetHandlers().find(name);
if (it != GetHandlers().end())
{
it->second(msg);
}
else
{
debug_warn("Unrecognised message");
// TODO: CLogger might not be initialised
LOG(ERROR, "atlas", "Unrecognised message (%s)", name.c_str());
}
delete msg;
}
}
//////////////////////////////////////////////////////////////////////////
if (state.rendering)
{
Render_();
glFinish();
SwapBuffers((void*)state.currentDC);
}
// Be nice to the processor if we're not doing anything useful, but
// nice to the user if we are
if (! recent_activity)
SDL_Delay(100);
else
SDL_Delay(0);
// Probable TODO: allow interruption of sleep by incoming messages
}
// TODO: delete all remaining messages, to avoid memory leak warnings
pthread_join(gameThread, NULL);
}