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