]>
Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
2e687855dd73d03a557bcf2108d9940d1706ed4c
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #include <cstdlib> // atexit, getenv
32 #include <unistd.h> // access
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>
42 #include "GameLayer.hh"
43 #include "MainLayer.hh"
44 #include "TitleLayer.hh"
52 MainLayer::MainLayer()
54 Mf::dispatcher::addHandler("video.context_recreated",
55 boost::bind(&MainLayer::contextRecreated
, this, _1
), this);
59 MainLayer::~MainLayer()
61 Mf::dispatcher::removeHandler(this);
65 void MainLayer::pushed(Mf::Engine
& e
)
69 //Mf::Scalar coeff[] = {0.0, 1.0};
70 //Mf::Lerp interp(coeff, 0.25);
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 engine
->push(TitleLayer::alloc());
81 void MainLayer::draw(Mf::Scalar alpha
) const
83 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
85 glMatrixMode(GL_PROJECTION
);
88 glMatrixMode(GL_MODELVIEW
);
92 bool MainLayer::handleEvent(const Mf::Event
& event
)
97 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
101 else if (event
.key
.keysym
.sym
== SDLK_f
)
103 engine
->getVideo().toggleFull();
105 else if (event
.key
.keysym
.sym
== SDLK_l
)
107 Mf::Video
& video
= engine
->getVideo();
108 video
.toggleCursorGrab();
109 video
.toggleCursorVisible();
111 else if (event
.key
.keysym
.sym
== SDLK_y
)
113 engine
->push(GameLayer::alloc());
117 case SDL_VIDEORESIZE
:
118 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
129 void MainLayer::quit()
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
141 void MainLayer::setupGL()
143 glEnable(GL_TEXTURE_2D
);
144 glEnable(GL_DEPTH_TEST
);
146 glEnable(GL_LINE_SMOOTH
);
147 glEnable(GL_POLYGON_SMOOTH
);
148 glShadeModel(GL_SMOOTH
);
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);
155 glClearColor(0.0, 0.0, 0.0, 1.0);
157 glMatrixMode(GL_PROJECTION
);
159 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
161 glMatrixMode(GL_MODELVIEW
);
164 void MainLayer::contextRecreated(const Mf::Notification
* note
)
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
175 std::cout
<< "Usage: "PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..." << std::endl
176 << "The alien-smashing action game." << std::endl
178 << "Options:" << std::endl
179 << " -h, --help" << std::endl
180 << " show this help and exit" << std::endl
181 << " -i, --info" << std::endl
182 << " show version and build information" << std::endl
183 << " detail=1|2|3" << std::endl
184 << " the level of detail of game scenes" << std::endl
185 << " fullscreen=true|false" << std::endl
186 << " if true, uses the entire display" << std::endl
187 << " maxfps=num" << std::endl
188 << " the maximum number of frames per second" << std::endl
190 << "See documentation for more options." << std::endl
;
193 void printInfo(int argc
, char* argv
[])
199 assets
.assign(YOINK_DATADIR
);
200 int accessible
= access(assets
.c_str(), R_OK
);
201 if (accessible
!= 0) assets
+= " (no access)";
203 char* temp
= getenv("YOINK_DATADIR");
207 accessible
= access(temp
, R_OK
);
208 if (accessible
!= 0) datadir
+= " (no access)";
211 temp
= getenv("YOINKRC");
215 accessible
= access(temp
, R_OK
);
216 if (accessible
!= 0) config
+= " (no access)";
219 std::cout
<< " Executable: " << argv
[0] << std::endl
220 << " Version: "VERSION
<< std::endl
222 << " Built: "__DATE__
" "__TIME__
<< std::endl
224 << " Compiler: "COMPILER_STRING
<< std::endl
225 << " Assets: " << assets
<< std::endl
231 #ifndef USE_DOUBLE_PRECISION
234 << "double-precision "
235 #ifndef PROFILING_ENABLED
242 << "threads" << std::endl
243 << " YOINKRC: " << config
<< std::endl
244 << "YOINK_DATADIR: " << datadir
<< std::endl
;
249 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
254 int main(int argc
, char* argv
[])
258 std::string
arg(argv
[1]);
259 if (arg
== "-h" || arg
== "--help")
264 else if (arg
== "-i" || arg
== "--info")
266 printInfo(argc
, argv
);
271 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
272 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
273 << "Send patches and bug reports to <"
274 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
279 #if YOINK_LOGLEVEL >= 4
280 Mf::setLogLevel(Mf::LOG_DEBUG
);
281 #elif YOINK_LOGLEVEL >= 3
282 Mf::setLogLevel(Mf::LOG_INFO
);
283 #elif YOINK_LOGLEVEL >= 2
284 Mf::setLogLevel(Mf::LOG_SCRIPT
);
285 #elif YOINK_LOGLEVEL >= 1
286 Mf::setLogLevel(Mf::LOG_ERROR
);
288 Mf::setLogLevel(Mf::LOG_NONE
);
292 // Add search paths; they should be searched in this order:
293 // 1. YOINK_DATADIR (environment)
294 // 2. YOINK_DATADIR (configure)
296 char* dataDir
= getenv("YOINK_DATADIR");
299 Mf::Resource::addSearchPath(dataDir
);
302 Mf::Resource::addSearchPath(YOINK_DATADIR
);
304 std::string iconFile
= Mf::Resource::getPath("yoink.png");
307 // Build the list of config files to search for, in this order:
308 // 1. YOINK_DATADIR/yoinkrc
311 // 4. YOINKRC (environment)
313 std::string configFiles
;
315 configFiles
+= Mf::Resource::getPath("yoinkrc");
316 configFiles
+= ":/etc/yoinkrc:$HOME/.yoinkrc";
318 char* configFile
= getenv("YOINKRC");
322 configFiles
+= configFile
;
328 Mf::Engine
app(argc
, argv
, PACKAGE_STRING
, iconFile
, configFiles
);
329 app
.push(MainLayer::alloc());
333 catch (Mf::Exception e
)
335 Mf::logError("unhandled exception: <<%s>>", e
.what());
336 Mf::logInfo("it's time to crash now :-(");
344 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.055013 seconds and 4 git commands to generate.