]>
Dogcows Code - chaz/yoink/blob - src/Main.cc
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
14 #include <cstdlib> // atexit
23 inline int isatty(int dummy
) { return 0; }
26 #include <stlplus/portability/file_system.hpp>
27 #include <stlplus/portability/subprocesses.hpp>
29 #include <moof/image.hh>
30 #include <moof/log.hh>
31 #include <moof/modal_dialog.hh>
32 #include <moof/opengl.hh>
33 #include <moof/resource.hh>
34 #include <moof/settings.hh>
35 #include <moof/string.hh>
36 #include <moof/video.hh>
42 Main::Main(moof::settings
& settings
) :
43 moof::application(settings
)
45 moof::dispatcher
& dispatcher
= moof::dispatcher::global();
46 video_reloaded_
= dispatcher
.add_target("video.newcontext",
47 boost::bind(&Main::setup_opengl
));
51 hotload_timer_
.init(boost::bind(&moof::resource::reload_as_needed
),
58 void Main::update(moof::scalar t
, moof::scalar dt
)
63 void Main::draw(moof::scalar alpha
) const
65 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
67 glMatrixMode(GL_PROJECTION
);
70 glMatrixMode(GL_MODELVIEW
);
76 void Main::handle_event(const moof::event
& event
)
78 if (yoink
.handle_event(event
)) return;
84 if (event
.key
.keysym
.sym
== SDLK_f
)
86 moof::video::current()->toggle_fullscreen();
88 else if (event
.key
.keysym
.sym
== SDLK_l
)
90 moof::video::current()->toggle_cursor_captured();
91 moof::video::current()->toggle_cursor_visible();
93 else if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
101 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
111 std::string
Main::search_paths()
113 // Add search paths; they should be searched in this order:
114 // 1. YOINK_DATADIR (environment)
115 // 2. YOINK_DATADIR (configure)
118 std::string datadir
= stlplus::env_vector()["YOINK_DATADIR"];
119 if (!datadir
.empty())
124 path
+= YOINK_DATADIR
;
129 std::string
Main::config_paths()
131 // Build the list of config files to search for, in this order:
132 // 1. YOINK_DATADIR/yoinkrc
133 // 2. /etc/yoinkrc (not for Windows)
135 // 4. YOINKRC (environment)
137 std::string path
= moof::resource::find_file("yoinkrc");
140 path
+= ":/etc/yoinkrc";
142 path
+= ":$HOME/.yoinkrc";
144 std::string rc_file
= stlplus::env_vector()["YOINKRC"];
145 if (!rc_file
.empty())
155 void Main::setup_opengl()
157 glEnable(GL_TEXTURE_2D
);
158 glEnable(GL_DEPTH_TEST
);
159 //glEnable(GL_CULL_FACE);
161 glEnable(GL_POINT_SMOOTH
);
162 glEnable(GL_LINE_SMOOTH
);
163 glEnable(GL_POLYGON_SMOOTH
);
164 glShadeModel(GL_SMOOTH
);
166 //glEnable(GL_BLEND);
167 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
168 glEnable(GL_ALPHA_TEST
);
169 glAlphaFunc(GL_GREATER
, 0.0);
171 glClearColor(1.0, 0.0, 0.0, 1.0);
173 //glEnable(GL_LIGHTING);
176 glEnable(GL_COLOR_MATERIAL
);
177 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
179 float amb
[] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
180 float dif
[] = {0.6f
, 0.6f
, 0.6f
, 1.0f
};
181 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
182 glLightfv(GL_LIGHT0
, GL_AMBIENT
, amb
);
183 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, dif
);
185 float spec
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
186 glLightfv(GL_LIGHT0
, GL_SPECULAR
, spec
);
190 void Main::print_usage()
192 std::cout
<< "Usage: "
193 << PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
195 << "The alien-smashing action game." << std::endl
197 << "Options:" << std::endl
198 << " -h, --help" << std::endl
199 << " show this help and exit" << std::endl
200 << " -i, --info" << std::endl
201 << " show version and build information" << std::endl
202 << " detail=1|2|3" << std::endl
203 << " the level of detail of game scenes" << std::endl
204 << " fullscreen=true|false" << std::endl
205 << " if true, uses the entire display" << std::endl
206 << " framerate=num" << std::endl
207 << " number of frames to draw per second" << std::endl
209 << "See documentation for more options." << std::endl
;
213 void Main::print_info(int argc
, char* argv
[])
215 bool color
= (isatty(1) == 1);
217 stlplus::env_vector environment
;
220 assets
.assign(YOINK_DATADIR
);
221 if (!stlplus::file_readable(assets
))
224 if (color
) assets
+= "\033[1;91m";
225 assets
+= "(no access)";
226 if (color
) assets
+= "\033[0m";
229 std::string datadir
= environment
["YOINK_DATADIR"];
230 if (!datadir
.empty())
232 if (!stlplus::folder_readable(datadir
))
235 if (color
) datadir
+= "\033[1;91m";
236 datadir
+= "(no access)";
237 if (color
) datadir
+= "\033[0m";
241 std::string rc_file
= environment
["YOINKRC"];
242 if (!rc_file
.empty())
244 if (!stlplus::file_readable(rc_file
)) rc_file
+= " (no access)";
247 std::cout
<< " Executable: " << argv
[0] << std::endl
249 << " Commit: "YOINK_GITHEAD
<< std::endl
251 << " Version: " << PACKAGE_VERSION
<< std::endl
252 << " Built: " << COMPILE_TIME
<< std::endl
253 << " Compiler: " << COMPILER_STRING
<< std::endl
254 << " Assets: " << assets
<< std::endl
255 << "Build options: ";
257 #if ENABLE_CLOCK_GETTIME
258 print_option("clock_gettime", true);
260 print_option("clock_gettime", false);
263 print_option("debug", true);
265 print_option("debug", false);
267 #if ENABLE_DOUBLE_PRECISION
268 print_option("double", true);
270 print_option("double", false);
273 print_option("gtk", true);
275 print_option("gtk", false);
277 #if ENABLE_HOTLOADING
278 print_option("hotload", true);
280 print_option("hotload", false);
283 print_option("profile", true);
285 print_option("profile", false);
288 print_option("qt4", true);
290 print_option("qt4", false);
293 print_option("threads", true);
295 print_option("threads", false);
297 std::cout
<< std::endl
;
298 std::cout
<< " YOINKRC: " << rc_file
<< std::endl
299 << "YOINK_DATADIR: " << datadir
<< std::endl
;
302 void Main::print_option(const std::string
& option
, bool enabled
)
306 if (enabled
) std::cout
<< "\033[1;94m";
307 else std::cout
<< "\033[1m";
309 if (!enabled
) std::cout
<< "-";
311 if (isatty(1) == 1) std::cout
<< "\033[0m";
318 if (isatty(1) == 1) std::cout
<< "\033[94m";
319 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
320 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
321 << "Send patches and bug reports to <"
322 PACKAGE_BUGREPORT
<< ">." << std::endl
<< moof::log::endl
;
327 if (isatty(1) == 1) std::cout
<< "\033[94m";
328 std::cout
<< std::endl
<< "Goodbye." << std::endl
<< moof::log::endl
;
332 int main(int argc
, char* argv
[])
334 moof::backend backend
;
338 std::string
arg(argv
[1]);
339 if (arg
== "-h" || arg
== "--help")
344 else if (arg
== "-i" || arg
== "--info")
346 Main::print_info(argc
, argv
);
354 moof::resource::set_search_paths(Main::search_paths());
356 moof::settings
settings(argc
, argv
, Main::config_paths());
358 enum moof::log::level logLevel
= moof::log::info
;
359 settings
.get("loglevel", logLevel
);
360 moof::log::level(logLevel
);
364 moof::image_handle
icon(PACKAGE
, "png");
365 if (icon
) icon
->set_as_icon();
366 else moof::log_error("no icon loaded");
369 class moof::video::attributes
attributes(settings
);
370 moof::video
video(PACKAGE_STRING
, attributes
);
371 video
.show_fps(true);
376 catch (const std::exception
& e
)
378 moof::modal_dialog
dialog(moof::modal_dialog::error
,
379 PACKAGE_STRING
, "unhandled exception",
383 catch (const char* e
)
385 moof::modal_dialog
dialog(moof::modal_dialog::error
,
386 PACKAGE_STRING
, "unhandled exception",
This page took 0.048636 seconds and 4 git commands to generate.