]>
Dogcows Code - chaz/yoink/blob - src/Main.cc
c76a6148bf84abb6a8b48c03f92f1e6de4055185
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
, moof::video
& video
) :
44 moof::view(settings
, video
)
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
)
61 if (children().size() == 0)
63 //moof::log_warning("main view has no children");
66 add_child(TitleLayer::alloc());
69 moof::view::update(t
, dt
);
72 void Main::draw(moof::scalar alpha
) const
74 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
76 glMatrixMode(GL_PROJECTION
);
79 glMatrixMode(GL_MODELVIEW
);
82 moof::view::draw(alpha
);
85 bool Main::handle_event(const moof::event
& event
)
87 if (moof::view::handle_event(event
)) return true;
92 if (event
.key
.keysym
.sym
== SDLK_f
)
94 video().toggle_fullscreen();
96 else if (event
.key
.keysym
.sym
== SDLK_l
)
98 video().toggle_cursor_captured();
99 video().toggle_cursor_visible();
103 case SDL_VIDEORESIZE
:
104 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
116 std::string
Main::search_paths()
118 // Add search paths; they should be searched in this order:
119 // 1. YOINK_DATADIR (environment)
120 // 2. YOINK_DATADIR (configure)
123 std::string datadir
= stlplus::env_vector()["YOINK_DATADIR"];
124 if (!datadir
.empty())
129 path
+= YOINK_DATADIR
;
134 std::string
Main::config_paths()
136 // Build the list of config files to search for, in this order:
137 // 1. YOINK_DATADIR/yoinkrc
138 // 2. /etc/yoinkrc (not for Windows)
140 // 4. YOINKRC (environment)
142 std::string path
= moof::resource::find_file("yoinkrc");
145 path
+= ":/etc/yoinkrc";
147 path
+= ":$HOME/.yoinkrc";
149 std::string rc_file
= stlplus::env_vector()["YOINKRC"];
150 if (!rc_file
.empty())
160 void Main::setup_opengl()
162 //glEnable(GL_TEXTURE_2D);
163 glEnable(GL_DEPTH_TEST
);
164 //glEnable(GL_CULL_FACE);
166 glEnable(GL_POINT_SMOOTH
);
167 glEnable(GL_LINE_SMOOTH
);
168 glEnable(GL_POLYGON_SMOOTH
);
169 glShadeModel(GL_SMOOTH
);
171 //glEnable(GL_BLEND);
172 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
173 glEnable(GL_ALPHA_TEST
);
174 glAlphaFunc(GL_GREATER
, 0.0);
176 glClearColor(0.0, 0.0, 0.0, 1.0);
178 //glEnable(GL_LIGHTING);
181 glEnable(GL_COLOR_MATERIAL
);
182 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
184 float amb
[] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
185 float dif
[] = {0.6f
, 0.6f
, 0.6f
, 1.0f
};
186 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
187 glLightfv(GL_LIGHT0
, GL_AMBIENT
, amb
);
188 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, dif
);
190 float spec
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
191 glLightfv(GL_LIGHT0
, GL_SPECULAR
, spec
);
195 void Main::print_usage()
197 std::cout
<< "Usage: "
198 << PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
200 << "The alien-smashing action game." << std::endl
202 << "Options:" << std::endl
203 << " -h, --help" << std::endl
204 << " show this help and exit" << std::endl
205 << " -i, --info" << std::endl
206 << " show version and build information" << std::endl
207 << " detail=1|2|3" << std::endl
208 << " the level of detail of game scenes" << std::endl
209 << " fullscreen=true|false" << std::endl
210 << " if true, uses the entire display" << std::endl
211 << " framerate=num" << std::endl
212 << " number of frames to draw per second" << std::endl
214 << "See documentation for more options." << std::endl
;
218 void Main::print_info(int argc
, char* argv
[])
220 bool color
= (isatty(1) == 1);
222 stlplus::env_vector environment
;
225 assets
.assign(YOINK_DATADIR
);
226 if (!stlplus::file_readable(assets
))
229 if (color
) assets
+= "\033[1;91m";
230 assets
+= "(no access)";
231 if (color
) assets
+= "\033[0m";
234 std::string datadir
= environment
["YOINK_DATADIR"];
235 if (!datadir
.empty())
237 if (!stlplus::folder_readable(datadir
))
240 if (color
) datadir
+= "\033[1;91m";
241 datadir
+= "(no access)";
242 if (color
) datadir
+= "\033[0m";
246 std::string rc_file
= environment
["YOINKRC"];
247 if (!rc_file
.empty())
249 if (!stlplus::file_readable(rc_file
)) rc_file
+= " (no access)";
252 std::cout
<< " Executable: " << argv
[0] << std::endl
254 << " Commit: "YOINK_GITHEAD
<< std::endl
256 << " Version: " << PACKAGE_VERSION
<< std::endl
257 << " Built: " << COMPILE_TIME
<< std::endl
258 << " Compiler: " << COMPILER_STRING
<< std::endl
259 << " Assets: " << assets
<< std::endl
260 << "Build options: ";
262 #if ENABLE_CLOCK_GETTIME
263 print_option("clock_gettime", true);
265 print_option("clock_gettime", false);
268 print_option("debug", true);
270 print_option("debug", 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);
282 #if PROFILING_ENABLED
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 //std::string iconPath(PACKAGE".png");
365 //iconPath = moof::resource::find_file(iconPath);
366 //moof::image icon(iconPath);
367 //icon.set_as_icon();
368 moof::image_handle
icon(PACKAGE
, "png");
369 if (icon
) icon
->set_as_icon();
370 else moof::log_error("no icon loaded");
373 class moof::video::attributes
attributes(settings
);
374 moof::video
video(PACKAGE_STRING
, attributes
);
375 Main
mainView(settings
, video
);
380 catch (const std::exception
& e
)
382 moof::modal_dialog
dialog(moof::modal_dialog::error
,
383 PACKAGE_STRING
, "unhandled exception",
387 catch (const char* e
)
389 moof::modal_dialog
dialog(moof::modal_dialog::error
,
390 PACKAGE_STRING
, "unhandled exception",
This page took 0.052228 seconds and 4 git commands to generate.