00001 #include <stdlib.h>
00002 #include <list>
00003 #include "tnl/tnl.h"
00004 #include "tnl/tnlLog.h"
00005 #include "opengl.h"
00006 #include "net/MMNet.h"
00007 #include "audio/audio.h"
00008
00009 #include "resources/resources.h"
00010 #include "input/Input.h"
00011
00012 #include "gamestate.h"
00013 #include "mainmenu.h"
00014
00015 #include "exception.h"
00016 #include <iostream>
00017 #include <assert.h>
00018
00019 #include "sounds.h"
00020 #include "textures.h"
00021 #include "animations.h"
00022 #include "client.h"
00023 #include "actors.h"
00024 #include "Music.h"
00025
00026 #define CONNECTION_ATTEMPT_TIME 10000
00027
00028 MainMenu* MAIN_MENU;
00029 static GameState *curGameState;
00030 static GameState *nextGameState = NULL;
00031 static bool switchGameState = false;
00032
00033 using namespace MMNet;
00034
00035 NetClient *client;
00036
00037 MMAudio::Source *arrowSources;
00038 std::list<int> freeArrowSources;
00039
00040 static void _setGameState();
00041
00042 static void update() {
00043
00044 try {
00045
00046
00047 if ( switchGameState) {
00048 _setGameState();
00049 switchGameState = false;
00050 }
00051
00052 assert(curGameState != NULL);
00053 curGameState->update();
00054
00055
00056 } catch (Exception &ex) {
00057 std::cerr << ex;
00058 throw;
00059 }
00060 }
00061
00062 static void display() {
00063 if (curGameState)
00064 curGameState->display();
00065 }
00066
00067 void setGameState(GameState *gs) {
00068 nextGameState = gs;
00069 switchGameState = true;
00070 }
00071
00072 static void _setGameState() {
00073 try {
00074 if (curGameState != NULL) {
00075 curGameState->exit();
00076 }
00077
00078
00079 if(nextGameState == NULL) {
00080 curGameState = MAIN_MENU;
00081 } else {
00082 curGameState = nextGameState;
00083 }
00084
00085 assert(curGameState != NULL);
00086 curGameState->init();
00087
00088 } catch (Exception &ex) {
00089 std::cerr << ex;
00090 throw;
00091 }
00092 }
00093
00094
00095 static void initGlut(int argc, char **argv) {
00096 glutInit(&argc, argv);
00097 glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
00098 glutInitWindowSize(MAX_SCREEN_X,MAX_SCREEN_Y);
00099 glutCreateWindow("Robin Hood - Thieves & Knights");
00100 glutDisplayFunc(display);
00101 glutIdleFunc(update);
00102 glutFullScreen();
00103 }
00104
00105 static void initInput(void) {
00106 InputInit();
00107 glutMouseFunc(InputSystemMouseButton);
00108 glutMotionFunc(InputSystemMouseMotion);
00109 glutPassiveMotionFunc(InputSystemMouseMotion);
00110 glutKeyboardFunc(InputSystemKeyDown);
00111 glutKeyboardUpFunc(InputSystemKeyUp);
00112 }
00113
00114 static void initNetwork(void) {
00115 using namespace TNL;
00116
00117 client = new NetClient(Address(IPProtocol, Address::Any), NULL);
00118 client->start();
00119 }
00120
00121 static void initEngine(int argc, char **argv) {
00122
00123 resInit(argv[0]);
00124
00125
00126 initGlut(argc, argv);
00127 initInput();
00128 MMAudio::audioInit();
00129 arrowSources = new MMAudio::Source[10];
00130 for (int i = 0; i < 10; i++)
00131 freeArrowSources.push_back(i);
00132 initAnimations();
00133 initSounds();
00134 initTextures();
00135 GameLoadActors();
00136 atexit(stopMusic);
00137 }
00138
00139 class StderrLogConsumer : public TNL::LogConsumer
00140 {
00141 public:
00142 void logString(const char *string)
00143 {
00144
00145 }
00146 };
00147
00148
00149 int main(int argc, char ** argv) {
00150 StderrLogConsumer logger;
00151 TNLLogEnable(LogGhostConnection, true);
00152 TNLLogEnable(LogEventConnection, true);
00153
00154 initEngine(argc, argv);
00155 initNetwork();
00156
00157 MAIN_MENU = new MainMenu(client);
00158 setGameState(MAIN_MENU);
00159
00160 try {
00161 glutMainLoop();
00162 } catch (Exception &ex) {
00163 std::cerr << "\nFatal Exception:\n" << ex;
00164 return 1;
00165 }
00166
00167 return 0;
00168 }