]> Dogcows Code - chaz/yoink/blob - src/Main.cc
ae183ec49ab845ee9f5921f8b47642e6445526be
[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 "config.h"
13
14 #include <cstdlib> // atexit
15 #include <exception>
16 #include <functional>
17 #include <iostream>
18 #include <string>
19
20 #include <stlplus/portability/file_system.hpp>
21 #include <stlplus/portability/subprocesses.hpp>
22
23 #include <moof/log.hh>
24 #include <moof/modal_dialog.hh>
25 #include <moof/opengl.hh>
26 #include <moof/resource.hh>
27 #include <moof/settings.hh>
28 #include <moof/video.hh>
29
30 #include "GameLayer.hh"
31 #include "Main.hh"
32 #include "TitleLayer.hh"
33 #include "version.h"
34
35
36 Main::Main(moof::settings& settings, moof::video& video) :
37 moof::view(settings, video)
38 {
39 moof::dispatcher& dispatcher = moof::dispatcher::global();
40 mNewContextDispatch = dispatcher.add_target("video.newcontext",
41 boost::bind(&Main::setupGL));
42 setupGL();
43 }
44
45
46 void Main::update(moof::scalar t, moof::scalar dt)
47 {
48 if (children().size() == 0)
49 {
50 //moof::log_warning("main view has no children");
51 //stop();
52 //return;
53 add_child(TitleLayer::alloc());
54 }
55
56 moof::view::update(t, dt);
57 }
58
59 void Main::draw(moof::scalar alpha) const
60 {
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62
63 glMatrixMode(GL_PROJECTION);
64 glLoadIdentity();
65
66 glMatrixMode(GL_MODELVIEW);
67 glLoadIdentity();
68
69 moof::view::draw(alpha);
70 }
71
72 bool Main::handle_event(const moof::event& event)
73 {
74 if (moof::view::handle_event(event)) return true;
75
76 switch (event.type)
77 {
78 case SDL_KEYUP:
79 if (event.key.keysym.sym == SDLK_f)
80 {
81 video().toggle_fullscreen();
82 }
83 else if (event.key.keysym.sym == SDLK_l)
84 {
85 video().toggle_cursor_captured();
86 video().toggle_cursor_visible();
87 }
88 break;
89
90 case SDL_VIDEORESIZE:
91 glViewport(0, 0, event.resize.w, event.resize.h);
92 break;
93
94 case SDL_QUIT:
95 stop();
96 return true;
97 }
98
99 return false;
100 }
101
102
103 std::string Main::getSearchPath()
104 {
105 // Add search paths; they should be searched in this order:
106 // 1. YOINK_DATADIR (environment)
107 // 2. YOINK_DATADIR (configure)
108
109 std::string path;
110 std::string datadir = stlplus::env_vector()["YOINK_DATADIR"];
111 if (!datadir.empty())
112 {
113 path += datadir;
114 path += ":";
115 }
116 path += YOINK_DATADIR;
117
118 return path;
119 }
120
121 std::string Main::getConfigPath()
122 {
123 // Build the list of config files to search for, in this order:
124 // 1. YOINK_DATADIR/yoinkrc
125 // 2. /etc/yoinkrc (not for Windows)
126 // 3. $HOME/.yoinkrc
127 // 4. YOINKRC (environment)
128
129 std::string path = moof::resource::find_file("yoinkrc");
130
131 #if !defined(_WIN32)
132 path += ":/etc/yoinkrc";
133 #endif
134 path += ":$HOME/.yoinkrc";
135
136 std::string rc_file = stlplus::env_vector()["YOINKRC"];
137 if (!rc_file.empty())
138 {
139 path += ":";
140 path += rc_file;
141 }
142
143 return path;
144 }
145
146
147 void Main::setupGL()
148 {
149 glEnable(GL_TEXTURE_2D);
150 glEnable(GL_DEPTH_TEST);
151
152 glEnable(GL_LINE_SMOOTH);
153 glEnable(GL_POLYGON_SMOOTH);
154 glShadeModel(GL_SMOOTH);
155
156 glEnable(GL_BLEND);
157 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 glEnable(GL_ALPHA_TEST);
159 glAlphaFunc(GL_GREATER, 0.0);
160
161 glClearColor(0.0, 0.0, 0.0, 1.0);
162
163 //glEnable(GL_LIGHTING);
164 glEnable(GL_LIGHT0);
165
166 float light[] = {1.0f, 1.0f, 1.0f, 1.0f};
167 glLightfv(GL_LIGHT0, GL_AMBIENT_AND_DIFFUSE, light);
168
169
170 //glMatrixMode(GL_PROJECTION);
171 //glLoadIdentity();
172 //moof::scalar ratio = moof::core.getVideo()->width() /
173 //moof::core.getVideo()->height();
174 //gluPerspective(60.0, ratio, 1.0, 250.0);
175
176 //glMatrixMode(GL_MODELVIEW);
177 }
178
179
180 void Main::printUsage()
181 {
182 std::cout << "Usage: "
183 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
184 << std::endl
185 << "The alien-smashing action game." << std::endl
186 << std::endl
187 << "Options:" << std::endl
188 << " -h, --help" << std::endl
189 << " show this help and exit" << std::endl
190 << " -i, --info" << std::endl
191 << " show version and build information" << std::endl
192 << " detail=1|2|3" << std::endl
193 << " the level of detail of game scenes" << std::endl
194 << " fullscreen=true|false" << std::endl
195 << " if true, uses the entire display" << std::endl
196 << " framerate=num" << std::endl
197 << " number of frames to draw per second" << std::endl
198 << std::endl
199 << "See documentation for more options." << std::endl;
200 }
201
202 void Main::printInfo(int argc, char* argv[])
203 {
204 stlplus::env_vector environment;
205
206 std::string assets;
207 assets.assign(YOINK_DATADIR);
208 if (!stlplus::file_readable(assets)) assets += " (no access)";
209
210 std::string datadir = environment["YOINK_DATADIR"];
211 if (!datadir.empty())
212 {
213 if (!stlplus::folder_readable(datadir)) datadir += " (no access)";
214 }
215
216 std::string rc_file = environment["YOINKRC"];
217 if (!rc_file.empty())
218 {
219 if (!stlplus::file_readable(rc_file)) rc_file += " (no access)";
220 }
221
222 std::cout << " Executable: " << argv[0] << std::endl
223 #ifdef YOINK_GITHEAD
224 << " Commit: "YOINK_GITHEAD << std::endl
225 #endif
226 << " Version: " << PACKAGE_VERSION << std::endl
227 << " Built: " << COMPILE_TIME << std::endl
228 << " Compiler: " << COMPILER_STRING << std::endl
229 << " Assets: " << assets << std::endl
230 << "Build options: "
231 #if !USE_CLOCK_GETTIME
232 << "-"
233 #endif
234 << "clock_gettime "
235 #if !DEBUG
236 << "-"
237 #endif
238 << "debug "
239 #if !USE_DOUBLE_PRECISION
240 << "-"
241 #endif
242 << "double-precision "
243 #if !USE_GTK
244 << "-"
245 #endif
246 << "gtk "
247 #if !USE_HOTLOADING
248 << "-"
249 #endif
250 << "hotloading "
251 #if !PROFILING_ENABLED
252 << "-"
253 #endif
254 << "profile "
255 #if !USE_QT4
256 << "-"
257 #endif
258 << "qt4 "
259 #if !USE_THREADS
260 << "-"
261 #endif
262 << "threads" << std::endl
263 << " YOINKRC: " << rc_file << std::endl
264 << "YOINK_DATADIR: " << datadir << std::endl;
265 }
266
267
268 void hello()
269 {
270 std::cout << std::endl << PACKAGE_STRING << std::endl
271 << "Compiled " << __TIME__ " " __DATE__ << std::endl
272 << "Send patches and bug reports to <"
273 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
274 }
275
276 void goodbye()
277 {
278 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
279 }
280
281
282 int main(int argc, char* argv[])
283 {
284 moof::backend backend;
285
286 // FIXME: This is temporary.
287 moof::timer reloadTimer(boost::bind(&moof::resource::reload_as_needed),
288 SCALAR(2.0),
289 moof::timer::repeat);
290
291 if (argc > 1)
292 {
293 std::string arg(argv[1]);
294 if (arg == "-h" || arg == "--help")
295 {
296 Main::printUsage();
297 return 0;
298 }
299 else if (arg == "-i" || arg == "--info")
300 {
301 Main::printInfo(argc, argv);
302 return 0;
303 }
304 }
305
306 hello();
307 atexit(goodbye);
308
309 moof::resource::add_search_paths(Main::getSearchPath());
310
311 moof::settings settings(argc, argv, Main::getConfigPath());
312
313 enum moof::log::level logLevel = moof::log::info;
314 settings.get("loglevel", logLevel);
315 moof::log::level(logLevel);
316
317 try
318 {
319 //std::string iconPath(PACKAGE".png");
320 //iconPath = moof::resource::find_file(iconPath);
321 //moof::image icon(iconPath);
322 //icon.set_as_icon();
323 moof::image_handle icon(PACKAGE, "png");
324 if (icon) icon->set_as_icon();
325 else moof::log_error("no icon loaded");
326 icon.unload();
327
328 class moof::video::attributes attributes(settings);
329 moof::video video(PACKAGE_STRING, attributes);
330 Main mainView(settings, video);
331
332 mainView.run();
333 return 0;
334 }
335 catch (const std::exception& e)
336 {
337 moof::modal_dialog dialog(moof::modal_dialog::error,
338 PACKAGE_STRING, "Unhandled Exception",
339 e.what());
340
341 dialog.run();
342 return 1;
343 }
344 }
345
This page took 0.045492 seconds and 3 git commands to generate.