]> Dogcows Code - chaz/yoink/blob - src/Main.cc
91a8d080ba04d14df9114e8a112f3ba179e4a63c
[chaz/yoink] / src / Main.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstdlib> // atexit, getenv
13 #include <iostream>
14 #include <string>
15 #include <unistd.h> // access
16
17 #include <Moof/Log.hh>
18 #include <Moof/ModalDialog.hh>
19 #include <Moof/OpenGL.hh>
20 #include <Moof/Resource.hh>
21 #include <Moof/Settings.hh>
22 #include <Moof/Transition.hh>
23 #include <Moof/Video.hh>
24
25 #include "ErrorHandler.hh"
26 #include "GameLayer.hh"
27 #include "Main.hh"
28 #include "TitleLayer.hh"
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "version.h"
34
35
36 Main::Main()
37 {
38 mDispatchHandler = Mf::core.addHandler("video.newcontext",
39 boost::bind(&Main::contextCreated));
40 setupGL();
41 }
42
43 void Main::addedToCore()
44 {
45 //Mf::Scalar coeff[] = {0.0, 1.0};
46 //Mf::Lerp interp(coeff, 0.25);
47
48 //Mf::LayerP gameLayer = GameLayer::alloc();
49 //Mf::Transition<Mf::Lerp>::Ptr transition =
50 //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
51 //core.push(transition);
52 //core.push(GameLayer::alloc());
53 Mf::core.push(TitleLayer::alloc());
54 }
55
56
57 void Main::update(Mf::Scalar t, Mf::Scalar dt)
58 {
59 if (Mf::core.getSize() == 1)
60 {
61 // this is the only layer left on the stack
62 //Mf::core.push(TitleLayer::alloc());
63 Mf::core.clear();
64 }
65 }
66
67 void Main::draw(Mf::Scalar alpha) const
68 {
69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70
71 glMatrixMode(GL_PROJECTION);
72 glLoadIdentity();
73
74 glMatrixMode(GL_MODELVIEW);
75 glLoadIdentity();
76 }
77
78 bool Main::handleEvent(const Mf::Event& event)
79 {
80 switch (event.type)
81 {
82 case SDL_KEYUP:
83 if (event.key.keysym.sym == SDLK_ESCAPE)
84 {
85 Mf::core.clear();
86 }
87 else if (event.key.keysym.sym == SDLK_f)
88 {
89 Mf::video->toggleFull();
90 }
91 else if (event.key.keysym.sym == SDLK_l)
92 {
93 Mf::video->toggleCursorGrab();
94 Mf::video->toggleCursorVisible();
95 }
96 break;
97
98 case SDL_VIDEORESIZE:
99 glViewport(0, 0, event.resize.w, event.resize.h);
100 break;
101
102 case SDL_QUIT:
103 Mf::core.clear();
104 break;
105 }
106
107 return false;
108 }
109
110
111 std::string Main::getSearchPath()
112 {
113 // Add search paths; they should be searched in this order:
114 // 1. YOINK_DATADIR (environment)
115 // 2. YOINK_DATADIR (configure)
116
117 std::string path;
118
119 char* dataDir = getenv("YOINK_DATADIR");
120 if (dataDir)
121 {
122 path += dataDir;
123 path += ":";
124 }
125 path += YOINK_DATADIR;
126
127 return path;
128 }
129
130 std::string Main::getConfigPath()
131 {
132 // Build the list of config files to search for, in this order:
133 // 1. YOINK_DATADIR/yoinkrc
134 // 2. /etc/yoinkrc (not for Windows)
135 // 3. $HOME/.yoinkrc
136 // 4. YOINKRC (environment)
137
138 std::string path = Mf::Resource::getPath("yoinkrc");
139
140 #if !defined(_WIN32) && !defined(__WIN32__)
141 path += ":/etc/yoinkrc";
142 #endif
143 path += ":$HOME/.yoinkrc";
144
145 char* configFile = getenv("YOINKRC");
146 if (configFile)
147 {
148 path += ":";
149 path += configFile;
150 }
151
152 return path;
153 }
154
155
156 void Main::setupGL()
157 {
158 glEnable(GL_TEXTURE_2D);
159 glEnable(GL_DEPTH_TEST);
160
161 glEnable(GL_LINE_SMOOTH);
162 glEnable(GL_POLYGON_SMOOTH);
163 glShadeModel(GL_SMOOTH);
164
165 //glEnable(GL_BLEND);
166 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167 glEnable(GL_ALPHA_TEST);
168 glAlphaFunc(GL_GREATER, 0.0);
169
170 glClearColor(0.0, 0.0, 0.0, 1.0);
171
172 //glMatrixMode(GL_PROJECTION);
173 //glLoadIdentity();
174 //Mf::Scalar ratio = Mf::core.getVideo()->getWidth() /
175 //Mf::core.getVideo()->getHeight();
176 //gluPerspective(60.0, ratio, 1.0, 250.0);
177
178 //glMatrixMode(GL_MODELVIEW);
179 }
180
181 void Main::contextCreated()
182 {
183 // whenever the context is destroyed and a new one created, it probably
184 // won't contain our state so we need to set that up again
185 setupGL();
186 }
187
188
189
190 void Main::printUsage()
191 {
192 std::cout << "Usage: "
193 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
194 << std::endl
195 << "The alien-smashing action game." << std::endl
196 << std::endl
197 << "Options:" << std::endl
198 << " -h, --help" << std::endl
199 << " show this help and exit" << std::endl
200 << " -i, --info" << std::endl
201 << " show version and build information" << std::endl
202 << " detail=1|2|3" << std::endl
203 << " the level of detail of game scenes" << std::endl
204 << " fullscreen=true|false" << std::endl
205 << " if true, uses the entire display" << std::endl
206 << " framerate=num" << std::endl
207 << " number of frames to draw per second" << std::endl
208 << std::endl
209 << "See documentation for more options." << std::endl;
210 }
211
212 void Main::printInfo(int argc, char* argv[])
213 {
214 std::string assets;
215 std::string datadir;
216 std::string config;
217
218 assets.assign(YOINK_DATADIR);
219 int accessible = access(assets.c_str(), R_OK);
220 if (accessible != 0) assets += " (no access)";
221
222 char* temp = getenv("YOINK_DATADIR");
223 if (temp)
224 {
225 datadir = temp;
226 accessible = access(temp, R_OK);
227 if (accessible != 0) datadir += " (no access)";
228 }
229
230 temp = getenv("YOINKRC");
231 if (temp)
232 {
233 config = temp;
234 accessible = access(temp, R_OK);
235 if (accessible != 0) config += " (no access)";
236 }
237
238 std::cout << " Executable: " << argv[0] << std::endl
239 << " Version: "VERSION << std::endl
240 << " Built: " << COMPILE_TIME << std::endl
241 << " Compiler: "COMPILER_STRING << std::endl
242 << " Assets: " << assets << std::endl
243 << "Build options: "
244 #ifdef NDEBUG
245 << "-"
246 #endif
247 << "debug "
248 #ifndef USE_DOUBLE_PRECISION
249 << "-"
250 #endif
251 << "double-precision "
252 #ifndef USE_GTK
253 << "-"
254 #endif
255 << "gtk "
256 #ifndef PROFILING_ENABLED
257 << "-"
258 #endif
259 << "profile "
260 #ifndef USE_QT4
261 << "-"
262 #endif
263 << "qt4 "
264 #ifndef USE_THREADS
265 << "-"
266 #endif
267 << "threads" << std::endl
268 << " YOINKRC: " << config << std::endl
269 << "YOINK_DATADIR: " << datadir << std::endl;
270 }
271
272
273 void hello()
274 {
275 std::cout << std::endl << PACKAGE_STRING << std::endl
276 << "Compiled " << __TIME__ " " __DATE__ << std::endl
277 << "Send patches and bug reports to <"
278 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
279 }
280
281 void goodbye()
282 {
283 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
284 }
285
286 int main(int argc, char* argv[])
287 {
288 if (argc > 1)
289 {
290 std::string arg(argv[1]);
291 if (arg == "-h" || arg == "--help")
292 {
293 Main::printUsage();
294 return 0;
295 }
296 else if (arg == "-i" || arg == "--info")
297 {
298 Main::printInfo(argc, argv);
299 return 0;
300 }
301 }
302
303 hello();
304 atexit(goodbye);
305
306 Mf::Resource::addSearchPaths(Main::getSearchPath());
307
308 Mf::settings.loadFromFiles(Main::getConfigPath());
309 Mf::settings.parseArgs(argc, argv);
310
311 Mf::Log::Level logLevel = Mf::Log::INFO;
312 Mf::settings.get("loglevel", logLevel);
313 Mf::Log::setLevel(logLevel);
314
315 try
316 {
317 Mf::Video video(PACKAGE_STRING,
318 Mf::Resource::getPath(PACKAGE".png"));
319 MainP app = Main::alloc();
320 Mf::core.push(app);
321 Mf::core.run();
322 }
323 catch (const Mf::Error& error)
324 {
325 Mf::ModalDialog dialog;
326 dialog.title = PACKAGE_STRING;
327 dialog.text1 = "Unhandled Exception";
328 dialog.text2 = getErrorString(error);
329 dialog.type = Mf::ModalDialog::CRITICAL;
330 dialog.run();
331
332 return 1;
333 }
334
335 return 0;
336 }
337
This page took 0.047024 seconds and 3 git commands to generate.