]>
Dogcows Code - chaz/yoink/blob - src/Main.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/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"
45 #include "TitleLayer.hh"
55 mDispatchHandler
= Mf::core
.addHandler("video.newcontext",
56 boost::bind(&Main::contextRecreated
));
60 void Main::addedToCore()
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 //core.push(transition);
69 //core.push(GameLayer::alloc());
70 Mf::core
.push(TitleLayer::alloc());
74 void Main::update(Mf::Scalar t
, Mf::Scalar dt
)
76 if (Mf::core
.getSize() == 1)
78 // this is the only layer left on the stack
79 //Mf::core.push(TitleLayer::alloc());
84 void Main::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 Main::handleEvent(const Mf::Event
& event
)
100 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
104 else if (event
.key
.keysym
.sym
== SDLK_f
)
106 Mf::video
->toggleFull();
108 else if (event
.key
.keysym
.sym
== SDLK_l
)
110 Mf::video
->toggleCursorGrab();
111 Mf::video
->toggleCursorVisible();
115 case SDL_VIDEORESIZE
:
116 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
128 std::string
Main::getSearchPath()
130 // Add search paths; they should be searched in this order:
131 // 1. YOINK_DATADIR (environment)
132 // 2. YOINK_DATADIR (configure)
136 char* dataDir
= getenv("YOINK_DATADIR");
142 path
+= YOINK_DATADIR
;
147 std::string
Main::getConfigPath()
149 // Build the list of config files to search for, in this order:
150 // 1. YOINK_DATADIR/yoinkrc
151 // 2. /etc/yoinkrc (not for Windows)
153 // 4. YOINKRC (environment)
155 std::string path
= Mf::Resource::getPath("yoinkrc");
157 #if !defined(_WIN32) && !defined(__WIN32__)
158 path
+= ":/etc/yoinkrc";
160 path
+= ":$HOME/.yoinkrc";
162 char* configFile
= getenv("YOINKRC");
175 glEnable(GL_TEXTURE_2D
);
176 glEnable(GL_DEPTH_TEST
);
178 glEnable(GL_LINE_SMOOTH
);
179 glEnable(GL_POLYGON_SMOOTH
);
180 glShadeModel(GL_SMOOTH
);
182 //glEnable(GL_BLEND);
183 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
184 glEnable(GL_ALPHA_TEST
);
185 glAlphaFunc(GL_GREATER
, 0.0);
187 glClearColor(0.0, 0.0, 0.0, 1.0);
189 //glMatrixMode(GL_PROJECTION);
191 //Mf::Scalar ratio = Mf::core.getVideo()->getWidth() /
192 //Mf::core.getVideo()->getHeight();
193 //gluPerspective(60.0, ratio, 1.0, 250.0);
195 //glMatrixMode(GL_MODELVIEW);
198 void Main::contextRecreated()
200 // whenever the context is destroyed and a new one created, it probably
201 // won't contain our state so we need to set that up again
207 void Main::printUsage()
209 std::cout
<< "Usage: "PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
211 << "The alien-smashing action game." << std::endl
213 << "Options:" << std::endl
214 << " -h, --help" << std::endl
215 << " show this help and exit" << std::endl
216 << " -i, --info" << std::endl
217 << " show version and build information" << std::endl
218 << " detail=1|2|3" << std::endl
219 << " the level of detail of game scenes" << std::endl
220 << " fullscreen=true|false" << std::endl
221 << " if true, uses the entire display" << std::endl
222 << " framerate=num" << std::endl
223 << " the target number of frames per second" << std::endl
225 << "See documentation for more options." << std::endl
;
228 void Main::printInfo(int argc
, char* argv
[])
234 assets
.assign(YOINK_DATADIR
);
235 int accessible
= access(assets
.c_str(), R_OK
);
236 if (accessible
!= 0) assets
+= " (no access)";
238 char* temp
= getenv("YOINK_DATADIR");
242 accessible
= access(temp
, R_OK
);
243 if (accessible
!= 0) datadir
+= " (no access)";
246 temp
= getenv("YOINKRC");
250 accessible
= access(temp
, R_OK
);
251 if (accessible
!= 0) config
+= " (no access)";
254 std::cout
<< " Executable: " << argv
[0] << std::endl
255 << " Version: "VERSION
<< std::endl
256 << " Built: " << COMPILE_TIME
<< std::endl
257 << " Compiler: "COMPILER_STRING
<< std::endl
258 << " Assets: " << assets
<< std::endl
264 #ifndef USE_DOUBLE_PRECISION
267 << "double-precision "
272 #ifndef PROFILING_ENABLED
283 << "threads" << std::endl
284 << " YOINKRC: " << config
<< std::endl
285 << "YOINK_DATADIR: " << datadir
<< std::endl
;
291 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
292 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
293 << "Send patches and bug reports to <"
294 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
299 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
302 int main(int argc
, char* argv
[])
306 std::string
arg(argv
[1]);
307 if (arg
== "-h" || arg
== "--help")
312 else if (arg
== "-i" || arg
== "--info")
314 Main::printInfo(argc
, argv
);
322 Mf::Resource::addSearchPaths(Main::getSearchPath());
324 Mf::settings
.loadFromFiles(Main::getConfigPath());
325 Mf::settings
.parseArgs(argc
, argv
);
327 Mf::Log::Level logLevel
;
328 if (Mf::settings
.get("loglevel", logLevel
)) Mf::Log::setLevel(logLevel
);
332 Mf::Video
video(PACKAGE_STRING
, Mf::Resource::getPath(PACKAGE
".png"));
333 MainP app
= Main::alloc();
337 catch (const Mf::Error
& error
)
339 Mf::ModalDialog dialog
;
340 dialog
.title
= PACKAGE_STRING
;
341 dialog
.text1
= "Unhandled Exception";
342 dialog
.text2
= getErrorString(error
);
343 dialog
.type
= Mf::ModalDialog::CRITICAL
;
353 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.054348 seconds and 4 git commands to generate.