4d88a709e69bb39a71e739a574823e254ff2f0ac
[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 <functional>
14 #include <iostream>
15 #include <string>
16 #include <unistd.h> // access
17
18 #include <Moof/Log.hh>
19 #include <Moof/ModalDialog.hh>
20 #include <Moof/OpenGL.hh>
21 #include <Moof/Resource.hh>
22 #include <Moof/Settings.hh>
23 #include <Moof/Video.hh>
24
25 #include "ErrorHandler.hh"
26 #include "GameLayer.hh"
27 #include "Main.hh"
28 #include "TitleLayer.hh"
29
30 #if HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 #include "version.h"
34
35
36 Main::Main(Mf::Settings& settings, Mf::Video& video) :
37 Mf::View(settings, video)
38 {
39 Mf::Dispatch& dispatch = Mf::Dispatch::global();
40 mNewContextDispatch = dispatch.addTarget("video.newcontext",
41 boost::bind(&Main::setupGL));
42 setupGL();
43 }
44
45
46 void Main::update(Mf::Scalar t, Mf::Scalar dt)
47 {
48 if (children().size() == 0)
49 {
50 //Mf::logWarning("main view has no children");
51 //stop();
52 //return;
53 addChild(TitleLayer::alloc());
54 }
55
56 Mf::View::update(t, dt);
57 }
58
59 void Main::draw(Mf::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 Mf::View::draw(alpha);
70 }
71
72 bool Main::handleEvent(const Mf::Event& event)
73 {
74 if (Mf::View::handleEvent(event)) return true;
75
76 switch (event.type)
77 {
78 case SDL_KEYUP:
79 if (event.key.keysym.sym == SDLK_f)
80 {
81 video().toggleFull();
82 }
83 else if (event.key.keysym.sym == SDLK_l)
84 {
85 video().toggleCursorGrab();
86 video().toggleCursorVisible();
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
111 char* dataDir = getenv("YOINK_DATADIR");
112 if (dataDir)
113 {
114 path += dataDir;
115 path += ":";
116 }
117 path += YOINK_DATADIR;
118
119 return path;
120 }
121
122 std::string Main::getConfigPath()
123 {
124 // Build the list of config files to search for, in this order:
125 // 1. YOINK_DATADIR/yoinkrc
126 // 2. /etc/yoinkrc (not for Windows)
127 // 3. $HOME/.yoinkrc
128 // 4. YOINKRC (environment)
129
130 std::string path("yoinkrc");
131 Mf::Resource::getPath(path);
132
133 #if !defined(_WIN32)
134 path += ":/etc/yoinkrc";
135 #endif
136 path += ":$HOME/.yoinkrc";
137
138 char* configFile = getenv("YOINKRC");
139 if (configFile)
140 {
141 path += ":";
142 path += configFile;
143 }
144
145 return path;
146 }
147
148
149 void Main::setupGL()
150 {
151 glEnable(GL_TEXTURE_2D);
152 glEnable(GL_DEPTH_TEST);
153
154 glEnable(GL_LINE_SMOOTH);
155 glEnable(GL_POLYGON_SMOOTH);
156 glShadeModel(GL_SMOOTH);
157
158 //glEnable(GL_BLEND);
159 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160 glEnable(GL_ALPHA_TEST);
161 glAlphaFunc(GL_GREATER, 0.0);
162
163 glClearColor(0.0, 0.0, 0.0, 1.0);
164
165 //glMatrixMode(GL_PROJECTION);
166 //glLoadIdentity();
167 //Mf::Scalar ratio = Mf::core.getVideo()->getWidth() /
168 //Mf::core.getVideo()->getHeight();
169 //gluPerspective(60.0, ratio, 1.0, 250.0);
170
171 //glMatrixMode(GL_MODELVIEW);
172 }
173
174
175 void Main::printUsage()
176 {
177 std::cout << "Usage: "
178 << PACKAGE" [-h|--help] [-i|--info] [OPTION=VALUE]..."
179 << std::endl
180 << "The alien-smashing action game." << std::endl
181 << std::endl
182 << "Options:" << std::endl
183 << " -h, --help" << std::endl
184 << " show this help and exit" << std::endl
185 << " -i, --info" << std::endl
186 << " show version and build information" << std::endl
187 << " detail=1|2|3" << std::endl
188 << " the level of detail of game scenes" << std::endl
189 << " fullscreen=true|false" << std::endl
190 << " if true, uses the entire display" << std::endl
191 << " framerate=num" << std::endl
192 << " number of frames to draw per second" << std::endl
193 << std::endl
194 << "See documentation for more options." << std::endl;
195 }
196
197 void Main::printInfo(int argc, char* argv[])
198 {
199 std::string assets;
200 std::string datadir;
201 std::string config;
202
203 assets.assign(YOINK_DATADIR);
204 int accessible = access(assets.c_str(), R_OK);
205 if (accessible != 0) assets += " (no access)";
206
207 char* temp = getenv("YOINK_DATADIR");
208 if (temp)
209 {
210 datadir = temp;
211 accessible = access(temp, R_OK);
212 if (accessible != 0) datadir += " (no access)";
213 }
214
215 temp = getenv("YOINKRC");
216 if (temp)
217 {
218 config = temp;
219 accessible = access(temp, R_OK);
220 if (accessible != 0) config += " (no access)";
221 }
222
223 std::cout << " Executable: " << argv[0] << std::endl
224 #ifdef YOINK_GITHEAD
225 << " Commit: "YOINK_GITHEAD << std::endl
226 #endif
227 << " Version: "VERSION << std::endl
228 << " Built: " << COMPILE_TIME << std::endl
229 << " Compiler: "COMPILER_STRING << std::endl
230 << " Assets: " << assets << std::endl
231 << "Build options: "
232 #ifndef HAVE_CLOCK_GETTIME
233 << "-"
234 #endif
235 << "clock_gettime "
236 #ifdef NDEBUG
237 << "-"
238 #endif
239 << "debug "
240 #ifndef USE_DOUBLE_PRECISION
241 << "-"
242 #endif
243 << "double-precision "
244 #ifndef USE_GTK
245 << "-"
246 #endif
247 << "gtk "
248 #ifndef PROFILING_ENABLED
249 << "-"
250 #endif
251 << "profile "
252 #ifndef USE_QT4
253 << "-"
254 #endif
255 << "qt4 "
256 #ifndef USE_THREADS
257 << "-"
258 #endif
259 << "threads" << std::endl
260 << " YOINKRC: " << config << std::endl
261 << "YOINK_DATADIR: " << datadir << std::endl;
262 }
263
264
265 void hello()
266 {
267 std::cout << std::endl << PACKAGE_STRING << std::endl
268 << "Compiled " << __TIME__ " " __DATE__ << std::endl
269 << "Send patches and bug reports to <"
270 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
271 }
272
273 void goodbye()
274 {
275 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
276 }
277
278
279 #include <Moof/Socket.hh>
280
281 int main(int argc, char* argv[])
282 {
283 Mf::ResolverTask task("4950", "compy", SOCK_DGRAM);
284 task.run();
285
286 int i = task.wait();
287 Mf::logWarning << "task ended with code: " << i << std::endl;
288
289 std::vector<Mf::SocketAddress>::const_iterator it;
290 for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
291 {
292 Mf::SocketAddress addr = *it;
293
294 Mf::logInfo << "address: " << addr.name() << ":" << addr.port()
295 << " (" << addr.type() << ")" << std::endl;
296 }
297
298
299 Mf::Packet packet(1000000);
300
301 packet << 5.1234f;
302 Mf::logInfo << "packet size: " << packet.size() << std::endl;
303
304 float meh3;
305 packet >> meh3;
306 Mf::logInfo << "float: " << meh3 << std::endl;
307 Mf::logInfo << "packet size: " << packet.size() << std::endl;
308
309
310 packet << uint16_t(45);
311 Mf::logInfo << "packet size: " << packet.size() << std::endl;
312 packet << uint64_t(1234567812345679ULL);
313 Mf::logInfo << "packet size: " << packet.size() << std::endl;
314
315 packet << true << false << false << true << false << true << true;
316 Mf::logInfo << "packet size: " << packet.size() << std::endl;
317
318 std::vector<short> hi;
319 hi.push_back(34);
320 hi.push_back(-12345);
321 hi.push_back(7734);
322
323 for (int a = 0; a < 15900; a++)
324 {
325 hi.push_back(a);
326 }
327
328 packet << hi;
329 Mf::logInfo << "packet size: " << packet.size() << std::endl;
330
331 packet << "hello world";
332 Mf::logInfo << "packet size: " << packet.size() << std::endl;
333
334 packet << false << false << false << true << false;
335 Mf::logInfo << "packet size: " << packet.size() << std::endl;
336
337
338 Mf::SocketAddress addr = Mf::SocketAddress::broadcast("4950");
339 //Mf::logInfo << "local addr: " << addr.name() << std::endl;
340
341 //for (it = task.addresses().begin(); it != task.addresses().end(); ++it)
342 //{
343 int bcast = 0;
344
345 //Mf::SocketAddress addr = *it;
346 Mf::Socket sock(addr);
347
348 sock.get(SO_BROADCAST, bcast);
349 Mf::logInfo << "bcast: " << bcast << std::endl;
350
351 sock.set(SO_BROADCAST, 1);
352
353 sock.get(SO_BROADCAST, bcast);
354 Mf::logInfo << "bcast: " << bcast << std::endl;
355
356 sock.write(packet);
357 //}
358
359
360 return 0;
361
362
363 if (argc > 1)
364 {
365 std::string arg(argv[1]);
366 if (arg == "-h" || arg == "--help")
367 {
368 Main::printUsage();
369 return 0;
370 }
371 else if (arg == "-i" || arg == "--info")
372 {
373 Main::printInfo(argc, argv);
374 return 0;
375 }
376 }
377
378 hello();
379 atexit(goodbye);
380
381 Mf::Resource::addSearchPaths(Main::getSearchPath());
382
383 Mf::Settings settings(argc, argv, Main::getConfigPath());
384
385 Mf::Log::Level logLevel = Mf::Log::INFO;
386 settings.get("loglevel", logLevel);
387 Mf::Log::setLevel(logLevel);
388
389 try
390 {
391 Mf::Video::Attributes attributes(settings);
392 attributes.caption = PACKAGE_STRING;
393 attributes.icon = Mf::Resource::getPath(PACKAGE".png");
394
395 Mf::Video video(attributes);
396 Main mainView(settings, video);
397
398 mainView.run();
399 return 0;
400 }
401 catch (const Mf::Error& error)
402 {
403 Mf::ModalDialog dialog(Mf::ModalDialog::CRITICAL,
404 PACKAGE_STRING, "Unhandled Exception",
405 getErrorString(error));
406
407 dialog.run();
408 return 1;
409 }
410 }
411
This page took 0.047136 seconds and 3 git commands to generate.