]>
Dogcows Code - chaz/yoink/blob - src/Main.cc
8b011638735b48061d32fdafcdbf8096095bbfaa
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
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>
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
),
56 void Main::update(moof::scalar t
, moof::scalar dt
)
61 void Main::draw(moof::scalar alpha
) const
63 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
65 glMatrixMode(GL_PROJECTION
);
68 glMatrixMode(GL_MODELVIEW
);
74 void Main::handle_event(const moof::event
& event
)
76 if (yoink
.handle_event(event
)) return;
81 if (event
.key
.keysym
.sym
== SDLK_f
)
83 moof::video::current()->toggle_fullscreen();
85 else if (event
.key
.keysym
.sym
== SDLK_l
)
87 moof::video::current()->toggle_cursor_captured();
88 moof::video::current()->toggle_cursor_visible();
90 else if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
97 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
105 std::string
Main::search_paths()
107 // Add search paths; they should be searched in this order:
108 // 1. YOINK_DATADIR (environment)
109 // 2. YOINK_DATADIR (configure)
112 std::string datadir
= stlplus::env_vector()["YOINK_DATADIR"];
113 if (!datadir
.empty())
118 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())
150 void Main::setup_opengl()
152 glEnable(GL_TEXTURE_2D
);
153 glEnable(GL_DEPTH_TEST
);
154 //glEnable(GL_CULL_FACE);
156 glEnable(GL_POINT_SMOOTH
);
157 glEnable(GL_LINE_SMOOTH
);
158 glEnable(GL_POLYGON_SMOOTH
);
159 glShadeModel(GL_SMOOTH
);
161 //glEnable(GL_BLEND);
162 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 glEnable(GL_ALPHA_TEST
);
164 glAlphaFunc(GL_GREATER
, 0.0);
166 glClearColor(1.0, 0.0, 0.0, 1.0);
168 //glEnable(GL_LIGHTING);
171 glEnable(GL_COLOR_MATERIAL
);
172 glColorMaterial(GL_FRONT_AND_BACK
, GL_AMBIENT_AND_DIFFUSE
);
174 float amb
[] = {0.1f
, 0.1f
, 0.1f
, 1.0f
};
175 float dif
[] = {0.6f
, 0.6f
, 0.6f
, 1.0f
};
176 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
177 glLightfv(GL_LIGHT0
, GL_AMBIENT
, amb
);
178 glLightfv(GL_LIGHT0
, GL_DIFFUSE
, dif
);
180 float spec
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
181 glLightfv(GL_LIGHT0
, GL_SPECULAR
, spec
);
184 void Main::print_usage()
186 std::cout
<< "Usage: "
187 << PACKAGE
" [-h|--help] [-i|--info] [OPTION=VALUE]..."
189 << "The alien-smashing action game." << std::endl
191 << "Options:" << std::endl
192 << " -h, --help" << std::endl
193 << " show this help and exit" << std::endl
194 << " -i, --info" << std::endl
195 << " show version and build information" << std::endl
196 << " detail=1|2|3" << std::endl
197 << " the level of detail of game scenes" << std::endl
198 << " fullscreen=true|false" << std::endl
199 << " if true, uses the entire display" << std::endl
200 << " framerate=num" << std::endl
201 << " number of frames to draw per second" << std::endl
203 << "See documentation for more options." << std::endl
;
206 void Main::print_info(int argc
, char* argv
[])
208 #if INCLUDE_CONFIG_FILE
209 extern char data_config_mk
[];
210 std::cout
<< data_config_mk
;
212 std::cout
<< std::endl
<< "Configuration not included." << std::endl
;
219 if (isatty(1) == 1) std::cout
<< "\033[94m";
220 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
221 << "Compiled " << __TIME__
" "__DATE__
<< std::endl
222 << "Send patches and bug reports to <"PACKAGE_BUGREPORT
">."
223 << std::endl
<< moof::log::endl
;
228 if (isatty(1) == 1) std::cout
<< "\033[94m";
229 std::cout
<< std::endl
<< "Goodbye." << std::endl
<< moof::log::endl
;
233 int main(int argc
, char* argv
[])
237 std::string
arg(argv
[1]);
238 if (arg
== "-h" || arg
== "--help")
243 else if (arg
== "-i" || arg
== "--info")
245 Main::print_info(argc
, argv
);
253 moof::backend backend
;
255 moof::resource::set_search_paths(Main::search_paths());
257 moof::settings
settings(argc
, argv
, Main::config_paths());
259 enum moof::log::level logLevel
= moof::log::info
;
260 settings
.get("loglevel", logLevel
);
261 moof::log::level(logLevel
);
265 moof::image_handle
icon("yoink", "png");
266 if (icon
) icon
->set_as_icon();
267 else moof::log_error("no icon loaded");
270 class moof::video::attributes
attributes(settings
);
271 moof::video
video(PACKAGE_STRING
, attributes
);
272 video
.show_fps(true);
277 catch (const std::exception
& e
)
279 moof::modal_dialog
dialog(moof::modal_dialog::error
,
280 PACKAGE_STRING
, "unhandled exception",
284 catch (const char* e
)
286 moof::modal_dialog
dialog(moof::modal_dialog::error
,
287 PACKAGE_STRING
, "unhandled exception", e
);
This page took 0.047284 seconds and 4 git commands to generate.