]> Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
dispatch class not a singleton, engine is static
[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/Log.hh>
35 #include <Moof/ModalDialog.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Resource.hh>
38 #include <Moof/Settings.hh>
39 #include <Moof/Transition.hh>
40 #include <Moof/Video.hh>
41
42 #include "ErrorHandler.hh"
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 mDispatchHandler = Mf::engine.addHandler("video.newcontext",
56 boost::bind(&MainLayer::contextRecreated, this));
57 setupGL();
58 }
59
60 void MainLayer::pushedOntoEngine()
61 {
62 //Mf::Scalar coeff[] = {0.0, 1.0};
63 //Mf::Lerp interp(coeff, 0.25);
64
65 //Mf::LayerP gameLayer = GameLayer::alloc();
66 //Mf::Transition<Mf::Lerp>::Ptr transition =
67 //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
68 //engine->push(transition);
69 //engine->push(GameLayer::alloc());
70 Mf::engine.push(TitleLayer::alloc());
71 }
72
73
74 void MainLayer::update(Mf::Scalar t, Mf::Scalar dt)
75 {
76 if (Mf::engine.getSize() == 1)
77 {
78 // this is the only layer left on the stack
79 //Mf::engine.push(TitleLayer::alloc());
80 Mf::engine.clear();
81 }
82 }
83
84 void MainLayer::draw(Mf::Scalar alpha) const
85 {
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
88 glMatrixMode(GL_PROJECTION);
89 glLoadIdentity();
90
91 glMatrixMode(GL_MODELVIEW);
92 glLoadIdentity();
93 }
94
95 bool MainLayer::handleEvent(const Mf::Event& event)
96 {
97 switch (event.type)
98 {
99 case SDL_KEYUP:
100 if (event.key.keysym.sym == SDLK_ESCAPE)
101 {
102 Mf::engine.clear();
103 }
104 else if (event.key.keysym.sym == SDLK_f)
105 {
106 Mf::engine.getVideo()->toggleFull();
107 }
108 else if (event.key.keysym.sym == SDLK_l)
109 {
110 Mf::VideoP video = Mf::engine.getVideo();
111 video->toggleCursorGrab();
112 video->toggleCursorVisible();
113 }
114 break;
115
116 case SDL_VIDEORESIZE:
117 glViewport(0, 0, event.resize.w, event.resize.h);
118 break;
119
120 case SDL_QUIT:
121 Mf::engine.clear();
122 break;
123 }
124
125 return false;
126 }
127
128
129 void MainLayer::setupGL()
130 {
131 glEnable(GL_TEXTURE_2D);
132 glEnable(GL_DEPTH_TEST);
133
134 glEnable(GL_LINE_SMOOTH);
135 glEnable(GL_POLYGON_SMOOTH);
136 glShadeModel(GL_SMOOTH);
137
138 //glEnable(GL_BLEND);
139 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 glEnable(GL_ALPHA_TEST);
141 glAlphaFunc(GL_GREATER, 0.0);
142
143 glClearColor(0.0, 0.0, 0.0, 1.0);
144
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
148
149 glMatrixMode(GL_MODELVIEW);
150 }
151
152 void MainLayer::contextRecreated()
153 {
154 // whenever the context is destroyed and a new one created, it probably
155 // won't contain our state so we need to set that up again
156 setupGL();
157 }
158
159
160
161 void printUsage()
162 {
163 std::cout << "Usage: "PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
164 << std::endl
165 << "The alien-smashing action game." << std::endl
166 << std::endl
167 << "Options:" << std::endl
168 << " -h, --help" << std::endl
169 << " show this help and exit" << std::endl
170 << " -i, --info" << std::endl
171 << " show version and build information" << std::endl
172 << " detail=1|2|3" << std::endl
173 << " the level of detail of game scenes" << std::endl
174 << " fullscreen=true|false" << std::endl
175 << " if true, uses the entire display" << std::endl
176 << " maxfps=num" << std::endl
177 << " the maximum number of frames per second" << std::endl
178 << std::endl
179 << "See documentation for more options." << std::endl;
180 }
181
182 void printInfo(int argc, char* argv[])
183 {
184 std::string assets;
185 std::string datadir;
186 std::string config;
187
188 assets.assign(YOINK_DATADIR);
189 int accessible = access(assets.c_str(), R_OK);
190 if (accessible != 0) assets += " (no access)";
191
192 char* temp = getenv("YOINK_DATADIR");
193 if (temp)
194 {
195 datadir = temp;
196 accessible = access(temp, R_OK);
197 if (accessible != 0) datadir += " (no access)";
198 }
199
200 temp = getenv("YOINKRC");
201 if (temp)
202 {
203 config = temp;
204 accessible = access(temp, R_OK);
205 if (accessible != 0) config += " (no access)";
206 }
207
208 std::cout << " Executable: " << argv[0] << std::endl
209 << " Version: "VERSION << std::endl
210 << " Built: " << COMPILE_TIME << std::endl
211 << " Compiler: "COMPILER_STRING << std::endl
212 << " Assets: " << assets << std::endl
213 << "Build options: "
214 #ifdef NDEBUG
215 << "-"
216 #endif
217 << "debug "
218 #ifndef USE_DOUBLE_PRECISION
219 << "-"
220 #endif
221 << "double-precision "
222 #ifndef USE_GTK
223 << "-"
224 #endif
225 << "gtk "
226 #ifndef PROFILING_ENABLED
227 << "-"
228 #endif
229 << "profile "
230 #ifndef USE_QT4
231 << "-"
232 #endif
233 << "qt4 "
234 #ifndef USE_THREADS
235 << "-"
236 #endif
237 << "threads" << std::endl
238 << " YOINKRC: " << config << std::endl
239 << "YOINK_DATADIR: " << datadir << std::endl;
240 }
241
242 void goodbye()
243 {
244 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
245 }
246
247
248 int main(int argc, char* argv[])
249 {
250 if (argc > 1)
251 {
252 std::string arg(argv[1]);
253 if (arg == "-h" || arg == "--help")
254 {
255 printUsage();
256 return 0;
257 }
258 else if (arg == "-i" || arg == "--info")
259 {
260 printInfo(argc, argv);
261 return 0;
262 }
263 }
264
265
266 std::cout << std::endl << PACKAGE_STRING << std::endl
267 << "Compiled " << __TIME__ " " __DATE__ << std::endl
268 << "Send patches and bug reports to <"
269 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
270
271 atexit(goodbye);
272
273
274 // make sure the engine started up okay
275 if (Mf::engine.getError().isError())
276 {
277 Mf::ModalDialog dialog;
278 dialog.title = PACKAGE_STRING;
279 dialog.text1 = "Fatal Error";
280 dialog.text2 = getErrorString(Mf::engine.getError());
281 dialog.type = Mf::ModalDialog::CRITICAL;
282 dialog.run();
283
284 return 1;
285 }
286
287
288 #if YOINK_LOGLEVEL >= 3
289 Mf::Log::setLevel(Mf::Log::INFO);
290 #elif YOINK_LOGLEVEL >= 2
291 Mf::Log::setLevel(Mf::Log::WARNING);
292 #elif YOINK_LOGLEVEL >= 1
293 Mf::Log::setLevel(Mf::Log::ERROR);
294 #elif YOINK_LOGLEVEL
295 Mf::Log::setLevel(Mf::Log::NONE);
296 #endif
297
298
299 // Add search paths; they should be searched in this order:
300 // 1. YOINK_DATADIR (environment)
301 // 2. YOINK_DATADIR (configure)
302
303 char* dataDir = getenv("YOINK_DATADIR");
304 if (dataDir)
305 {
306 Mf::Resource::addSearchPath(dataDir);
307 }
308
309 Mf::Resource::addSearchPath(YOINK_DATADIR);
310
311
312 // Build the list of config files to search for, in this order:
313 // 1. YOINK_DATADIR/yoinkrc
314 // 2. /etc/yoinkrc (not for Windows)
315 // 3. $HOME/.yoinkrc
316 // 4. YOINKRC (environment)
317
318 std::string configFiles;
319
320 configFiles += Mf::Resource::getPath("yoinkrc");
321 #if !defined(_WIN32) && !defined(__WIN32__)
322 configFiles += ":/etc/yoinkrc";
323 #endif
324 configFiles += ":$HOME/.yoinkrc";
325
326 char* configFile = getenv("YOINKRC");
327 if (configFile)
328 {
329 configFiles += ":";
330 configFiles += configFile;
331 }
332
333 Mf::Settings& settings = Mf::Settings::getInstance();
334 settings.loadFromFile(configFiles);
335 settings.parseArgs(argc, argv);
336
337 std::string iconFile = Mf::Resource::getPath(PACKAGE".png");
338
339
340 try
341 {
342 Mf::engine.setVideo(Mf::Video::alloc(PACKAGE_STRING, iconFile));
343 Mf::engine.push(MainLayer::alloc());
344
345 Mf::engine.run();
346 }
347 catch (const Mf::Error& error)
348 {
349 Mf::ModalDialog dialog;
350 dialog.title = PACKAGE_STRING;
351 dialog.text1 = "Unhandled Exception";
352 dialog.text2 = getErrorString(error);
353 dialog.type = Mf::ModalDialog::CRITICAL;
354 dialog.run();
355
356 return 1;
357 }
358
359 return 0;
360 }
361
362
363 /** vim: set ts=4 sw=4 tw=80: *************************************************/
364
This page took 0.049451 seconds and 4 git commands to generate.