]> Dogcows Code - chaz/yoink/blob - src/Main.cc
build system enhancements
[chaz/yoink] / src / Main.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
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.
9 *
10 **************************************************************************/
11
12 #include "config.h"
13
14 #include <cstdlib> // atexit
15 #include <exception>
16 #include <functional>
17 #include <iostream>
18 #include <string>
19
20 #if PLATFORM_WIN32
21 inline int isatty(int dummy) { return 0; }
22 #else
23 #include <termios.h>
24 #endif
25
26 #include <stlplus/portability/file_system.hpp>
27 #include <stlplus/portability/subprocesses.hpp>
28
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>
37
38 #include "Main.hh"
39
40
41 Main::Main(moof::settings& settings) :
42 moof::application(settings)
43 {
44 moof::dispatcher& dispatcher = moof::dispatcher::global();
45 video_reloaded_ = dispatcher.add_target("video.newcontext",
46 boost::bind(&Main::setup_opengl));
47 setup_opengl();
48
49 #if ENABLE_HOTLOADING
50 hotload_timer_.init(boost::bind(&moof::resource::reload_as_needed),
51 SCALAR(0.25),
52 moof::timer::repeat);
53 #endif
54 }
55
56
57 void Main::update(moof::scalar t, moof::scalar dt)
58 {
59 yoink.update(t, dt);
60 }
61
62 void Main::draw(moof::scalar alpha) const
63 {
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65
66 glMatrixMode(GL_PROJECTION);
67 glLoadIdentity();
68
69 glMatrixMode(GL_MODELVIEW);
70 glLoadIdentity();
71
72 yoink.draw(alpha);
73 }
74
75 void Main::handle_event(const moof::event& event)
76 {
77 if (yoink.handle_event(event)) return;
78
79 switch (event.type)
80 {
81 case SDL_KEYUP:
82
83 if (event.key.keysym.sym == SDLK_f)
84 {
85 moof::video::current()->toggle_fullscreen();
86 }
87 else if (event.key.keysym.sym == SDLK_l)
88 {
89 moof::video::current()->toggle_cursor_captured();
90 moof::video::current()->toggle_cursor_visible();
91 }
92 else if (event.key.keysym.sym == SDLK_ESCAPE)
93 {
94 stop();
95 }
96 break;
97
98 case SDL_VIDEORESIZE:
99
100 glViewport(0, 0, event.resize.w, event.resize.h);
101 break;
102
103 case SDL_QUIT:
104
105 stop();
106 }
107 }
108
109
110 std::string Main::search_paths()
111 {
112 // Add search paths; they should be searched in this order:
113 // 1. YOINK_DATADIR (environment)
114 // 2. YOINK_DATADIR (configure)
115
116 std::string path;
117 std::string datadir = stlplus::env_vector()["YOINK_DATADIR"];
118 if (!datadir.empty())
119 {
120 path += datadir;
121 path += ":";
122 }
123 path += YOINK_DATADIR;
124 path += ":";
125 path += "data";
126
127 return path;
128 }
129
130 std::string Main::config_paths()
131 {
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)
135 // 3. $HOME/.yoinkrc
136 // 4. YOINKRC (environment)
137
138 std::string path = moof::resource::find_file("yoinkrc");
139
140 #if PLATFORM_POSIX
141 path += ":/etc/yoinkrc";
142 #endif
143 path += ":$HOME/.yoinkrc";
144
145 std::string rc_file = stlplus::env_vector()["YOINKRC"];
146 if (!rc_file.empty())
147 {
148 path += ":";
149 path += rc_file;
150 }
151
152 return path;
153 }
154
155
156 void Main::setup_opengl()
157 {
158 glEnable(GL_TEXTURE_2D);
159 glEnable(GL_DEPTH_TEST);
160 //glEnable(GL_CULL_FACE);
161
162 glEnable(GL_POINT_SMOOTH);
163 glEnable(GL_LINE_SMOOTH);
164 glEnable(GL_POLYGON_SMOOTH);
165 glShadeModel(GL_SMOOTH);
166
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);
171
172 glClearColor(1.0, 0.0, 0.0, 1.0);
173
174 //glEnable(GL_LIGHTING);
175 glEnable(GL_LIGHT0);
176
177 glEnable(GL_COLOR_MATERIAL);
178 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
179
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);
185
186 float spec[] = {1.0f, 1.0f, 1.0f, 1.0f};
187 glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
188 }
189
190
191 void Main::print_usage()
192 {
193 std::cout << "Usage: "
194 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
195 << std::endl
196 << "The alien-smashing action game." << std::endl
197 << 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
209 << std::endl
210 << "See documentation for more options." << std::endl;
211 }
212
213 void Main::print_info(int argc, char* argv[])
214 {
215 #if INCLUDE_CONFIG_FILE
216 extern char data_config_mk[];
217 std::cout << data_config_mk;
218 #else
219 std::cout << std::endl << "Configuration not included." << std::endl;
220 #endif
221 }
222
223 void Main::print_option(const std::string& option, bool enabled)
224 {
225 if (isatty(1) == 1)
226 {
227 if (enabled) std::cout << "\033[1;94m";
228 else std::cout << "\033[1m";
229 }
230 if (!enabled) std::cout << "-";
231 std::cout << option;
232 if (isatty(1) == 1) std::cout << "\033[0m";
233 std::cout << " ";
234 }
235
236
237 void hello()
238 {
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;
244 }
245
246 void goodbye()
247 {
248 if (isatty(1) == 1) std::cout << "\033[94m";
249 std::cout << std::endl << "Goodbye." << std::endl << moof::log::endl;
250 }
251
252
253 int main(int argc, char* argv[])
254 {
255 if (argc > 1)
256 {
257 std::string arg(argv[1]);
258 if (arg == "-h" || arg == "--help")
259 {
260 Main::print_usage();
261 return 0;
262 }
263 else if (arg == "-i" || arg == "--info")
264 {
265 Main::print_info(argc, argv);
266 return 0;
267 }
268 }
269
270 hello();
271 atexit(goodbye);
272
273 moof::backend backend;
274
275 moof::resource::set_search_paths(Main::search_paths());
276
277 moof::settings settings(argc, argv, Main::config_paths());
278
279 enum moof::log::level logLevel = moof::log::info;
280 settings.get("loglevel", logLevel);
281 moof::log::level(logLevel);
282
283 try
284 {
285 moof::image_handle icon("yoink", "png");
286 if (icon) icon->set_as_icon();
287 else moof::log_error("no icon loaded");
288 icon.unload();
289
290 class moof::video::attributes attributes(settings);
291 moof::video video(PACKAGE_STRING, attributes);
292 video.show_fps(true);
293
294 Main app(settings);
295 return app.run();
296 }
297 catch (const std::exception& e)
298 {
299 moof::modal_dialog dialog(moof::modal_dialog::error,
300 PACKAGE_STRING, "unhandled exception",
301 e.what());
302 dialog.run();
303 }
304 catch (const char* e)
305 {
306 moof::modal_dialog dialog(moof::modal_dialog::error,
307 PACKAGE_STRING, "unhandled exception",
308 e);
309 dialog.run();
310 }
311
312 return 1;
313 }
314
This page took 0.049029 seconds and 5 git commands to generate.