]> Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
4975e17f1b28db30e9df1d95a73469a9f6a85456
[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 "
209 #ifndef PROFILING_ENABLED
210 << "-"
211 #endif
212 << "profile "
213 << std::endl;
214 #if !defined (_WIN32) && !defined(__WIN32__)
215 system("uname -a");
216 #endif
217 }
218
219 void goodbye()
220 {
221 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
222 }
223
224
225
226 int main(int argc, char* argv[])
227 {
228 if (argc > 1)
229 {
230 std::string arg(argv[1]);
231 if (arg == "-h" || arg == "--help")
232 {
233 printUsage();
234 return 0;
235 }
236 else if (arg == "-i" || arg == "--info")
237 {
238 printInfo();
239 return 0;
240 }
241 }
242
243 std::cout << std::endl << PACKAGE_STRING << std::endl
244 << "Compiled " << __TIME__ " " __DATE__ << std::endl
245 << "Send patches and bug reports to <"
246 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
247
248 atexit(goodbye);
249
250
251 #if YOINK_LOGLEVEL >= 4
252 Mf::setLogLevel(Mf::LOG_DEBUG);
253 #elif YOINK_LOGLEVEL >= 3
254 Mf::setLogLevel(Mf::LOG_INFO);
255 #elif YOINK_LOGLEVEL >= 2
256 Mf::setLogLevel(Mf::LOG_SCRIPT);
257 #elif YOINK_LOGLEVEL >= 1
258 Mf::setLogLevel(Mf::LOG_ERROR);
259 #elif YOINK_LOGLEVEL
260 Mf::setLogLevel(Mf::LOG_NONE);
261 #endif
262
263
264 // Add search paths; they should be searched in this order:
265 // 1. YOINK_DATADIR (environment)
266 // 2. YOINK_DATADIR (configure)
267
268 char* dataDir = getenv("YOINK_DATADIR");
269 if (dataDir)
270 {
271 Mf::Resource::addSearchPath(dataDir);
272 }
273
274 Mf::Resource::addSearchPath(YOINK_DATADIR);
275
276 std::string iconFile = Mf::Resource::getPath("yoink.png");
277
278
279 // Build the list of config files to search for, in this order:
280 // 1. YOINK_DATADIR/yoinkrc
281 // 2. /etc/yoinkrc
282 // 3. $HOME/.yoinkrc
283 // 4. YOINKRC (environment)
284
285 std::string configFiles;
286
287 configFiles += Mf::Resource::getPath("yoinkrc");
288 configFiles += ":/etc/yoinkrc:$HOME/.yoinkrc";
289
290 char* configFile = getenv("YOINKRC");
291 if (configFile)
292 {
293 configFiles += ":";
294 configFiles += configFile;
295 }
296
297
298 try
299 {
300 Mf::Engine app(argc, argv, PACKAGE_STRING, iconFile, configFiles);
301 app.push(MainLayer::alloc());
302
303 app.run();
304 }
305 catch (Mf::Exception e)
306 {
307 Mf::logError("unhandled exception: <<%s>>", e.what());
308 Mf::logInfo("it's time to crash now :-(");
309 throw e;
310 }
311
312 return 0;
313 }
314
315
316 /** vim: set ts=4 sw=4 tw=80: *************************************************/
317
This page took 0.0438 seconds and 3 git commands to generate.