]> Dogcows Code - chaz/yoink/blob - src/Main.cc
2df3f1e93da9e96cf114f998df45b5e063c7d03e
[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 <cstdlib> // atexit, getenv
13 #include <exception>
14 #include <functional>
15 #include <iostream>
16 #include <string>
17 #include <unistd.h> // access
18
19 #include <moof/log.hh>
20 #include <moof/modal_dialog.hh>
21 #include <moof/opengl.hh>
22 #include <moof/resource.hh>
23 #include <moof/settings.hh>
24 #include <moof/video.hh>
25
26 #include "GameLayer.hh"
27 #include "Main.hh"
28 #include "TitleLayer.hh"
29
30 #include "version.h"
31
32
33 Main::Main(moof::settings& settings, moof::video& video) :
34 moof::view(settings, video)
35 {
36 moof::dispatcher& dispatcher = moof::dispatcher::global();
37 mNewContextDispatch = dispatcher.add_target("video.newcontext",
38 boost::bind(&Main::setupGL));
39 setupGL();
40 }
41
42
43 void Main::update(moof::scalar t, moof::scalar dt)
44 {
45 if (children().size() == 0)
46 {
47 //moof::log_warning("main view has no children");
48 //stop();
49 //return;
50 add_child(TitleLayer::alloc());
51 }
52
53 moof::view::update(t, dt);
54 }
55
56 void Main::draw(moof::scalar alpha) const
57 {
58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
59
60 glMatrixMode(GL_PROJECTION);
61 glLoadIdentity();
62
63 glMatrixMode(GL_MODELVIEW);
64 glLoadIdentity();
65
66 moof::view::draw(alpha);
67 }
68
69 bool Main::handle_event(const moof::event& event)
70 {
71 if (moof::view::handle_event(event)) return true;
72
73 switch (event.type)
74 {
75 case SDL_KEYUP:
76 if (event.key.keysym.sym == SDLK_f)
77 {
78 video().toggle_fullscreen();
79 }
80 else if (event.key.keysym.sym == SDLK_l)
81 {
82 video().toggle_cursor_captured();
83 video().toggle_cursor_visible();
84 }
85 break;
86
87 case SDL_VIDEORESIZE:
88 glViewport(0, 0, event.resize.w, event.resize.h);
89 break;
90
91 case SDL_QUIT:
92 stop();
93 return true;
94 }
95
96 return false;
97 }
98
99
100 std::string Main::getSearchPath()
101 {
102 // Add search paths; they should be searched in this order:
103 // 1. YOINK_DATADIR (environment)
104 // 2. YOINK_DATADIR (configure)
105
106 std::string path;
107
108 char* dataDir = getenv("YOINK_DATADIR");
109 if (dataDir)
110 {
111 path += dataDir;
112 path += ":";
113 }
114 path += YOINK_DATADIR;
115
116 return path;
117 }
118
119 std::string Main::getConfigPath()
120 {
121 // Build the list of config files to search for, in this order:
122 // 1. YOINK_DATADIR/yoinkrc
123 // 2. /etc/yoinkrc (not for Windows)
124 // 3. $HOME/.yoinkrc
125 // 4. YOINKRC (environment)
126
127 std::string path("yoinkrc");
128 moof::resource::find(path);
129
130 #if !defined(_WIN32)
131 path += ":/etc/yoinkrc";
132 #endif
133 path += ":$HOME/.yoinkrc";
134
135 char* configFile = getenv("YOINKRC");
136 if (configFile)
137 {
138 path += ":";
139 path += configFile;
140 }
141
142 return path;
143 }
144
145
146 void Main::setupGL()
147 {
148 glEnable(GL_TEXTURE_2D);
149 glEnable(GL_DEPTH_TEST);
150
151 glEnable(GL_LINE_SMOOTH);
152 glEnable(GL_POLYGON_SMOOTH);
153 glShadeModel(GL_SMOOTH);
154
155 //glEnable(GL_BLEND);
156 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
157 glEnable(GL_ALPHA_TEST);
158 glAlphaFunc(GL_GREATER, 0.0);
159
160 glClearColor(0.0, 0.0, 0.0, 1.0);
161
162 //glMatrixMode(GL_PROJECTION);
163 //glLoadIdentity();
164 //moof::scalar ratio = moof::core.getVideo()->width() /
165 //moof::core.getVideo()->height();
166 //gluPerspective(60.0, ratio, 1.0, 250.0);
167
168 //glMatrixMode(GL_MODELVIEW);
169 }
170
171
172 void Main::printUsage()
173 {
174 std::cout << "Usage: "
175 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
176 << std::endl
177 << "The alien-smashing action game." << std::endl
178 << std::endl
179 << "Options:" << std::endl
180 << " -h, --help" << std::endl
181 << " show this help and exit" << std::endl
182 << " -i, --info" << std::endl
183 << " show version and build information" << std::endl
184 << " detail=1|2|3" << std::endl
185 << " the level of detail of game scenes" << std::endl
186 << " fullscreen=true|false" << std::endl
187 << " if true, uses the entire display" << std::endl
188 << " framerate=num" << std::endl
189 << " number of frames to draw per second" << std::endl
190 << std::endl
191 << "See documentation for more options." << std::endl;
192 }
193
194 void Main::printInfo(int argc, char* argv[])
195 {
196 std::string assets;
197 std::string datadir;
198 std::string config;
199
200 assets.assign(YOINK_DATADIR);
201 int accessible = access(assets.c_str(), R_OK);
202 if (accessible != 0) assets += " (no access)";
203
204 char* temp = getenv("YOINK_DATADIR");
205 if (temp)
206 {
207 datadir = temp;
208 accessible = access(temp, R_OK);
209 if (accessible != 0) datadir += " (no access)";
210 }
211
212 temp = getenv("YOINKRC");
213 if (temp)
214 {
215 config = temp;
216 accessible = access(temp, R_OK);
217 if (accessible != 0) config += " (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: " << config << 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 //moof::resource::add_type<AnotherAsset>("k");
344
345 //{
346 //moof::resource_ptr myAsset = moof::resource::load(assetName,
347 //"prefix", "mine");
348
349 //MyAsset_handle aCopy = myAsset;
350
351 //MyAsset_handle copy2 = moof::resource::load(assetName, "asdfas", "mine");
352
353 ////if (myAsset->check<MyAsset>()) myAsset->get<AnotherAsset>()->cool();
354 //myAsset->get<MyAsset>()->cool();
355 ////myAsset->get<AnotherAsset>()->groovy();
356
357 //aCopy.get()->cool();
358 //copy2.get()->cool();
359
360 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "mine"));
361 //}
362 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "k"));
363 //moof::resource::load(assetName, "", "mine")->get<MyAsset>()->cool();
364
365 ////if (myAsset) myAsset.get()->cool();
366 ////else moof::log_error("asset not obtained...");
367
368 MyAsset_handle myAsset = moof::resource::load("/home/chaz/meh.mine");
369 MyAsset* asset = myAsset.get();
370 if (asset) asset->cool();
371 else moof::log_warning("no asset obtained!!");
372
373 moof::timer reloadTimer(
374 boost::bind(&moof::resource::reload_as_needed),
375 SCALAR(2.0),
376 moof::timer::repeat);
377
378 //for (;;)
379 //{
380 //myAsset.get()->cool();
381 //moof::resource::reload_as_needed();
382 //sleep(1);
383 //}
384
385 //return 0;
386
387 moof::resolver_task task("4950", "lappy");
388 task.run();
389
390 int i = task.wait();
391 moof::log_warning("task ended with code:", i);
392
393 std::vector<moof::socket::address>::const_iterator it;
394 for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
395 {
396 moof::socket::address addr = *it;
397
398 moof::log_info("address:", addr, "(", addr.type(), ")");
399 }
400
401 //ASSERT(5 == 8 && "uh oh that's not right");
402
403
404 std::vector<uint8_t> hi;
405 for (int a = 0; a < 4000; a++)
406 {
407 hi.push_back(a);
408 }
409
410 moof::log_info("array size:", hi.size());
411 moof::packet packet;
412 packet << hi;
413
414 //for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
415 //{
416 //moof::socket sock(*it);
417 moof::socket sock(moof::socket::address::broadcast("4950"));
418 //moof::socket sock("4950", "lappy", SOCK_DGRAM);
419 sock.set(SO_BROADCAST, 1);
420 sock.connect();
421
422 //if (sock.isConnected())
423 //{
424 moof::packet pack;
425 pack << "hello world";
426 sock.write(pack);
427 //sock.write(pack, sock.address());
428 moof::log_info("sent", pack.size(), "bytes");
429
430 const char* data = pack.bytes();
431 for (unsigned i = 0; i < pack.size(); ++i)
432 {
433 moof::log_warning("meh:", data[i]);
434 }
435
436 char data2[56];
437 pack.read(data2, 13);
438 for (int i = 0; i < 13; ++i)
439 {
440 moof::log_warning("meh:", data2[i]);
441 }
442 //}
443 //else
444 //{
445 //moof::log_error("NOT CONNECTED");
446 //}
447 //}
448
449
450 //return 0;
451
452
453 if (argc > 1)
454 {
455 std::string arg(argv[1]);
456 if (arg == "-h" || arg == "--help")
457 {
458 Main::printUsage();
459 return 0;
460 }
461 else if (arg == "-i" || arg == "--info")
462 {
463 Main::printInfo(argc, argv);
464 return 0;
465 }
466 }
467
468 hello();
469 atexit(goodbye);
470
471 moof::resource::add_search_paths(Main::getSearchPath());
472
473 moof::settings settings(argc, argv, Main::getConfigPath());
474
475 enum moof::log::level logLevel = moof::log::info;
476 settings.get("loglevel", logLevel);
477 moof::log::level(logLevel);
478
479 try
480 {
481 std::string iconPath(PACKAGE".png");
482 moof::resource::find(iconPath);
483 moof::image icon(iconPath);
484 icon.set_as_icon();
485
486 class moof::video::attributes attributes(settings);
487 moof::video video(PACKAGE_STRING, attributes);
488 Main mainView(settings, video);
489
490 mainView.run();
491 return 0;
492 }
493 catch (const std::exception& e)
494 {
495 moof::modal_dialog dialog(moof::modal_dialog::error,
496 PACKAGE_STRING, "Unhandled Exception",
497 e.what());
498
499 dialog.run();
500 return 1;
501 }
502 }
503
This page took 0.057397 seconds and 3 git commands to generate.