]> Dogcows Code - chaz/yoink/blob - src/Main.cc
bugfix: resource file searching was broken
[chaz/yoink] / src / Main.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include <cstdlib> // atexit, getenv
13 #include <functional>
14 #include <iostream>
15 #include <string>
16 #include <unistd.h> // access
17
18 #include <Moof/Log.hh>
19 #include <Moof/ModalDialog.hh>
20 #include <Moof/OpenGL.hh>
21 #include <Moof/Resource.hh>
22 #include <Moof/Settings.hh>
23 #include <Moof/Video.hh>
24
25 #include "ErrorHandler.hh"
26 #include "GameLayer.hh"
27 #include "Main.hh"
28 #include "TitleLayer.hh"
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "version.h"
34
35
36 Main::Main(Mf::Settings& settings, Mf::Video& video) :
37 Mf::View(settings, video)
38 {
39 Mf::Dispatch& dispatch = Mf::Dispatch::global();
40 mNewContextDispatch = dispatch.addTarget("video.newcontext",
41 boost::bind(&Main::setupGL));
42 setupGL();
43
44 addChild(TitleLayer::alloc());
45 }
46
47
48 void Main::update(Mf::Scalar t, Mf::Scalar dt)
49 {
50 if (children().size() == 0)
51 {
52 Mf::logWarning("main view has no children");
53 stop();
54 return;
55 }
56
57 Mf::View::update(t, dt);
58 }
59
60 void Main::draw(Mf::Scalar alpha) const
61 {
62 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
63
64 glMatrixMode(GL_PROJECTION);
65 glLoadIdentity();
66
67 glMatrixMode(GL_MODELVIEW);
68 glLoadIdentity();
69
70 Mf::View::draw(alpha);
71 }
72
73 bool Main::handleEvent(const Mf::Event& event)
74 {
75 if (Mf::View::handleEvent(event)) return true;
76
77 switch (event.type)
78 {
79 case SDL_KEYUP:
80 if (event.key.keysym.sym == SDLK_f)
81 {
82 video().toggleFull();
83 }
84 else if (event.key.keysym.sym == SDLK_l)
85 {
86 video().toggleCursorGrab();
87 video().toggleCursorVisible();
88 }
89 break;
90
91 case SDL_VIDEORESIZE:
92 glViewport(0, 0, event.resize.w, event.resize.h);
93 break;
94
95 case SDL_QUIT:
96 stop();
97 return true;
98 }
99
100 return false;
101 }
102
103
104 std::string Main::getSearchPath()
105 {
106 // Add search paths; they should be searched in this order:
107 // 1. YOINK_DATADIR (environment)
108 // 2. YOINK_DATADIR (configure)
109
110 std::string path;
111
112 char* dataDir = getenv("YOINK_DATADIR");
113 if (dataDir)
114 {
115 path += dataDir;
116 path += ":";
117 }
118 path += YOINK_DATADIR;
119
120 return path;
121 }
122
123 std::string Main::getConfigPath()
124 {
125 // Build the list of config files to search for, in this order:
126 // 1. YOINK_DATADIR/yoinkrc
127 // 2. /etc/yoinkrc (not for Windows)
128 // 3. $HOME/.yoinkrc
129 // 4. YOINKRC (environment)
130
131 std::string path("yoinkrc");
132 Mf::Resource::getPath(path);
133
134 #if !defined(_WIN32)
135 path += ":/etc/yoinkrc";
136 #endif
137 path += ":$HOME/.yoinkrc";
138
139 char* configFile = getenv("YOINKRC");
140 if (configFile)
141 {
142 path += ":";
143 path += configFile;
144 }
145
146 return path;
147 }
148
149
150 void Main::setupGL()
151 {
152 glEnable(GL_TEXTURE_2D);
153 glEnable(GL_DEPTH_TEST);
154
155 glEnable(GL_LINE_SMOOTH);
156 glEnable(GL_POLYGON_SMOOTH);
157 glShadeModel(GL_SMOOTH);
158
159 //glEnable(GL_BLEND);
160 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161 glEnable(GL_ALPHA_TEST);
162 glAlphaFunc(GL_GREATER, 0.0);
163
164 glClearColor(0.0, 0.0, 0.0, 1.0);
165
166 //glMatrixMode(GL_PROJECTION);
167 //glLoadIdentity();
168 //Mf::Scalar ratio = Mf::core.getVideo()->getWidth() /
169 //Mf::core.getVideo()->getHeight();
170 //gluPerspective(60.0, ratio, 1.0, 250.0);
171
172 //glMatrixMode(GL_MODELVIEW);
173 }
174
175
176 void Main::printUsage()
177 {
178 std::cout << "Usage: "
179 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
180 << std::endl
181 << "The alien-smashing action game." << std::endl
182 << std::endl
183 << "Options:" << std::endl
184 << " -h, --help" << std::endl
185 << " show this help and exit" << std::endl
186 << " -i, --info" << std::endl
187 << " show version and build information" << std::endl
188 << " detail=1|2|3" << std::endl
189 << " the level of detail of game scenes" << std::endl
190 << " fullscreen=true|false" << std::endl
191 << " if true, uses the entire display" << std::endl
192 << " framerate=num" << std::endl
193 << " number of frames to draw per second" << std::endl
194 << std::endl
195 << "See documentation for more options." << std::endl;
196 }
197
198 void Main::printInfo(int argc, char* argv[])
199 {
200 std::string assets;
201 std::string datadir;
202 std::string config;
203
204 assets.assign(YOINK_DATADIR);
205 int accessible = access(assets.c_str(), R_OK);
206 if (accessible != 0) assets += " (no access)";
207
208 char* temp = getenv("YOINK_DATADIR");
209 if (temp)
210 {
211 datadir = temp;
212 accessible = access(temp, R_OK);
213 if (accessible != 0) datadir += " (no access)";
214 }
215
216 temp = getenv("YOINKRC");
217 if (temp)
218 {
219 config = temp;
220 accessible = access(temp, R_OK);
221 if (accessible != 0) config += " (no access)";
222 }
223
224 std::cout << " Executable: " << argv[0] << std::endl
225 << " Version: "VERSION << std::endl
226 << " Built: " << COMPILE_TIME << std::endl
227 << " Compiler: "COMPILER_STRING << std::endl
228 << " Assets: " << assets << std::endl
229 << "Build options: "
230 #ifdef NDEBUG
231 << "-"
232 #endif
233 << "debug "
234 #ifndef USE_DOUBLE_PRECISION
235 << "-"
236 #endif
237 << "double-precision "
238 #ifndef USE_GTK
239 << "-"
240 #endif
241 << "gtk "
242 #ifndef PROFILING_ENABLED
243 << "-"
244 #endif
245 << "profile "
246 #ifndef USE_QT4
247 << "-"
248 #endif
249 << "qt4 "
250 #ifndef USE_THREADS
251 << "-"
252 #endif
253 << "threads" << std::endl
254 << " YOINKRC: " << config << std::endl
255 << "YOINK_DATADIR: " << datadir << std::endl;
256 }
257
258
259 void hello()
260 {
261 std::cout << std::endl << PACKAGE_STRING << std::endl
262 << "Compiled " << __TIME__ " " __DATE__ << std::endl
263 << "Send patches and bug reports to <"
264 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
265 }
266
267 void goodbye()
268 {
269 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
270 }
271
272
273 int main(int argc, char* argv[])
274 {
275 if (argc > 1)
276 {
277 std::string arg(argv[1]);
278 if (arg == "-h" || arg == "--help")
279 {
280 Main::printUsage();
281 return 0;
282 }
283 else if (arg == "-i" || arg == "--info")
284 {
285 Main::printInfo(argc, argv);
286 return 0;
287 }
288 }
289
290 hello();
291 atexit(goodbye);
292
293 Mf::Resource::addSearchPaths(Main::getSearchPath());
294
295 Mf::Settings settings(argc, argv, Main::getConfigPath());
296
297 Mf::Log::Level logLevel = Mf::Log::INFO;
298 settings.get("loglevel", logLevel);
299 Mf::Log::setLevel(logLevel);
300
301 try
302 {
303 Mf::Video::Attributes attributes(settings);
304 attributes.caption = PACKAGE_STRING;
305 attributes.icon = Mf::Resource::getPath(PACKAGE".png");
306
307 Mf::Video video(attributes);
308 Main mainView(settings, video);
309
310 mainView.run();
311 return 0;
312 }
313 catch (const Mf::Error& error)
314 {
315 Mf::ModalDialog dialog(Mf::ModalDialog::CRITICAL,
316 PACKAGE_STRING, "Unhandled Exception",
317 getErrorString(error));
318
319 dialog.run();
320 return 1;
321 }
322 }
323
This page took 0.045209 seconds and 4 git commands to generate.