]> Dogcows Code - chaz/yoink/blob - src/Main.cc
cleaned up interpolator classes
[chaz/yoink] / src / Main.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 #include <unistd.h> // access
33
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>
41
42 #include "ErrorHandler.hh"
43 #include "GameLayer.hh"
44 #include "Main.hh"
45 #include "TitleLayer.hh"
46
47 #if HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50 #include "version.h"
51
52
53 Main::Main()
54 {
55 mDispatchHandler = Mf::core.addHandler("video.newcontext",
56 boost::bind(&Main::contextCreated));
57 setupGL();
58 }
59
60 void Main::addedToCore()
61 {
62 //Mf::Scalar coeff[] = {0.0, 1.0};
63 //Mf::Lerp interp(coeff, 0.25);
64
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());
71 }
72
73
74 void Main::update(Mf::Scalar t, Mf::Scalar dt)
75 {
76 if (Mf::core.getSize() == 1)
77 {
78 // this is the only layer left on the stack
79 //Mf::core.push(TitleLayer::alloc());
80 Mf::core.clear();
81 }
82 }
83
84 void Main::draw(Mf::Scalar alpha) const
85 {
86 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
87
88 glMatrixMode(GL_PROJECTION);
89 glLoadIdentity();
90
91 glMatrixMode(GL_MODELVIEW);
92 glLoadIdentity();
93 }
94
95 bool Main::handleEvent(const Mf::Event& event)
96 {
97 switch (event.type)
98 {
99 case SDL_KEYUP:
100 if (event.key.keysym.sym == SDLK_ESCAPE)
101 {
102 Mf::core.clear();
103 }
104 else if (event.key.keysym.sym == SDLK_f)
105 {
106 Mf::video->toggleFull();
107 }
108 else if (event.key.keysym.sym == SDLK_l)
109 {
110 Mf::video->toggleCursorGrab();
111 Mf::video->toggleCursorVisible();
112 }
113 break;
114
115 case SDL_VIDEORESIZE:
116 glViewport(0, 0, event.resize.w, event.resize.h);
117 break;
118
119 case SDL_QUIT:
120 Mf::core.clear();
121 break;
122 }
123
124 return false;
125 }
126
127
128 std::string Main::getSearchPath()
129 {
130 // Add search paths; they should be searched in this order:
131 // 1. YOINK_DATADIR (environment)
132 // 2. YOINK_DATADIR (configure)
133
134 std::string path;
135
136 char* dataDir = getenv("YOINK_DATADIR");
137 if (dataDir)
138 {
139 path += dataDir;
140 path += ":";
141 }
142 path += YOINK_DATADIR;
143
144 return path;
145 }
146
147 std::string Main::getConfigPath()
148 {
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)
152 // 3. $HOME/.yoinkrc
153 // 4. YOINKRC (environment)
154
155 std::string path = Mf::Resource::getPath("yoinkrc");
156
157 #if !defined(_WIN32) && !defined(__WIN32__)
158 path += ":/etc/yoinkrc";
159 #endif
160 path += ":$HOME/.yoinkrc";
161
162 char* configFile = getenv("YOINKRC");
163 if (configFile)
164 {
165 path += ":";
166 path += configFile;
167 }
168
169 return path;
170 }
171
172
173 void Main::setupGL()
174 {
175 glEnable(GL_TEXTURE_2D);
176 glEnable(GL_DEPTH_TEST);
177
178 glEnable(GL_LINE_SMOOTH);
179 glEnable(GL_POLYGON_SMOOTH);
180 glShadeModel(GL_SMOOTH);
181
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);
186
187 glClearColor(0.0, 0.0, 0.0, 1.0);
188
189 //glMatrixMode(GL_PROJECTION);
190 //glLoadIdentity();
191 //Mf::Scalar ratio = Mf::core.getVideo()->getWidth() /
192 //Mf::core.getVideo()->getHeight();
193 //gluPerspective(60.0, ratio, 1.0, 250.0);
194
195 //glMatrixMode(GL_MODELVIEW);
196 }
197
198 void Main::contextCreated()
199 {
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
202 setupGL();
203 }
204
205
206
207 void Main::printUsage()
208 {
209 std::cout << "Usage: "PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
210 << std::endl
211 << "The alien-smashing action game." << std::endl
212 << 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
224 << std::endl
225 << "See documentation for more options." << std::endl;
226 }
227
228 void Main::printInfo(int argc, char* argv[])
229 {
230 std::string assets;
231 std::string datadir;
232 std::string config;
233
234 assets.assign(YOINK_DATADIR);
235 int accessible = access(assets.c_str(), R_OK);
236 if (accessible != 0) assets += " (no access)";
237
238 char* temp = getenv("YOINK_DATADIR");
239 if (temp)
240 {
241 datadir = temp;
242 accessible = access(temp, R_OK);
243 if (accessible != 0) datadir += " (no access)";
244 }
245
246 temp = getenv("YOINKRC");
247 if (temp)
248 {
249 config = temp;
250 accessible = access(temp, R_OK);
251 if (accessible != 0) config += " (no access)";
252 }
253
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
259 << "Build options: "
260 #ifdef NDEBUG
261 << "-"
262 #endif
263 << "debug "
264 #ifndef USE_DOUBLE_PRECISION
265 << "-"
266 #endif
267 << "double-precision "
268 #ifndef USE_GTK
269 << "-"
270 #endif
271 << "gtk "
272 #ifndef PROFILING_ENABLED
273 << "-"
274 #endif
275 << "profile "
276 #ifndef USE_QT4
277 << "-"
278 #endif
279 << "qt4 "
280 #ifndef USE_THREADS
281 << "-"
282 #endif
283 << "threads" << std::endl
284 << " YOINKRC: " << config << std::endl
285 << "YOINK_DATADIR: " << datadir << std::endl;
286 }
287
288
289 void hello()
290 {
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;
295 }
296
297 void goodbye()
298 {
299 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
300 }
301
302 int main(int argc, char* argv[])
303 {
304 if (argc > 1)
305 {
306 std::string arg(argv[1]);
307 if (arg == "-h" || arg == "--help")
308 {
309 Main::printUsage();
310 return 0;
311 }
312 else if (arg == "-i" || arg == "--info")
313 {
314 Main::printInfo(argc, argv);
315 return 0;
316 }
317 }
318
319 hello();
320 atexit(goodbye);
321
322 Mf::Resource::addSearchPaths(Main::getSearchPath());
323
324 Mf::settings.loadFromFiles(Main::getConfigPath());
325 Mf::settings.parseArgs(argc, argv);
326
327 Mf::Log::Level logLevel;
328 if (Mf::settings.get("loglevel", logLevel)) Mf::Log::setLevel(logLevel);
329
330 try
331 {
332 Mf::Video video(PACKAGE_STRING, Mf::Resource::getPath(PACKAGE".png"));
333 MainP app = Main::alloc();
334 Mf::core.push(app);
335 Mf::core.run();
336 }
337 catch (const Mf::Error& error)
338 {
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;
344 dialog.run();
345
346 return 1;
347 }
348
349 return 0;
350 }
351
352
353 /** vim: set ts=4 sw=4 tw=80: *************************************************/
354
This page took 0.04971 seconds and 4 git commands to generate.