1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // $Id: main.cpp 3691 2013-02-23 20:00:10Z hypereye $
5 //
6 // Copyright (C) 2006-2012 by The Odamex Team.
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // DESCRIPTION:
19 //	Main
20 //
21 // AUTHORS:
22 //  Michael Wood (mwoodj at huntsvegas dot org)
23 //
24 //-----------------------------------------------------------------------------
25 
26 #include <iostream>
27 #include <sstream>
28 
29 #include <agar/core.h>
30 #include <agar/gui.h>
31 
32 #ifdef AG_DEBUG
33 #include <agar/dev.h>
34 #endif
35 
36 #include <SDL/SDL.h>
37 
38 #include "agol_main.h"
39 #include "net_io.h"
40 #include "gui_config.h"
41 
42 #ifdef _XBOX
43 #include "xbox_main.h"
44 #endif
45 
46 using namespace std;
47 
48 namespace agOdalaunch {
49 
50 #ifdef AG_DEBUG
AGOL_StartDebugger(void)51 static void AGOL_StartDebugger(void)
52 {
53 	AG_Window *win;
54 
55 	if((win = (AG_Window*)AG_GuiDebugger(agWindowFocused)) != NULL)
56 	{
57 		AG_WindowShow(win);
58 	}
59 }
60 #endif
61 
AGOL_InitVideo(const string & drivers,const int width,const int height,const short depth)62 int AGOL_InitVideo(const string& drivers, const int width, const int height, const short depth)
63 {
64 	ostringstream spec;
65 
66 	cout << "Initializing with resolution (" << width << "x" << height << ")..." << endl;
67 
68 	/* Initialize Agar-GUI. */
69 	if(drivers.size())
70 	{
71 		spec << drivers;
72 	}
73 	else
74 	{
75 		spec << "<OpenGL>";
76 	}
77 
78 	spec << "(width=" << width << ":height=" << height << ":depth=" << depth << ")";
79 
80 	if (AG_InitGraphics(spec.str().c_str()) == -1)
81 	{
82 		cerr << AG_GetError() << endl;
83 		return -1;
84 	}
85 
86 #ifdef _XBOX
87 	// Software cursor only updates at the refresh rate so make it respectable
88 	if(agDriverSw)
89 	{
90 		AG_SetRefreshRate(60);
91 	}
92 #endif
93 
94 	// Pick up GUI subsystem options
95 	GuiConfig::Load();
96 
97 	return 0;
98 }
99 
100 } // namespace
101 
102 using namespace agOdalaunch;
103 
104 #ifdef GCONSOLE
agol_main(int argc,char * argv[])105 int agol_main(int argc, char *argv[])
106 #else
107 int main(int argc, char *argv[])
108 #endif
109 {
110 	AGOL_MainWindow *mainWindow;
111 	string           drivers;
112 	char            *optArg;
113 	int              c;
114 	int              width, height;
115 
116 	/* Initialize Agar-Core. */
117 	if (AG_InitCore("ag-odalaunch", AG_VERBOSE | AG_CREATE_DATADIR) == -1)
118 	{
119 		cerr << AG_GetError() << endl;
120 		return (-1);
121 	}
122 
123 	// Initial config load
124 	GuiConfig::Load();
125 
126 	while ((c = AG_Getopt(argc, argv, "?d:f", &optArg, NULL)) != -1)
127 	{
128 		switch (c)
129 		{
130 			case 'd':
131 				drivers = optArg;
132 				break;
133 			case 'f':
134 				/* Force full screen */
135 				GuiConfig::Write("view.full-screen", true);
136 				break;
137 			case '?':
138 			default:
139 				cout << agProgName << "[-df] [-d driver] " << endl;
140 				exit(0);
141 		}
142 	}
143 
144 	// Set the default font size
145 	if(!GuiConfig::IsDefined("font.size"))
146 	{
147 		GuiConfig::Write("font.size", 10);
148 	}
149 
150 #ifdef GCONSOLE
151 	// For now just use a resolution that compensates for overscan on most televisions
152 	width = 600;
153 	height = 450;
154 #else
155 	// Get the dimensions for initialization
156 	if(GuiConfig::Read("MainWindow-Width", width) || width <= 0)
157 	{
158 		width = 640;
159 	}
160 	if(GuiConfig::Read("MainWindow-Height", height) || height <= 0)
161 	{
162 		height = 480;
163 	}
164 #endif
165 
166 	// Check if a video driver is specified in the config file
167 	if(!drivers.size())
168 	{
169 		GuiConfig::Read("VideoDriver", drivers);
170 
171 #ifdef _XBOX
172 		if(!drivers.size())
173 		{
174 			drivers = "sdlfb";
175 		}
176 #endif
177 	}
178 
179 	if(AGOL_InitVideo(drivers, width, height, 32))
180 	{
181 		return (-1);
182 	}
183 
184 	// Initialize socket API
185 	BufferedSocket::InitializeSocketAPI();
186 
187 	// Create the main window
188 	mainWindow = new AGOL_MainWindow(width, height);
189 
190 	// Set key bindings
191 	AG_BindGlobalKey(AG_KEY_ESCAPE, AG_KEYMOD_ANY, AG_QuitGUI);
192 	AG_BindGlobalKey(AG_KEY_F8, AG_KEYMOD_ANY, AG_ViewCapture);
193 #ifdef AG_DEBUG
194 	AG_BindGlobalKey(AG_KEY_F12,    AG_KEYMOD_ANY,  AGOL_StartDebugger);
195 #endif
196 
197 #ifdef _XBOX
198 	// Initialize the Xbox controller
199 	Xbox::InitializeJoystick();
200 #endif
201 
202 	// Event (main) Loop
203 	AG_EventLoop();
204 
205 	delete mainWindow;
206 
207 	GuiConfig::Save();
208 
209 	AG_Destroy();
210 
211 	// Shutdown socket API
212 	BufferedSocket::ShutdownSocketAPI();
213 
214 	cout << "Exiting..." << endl;
215 
216 	return 0;
217 }
218 
219