]> Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
3429f52c5663f932b677407ab6c904a33d875c62
[chaz/yoink] / src / MainLayer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cstdlib> // atexit, getenv
30 #include <iostream>
31 #include <string>
32 #include <unistd.h> // access
33
34 #include <Moof/Dispatcher.hh>
35 #include <Moof/Exception.hh>
36 #include <Moof/Log.hh>
37 #include <Moof/ModalDialog.hh>
38 #include <Moof/OpenGL.hh>
39 #include <Moof/Resource.hh>
40 #include <Moof/Transition.hh>
41 #include <Moof/Video.hh>
42
43 #include "GameLayer.hh"
44 #include "MainLayer.hh"
45 #include "TitleLayer.hh"
46
47 #if HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50 #include "version.h"
51
52
53 MainLayer::MainLayer()
54 {
55 Mf::dispatcher::addHandler("video.context_recreated",
56 boost::bind(&MainLayer::contextRecreated, this, _1), this);
57 setupGL();
58 }
59
60 MainLayer::~MainLayer()
61 {
62 Mf::dispatcher::removeHandler(this);
63 }
64
65
66 void MainLayer::pushed(Mf::Engine& engine)
67 {
68 mEngine = &engine;
69
70 //Mf::Scalar coeff[] = {0.0, 1.0};
71 //Mf::Lerp interp(coeff, 0.25);
72
73 //Mf::LayerP gameLayer = GameLayer::alloc();
74 //Mf::Transition<Mf::Lerp>::Ptr transition =
75 //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
76 //engine->push(transition);
77 //engine->push(GameLayer::alloc());
78 mEngine->push(TitleLayer::alloc());
79 }
80
81
82 void MainLayer::draw(Mf::Scalar alpha) const
83 {
84 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
85
86 glMatrixMode(GL_PROJECTION);
87 glLoadIdentity();
88
89 glMatrixMode(GL_MODELVIEW);
90 glLoadIdentity();
91 }
92
93 bool MainLayer::handleEvent(const Mf::Event& event)
94 {
95 switch (event.type)
96 {
97 case SDL_KEYDOWN:
98 if (event.key.keysym.sym == SDLK_ESCAPE)
99 {
100 quit();
101 }
102 else if (event.key.keysym.sym == SDLK_f)
103 {
104 mEngine->getVideo().toggleFull();
105 }
106 else if (event.key.keysym.sym == SDLK_l)
107 {
108 Mf::Video& video = mEngine->getVideo();
109 video.toggleCursorGrab();
110 video.toggleCursorVisible();
111 }
112 else if (event.key.keysym.sym == SDLK_y)
113 {
114 mEngine->push(GameLayer::alloc());
115 }
116 break;
117
118 case SDL_VIDEORESIZE:
119 glViewport(0, 0, event.resize.w, event.resize.h);
120 break;
121
122 case SDL_QUIT:
123 quit();
124 break;
125 }
126
127 return false;
128 }
129
130 void MainLayer::quit()
131 {
132 #if NDEBUG
133 // we don't really need to unwind the stack and shut everything down because
134 // the operating system will take care of cleaning up
135 exit(0);
136 #else
137 mEngine->clear();
138 #endif
139 }
140
141
142 void MainLayer::setupGL()
143 {
144 glEnable(GL_TEXTURE_2D);
145 glEnable(GL_DEPTH_TEST);
146
147 glEnable(GL_LINE_SMOOTH);
148 glEnable(GL_POLYGON_SMOOTH);
149 glShadeModel(GL_SMOOTH);
150
151 //glEnable(GL_BLEND);
152 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
153 glEnable(GL_ALPHA_TEST);
154 glAlphaFunc(GL_GREATER, 0.0);
155
156 glClearColor(0.0, 0.0, 0.0, 1.0);
157
158 glMatrixMode(GL_PROJECTION);
159 glLoadIdentity();
160 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
161
162 glMatrixMode(GL_MODELVIEW);
163 }
164
165 void MainLayer::contextRecreated(const Mf::Notification* note)
166 {
167 // whenever the context is destroyed and a new one created, it probably
168 // won't contain our state so we need to set that up again
169 setupGL();
170 }
171
172
173
174 void printUsage()
175 {
176 std::cout << "Usage: "PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
177 << std::endl
178 << "The alien-smashing action game." << std::endl
179 << std::endl
180 << "Options:" << std::endl
181 << " -h, --help" << std::endl
182 << " show this help and exit" << std::endl
183 << " -i, --info" << std::endl
184 << " show version and build information" << std::endl
185 << " detail=1|2|3" << std::endl
186 << " the level of detail of game scenes" << std::endl
187 << " fullscreen=true|false" << std::endl
188 << " if true, uses the entire display" << std::endl
189 << " maxfps=num" << std::endl
190 << " the maximum number of frames per second" << std::endl
191 << std::endl
192 << "See documentation for more options." << std::endl;
193 }
194
195 void printInfo(int argc, char* argv[])
196 {
197 std::string assets;
198 std::string datadir;
199 std::string config;
200
201 assets.assign(YOINK_DATADIR);
202 int accessible = access(assets.c_str(), R_OK);
203 if (accessible != 0) assets += " (no access)";
204
205 char* temp = getenv("YOINK_DATADIR");
206 if (temp)
207 {
208 datadir = temp;
209 accessible = access(temp, R_OK);
210 if (accessible != 0) datadir += " (no access)";
211 }
212
213 temp = getenv("YOINKRC");
214 if (temp)
215 {
216 config = temp;
217 accessible = access(temp, R_OK);
218 if (accessible != 0) config += " (no access)";
219 }
220
221 std::cout << " Executable: " << argv[0] << std::endl
222 << " Version: "VERSION << std::endl
223 #if defined(__DATE__) && defined(__TIME__)
224 << " Built: "__DATE__" "__TIME__ << std::endl
225 #endif
226 << " Compiler: "COMPILER_STRING << std::endl
227 << " Assets: " << assets << std::endl
228 << "Build options: "
229 #ifdef NDEBUG
230 << "-"
231 #endif
232 << "debug "
233 #ifndef USE_DOUBLE_PRECISION
234 << "-"
235 #endif
236 << "double-precision "
237 #ifndef USE_GTK
238 << "-"
239 #endif
240 << "gtk "
241 #ifndef PROFILING_ENABLED
242 << "-"
243 #endif
244 << "profile "
245 #ifndef USE_QT4
246 << "-"
247 #endif
248 << "qt4 "
249 #ifndef USE_THREADS
250 << "-"
251 #endif
252 << "threads" << std::endl
253 << " YOINKRC: " << config << std::endl
254 << "YOINK_DATADIR: " << datadir << std::endl;
255 }
256
257 void goodbye()
258 {
259 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
260 }
261
262
263 int main(int argc, char* argv[])
264 {
265 if (argc > 1)
266 {
267 std::string arg(argv[1]);
268 if (arg == "-h" || arg == "--help")
269 {
270 printUsage();
271 return 0;
272 }
273 else if (arg == "-i" || arg == "--info")
274 {
275 printInfo(argc, argv);
276 return 0;
277 }
278 }
279
280 std::cout << std::endl << PACKAGE_STRING << std::endl
281 << "Compiled " << __TIME__ " " __DATE__ << std::endl
282 << "Send patches and bug reports to <"
283 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
284
285 atexit(goodbye);
286
287
288 #if YOINK_LOGLEVEL >= 4
289 Mf::setLogLevel(Mf::LOG_DEBUG);
290 #elif YOINK_LOGLEVEL >= 3
291 Mf::setLogLevel(Mf::LOG_INFO);
292 #elif YOINK_LOGLEVEL >= 2
293 Mf::setLogLevel(Mf::LOG_SCRIPT);
294 #elif YOINK_LOGLEVEL >= 1
295 Mf::setLogLevel(Mf::LOG_ERROR);
296 #elif YOINK_LOGLEVEL
297 Mf::setLogLevel(Mf::LOG_NONE);
298 #endif
299
300
301 // Add search paths; they should be searched in this order:
302 // 1. YOINK_DATADIR (environment)
303 // 2. YOINK_DATADIR (configure)
304
305 char* dataDir = getenv("YOINK_DATADIR");
306 if (dataDir)
307 {
308 Mf::Resource::addSearchPath(dataDir);
309 }
310
311 Mf::Resource::addSearchPath(YOINK_DATADIR);
312
313 std::string iconFile = Mf::Resource::getPath(PACKAGE".png");
314
315
316 // Build the list of config files to search for, in this order:
317 // 1. YOINK_DATADIR/yoinkrc
318 // 2. /etc/yoinkrc
319 // 3. $HOME/.yoinkrc
320 // 4. YOINKRC (environment)
321
322 std::string configFiles;
323
324 configFiles += Mf::Resource::getPath("yoinkrc");
325 configFiles += ":/etc/yoinkrc:$HOME/.yoinkrc";
326
327 char* configFile = getenv("YOINKRC");
328 if (configFile)
329 {
330 configFiles += ":";
331 configFiles += configFile;
332 }
333
334 try
335 {
336 Mf::Engine app(argc, argv, PACKAGE_STRING, iconFile, configFiles);
337 app.push(MainLayer::alloc());
338
339 app.run();
340 }
341 catch (const Mf::Exception& e)
342 {
343 Mf::logError("unhandled exception (code %d): \"%s\"",
344 e.code(), e.what());
345
346 Mf::ModalDialog dialog;
347 dialog.title = PACKAGE_STRING;
348 dialog.text1 = "Unhandled Exception";
349 dialog.text2 = e.what();
350 dialog.type = Mf::ModalDialog::CRITICAL;
351 dialog.run();
352
353 return 1;
354 }
355
356 return 0;
357 }
358
359
360 /** vim: set ts=4 sw=4 tw=80: *************************************************/
361
This page took 0.045874 seconds and 3 git commands to generate.