8b011638735b48061d32fdafcdbf8096095bbfaa
[chaz/yoink] / src / Main.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
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.
7 *
8 *****************************************************************************/
9
10 #if HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <cstdlib> // atexit
15 #include <exception>
16 #include <functional>
17 #include <iostream>
18 #include <string>
19
20 #if PLATFORM_POSIX
21 #include <termios.h>
22 #else
23 inline int isatty(int dummy) { return 0; }
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 void Main::update(moof::scalar t, moof::scalar dt)
57 {
58 yoink.update(t, dt);
59 }
60
61 void Main::draw(moof::scalar alpha) const
62 {
63 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
64
65 glMatrixMode(GL_PROJECTION);
66 glLoadIdentity();
67
68 glMatrixMode(GL_MODELVIEW);
69 glLoadIdentity();
70
71 yoink.draw(alpha);
72 }
73
74 void Main::handle_event(const moof::event& event)
75 {
76 if (yoink.handle_event(event)) return;
77
78 switch (event.type)
79 {
80 case SDL_KEYUP:
81 if (event.key.keysym.sym == SDLK_f)
82 {
83 moof::video::current()->toggle_fullscreen();
84 }
85 else if (event.key.keysym.sym == SDLK_l)
86 {
87 moof::video::current()->toggle_cursor_captured();
88 moof::video::current()->toggle_cursor_visible();
89 }
90 else if (event.key.keysym.sym == SDLK_ESCAPE)
91 {
92 stop();
93 }
94 break;
95
96 case SDL_VIDEORESIZE:
97 glViewport(0, 0, event.resize.w, event.resize.h);
98 break;
99
100 case SDL_QUIT:
101 stop();
102 }
103 }
104
105 std::string Main::search_paths()
106 {
107 // Add search paths; they should be searched in this order:
108 // 1. YOINK_DATADIR (environment)
109 // 2. YOINK_DATADIR (configure)
110
111 std::string path;
112 std::string datadir = stlplus::env_vector()["YOINK_DATADIR"];
113 if (!datadir.empty())
114 {
115 path += datadir;
116 path += ":";
117 }
118 path += YOINK_DATADIR;
119 path += ":";
120 path += "data";
121
122 return path;
123 }
124
125 std::string Main::config_paths()
126 {
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)
130 // 3. $HOME/.yoinkrc
131 // 4. YOINKRC (environment)
132
133 std::string path = moof::resource::find_file("yoinkrc");
134
135 #if PLATFORM_POSIX
136 path += ":/etc/yoinkrc";
137 #endif
138 path += ":$HOME/.yoinkrc";
139
140 std::string rc_file = stlplus::env_vector()["YOINKRC"];
141 if (!rc_file.empty())
142 {
143 path += ":";
144 path += rc_file;
145 }
146
147 return path;
148 }
149
150 void Main::setup_opengl()
151 {
152 glEnable(GL_TEXTURE_2D);
153 glEnable(GL_DEPTH_TEST);
154 //glEnable(GL_CULL_FACE);
155
156 glEnable(GL_POINT_SMOOTH);
157 glEnable(GL_LINE_SMOOTH);
158 glEnable(GL_POLYGON_SMOOTH);
159 glShadeModel(GL_SMOOTH);
160
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);
165
166 glClearColor(1.0, 0.0, 0.0, 1.0);
167
168 //glEnable(GL_LIGHTING);
169 glEnable(GL_LIGHT0);
170
171 glEnable(GL_COLOR_MATERIAL);
172 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
173
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);
179
180 float spec[] = {1.0f, 1.0f, 1.0f, 1.0f};
181 glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
182 }
183
184 void Main::print_usage()
185 {
186 std::cout << "Usage: "
187 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
188 << std::endl
189 << "The alien-smashing action game." << std::endl
190 << 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
202 << std::endl
203 << "See documentation for more options." << std::endl;
204 }
205
206 void Main::print_info(int argc, char* argv[])
207 {
208 #if INCLUDE_CONFIG_FILE
209 extern char data_config_mk[];
210 std::cout << data_config_mk;
211 #else
212 std::cout << std::endl << "Configuration not included." << std::endl;
213 #endif
214 }
215
216
217 void hello()
218 {
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;
224 }
225
226 void goodbye()
227 {
228 if (isatty(1) == 1) std::cout << "\033[94m";
229 std::cout << std::endl << "Goodbye." << std::endl << moof::log::endl;
230 }
231
232
233 int main(int argc, char* argv[])
234 {
235 if (argc > 1)
236 {
237 std::string arg(argv[1]);
238 if (arg == "-h" || arg == "--help")
239 {
240 Main::print_usage();
241 return 0;
242 }
243 else if (arg == "-i" || arg == "--info")
244 {
245 Main::print_info(argc, argv);
246 return 0;
247 }
248 }
249
250 hello();
251 atexit(goodbye);
252
253 moof::backend backend;
254
255 moof::resource::set_search_paths(Main::search_paths());
256
257 moof::settings settings(argc, argv, Main::config_paths());
258
259 enum moof::log::level logLevel = moof::log::info;
260 settings.get("loglevel", logLevel);
261 moof::log::level(logLevel);
262
263 try
264 {
265 moof::image_handle icon("yoink", "png");
266 if (icon) icon->set_as_icon();
267 else moof::log_error("no icon loaded");
268 icon.unload();
269
270 class moof::video::attributes attributes(settings);
271 moof::video video(PACKAGE_STRING, attributes);
272 video.show_fps(true);
273
274 Main app(settings);
275 return app.run();
276 }
277 catch (const std::exception& e)
278 {
279 moof::modal_dialog dialog(moof::modal_dialog::error,
280 PACKAGE_STRING, "unhandled exception",
281 e.what());
282 dialog.run();
283 }
284 catch (const char* e)
285 {
286 moof::modal_dialog dialog(moof::modal_dialog::error,
287 PACKAGE_STRING, "unhandled exception", e);
288 dialog.run();
289 }
290 return 1;
291 }
292
This page took 0.04076 seconds and 3 git commands to generate.