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