]>
Dogcows Code - chaz/yoink/blob - src/Main.cc
a674bbcb0dd9675a560cd2e6564f8c17da1aa7b4
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
21 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>
41 Main::Main(moof::settings
& settings
) :
42 moof::application(settings
)
44 moof::dispatcher
& dispatcher
= moof::dispatcher::global();
45 video_reloaded_
= dispatcher
.add_target("video.newcontext",
46 boost::bind(&Main::setup_opengl
));
50 hotload_timer_
.init(boost::bind(&moof::resource::reload_as_needed
),
57 void Main::update(moof::scalar t
, moof::scalar dt
)
62 void Main::draw(moof::scalar alpha
) const
64 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
66 glMatrixMode(GL_PROJECTION
);
69 glMatrixMode(GL_MODELVIEW
);
75 void Main::handle_event(const moof::event
& event
)
77 if (yoink
.handle_event(event
)) return;
83 if (event
.key
.keysym
.sym
== SDLK_f
)
85 moof::video::current()->toggle_fullscreen();
87 else if (event
.key
.keysym
.sym
== SDLK_l
)
89 moof::video::current()->toggle_cursor_captured();
90 moof::video::current()->toggle_cursor_visible();
92 else if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
100 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
110 std::string
Main::search_paths()
112 // Add search paths; they should be searched in this order:
113 // 1. YOINK_DATADIR (environment)
114 // 2. YOINK_DATADIR (configure)
117 std::string datadir
= stlplus::env_vector()["YOINK_DATADIR"];
118 if (!datadir
.empty())
123 path
+= YOINK_DATADIR
;
130 std::string
Main::config_paths()
132 // Build the list of config files to search for, in this order:
133 // 1. YOINK_DATADIR/yoinkrc
134 // 2. /etc/yoinkrc (not for Windows)
136 // 4. YOINKRC (environment)
138 std::string path
= moof::resource::find_file("yoinkrc");
141 path
+= ":/etc/yoinkrc";
143 path
+= ":$HOME/.yoinkrc";
145 std::string rc_file
= stlplus::env_vector()["YOINKRC"];
146 if (!rc_file
.empty())
156 void Main::setup_opengl()
158 glEnable(GL_TEXTURE_2D
);
159 glEnable(GL_DEPTH_TEST
);
160 //glEnable(GL_CULL_FACE);
162 glEnable(GL_POINT_SMOOTH
);
163 glEnable(GL_LINE_SMOOTH
);
164 glEnable(GL_POLYGON_SMOOTH
);
165 glShadeModel(GL_SMOOTH
);
167 //glEnable(GL_BLEND);
168 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
169 glEnable(GL_ALPHA_TEST
);
170 glAlphaFunc(GL_GREATER
, 0.0);
172 glClearColor(1.0, 0.0, 0.0, 1.0);
174 //glEnable(GL_LIGHTING);
177 glEnable(GL_COLOR_MATERIAL
);
178 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
180 float amb
[] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
181 float dif
[] = {0.6f
, 0.6f
, 0.6f
, 1.0f
};
182 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
183 glLightfv(GL_LIGHT0
, GL_AMBIENT
, amb
);
184 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, dif
);
186 float spec
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
187 glLightfv(GL_LIGHT0
, GL_SPECULAR
, spec
);
191 void Main::print_usage()
193 std::cout
<< "Usage: "
194 << PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
196 << "The alien-smashing action game." << std::endl
198 << "Options:" << std::endl
199 << " -h, --help" << std::endl
200 << " show this help and exit" << std::endl
201 << " -i, --info" << std::endl
202 << " show version and build information" << std::endl
203 << " detail=1|2|3" << std::endl
204 << " the level of detail of game scenes" << std::endl
205 << " fullscreen=true|false" << std::endl
206 << " if true, uses the entire display" << std::endl
207 << " framerate=num" << std::endl
208 << " number of frames to draw per second" << std::endl
210 << "See documentation for more options." << std::endl
;
213 void Main::print_info(int argc
, char* argv
[])
215 #if INCLUDE_CONFIG_FILE
216 extern char data_config_mk
[];
217 std::cout
<< data_config_mk
;
219 std::cout
<< std::endl
<< "Configuration not included." << std::endl
;
223 void Main::print_option(const std::string
& option
, bool enabled
)
227 if (enabled
) std::cout
<< "\033[1;94m";
228 else std::cout
<< "\033[1m";
230 if (!enabled
) std::cout
<< "-";
232 if (isatty(1) == 1) std::cout
<< "\033[0m";
239 if (isatty(1) == 1) std::cout
<< "\033[94m";
240 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
241 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
242 << "Send patches and bug reports to <"
243 PACKAGE_BUGREPORT
<< ">." << std::endl
<< moof::log::endl
;
248 if (isatty(1) == 1) std::cout
<< "\033[94m";
249 std::cout
<< std::endl
<< "Goodbye." << std::endl
<< moof::log::endl
;
253 int main(int argc
, char* argv
[])
257 std::string
arg(argv
[1]);
258 if (arg
== "-h" || arg
== "--help")
263 else if (arg
== "-i" || arg
== "--info")
265 Main::print_info(argc
, argv
);
273 moof::backend backend
;
275 moof::resource::set_search_paths(Main::search_paths());
277 moof::settings
settings(argc
, argv
, Main::config_paths());
279 enum moof::log::level logLevel
= moof::log::info
;
280 settings
.get("loglevel", logLevel
);
281 moof::log::level(logLevel
);
285 moof::image_handle
icon("yoink", "png");
286 if (icon
) icon
->set_as_icon();
287 else moof::log_error("no icon loaded");
290 class moof::video::attributes
attributes(settings
);
291 moof::video
video(PACKAGE_STRING
, attributes
);
292 video
.show_fps(true);
297 catch (const std::exception
& e
)
299 moof::modal_dialog
dialog(moof::modal_dialog::error
,
300 PACKAGE_STRING
, "unhandled exception",
304 catch (const char* e
)
306 moof::modal_dialog
dialog(moof::modal_dialog::error
,
307 PACKAGE_STRING
, "unhandled exception",
This page took 0.0555 seconds and 4 git commands to generate.