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