]> Dogcows Code - chaz/yoink/blob - src/Main.cc
fixed some resource management bugs
[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 #include <stlplus/portability/file_system.hpp>
21 #include <stlplus/portability/subprocesses.hpp>
22
23 #include <moof/log.hh>
24 #include <moof/modal_dialog.hh>
25 #include <moof/opengl.hh>
26 #include <moof/resource.hh>
27 #include <moof/settings.hh>
28 #include <moof/video.hh>
29
30 #include "GameLayer.hh"
31 #include "Main.hh"
32 #include "TitleLayer.hh"
33 #include "version.h"
34
35
36 Main::Main(moof::settings& settings, moof::video& video) :
37 moof::view(settings, video)
38 {
39 moof::dispatcher& dispatcher = moof::dispatcher::global();
40 mNewContextDispatch = dispatcher.add_target("video.newcontext",
41 boost::bind(&Main::setupGL));
42 setupGL();
43 }
44
45
46 void Main::update(moof::scalar t, moof::scalar dt)
47 {
48 if (children().size() == 0)
49 {
50 //moof::log_warning("main view has no children");
51 //stop();
52 //return;
53 add_child(TitleLayer::alloc());
54 }
55
56 moof::view::update(t, dt);
57 }
58
59 void Main::draw(moof::scalar alpha) const
60 {
61 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62
63 glMatrixMode(GL_PROJECTION);
64 glLoadIdentity();
65
66 glMatrixMode(GL_MODELVIEW);
67 glLoadIdentity();
68
69 moof::view::draw(alpha);
70 }
71
72 bool Main::handle_event(const moof::event& event)
73 {
74 if (moof::view::handle_event(event)) return true;
75
76 switch (event.type)
77 {
78 case SDL_KEYUP:
79 if (event.key.keysym.sym == SDLK_f)
80 {
81 video().toggle_fullscreen();
82 }
83 else if (event.key.keysym.sym == SDLK_l)
84 {
85 video().toggle_cursor_captured();
86 video().toggle_cursor_visible();
87 }
88 break;
89
90 case SDL_VIDEORESIZE:
91 glViewport(0, 0, event.resize.w, event.resize.h);
92 break;
93
94 case SDL_QUIT:
95 stop();
96 return true;
97 }
98
99 return false;
100 }
101
102
103 std::string Main::getSearchPath()
104 {
105 // Add search paths; they should be searched in this order:
106 // 1. YOINK_DATADIR (environment)
107 // 2. YOINK_DATADIR (configure)
108
109 std::string path;
110 std::string datadir = stlplus::env_vector()["YOINK_DATADIR"];
111 if (!datadir.empty())
112 {
113 path += datadir;
114 path += ":";
115 }
116 path += YOINK_DATADIR;
117
118 return path;
119 }
120
121 std::string Main::getConfigPath()
122 {
123 // Build the list of config files to search for, in this order:
124 // 1. YOINK_DATADIR/yoinkrc
125 // 2. /etc/yoinkrc (not for Windows)
126 // 3. $HOME/.yoinkrc
127 // 4. YOINKRC (environment)
128
129 std::string path = moof::resource::find_file("yoinkrc");
130
131 #if !defined(_WIN32)
132 path += ":/etc/yoinkrc";
133 #endif
134 path += ":$HOME/.yoinkrc";
135
136 std::string rc_file = stlplus::env_vector()["YOINKRC"];
137 if (!rc_file.empty())
138 {
139 path += ":";
140 path += rc_file;
141 }
142
143 return path;
144 }
145
146
147 void Main::setupGL()
148 {
149 glEnable(GL_TEXTURE_2D);
150 glEnable(GL_DEPTH_TEST);
151
152 glEnable(GL_LINE_SMOOTH);
153 glEnable(GL_POLYGON_SMOOTH);
154 glShadeModel(GL_SMOOTH);
155
156 //glEnable(GL_BLEND);
157 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
158 glEnable(GL_ALPHA_TEST);
159 glAlphaFunc(GL_GREATER, 0.0);
160
161 glClearColor(0.0, 0.0, 0.0, 1.0);
162
163 //glMatrixMode(GL_PROJECTION);
164 //glLoadIdentity();
165 //moof::scalar ratio = moof::core.getVideo()->width() /
166 //moof::core.getVideo()->height();
167 //gluPerspective(60.0, ratio, 1.0, 250.0);
168
169 //glMatrixMode(GL_MODELVIEW);
170 }
171
172
173 void Main::printUsage()
174 {
175 std::cout << "Usage: "
176 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
177 << std::endl
178 << "The alien-smashing action game." << std::endl
179 << std::endl
180 << "Options:" << std::endl
181 << " -h, --help" << std::endl
182 << " show this help and exit" << std::endl
183 << " -i, --info" << std::endl
184 << " show version and build information" << std::endl
185 << " detail=1|2|3" << std::endl
186 << " the level of detail of game scenes" << std::endl
187 << " fullscreen=true|false" << std::endl
188 << " if true, uses the entire display" << std::endl
189 << " framerate=num" << std::endl
190 << " number of frames to draw per second" << std::endl
191 << std::endl
192 << "See documentation for more options." << std::endl;
193 }
194
195 void Main::printInfo(int argc, char* argv[])
196 {
197 stlplus::env_vector environment;
198
199 std::string assets;
200 assets.assign(YOINK_DATADIR);
201 if (!stlplus::file_readable(assets)) assets += " (no access)";
202
203 std::string datadir = environment["YOINK_DATADIR"];
204 if (!datadir.empty())
205 {
206 if (!stlplus::folder_readable(datadir)) datadir += " (no access)";
207 }
208
209 std::string rc_file = environment["YOINKRC"];
210 if (!rc_file.empty())
211 {
212 if (!stlplus::file_readable(rc_file)) rc_file += " (no access)";
213 }
214
215 std::cout << " Executable: " << argv[0] << std::endl
216 #ifdef YOINK_GITHEAD
217 << " Commit: "YOINK_GITHEAD << std::endl
218 #endif
219 << " Version: " << PACKAGE_VERSION << std::endl
220 << " Built: " << COMPILE_TIME << std::endl
221 << " Compiler: " << COMPILER_STRING << std::endl
222 << " Assets: " << assets << std::endl
223 << "Build options: "
224 #ifndef HAVE_CLOCK_GETTIME
225 << "-"
226 #endif
227 << "clock_gettime "
228 #ifdef NDEBUG
229 << "-"
230 #endif
231 << "debug "
232 #ifndef USE_DOUBLE_PRECISION
233 << "-"
234 #endif
235 << "double-precision "
236 #ifndef USE_GTK
237 << "-"
238 #endif
239 << "gtk "
240 #ifndef USE_HOTLOADING
241 << "-"
242 #endif
243 << "hotloading "
244 #ifndef PROFILING_ENABLED
245 << "-"
246 #endif
247 << "profile "
248 #ifndef USE_QT4
249 << "-"
250 #endif
251 << "qt4 "
252 #ifndef USE_THREADS
253 << "-"
254 #endif
255 << "threads" << std::endl
256 << " YOINKRC: " << rc_file << std::endl
257 << "YOINK_DATADIR: " << datadir << std::endl;
258 }
259
260
261 void hello()
262 {
263 std::cout << std::endl << PACKAGE_STRING << std::endl
264 << "Compiled " << __TIME__ " " __DATE__ << std::endl
265 << "Send patches and bug reports to <"
266 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
267 }
268
269 void goodbye()
270 {
271 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
272 }
273
274
275 int main(int argc, char* argv[])
276 {
277 moof::backend backend;
278
279 // FIXME: This is temporary.
280 moof::timer reloadTimer(boost::bind(&moof::resource::reload_as_needed),
281 SCALAR(2.0),
282 moof::timer::repeat);
283
284 if (argc > 1)
285 {
286 std::string arg(argv[1]);
287 if (arg == "-h" || arg == "--help")
288 {
289 Main::printUsage();
290 return 0;
291 }
292 else if (arg == "-i" || arg == "--info")
293 {
294 Main::printInfo(argc, argv);
295 return 0;
296 }
297 }
298
299 hello();
300 atexit(goodbye);
301
302 moof::resource::add_search_paths(Main::getSearchPath());
303
304 moof::settings settings(argc, argv, Main::getConfigPath());
305
306 enum moof::log::level logLevel = moof::log::info;
307 settings.get("loglevel", logLevel);
308 moof::log::level(logLevel);
309
310 try
311 {
312 //std::string iconPath(PACKAGE".png");
313 //iconPath = moof::resource::find_file(iconPath);
314 //moof::image icon(iconPath);
315 //icon.set_as_icon();
316 moof::image_handle icon(PACKAGE, "png");
317 if (icon) icon->set_as_icon();
318 else moof::log_error("no icon loaded");
319 icon.unload();
320
321 class moof::video::attributes attributes(settings);
322 moof::video video(PACKAGE_STRING, attributes);
323 Main mainView(settings, video);
324
325 mainView.run();
326 return 0;
327 }
328 catch (const std::exception& e)
329 {
330 moof::modal_dialog dialog(moof::modal_dialog::error,
331 PACKAGE_STRING, "Unhandled Exception",
332 e.what());
333
334 dialog.run();
335 return 1;
336 }
337 }
338
This page took 0.046759 seconds and 5 git commands to generate.