]> Dogcows Code - chaz/yoink/blob - src/Main.cc
8361b39d3dffe7a5e24eda753650e2722aad6092
[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 stlplus::env_vector environment;
111
112 std::string datadir = environment["YOINK_DATADIR"];
113 if (!datadir.empty())
114 {
115 path += datadir;
116 path += ":";
117 }
118 path += YOINK_DATADIR;
119
120 return path;
121 }
122
123 std::string Main::getConfigPath()
124 {
125 // Build the list of config files to search for, in this order:
126 // 1. YOINK_DATADIR/yoinkrc
127 // 2. /etc/yoinkrc (not for Windows)
128 // 3. $HOME/.yoinkrc
129 // 4. YOINKRC (environment)
130
131 std::string path("yoinkrc");
132 moof::resource::find(path);
133
134 stlplus::env_vector environment;
135
136 #if !defined(_WIN32)
137 path += ":/etc/yoinkrc";
138 #endif
139 path += ":$HOME/.yoinkrc";
140
141 std::string rc_file = environment["YOINKRC"];
142 if (!rc_file.empty())
143 {
144 path += ":";
145 path += rc_file;
146 }
147
148 return path;
149 }
150
151
152 void Main::setupGL()
153 {
154 glEnable(GL_TEXTURE_2D);
155 glEnable(GL_DEPTH_TEST);
156
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(0.0, 0.0, 0.0, 1.0);
167
168 //glMatrixMode(GL_PROJECTION);
169 //glLoadIdentity();
170 //moof::scalar ratio = moof::core.getVideo()->width() /
171 //moof::core.getVideo()->height();
172 //gluPerspective(60.0, ratio, 1.0, 250.0);
173
174 //glMatrixMode(GL_MODELVIEW);
175 }
176
177
178 void Main::printUsage()
179 {
180 std::cout << "Usage: "
181 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
182 << std::endl
183 << "The alien-smashing action game." << std::endl
184 << std::endl
185 << "Options:" << std::endl
186 << " -h, --help" << std::endl
187 << " show this help and exit" << std::endl
188 << " -i, --info" << std::endl
189 << " show version and build information" << std::endl
190 << " detail=1|2|3" << std::endl
191 << " the level of detail of game scenes" << std::endl
192 << " fullscreen=true|false" << std::endl
193 << " if true, uses the entire display" << std::endl
194 << " framerate=num" << std::endl
195 << " number of frames to draw per second" << std::endl
196 << std::endl
197 << "See documentation for more options." << std::endl;
198 }
199
200 void Main::printInfo(int argc, char* argv[])
201 {
202 stlplus::env_vector environment;
203
204 std::string assets;
205 assets.assign(YOINK_DATADIR);
206 if (!stlplus::file_readable(assets)) assets += " (no access)";
207
208 std::string datadir = environment["YOINK_DATADIR"];
209 if (!datadir.empty())
210 {
211 if (!stlplus::folder_readable(datadir)) datadir += " (no access)";
212 }
213
214 std::string rc_file = environment["YOINKRC"];
215 if (!rc_file.empty())
216 {
217 if (!stlplus::file_readable(rc_file)) rc_file += " (no access)";
218 }
219
220 std::cout << " Executable: " << argv[0] << std::endl
221 #ifdef YOINK_GITHEAD
222 << " Commit: "YOINK_GITHEAD << std::endl
223 #endif
224 << " Version: "VERSION << std::endl
225 << " Built: " << COMPILE_TIME << std::endl
226 << " Compiler: "COMPILER_STRING << std::endl
227 << " Assets: " << assets << std::endl
228 << "Build options: "
229 #ifndef HAVE_CLOCK_GETTIME
230 << "-"
231 #endif
232 << "clock_gettime "
233 #ifdef NDEBUG
234 << "-"
235 #endif
236 << "debug "
237 #ifndef USE_DOUBLE_PRECISION
238 << "-"
239 #endif
240 << "double-precision "
241 #ifndef USE_GTK
242 << "-"
243 #endif
244 << "gtk "
245 #ifndef USE_HOTLOADING
246 << "-"
247 #endif
248 << "hotloading "
249 #ifndef PROFILING_ENABLED
250 << "-"
251 #endif
252 << "profile "
253 #ifndef USE_QT4
254 << "-"
255 #endif
256 << "qt4 "
257 #ifndef USE_THREADS
258 << "-"
259 #endif
260 << "threads" << std::endl
261 << " YOINKRC: " << rc_file << std::endl
262 << "YOINK_DATADIR: " << datadir << std::endl;
263 }
264
265
266 void hello()
267 {
268 std::cout << std::endl << PACKAGE_STRING << std::endl
269 << "Compiled " << __TIME__ " " __DATE__ << std::endl
270 << "Send patches and bug reports to <"
271 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
272 }
273
274 void goodbye()
275 {
276 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
277 }
278
279
280 #include <moof/socket.hh>
281
282 #include <fstream>
283
284 class MyAsset
285 {
286 public:
287 MyAsset(const std::string& path)
288 {
289 moof::log_info("MyAsset loading:", path);
290
291 char buffer[1024];
292
293 std::ifstream stream(path.c_str());
294 stream.getline(buffer, sizeof(buffer));
295 str = buffer;
296 stream.close();
297
298 cool();
299 }
300
301 void cool()
302 {
303 moof::log_info("MyAsset COOL:", str);
304 }
305
306 void groovy()
307 {
308 moof::log_info("MyAsset GROOVY!!!!", str);
309 }
310
311 std::string str;
312 };
313
314 typedef moof::resource_handle<MyAsset> MyAsset_handle;
315
316 class AnotherAsset
317 {
318 public:
319 AnotherAsset(const std::string& path, double d = 5.0)
320 {
321 moof::log_info("AnotherAsset loading:", path);
322 dude = d;
323 }
324
325
326 void cool()
327 {
328 moof::log_info("AnotherAsset cool", dude);
329 }
330
331 void groovy()
332 {
333 moof::log_info("AnotherAsset GROOVY!!!!", dude);
334 }
335
336 double dude;
337 };
338
339
340 int main(int argc, char* argv[])
341 {
342 moof::resource::register_type<MyAsset>("mine");
343
344 //moof::resource::add_type<AnotherAsset>("k");
345
346 //{
347 //moof::resource_ptr myAsset = moof::resource::load(assetName,
348 //"prefix", "mine");
349
350 //MyAsset_handle aCopy = myAsset;
351
352 //MyAsset_handle copy2 = moof::resource::load(assetName, "asdfas", "mine");
353
354 ////if (myAsset->check<MyAsset>()) myAsset->get<AnotherAsset>()->cool();
355 //myAsset->get<MyAsset>()->cool();
356 ////myAsset->get<AnotherAsset>()->groovy();
357
358 //aCopy.get()->cool();
359 //copy2.get()->cool();
360
361 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "mine"));
362 //}
363 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "k"));
364 //moof::resource::load(assetName, "", "mine")->get<MyAsset>()->cool();
365
366 ////if (myAsset) myAsset.get()->cool();
367 ////else moof::log_error("asset not obtained...");
368
369 MyAsset_handle myAsset = moof::resource::load("/home/chaz/meh.mine");
370 MyAsset* asset = myAsset.get();
371 if (asset) asset->cool();
372 else moof::log_warning("no asset obtained!!");
373
374 //moof::timer reloadTimer(
375 //boost::bind(&moof::resource::reload_as_needed),
376 //SCALAR(2.0),
377 //moof::timer::repeat);
378
379 for (;;)
380 {
381 if (myAsset) myAsset.get()->cool();
382 moof::resource::reload_as_needed();
383 sleep(1);
384 }
385
386
387 if (argc > 1)
388 {
389 std::string arg(argv[1]);
390 if (arg == "-h" || arg == "--help")
391 {
392 Main::printUsage();
393 return 0;
394 }
395 else if (arg == "-i" || arg == "--info")
396 {
397 Main::printInfo(argc, argv);
398 return 0;
399 }
400 }
401
402 hello();
403 atexit(goodbye);
404
405 moof::resource::add_search_paths(Main::getSearchPath());
406
407 moof::settings settings(argc, argv, Main::getConfigPath());
408
409 enum moof::log::level logLevel = moof::log::info;
410 settings.get("loglevel", logLevel);
411 moof::log::level(logLevel);
412
413 try
414 {
415 std::string iconPath(PACKAGE".png");
416 moof::resource::find(iconPath);
417 moof::image icon(iconPath);
418 icon.set_as_icon();
419
420 class moof::video::attributes attributes(settings);
421 moof::video video(PACKAGE_STRING, attributes);
422 Main mainView(settings, video);
423
424 mainView.run();
425 return 0;
426 }
427 catch (const std::exception& e)
428 {
429 moof::modal_dialog dialog(moof::modal_dialog::error,
430 PACKAGE_STRING, "Unhandled Exception",
431 e.what());
432
433 dialog.run();
434 return 1;
435 }
436 }
437
This page took 0.050821 seconds and 3 git commands to generate.