1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #include "main.h"
13 
14 #include <string>
15 #include <cstdlib>
16 
17 #include "game.h"
18 #include "main_menu.h"
19 #include "program.h"
20 #include "config.h"
21 #include "metrics.h"
22 #include "game_util.h"
23 #include "platform_util.h"
24 #include "platform_main.h"
25 #include "leak_dumper.h"
26 
27 using namespace std;
28 using namespace Shared::Platform;
29 using namespace Shared::Util;
30 
31 namespace Glest{ namespace Game{
32 
33 // =====================================================
34 // 	class ExceptionHandler
35 // =====================================================
36 
37 class ExceptionHandler: public PlatformExceptionHandler{
38 public:
handle()39 	virtual void handle(){
40 		message("An error ocurred and Glest will close.\nPlease report this bug to "+mailString+", attaching the generated "+getCrashDumpFileName()+" file.");
41 	}
42 };
43 
44 // =====================================================
45 // 	class MainWindow
46 // =====================================================
47 
MainWindow(Program * program)48 MainWindow::MainWindow(Program *program){
49 	this->program= program;
50 }
51 
~MainWindow()52 MainWindow::~MainWindow(){
53 	delete program;
54 }
55 
eventMouseDown(int x,int y,MouseButton mouseButton)56 void MainWindow::eventMouseDown(int x, int y, MouseButton mouseButton){
57 	switch(mouseButton){
58 	case mbLeft:
59 		program->mouseDownLeft(x, getH() - y);
60 		break;
61 	case mbRight:
62 		program->mouseDownRight(x, getH() - y);
63 		break;
64 	default:
65 		break;
66 	}
67 }
68 
eventMouseUp(int x,int y,MouseButton mouseButton)69 void MainWindow::eventMouseUp(int x, int y, MouseButton mouseButton){
70 	if(mouseButton==mbLeft){
71 		program->mouseUpLeft(x, getH() - y);
72 	}
73 }
74 
eventMouseDoubleClick(int x,int y,MouseButton mouseButton)75 void MainWindow::eventMouseDoubleClick(int x, int y, MouseButton mouseButton){
76 	if(mouseButton == mbLeft){
77 		program->mouseDoubleClickLeft(x,  getH() - y);
78 	}
79 }
80 
eventMouseMove(int x,int y,const MouseState * ms)81 void MainWindow::eventMouseMove(int x, int y, const MouseState *ms){
82 	program->mouseMove(x, getH() - y, ms);
83 }
84 
eventKeyDown(char key)85 void MainWindow::eventKeyDown(char key){
86 	program->keyDown(key);
87 }
88 
eventKeyUp(char key)89 void MainWindow::eventKeyUp(char key){
90 	program->keyUp(key);
91 }
92 
eventKeyPress(char c)93 void MainWindow::eventKeyPress(char c){
94 	program->keyPress(c);
95 }
96 
eventActivate(bool active)97 void MainWindow::eventActivate(bool active){
98 	if(!active){
99 		//minimize();
100 	}
101 }
102 
eventResize(SizeState sizeState)103 void MainWindow::eventResize(SizeState sizeState){
104 	program->resize(sizeState);
105 }
106 
eventClose()107 void MainWindow::eventClose(){
108 	delete program;
109 	program= NULL;
110 }
111 
112 // =====================================================
113 // Main
114 // =====================================================
115 
glestMain(int argc,char ** argv)116 int glestMain(int argc, char** argv){
117 
118 	MainWindow *mainWindow= NULL;
119 	Program *program= NULL;
120 	ExceptionHandler exceptionHandler;
121 	exceptionHandler.install( getCrashDumpFileName() );
122 
123 	try{
124 		Config &config = Config::getInstance();
125 
126 		showCursor(config.getBool("Windowed"));
127 
128 		program= new Program();
129 		mainWindow= new MainWindow(program);
130 
131 		//parse command line
132 		if(argc==2 && string(argv[1])=="-server"){
133 			program->initServer(mainWindow);
134 		}
135 		else if(argc==3 && string(argv[1])=="-client"){
136 			program->initClient(mainWindow, Ip(argv[2]));
137 		}
138 		else{
139 			program->initNormal(mainWindow);
140 		}
141 
142 		//main loop
143 		while(Window::handleEvent()){
144 			program->loop();
145 		}
146 	}
147 	catch(const exception &e){
148 		restoreVideoMode();
149 		exceptionMessage(e);
150 	}
151 
152 	delete mainWindow;
153 
154 	return 0;
155 }
156 
157 }}//end namespace
158 
159 MAIN_FUNCTION(Glest::Game::glestMain)
160