]>
Dogcows Code - chaz/yoink/blob - MainLayer.cc
ad2dbbc5f22d9a607f69327783032eee338c4236
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/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>
42 #include "ErrorHandler.hh"
43 #include "GameLayer.hh"
44 #include "MainLayer.hh"
45 #include "TitleLayer.hh"
53 MainLayer::MainLayer()
55 mDispatchHandler
= Mf::engine
.addHandler("video.newcontext",
56 boost::bind(&MainLayer::contextRecreated
, this));
60 void MainLayer::pushedOntoEngine()
62 //Mf::Scalar coeff[] = {0.0, 1.0};
63 //Mf::Lerp interp(coeff, 0.25);
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 Mf::engine
.push(TitleLayer::alloc());
74 void MainLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
76 if (Mf::engine
.getSize() == 1)
78 // this is the only layer left on the stack
79 //Mf::engine.push(TitleLayer::alloc());
84 void MainLayer::draw(Mf::Scalar alpha
) const
86 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
88 glMatrixMode(GL_PROJECTION
);
91 glMatrixMode(GL_MODELVIEW
);
95 bool MainLayer::handleEvent(const Mf::Event
& event
)
100 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
104 else if (event
.key
.keysym
.sym
== SDLK_f
)
106 Mf::engine
.getVideo()->toggleFull();
108 else if (event
.key
.keysym
.sym
== SDLK_l
)
110 Mf::VideoP video
= Mf::engine
.getVideo();
111 video
->toggleCursorGrab();
112 video
->toggleCursorVisible();
116 case SDL_VIDEORESIZE
:
117 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
129 void MainLayer::setupGL()
131 glEnable(GL_TEXTURE_2D
);
132 glEnable(GL_DEPTH_TEST
);
134 glEnable(GL_LINE_SMOOTH
);
135 glEnable(GL_POLYGON_SMOOTH
);
136 glShadeModel(GL_SMOOTH
);
138 //glEnable(GL_BLEND);
139 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 glEnable(GL_ALPHA_TEST
);
141 glAlphaFunc(GL_GREATER
, 0.0);
143 glClearColor(0.0, 0.0, 0.0, 1.0);
145 //glMatrixMode(GL_PROJECTION);
147 //Mf::Scalar ratio = Mf::engine.getVideo()->getWidth() /
148 //Mf::engine.getVideo()->getHeight();
149 //gluPerspective(60.0, ratio, 1.0, 250.0);
151 //glMatrixMode(GL_MODELVIEW);
154 void MainLayer::contextRecreated()
156 // whenever the context is destroyed and a new one created, it probably
157 // won't contain our state so we need to set that up again
165 std::cout
<< "Usage: "PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
167 << "The alien-smashing action game." << std::endl
169 << "Options:" << std::endl
170 << " -h, --help" << std::endl
171 << " show this help and exit" << std::endl
172 << " -i, --info" << std::endl
173 << " show version and build information" << std::endl
174 << " detail=1|2|3" << std::endl
175 << " the level of detail of game scenes" << std::endl
176 << " fullscreen=true|false" << std::endl
177 << " if true, uses the entire display" << std::endl
178 << " maxfps=num" << std::endl
179 << " the maximum number of frames per second" << std::endl
181 << "See documentation for more options." << std::endl
;
184 void printInfo(int argc
, char* argv
[])
190 assets
.assign(YOINK_DATADIR
);
191 int accessible
= access(assets
.c_str(), R_OK
);
192 if (accessible
!= 0) assets
+= " (no access)";
194 char* temp
= getenv("YOINK_DATADIR");
198 accessible
= access(temp
, R_OK
);
199 if (accessible
!= 0) datadir
+= " (no access)";
202 temp
= getenv("YOINKRC");
206 accessible
= access(temp
, R_OK
);
207 if (accessible
!= 0) config
+= " (no access)";
210 std::cout
<< " Executable: " << argv
[0] << std::endl
211 << " Version: "VERSION
<< std::endl
212 << " Built: " << COMPILE_TIME
<< std::endl
213 << " Compiler: "COMPILER_STRING
<< std::endl
214 << " Assets: " << assets
<< std::endl
220 #ifndef USE_DOUBLE_PRECISION
223 << "double-precision "
228 #ifndef PROFILING_ENABLED
239 << "threads" << std::endl
240 << " YOINKRC: " << config
<< std::endl
241 << "YOINK_DATADIR: " << datadir
<< std::endl
;
246 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
250 int main(int argc
, char* argv
[])
254 std::string
arg(argv
[1]);
255 if (arg
== "-h" || arg
== "--help")
260 else if (arg
== "-i" || arg
== "--info")
262 printInfo(argc
, argv
);
268 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
269 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
270 << "Send patches and bug reports to <"
271 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
276 // make sure the engine started up okay
277 const Mf::Error
& error
= Mf::engine
.getError();
280 Mf::ModalDialog dialog
;
281 dialog
.title
= PACKAGE_STRING
;
282 dialog
.text1
= "Uh oh!";
283 dialog
.text2
= getErrorString(error
);
284 dialog
.type
= Mf::ModalDialog::CRITICAL
;
287 // openal errors are not fatal
288 if (error
.code() != Mf::Error::OPENAL_INIT
) return 1;
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
);
305 // Build the list of config files to search for, in this order:
306 // 1. YOINK_DATADIR/yoinkrc
307 // 2. /etc/yoinkrc (not for Windows)
309 // 4. YOINKRC (environment)
311 std::string configFiles
= Mf::Resource::getPath("yoinkrc");
312 #if !defined(_WIN32) && !defined(__WIN32__)
313 configFiles
+= ":/etc/yoinkrc";
315 configFiles
+= ":$HOME/.yoinkrc";
317 char* configFile
= getenv("YOINKRC");
321 configFiles
+= configFile
;
324 Mf::Settings
& settings
= Mf::Settings::getInstance();
325 settings
.loadFromFile(configFiles
);
326 settings
.parseArgs(argc
, argv
);
328 Mf::Log::Level logLevel
;
329 if (settings
.get("loglevel", logLevel
)) Mf::Log::setLevel(logLevel
);
331 Mf::engine
.initWithSettings(settings
);
333 std::string iconFile
= Mf::Resource::getPath(PACKAGE
".png");
338 Mf::engine
.setVideo(Mf::Video::alloc(PACKAGE_STRING
, iconFile
));
339 Mf::engine
.push(MainLayer::alloc());
343 catch (const Mf::Error
& error
)
345 Mf::ModalDialog dialog
;
346 dialog
.title
= PACKAGE_STRING
;
347 dialog
.text1
= "Unhandled Exception";
348 dialog
.text2
= getErrorString(error
);
349 dialog
.type
= Mf::ModalDialog::CRITICAL
;
359 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.049108 seconds and 3 git commands to generate.