]> Dogcows Code - chaz/yoink/blob - src/client.cc
compression functions; fixed texture seams
[chaz/yoink] / src / client.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 #include <cstdlib> // atexit
11 #include <exception>
12 #include <functional>
13 #include <iostream>
14 #include <sstream>
15 #include <string>
16
17 #include <stlplus/portability/file_system.hpp>
18 #include <stlplus/portability/subprocesses.hpp>
19
20 #include <moof/compression.hh>
21 #include <moof/image.hh>
22 #include <moof/log.hh>
23 #include <moof/modal_dialog.hh>
24 #include <moof/opengl.hh>
25 #include <moof/resource.hh>
26 #include <moof/settings.hh>
27 #include <moof/string.hh>
28 #include <moof/video.hh>
29
30 #include "client.hh"
31
32
33 #if HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #if PLATFORM_POSIX
38 #include <termios.h>
39 #else
40 inline int isatty(int dummy) { return 0; }
41 #endif
42
43
44 Main::Main(moof::settings& settings) :
45 moof::application(settings)
46 {
47 moof::dispatcher& dispatcher = moof::dispatcher::global();
48 video_reloaded_ = dispatcher.add_target("video.newcontext",
49 boost::bind(&Main::setup_opengl));
50 setup_opengl();
51
52 #if ENABLE_HOTLOADING
53 hotload_timer_.init(boost::bind(&moof::resource::reload_as_needed),
54 SCALAR(0.25), moof::timer::repeat);
55 #endif
56 }
57
58 void Main::setup_opengl()
59 {
60 glEnable(GL_TEXTURE_2D);
61 glEnable(GL_DEPTH_TEST);
62 //glEnable(GL_CULL_FACE);
63
64 //glEnable(GL_POINT_SMOOTH);
65 //glEnable(GL_LINE_SMOOTH);
66 //glEnable(GL_POLYGON_SMOOTH);
67 //glShadeModel(GL_SMOOTH);
68
69 //glEnable(GL_BLEND);
70 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
71 glEnable(GL_ALPHA_TEST);
72 glAlphaFunc(GL_GREATER, 0.0);
73
74 glEnable(GL_MULTISAMPLE);
75
76 glClearColor(1.0, 0.0, 0.0, 1.0);
77
78 //glEnable(GL_LIGHTING);
79 //glEnable(GL_LIGHT0);
80
81 //glEnable(GL_COLOR_MATERIAL);
82 //glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
83
84 float amb[] = {0.1f, 0.1f, 0.1f, 1.0f};
85 float dif[] = {0.6f, 0.6f, 0.6f, 1.0f};
86 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light);
87 glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
88 glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
89
90 float spec[] = {1.0f, 1.0f, 1.0f, 1.0f};
91 glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
92 }
93
94 void Main::update(moof::scalar t, moof::scalar dt)
95 {
96 yoink.update(t, dt);
97 }
98
99 void Main::draw(moof::scalar alpha) const
100 {
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102
103 glMatrixMode(GL_PROJECTION);
104 glLoadIdentity();
105
106 glMatrixMode(GL_MODELVIEW);
107 glLoadIdentity();
108
109 yoink.draw(alpha);
110 }
111
112 void Main::handle_event(const moof::event& event)
113 {
114 if (yoink.handle_event(event)) return;
115
116 switch (event.type)
117 {
118 case SDL_KEYUP:
119 if (event.key.keysym.sym == SDLK_f)
120 {
121 moof::video::current().toggle_fullscreen();
122 }
123 else if (event.key.keysym.sym == SDLK_l)
124 {
125 moof::video::current().toggle_cursor_captured();
126 moof::video::current().toggle_cursor_visible();
127 }
128 else if (event.key.keysym.sym == SDLK_ESCAPE)
129 {
130 stop();
131 }
132 break;
133
134 case SDL_VIDEORESIZE:
135 glViewport(0, 0, event.resize.w, event.resize.h);
136 break;
137
138 case SDL_QUIT:
139 stop();
140 }
141 }
142
143
144 static std::string search_paths()
145 {
146 // Add search paths; they should be searched in this order:
147 // 1. YOINK_DATADIR (environment)
148 // 2. YOINK_DATADIR (configure)
149
150 std::string path;
151 std::string datadir = stlplus::env_vector()["YOINK_DATADIR"];
152 if (!datadir.empty())
153 {
154 path += datadir;
155 path += ":";
156 }
157 path += YOINK_DATADIR;
158 path += ":";
159 path += "data";
160
161 return path;
162 }
163
164 static std::string config_paths()
165 {
166 // Build the list of config files to search for, in this order:
167 // 1. YOINK_DATADIR/yoinkrc
168 // 2. /etc/yoinkrc (not for Windows)
169 // 3. $HOME/.yoinkrc
170 // 4. YOINKRC (environment)
171
172 std::string path = moof::resource::find_file("yoinkrc");
173
174 #if PLATFORM_POSIX
175 path += ":/etc/yoinkrc";
176 #endif
177 path += ":$HOME/.yoinkrc";
178
179 std::string rc_file = stlplus::env_vector()["YOINKRC"];
180 if (!rc_file.empty())
181 {
182 path += ":";
183 path += rc_file;
184 }
185
186 return path;
187 }
188
189 static void print_usage()
190 {
191 std::cout << "Usage: "
192 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
193 << std::endl
194 << "The alien-smashing action game." << std::endl
195 << std::endl
196 << "Options:" << std::endl
197 << " -h, --help" << std::endl
198 << " show this help and exit" << std::endl
199 << " -i, --info" << std::endl
200 << " show version and build information" << std::endl
201 << " detail=1|2|3" << std::endl
202 << " the level of detail of game scenes" << std::endl
203 << " fullscreen=true|false" << std::endl
204 << " if true, uses the entire display" << std::endl
205 << " framerate=num" << std::endl
206 << " number of frames to draw per second" << std::endl
207 << std::endl
208 << "See documentation for more options." << std::endl;
209 }
210
211 static void print_info(int argc, char* argv[])
212 {
213 #if INCLUDE_CONFIG_FILE
214 extern size_t data_config_gz_size;
215 extern char data_config_gz[];
216 moof::inflate(data_config_gz, data_config_gz_size, std::cout);
217 #else
218 std::cout << std::endl << "No configuration available. :-("
219 << std::endl;
220 #endif
221 }
222
223
224 static void hello()
225 {
226 if (isatty(1) == 1) std::cout << "\033[94m";
227 std::cout << std::endl << PACKAGE_STRING << std::endl
228 << "Compiled " << __TIME__" "__DATE__ << std::endl
229 << "Send patches and bug reports to <"PACKAGE_BUGREPORT">."
230 << std::endl << moof::log::endl;
231 }
232
233 static void goodbye()
234 {
235 if (isatty(1) == 1) std::cout << "\033[94m";
236 std::cout << std::endl << "Goodbye." << std::endl << moof::log::endl;
237 }
238
239
240 int main(int argc, char* argv[])
241 {
242 if (argc > 1)
243 {
244 std::string arg(argv[1]);
245 if (arg == "-h" || arg == "--help")
246 {
247 print_usage();
248 return 0;
249 }
250 else if (arg == "-i" || arg == "--info")
251 {
252 print_info(argc, argv);
253 return 0;
254 }
255 }
256
257 hello();
258 atexit(goodbye);
259
260 moof::backend backend;
261
262 moof::resource::set_search_paths(search_paths());
263
264 moof::settings settings(argc, argv, config_paths());
265
266 enum moof::log::level logLevel = moof::log::info;
267 settings.get("loglevel", logLevel);
268 moof::log::level(logLevel);
269
270 std::cout.precision(10);
271
272 try
273 {
274 moof::image_handle icon("yoink", "png");
275 if (icon) icon->set_as_icon();
276 else moof::log_error("no icon loaded");
277 icon.unload();
278
279 class moof::video::attributes attributes(settings);
280 moof::video video(PACKAGE_STRING, attributes);
281
282 bool showfps = false;
283 settings.get("showfps", showfps);
284 video.show_fps(showfps);
285
286 Main app(settings);
287 return app.run();
288 }
289 catch (const std::exception& e)
290 {
291 moof::modal_dialog dialog(moof::modal_dialog::error,
292 PACKAGE_STRING, "unhandled exception",
293 e.what());
294 dialog.run();
295 }
296 catch (const char* e)
297 {
298 moof::modal_dialog dialog(moof::modal_dialog::error,
299 PACKAGE_STRING, "unhandled exception", e);
300 dialog.run();
301 }
302 return 1;
303 }
304
This page took 0.045194 seconds and 4 git commands to generate.