// vim:ts=2 sw=2
#include "uop.h"
#include <SDL/SDL.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include "audio.h"
#include "gumps.h"
#include "interface.h"
#include "cliloc.h"
#include "texthandler.h"
#include "tiledata.h"
#include "uopconfig.h"
#include "network.h"
#include "action.h"
#include "animation.h"
#include "serverhandler.h"
#include "interfacehandler.h"
#include "patchhandler.h"
#include "message.h"
#include "world.h"
SDL_Surface *screen;
Audio *audio;
Gumps *gumps;
Interface *intrface;
vector<Cliloc *>intlocs;
vector<Cliloc *>clilocs;
Cliloc *profession;
TextHandler *textHandler;
TileData *tileData;
UOPConfig *config;
Network *network;
Action *action;
ServerHandler *serverHandler;
InterfaceHandler *interfaceHandler;
PatchHandler *patchHandler;
World *world;
Animations *anims;
void onExit();
int main(int argc,char **argv)
{
ifstream f;
//Uint32 offset,object;
//SDL_Event event;
Uint32 ticks,lastUpdate=0;
Uint8 act[1];
bool updateAction;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
cerr << "Unable to initialize SDL: " << SDL_GetError() << endl;
exit(1);
}
atexit(&onExit);
screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
if (screen == NULL)
{
cerr << "Unable to set video mode: " << SDL_GetError() << endl;
exit(2);
}
SDL_WM_SetCaption("Ultima Online Project", NULL);
#ifdef WIN32
config = new WindowsConfig;
#else
config = new TextConfig;
#endif
config->loadKeys();
network=new Network();
serverHandler=new ServerHandler();
action=new Action();
tileData=new TileData();
audio=new Audio();
gumps=new Gumps();
anims=new Animations();
int i;
for (i=0;i<=10;i++)
{
string name="intloc";
if (i<10) name+='0'; else name+=('0'+(i/10));
name+=('0'+(i%10));
intlocs.push_back(new Cliloc(name));
}
for (i=0;i<=52;i++)
{
string name="cliloc";
if (i<10) name+='0'; else name+=('0'+(i/10));
name+=('0'+(i%10));
clilocs.push_back(new Cliloc(name));
}
profession=new Cliloc("professn");
textHandler=new TextHandler();
intrface=new Interface();
interfaceHandler=new InterfaceHandler();
patchHandler=new PatchHandler();
world=new World();
patchHandler->load();
intrface->loadDef();
audio->start();
interfaceHandler->addGump("accountlogin");
updateAction=true;
try{
while (true)
{
updateAction=interfaceHandler->handleEvents();
ticks=SDL_GetTicks();
if (ticks-lastUpdate>50 || updateAction)
{
updateAction=false;
lastUpdate=ticks;
SDL_LockSurface(screen);
interfaceHandler->draw();
Message::drawMessageStack();
SDL_UnlockSurface(screen);
SDL_UpdateRect(screen,0,0,640,480);
}
SDL_Delay(10);
if (network->ready())
{
if (network->read(act,1)!=-1)
serverHandler->handlePacket(act[0]);
}
}
}catch (string s){
cerr << "Unexpected exception:" << endl;
cerr << s << endl;
throw;
//exit(1);
}
}
void onExit()
{
if (config)
config->saveKeys();
SDL_Quit();
}