]>
Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
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/ModalDialog.hh>
38 #include <Moof/OpenGL.hh>
39 #include <Moof/Resource.hh>
40 #include <Moof/Transition.hh>
41 #include <Moof/Video.hh>
43 #include "GameLayer.hh"
44 #include "MainLayer.hh"
45 #include "TitleLayer.hh"
53 MainLayer::MainLayer()
55 Mf::dispatcher::addHandler("video.context_recreated",
56 boost::bind(&MainLayer::contextRecreated
, this, _1
), this);
60 MainLayer::~MainLayer()
62 Mf::dispatcher::removeHandler(this);
66 void MainLayer::pushed(Mf::Engine
& engine
)
70 //Mf::Scalar coeff[] = {0.0, 1.0};
71 //Mf::Lerp interp(coeff, 0.25);
73 //Mf::LayerP gameLayer = GameLayer::alloc();
74 //Mf::Transition<Mf::Lerp>::Ptr transition =
75 //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
76 //engine->push(transition);
77 //engine->push(GameLayer::alloc());
78 mEngine
->push(TitleLayer::alloc());
82 void MainLayer::draw(Mf::Scalar alpha
) const
84 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
86 glMatrixMode(GL_PROJECTION
);
89 glMatrixMode(GL_MODELVIEW
);
93 bool MainLayer::handleEvent(const Mf::Event
& event
)
98 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
102 else if (event
.key
.keysym
.sym
== SDLK_f
)
104 mEngine
->getVideo().toggleFull();
106 else if (event
.key
.keysym
.sym
== SDLK_l
)
108 Mf::Video
& video
= mEngine
->getVideo();
109 video
.toggleCursorGrab();
110 video
.toggleCursorVisible();
112 else if (event
.key
.keysym
.sym
== SDLK_y
)
114 mEngine
->push(GameLayer::alloc());
118 case SDL_VIDEORESIZE
:
119 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
130 void MainLayer::quit()
132 // remove all the layers
137 void MainLayer::setupGL()
139 glEnable(GL_TEXTURE_2D
);
140 glEnable(GL_DEPTH_TEST
);
142 glEnable(GL_LINE_SMOOTH
);
143 glEnable(GL_POLYGON_SMOOTH
);
144 glShadeModel(GL_SMOOTH
);
146 //glEnable(GL_BLEND);
147 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
148 glEnable(GL_ALPHA_TEST
);
149 glAlphaFunc(GL_GREATER
, 0.0);
151 glClearColor(0.0, 0.0, 0.0, 1.0);
153 glMatrixMode(GL_PROJECTION
);
155 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
157 glMatrixMode(GL_MODELVIEW
);
160 void MainLayer::contextRecreated(const Mf::Notification
* note
)
162 // whenever the context is destroyed and a new one created, it probably
163 // won't contain our state so we need to set that up again
171 std::cout
<< "Usage: "PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
173 << "The alien-smashing action game." << std::endl
175 << "Options:" << std::endl
176 << " -h, --help" << std::endl
177 << " show this help and exit" << std::endl
178 << " -i, --info" << std::endl
179 << " show version and build information" << std::endl
180 << " detail=1|2|3" << std::endl
181 << " the level of detail of game scenes" << std::endl
182 << " fullscreen=true|false" << std::endl
183 << " if true, uses the entire display" << std::endl
184 << " maxfps=num" << std::endl
185 << " the maximum number of frames per second" << std::endl
187 << "See documentation for more options." << std::endl
;
190 void printInfo(int argc
, char* argv
[])
196 assets
.assign(YOINK_DATADIR
);
197 int accessible
= access(assets
.c_str(), R_OK
);
198 if (accessible
!= 0) assets
+= " (no access)";
200 char* temp
= getenv("YOINK_DATADIR");
204 accessible
= access(temp
, R_OK
);
205 if (accessible
!= 0) datadir
+= " (no access)";
208 temp
= getenv("YOINKRC");
212 accessible
= access(temp
, R_OK
);
213 if (accessible
!= 0) config
+= " (no access)";
216 std::cout
<< " Executable: " << argv
[0] << std::endl
217 << " Version: "VERSION
<< std::endl
218 #if defined(__DATE__) && defined(__TIME__)
219 << " Built: "__DATE__
" "__TIME__
<< std::endl
221 << " Compiler: "COMPILER_STRING
<< std::endl
222 << " Assets: " << assets
<< std::endl
228 #ifndef USE_DOUBLE_PRECISION
231 << "double-precision "
236 #ifndef PROFILING_ENABLED
247 << "threads" << std::endl
248 << " YOINKRC: " << config
<< std::endl
249 << "YOINK_DATADIR: " << datadir
<< std::endl
;
254 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
258 int main(int argc
, char* argv
[])
262 std::string
arg(argv
[1]);
263 if (arg
== "-h" || arg
== "--help")
268 else if (arg
== "-i" || arg
== "--info")
270 printInfo(argc
, argv
);
275 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
276 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
277 << "Send patches and bug reports to <"
278 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
283 #if YOINK_LOGLEVEL >= 4
284 Mf::setLogLevel(Mf::LOG_DEBUG
);
285 #elif YOINK_LOGLEVEL >= 3
286 Mf::setLogLevel(Mf::LOG_INFO
);
287 #elif YOINK_LOGLEVEL >= 2
288 Mf::setLogLevel(Mf::LOG_SCRIPT
);
289 #elif YOINK_LOGLEVEL >= 1
290 Mf::setLogLevel(Mf::LOG_ERROR
);
292 Mf::setLogLevel(Mf::LOG_NONE
);
296 // Add search paths; they should be searched in this order:
297 // 1. YOINK_DATADIR (environment)
298 // 2. YOINK_DATADIR (configure)
300 char* dataDir
= getenv("YOINK_DATADIR");
303 Mf::Resource::addSearchPath(dataDir
);
306 Mf::Resource::addSearchPath(YOINK_DATADIR
);
308 std::string iconFile
= Mf::Resource::getPath(PACKAGE
".png");
311 // Build the list of config files to search for, in this order:
312 // 1. YOINK_DATADIR/yoinkrc
315 // 4. YOINKRC (environment)
317 std::string configFiles
;
319 configFiles
+= Mf::Resource::getPath("yoinkrc");
320 configFiles
+= ":/etc/yoinkrc:$HOME/.yoinkrc";
322 char* configFile
= getenv("YOINKRC");
326 configFiles
+= configFile
;
331 Mf::Engine
app(argc
, argv
, PACKAGE_STRING
, iconFile
, configFiles
);
332 app
.push(MainLayer::alloc());
336 catch (const Mf::Exception
& e
)
338 Mf::logError("unhandled exception (code %d): \"%s\"",
341 Mf::ModalDialog dialog
;
342 dialog
.title
= PACKAGE_STRING
;
343 dialog
.text1
= "Unhandled Exception";
344 dialog
.text2
= e
.what();
345 dialog
.type
= Mf::ModalDialog::CRITICAL
;
355 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.046559 seconds and 4 git commands to generate.