]> Dogcows Code - chaz/yoink/blob - src/Main.cc
lua build scripts
[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 "../config.h"
31 #include "version.h"
32
33
34 Main::Main(moof::settings& settings, moof::video& video) :
35 moof::view(settings, video)
36 {
37 moof::dispatcher& dispatcher = moof::dispatcher::global();
38 mNewContextDispatch = dispatcher.add_target("video.newcontext",
39 boost::bind(&Main::setupGL));
40 setupGL();
41 }
42
43
44 void Main::update(moof::scalar t, moof::scalar dt)
45 {
46 if (children().size() == 0)
47 {
48 //moof::log_warning("main view has no children");
49 //stop();
50 //return;
51 add_child(TitleLayer::alloc());
52 }
53
54 moof::view::update(t, dt);
55 }
56
57 void Main::draw(moof::scalar alpha) const
58 {
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
60
61 glMatrixMode(GL_PROJECTION);
62 glLoadIdentity();
63
64 glMatrixMode(GL_MODELVIEW);
65 glLoadIdentity();
66
67 moof::view::draw(alpha);
68 }
69
70 bool Main::handle_event(const moof::event& event)
71 {
72 if (moof::view::handle_event(event)) return true;
73
74 switch (event.type)
75 {
76 case SDL_KEYUP:
77 if (event.key.keysym.sym == SDLK_f)
78 {
79 video().toggle_fullscreen();
80 }
81 else if (event.key.keysym.sym == SDLK_l)
82 {
83 video().toggle_cursor_captured();
84 video().toggle_cursor_visible();
85 }
86 break;
87
88 case SDL_VIDEORESIZE:
89 glViewport(0, 0, event.resize.w, event.resize.h);
90 break;
91
92 case SDL_QUIT:
93 stop();
94 return true;
95 }
96
97 return false;
98 }
99
100
101 std::string Main::getSearchPath()
102 {
103 // Add search paths; they should be searched in this order:
104 // 1. YOINK_DATADIR (environment)
105 // 2. YOINK_DATADIR (configure)
106
107 std::string path;
108
109 char* dataDir = getenv("YOINK_DATADIR");
110 if (dataDir)
111 {
112 path += dataDir;
113 path += ":";
114 }
115 path += YOINK_DATADIR;
116
117 return path;
118 }
119
120 std::string Main::getConfigPath()
121 {
122 // Build the list of config files to search for, in this order:
123 // 1. YOINK_DATADIR/yoinkrc
124 // 2. /etc/yoinkrc (not for Windows)
125 // 3. $HOME/.yoinkrc
126 // 4. YOINKRC (environment)
127
128 std::string path("yoinkrc");
129 moof::resource::find(path);
130
131 #if !defined(_WIN32)
132 path += ":/etc/yoinkrc";
133 #endif
134 path += ":$HOME/.yoinkrc";
135
136 char* configFile = getenv("YOINKRC");
137 if (configFile)
138 {
139 path += ":";
140 path += configFile;
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 std::string assets;
198 std::string datadir;
199 std::string config;
200
201 assets.assign(YOINK_DATADIR);
202 int accessible = access(assets.c_str(), R_OK);
203 if (accessible != 0) assets += " (no access)";
204
205 char* temp = getenv("YOINK_DATADIR");
206 if (temp)
207 {
208 datadir = temp;
209 accessible = access(temp, R_OK);
210 if (accessible != 0) datadir += " (no access)";
211 }
212
213 temp = getenv("YOINKRC");
214 if (temp)
215 {
216 config = temp;
217 accessible = access(temp, R_OK);
218 if (accessible != 0) config += " (no access)";
219 }
220
221 std::cout << " Executable: " << argv[0] << std::endl
222 #ifdef YOINK_GITHEAD
223 << " Commit: "YOINK_GITHEAD << std::endl
224 #endif
225 << " Version: "VERSION << std::endl
226 << " Built: " << COMPILE_TIME << std::endl
227 << " Compiler: "COMPILER_STRING << std::endl
228 << " Assets: " << assets << std::endl
229 << "Build options: "
230 #ifndef HAVE_CLOCK_GETTIME
231 << "-"
232 #endif
233 << "clock_gettime "
234 #ifdef NDEBUG
235 << "-"
236 #endif
237 << "debug "
238 #ifndef USE_DOUBLE_PRECISION
239 << "-"
240 #endif
241 << "double-precision "
242 #ifndef USE_GTK
243 << "-"
244 #endif
245 << "gtk "
246 #ifndef USE_HOTLOADING
247 << "-"
248 #endif
249 << "hotloading "
250 #ifndef PROFILING_ENABLED
251 << "-"
252 #endif
253 << "profile "
254 #ifndef USE_QT4
255 << "-"
256 #endif
257 << "qt4 "
258 #ifndef USE_THREADS
259 << "-"
260 #endif
261 << "threads" << std::endl
262 << " YOINKRC: " << config << std::endl
263 << "YOINK_DATADIR: " << datadir << std::endl;
264 }
265
266
267 void hello()
268 {
269 std::cout << std::endl << PACKAGE_STRING << std::endl
270 << "Compiled " << __TIME__ " " __DATE__ << std::endl
271 << "Send patches and bug reports to <"
272 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
273 }
274
275 void goodbye()
276 {
277 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
278 }
279
280
281 #include <moof/socket.hh>
282
283 #include <fstream>
284
285 class MyAsset
286 {
287 public:
288 MyAsset(const std::string& path)
289 {
290 moof::log_info("MyAsset loading:", path);
291
292 char buffer[1024];
293
294 std::ifstream stream(path.c_str());
295 stream.getline(buffer, sizeof(buffer));
296 str = buffer;
297 stream.close();
298
299 cool();
300 }
301
302 void cool()
303 {
304 moof::log_info("MyAsset COOL:", str);
305 }
306
307 void groovy()
308 {
309 moof::log_info("MyAsset GROOVY!!!!", str);
310 }
311
312 std::string str;
313 };
314
315 typedef moof::resource_handle<MyAsset> MyAsset_handle;
316
317 class AnotherAsset
318 {
319 public:
320 AnotherAsset(const std::string& path, double d = 5.0)
321 {
322 moof::log_info("AnotherAsset loading:", path);
323 dude = d;
324 }
325
326
327 void cool()
328 {
329 moof::log_info("AnotherAsset cool", dude);
330 }
331
332 void groovy()
333 {
334 moof::log_info("AnotherAsset GROOVY!!!!", dude);
335 }
336
337 double dude;
338 };
339
340
341 int main(int argc, char* argv[])
342 {
343 moof::resource::register_type<MyAsset>("mine");
344
345 //moof::resource::add_type<AnotherAsset>("k");
346
347 //{
348 //moof::resource_ptr myAsset = moof::resource::load(assetName,
349 //"prefix", "mine");
350
351 //MyAsset_handle aCopy = myAsset;
352
353 //MyAsset_handle copy2 = moof::resource::load(assetName, "asdfas", "mine");
354
355 ////if (myAsset->check<MyAsset>()) myAsset->get<AnotherAsset>()->cool();
356 //myAsset->get<MyAsset>()->cool();
357 ////myAsset->get<AnotherAsset>()->groovy();
358
359 //aCopy.get()->cool();
360 //copy2.get()->cool();
361
362 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "mine"));
363 //}
364 //log_info("rsrc ptr:", moof::resource::load(assetName, "", "k"));
365 //moof::resource::load(assetName, "", "mine")->get<MyAsset>()->cool();
366
367 ////if (myAsset) myAsset.get()->cool();
368 ////else moof::log_error("asset not obtained...");
369
370 MyAsset_handle myAsset = moof::resource::load("/home/chaz/meh.mine");
371 MyAsset* asset = myAsset.get();
372 if (asset) asset->cool();
373 else moof::log_warning("no asset obtained!!");
374
375 moof::timer reloadTimer(
376 boost::bind(&moof::resource::reload_as_needed),
377 SCALAR(2.0),
378 moof::timer::repeat);
379
380 //for (;;)
381 //{
382 //myAsset.get()->cool();
383 //moof::resource::reload_as_needed();
384 //sleep(1);
385 //}
386
387 //return 0;
388
389 moof::resolver_task task("4950", "lappy");
390 task.run();
391
392 int i = task.wait();
393 moof::log_warning("task ended with code:", i);
394
395 std::vector<moof::socket::address>::const_iterator it;
396 for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
397 {
398 moof::socket::address addr = *it;
399
400 moof::log_info("address:", addr, "(", addr.type(), ")");
401 }
402
403 //ASSERT(5 == 8 && "uh oh that's not right");
404
405
406 std::vector<uint8_t> hi;
407 for (int a = 0; a < 4000; a++)
408 {
409 hi.push_back(a);
410 }
411
412 moof::log_info("array size:", hi.size());
413 moof::packet packet;
414 packet << hi;
415
416 //for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
417 //{
418 //moof::socket sock(*it);
419 moof::socket sock(moof::socket::address::broadcast("4950"));
420 //moof::socket sock("4950", "lappy", SOCK_DGRAM);
421 sock.set(SO_BROADCAST, 1);
422 sock.connect();
423
424 //if (sock.isConnected())
425 //{
426 moof::packet pack;
427 pack << "hello world";
428 sock.write(pack);
429 //sock.write(pack, sock.address());
430 moof::log_info("sent", pack.size(), "bytes");
431
432 const char* data = pack.bytes();
433 for (unsigned i = 0; i < pack.size(); ++i)
434 {
435 moof::log_warning("meh:", data[i]);
436 }
437
438 char data2[56];
439 pack.read(data2, 13);
440 for (int i = 0; i < 13; ++i)
441 {
442 moof::log_warning("meh:", data2[i]);
443 }
444 //}
445 //else
446 //{
447 //moof::log_error("NOT CONNECTED");
448 //}
449 //}
450
451
452 //return 0;
453
454
455 if (argc > 1)
456 {
457 std::string arg(argv[1]);
458 if (arg == "-h" || arg == "--help")
459 {
460 Main::printUsage();
461 return 0;
462 }
463 else if (arg == "-i" || arg == "--info")
464 {
465 Main::printInfo(argc, argv);
466 return 0;
467 }
468 }
469
470 hello();
471 atexit(goodbye);
472
473 moof::resource::add_search_paths(Main::getSearchPath());
474
475 moof::settings settings(argc, argv, Main::getConfigPath());
476
477 enum moof::log::level logLevel = moof::log::info;
478 settings.get("loglevel", logLevel);
479 moof::log::level(logLevel);
480
481 try
482 {
483 std::string iconPath(PACKAGE".png");
484 moof::resource::find(iconPath);
485 moof::image icon(iconPath);
486 icon.set_as_icon();
487
488 class moof::video::attributes attributes(settings);
489 moof::video video(PACKAGE_STRING, attributes);
490 Main mainView(settings, video);
491
492 mainView.run();
493 return 0;
494 }
495 catch (const std::exception& e)
496 {
497 moof::modal_dialog dialog(moof::modal_dialog::error,
498 PACKAGE_STRING, "Unhandled Exception",
499 e.what());
500
501 dialog.run();
502 return 1;
503 }
504 }
505
This page took 0.054552 seconds and 4 git commands to generate.