]>
Dogcows Code - chaz/yoink/blob - src/Main.cc
e16803739ae6e809d5bf049040debbb3b75e93fd
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/log.hh>
30 #include <moof/modal_dialog.hh>
31 #include <moof/opengl.hh>
32 #include <moof/resource.hh>
33 #include <moof/settings.hh>
34 #include <moof/string.hh>
35 #include <moof/video.hh>
37 #include "GameLayer.hh"
39 #include "TitleLayer.hh"
43 Main::Main(moof::settings
& settings
) :
44 moof::application(settings
)
46 moof::dispatcher
& dispatcher
= moof::dispatcher::global();
47 video_reloaded_
= dispatcher
.add_target("video.newcontext",
48 boost::bind(&Main::setup_opengl
));
52 hotload_timer_
.init(boost::bind(&moof::resource::reload_as_needed
),
59 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
);
74 void Main::handle_event(const moof::event
& event
)
80 if (event
.key
.keysym
.sym
== SDLK_f
)
82 moof::video::current()->toggle_fullscreen();
84 else if (event
.key
.keysym
.sym
== SDLK_l
)
86 moof::video::current()->toggle_cursor_captured();
87 moof::video::current()->toggle_cursor_visible();
89 else if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
97 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
107 std::string
Main::search_paths()
109 // Add search paths; they should be searched in this order:
110 // 1. YOINK_DATADIR (environment)
111 // 2. YOINK_DATADIR (configure)
114 std::string datadir
= stlplus::env_vector()["YOINK_DATADIR"];
115 if (!datadir
.empty())
120 path
+= YOINK_DATADIR
;
125 std::string
Main::config_paths()
127 // Build the list of config files to search for, in this order:
128 // 1. YOINK_DATADIR/yoinkrc
129 // 2. /etc/yoinkrc (not for Windows)
131 // 4. YOINKRC (environment)
133 std::string path
= moof::resource::find_file("yoinkrc");
136 path
+= ":/etc/yoinkrc";
138 path
+= ":$HOME/.yoinkrc";
140 std::string rc_file
= stlplus::env_vector()["YOINKRC"];
141 if (!rc_file
.empty())
151 void Main::setup_opengl()
153 //glEnable(GL_TEXTURE_2D);
154 glEnable(GL_DEPTH_TEST
);
155 //glEnable(GL_CULL_FACE);
157 glEnable(GL_POINT_SMOOTH
);
158 glEnable(GL_LINE_SMOOTH
);
159 glEnable(GL_POLYGON_SMOOTH
);
160 glShadeModel(GL_SMOOTH
);
162 //glEnable(GL_BLEND);
163 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
164 glEnable(GL_ALPHA_TEST
);
165 glAlphaFunc(GL_GREATER
, 0.0);
167 glClearColor(1.0, 0.0, 0.0, 1.0);
169 //glEnable(GL_LIGHTING);
172 glEnable(GL_COLOR_MATERIAL
);
173 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
175 float amb
[] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
176 float dif
[] = {0.6f
, 0.6f
, 0.6f
, 1.0f
};
177 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
178 glLightfv(GL_LIGHT0
, GL_AMBIENT
, amb
);
179 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, dif
);
181 float spec
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
182 glLightfv(GL_LIGHT0
, GL_SPECULAR
, spec
);
186 void Main::print_usage()
188 std::cout
<< "Usage: "
189 << PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
191 << "The alien-smashing action game." << std::endl
193 << "Options:" << std::endl
194 << " -h, --help" << std::endl
195 << " show this help and exit" << std::endl
196 << " -i, --info" << std::endl
197 << " show version and build information" << std::endl
198 << " detail=1|2|3" << std::endl
199 << " the level of detail of game scenes" << std::endl
200 << " fullscreen=true|false" << std::endl
201 << " if true, uses the entire display" << std::endl
202 << " framerate=num" << std::endl
203 << " number of frames to draw per second" << std::endl
205 << "See documentation for more options." << std::endl
;
209 void Main::print_info(int argc
, char* argv
[])
211 bool color
= (isatty(1) == 1);
213 stlplus::env_vector environment
;
216 assets
.assign(YOINK_DATADIR
);
217 if (!stlplus::file_readable(assets
))
220 if (color
) assets
+= "\033[1;91m";
221 assets
+= "(no access)";
222 if (color
) assets
+= "\033[0m";
225 std::string datadir
= environment
["YOINK_DATADIR"];
226 if (!datadir
.empty())
228 if (!stlplus::folder_readable(datadir
))
231 if (color
) datadir
+= "\033[1;91m";
232 datadir
+= "(no access)";
233 if (color
) datadir
+= "\033[0m";
237 std::string rc_file
= environment
["YOINKRC"];
238 if (!rc_file
.empty())
240 if (!stlplus::file_readable(rc_file
)) rc_file
+= " (no access)";
243 std::cout
<< " Executable: " << argv
[0] << std::endl
245 << " Commit: "YOINK_GITHEAD
<< std::endl
247 << " Version: " << PACKAGE_VERSION
<< std::endl
248 << " Built: " << COMPILE_TIME
<< std::endl
249 << " Compiler: " << COMPILER_STRING
<< std::endl
250 << " Assets: " << assets
<< std::endl
251 << "Build options: ";
253 #if ENABLE_CLOCK_GETTIME
254 print_option("clock_gettime", true);
256 print_option("clock_gettime", false);
259 print_option("debug", true);
261 print_option("debug", false);
264 print_option("gtk", true);
266 print_option("gtk", false);
268 #if ENABLE_HOTLOADING
269 print_option("hotload", true);
271 print_option("hotload", false);
274 print_option("profile", true);
276 print_option("profile", false);
279 print_option("qt4", true);
281 print_option("qt4", false);
284 print_option("threads", true);
286 print_option("threads", false);
288 std::cout
<< std::endl
;
289 std::cout
<< " YOINKRC: " << rc_file
<< std::endl
290 << "YOINK_DATADIR: " << datadir
<< std::endl
;
293 void Main::print_option(const std::string
& option
, bool enabled
)
297 if (enabled
) std::cout
<< "\033[1;94m";
298 else std::cout
<< "\033[1m";
300 if (!enabled
) std::cout
<< "-";
302 if (isatty(1) == 1) std::cout
<< "\033[0m";
309 if (isatty(1) == 1) std::cout
<< "\033[94m";
310 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
311 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
312 << "Send patches and bug reports to <"
313 PACKAGE_BUGREPORT
<< ">." << std::endl
<< moof::log::endl
;
318 if (isatty(1) == 1) std::cout
<< "\033[94m";
319 std::cout
<< std::endl
<< "Goodbye." << std::endl
<< moof::log::endl
;
323 int main(int argc
, char* argv
[])
325 moof::backend backend
;
329 std::string
arg(argv
[1]);
330 if (arg
== "-h" || arg
== "--help")
335 else if (arg
== "-i" || arg
== "--info")
337 Main::print_info(argc
, argv
);
345 moof::resource::set_search_paths(Main::search_paths());
347 moof::settings
settings(argc
, argv
, Main::config_paths());
349 enum moof::log::level logLevel
= moof::log::info
;
350 settings
.get("loglevel", logLevel
);
351 moof::log::level(logLevel
);
355 moof::image_handle
icon(PACKAGE
, "png");
356 if (icon
) icon
->set_as_icon();
357 else moof::log_error("no icon loaded");
360 class moof::video::attributes
attributes(settings
);
361 moof::video
video(PACKAGE_STRING
, attributes
);
366 catch (const std::exception
& e
)
368 moof::modal_dialog
dialog(moof::modal_dialog::error
,
369 PACKAGE_STRING
, "unhandled exception",
373 catch (const char* e
)
375 moof::modal_dialog
dialog(moof::modal_dialog::error
,
376 PACKAGE_STRING
, "unhandled exception",
This page took 0.04731 seconds and 3 git commands to generate.