]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
cleaned up dispatcher
[chaz/yoink] / src / YoinkApp.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #include <cstdlib> // getenv
30 #include <iostream>
31 #include <string>
32
33 #include <boost/bind.hpp>
34
35 #include <Moof/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Settings.hh>
38 #include <Moof/Timer.hh>
39 #include <Moof/Video.hh>
40
41 #include "YoinkApp.hh"
42
43 #include <SDL/SDL_sound.h>
44 #include <AL/al.h>
45 #include <AL/alut.h>
46
47 #if HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51
52 static std::string configFiles()
53 {
54 std::string files;
55
56 char* configFile = getenv("YOINKRC");
57 char* dataDir = getenv("YOINK_DATADIR");
58
59 if (configFile)
60 {
61 // if a config file from the environment variable is specified, we want
62 // to load it first so it has precedence
63 files += configFile;
64 files += ":";
65 }
66
67 // add the colon-delimited paths from configure
68 files += YOINK_CONFIGFILES;
69
70 if (dataDir)
71 {
72 // if another data directory is set in the environment, look for a
73 // config file there
74 files += ":";
75 files += dataDir;
76 files += "/yoinkrc";
77 }
78
79 // look in the configured data directory last of all
80 files += ":";
81 files += (dataDir ? dataDir : YOINK_DATADIR);
82 files += "/yoinkrc";
83
84 return files;
85 }
86
87 static std::string iconFile()
88 {
89 char* dataDir = getenv("YOINK_DATADIR");
90
91 // first set up the search paths so we can find the icon and other resources
92 if (dataDir)
93 {
94 // look first in the data directory specified by the environment
95 Mf::Resource::addSearchPath(dataDir);
96 }
97
98 // then look in the configured data directory
99 Mf::Resource::addSearchPath(YOINK_DATADIR);
100
101 return Mf::Resource::getPath("yoink.png");
102 }
103
104
105 YoinkApp::YoinkApp(int argc, char* argv[]) :
106 Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile()),
107 music("NightFusion"),
108 punchSound("RobotPunch")
109 {
110 Mf::Dispatcher::instance().addHandler("video.context_recreated",
111 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
112 setupGL();
113
114 Mf::Scalar fade[2] = {0.0, 1.0};
115 musicFade.init(fade, 0.0);
116 music.play();
117
118 state = 0.0;
119
120 heroine = CharacterPtr(new Character("RobotTrooper"));
121 heroine->getAnimation().startSequence("Run");
122
123 font = new TilemapFont;
124
125 Mf::Scalar coeffs[4];
126 coeffs[0] = 0.0;
127 coeffs[1] = 1.5;
128 coeffs[2] = -0.5;
129 coeffs[3] = 1.0;
130 interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE);
131
132 Mf::Scalar coeff[2] = {1.0, 0.0};
133 fadeIn.init(coeff, 0.1);
134
135 testScene = new Mf::Scene("Test");
136 heroine->treeNode = testScene->getOctree()->insert(heroine);
137 }
138
139 YoinkApp::~YoinkApp()
140 {
141 //delete heroine;
142 delete font;
143 delete testScene;
144
145 Mf::Dispatcher::instance().removeHandler(this);
146 }
147
148
149 void YoinkApp::setupGL()
150 {
151 glEnable(GL_TEXTURE_2D);
152
153 //glEnable(GL_CULL_FACE);
154 glEnable(GL_DEPTH_TEST);
155
156 glShadeModel(GL_SMOOTH);
157 //glEnable(GL_POLYGON_SMOOTH);
158
159 //int texSize;
160 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
161 //std::cout << "texture size: " << texSize << std::endl;
162
163 //glEnable(GL_BLEND);
164 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
165 glEnable(GL_ALPHA_TEST);
166 glAlphaFunc(GL_GREATER, 0.0);
167
168 glClearColor(1.0, 0.0, 0.0, 1.0);
169
170 //glMatrixMode(GL_PROJECTION);
171 //glLoadIdentity();
172 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
173 camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
174 camera.uploadProjectionToGL();
175
176 //glMatrixMode(GL_MODELVIEW);
177
178 //glLineWidth(10.0f);
179 }
180
181 void YoinkApp::contextRecreated(const Mf::Notification* note)
182 {
183 // Whenever the context and a new one created, it probably won't contain our
184 // state so we need to set that up again.
185 setupGL();
186 }
187
188
189 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
190 {
191 //dt *= 0.2;
192
193 musicFade.update(dt);
194 music.update(t, dt);
195 music.setGain(musicFade.getValue());
196
197 fadeIn.update(dt);
198
199 camera.update(t, dt);
200
201 heroine->update(t, dt);
202 heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode);
203
204 //camera.lookAt(heroine->getSphere().point);
205 camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256));
206
207 interp.update(dt);
208 hud.setBar1Progress(interp.getValue());
209 hud.setBar2Progress(1.0 - interp.getValue());
210
211 prevstate = state;
212 state += dt;
213 }
214
215
216 void YoinkApp::draw(Mf::Scalar alpha)
217 {
218 //Mf::Vector4 meh;
219 //meh.random(0.0, 1.0);
220 //static Mf::Vector4 c1(meh);
221
222 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
223
224 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
225 //Mf::Scalar sinstate = std::sin(drawstate);
226 //Mf::Scalar cosstate = std::cos(drawstate);
227
228
229 glMatrixMode(GL_MODELVIEW);
230 //glLoadIdentity();
231
232 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
233 //glTranslatef(x, y, z);
234 glLoadMatrix(camera.getModelviewMatrix().data());
235
236 // DRAW THE SCENE
237 Mf::Texture::resetBind();
238 testScene->draw(alpha, camera);
239
240
241
242 //heroine->draw(alpha);
243
244
245 hud.draw();
246
247
248 glEnable(GL_BLEND);
249 glMatrixMode(GL_PROJECTION);
250 glPushMatrix();
251 glLoadIdentity();
252 glMatrixMode(GL_MODELVIEW);
253 glPushMatrix();
254 glLoadIdentity();
255 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
256 Mf::Texture::resetBind();
257
258 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
259 glBegin(GL_QUADS);
260 glVertex3f(-1.0, -1.0, -0.1);
261 glVertex3f(1.0, -1.0, -0.1);
262 glVertex3f(1.0, 1.0, -0.1);
263 glVertex3f(-1.0, 1.0, -0.1);
264 glEnd();
265
266 glDisable(GL_BLEND);
267
268 glMatrixMode(GL_PROJECTION);
269 glPopMatrix();
270 glMatrixMode(GL_MODELVIEW);
271 glPopMatrix();
272
273 /*
274 glLoadIdentity();
275
276 someChar->getTilemap().bind();
277 glColor3f(1.0, 1.0, 1.0);
278
279 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
280
281 Mf::Scalar coords[8];
282 someChar->getTilemap().getTileCoords(heroFrame, coords);
283
284 glBegin(GL_QUADS);
285 glTexCoord2f(coords[0], coords[1]);
286 glVertex3f(-1.0, 0.0, 0.0);
287 glTexCoord2f(coords[2], coords[3]);
288 glVertex3f(0.0, 0.0, 0.0);
289 glTexCoord2f(coords[4], coords[5]);
290 glVertex3f(0.0, 1.0, 0.0);
291 glTexCoord2f(coords[6], coords[7]);
292 glVertex3f(-1.0, 1.0, 0.0);
293 glEnd();
294
295
296 someChar->getTilemap().getTileCoords(heroFrame, coords,
297 Mf::Tilemap::REVERSE);
298
299 glBegin(GL_QUADS);
300 glTexCoord2f(coords[0], coords[1]);
301 glVertex3f(0.0, 0.0, 0.0);
302 glTexCoord2f(coords[2], coords[3]);
303 glVertex3f(1.0, 0.0, 0.0);
304 glTexCoord2f(coords[4], coords[5]);
305 glVertex3f(1.0, 1.0, 0.0);
306 glTexCoord2f(coords[6], coords[7]);
307 glVertex3f(0.0, 1.0, 0.0);
308 glEnd();
309
310 glColor4f(1.0,0.0,0.0,0.5);
311
312 glBindTexture(GL_TEXTURE_2D, 0);
313 glColor4v(c1.data());
314
315 glRectd(-cosstate, -sinstate, sinstate, cosstate);
316 glRectf(0.0f, 0.0f, sinstate, cosstate);
317
318 font->bind();
319
320 font->getTileCoords('c', coords);
321
322 glBegin(GL_QUADS);
323 glTexCoord2f(coords[0], coords[1]);
324 glVertex3f(-1.0, 0.0, 0.0);
325 glTexCoord2f(coords[2], coords[3]);
326 glVertex3f(0.0, 0.0, 0.0);
327 glTexCoord2f(coords[4], coords[5]);
328 glVertex3f(0.0, 1.0, 0.0);
329 glTexCoord2f(coords[6], coords[7]);
330 glVertex3f(-1.0, 1.0, 0.0);
331 glEnd();
332
333 font->getTileCoords('h', coords);
334
335 glBegin(GL_QUADS);
336 glTexCoord2f(coords[0], coords[1]);
337 glVertex3f(0.0, 0.0, 0.0);
338 glTexCoord2f(coords[2], coords[3]);
339 glVertex3f(1.0, 0.0, 0.0);
340 glTexCoord2f(coords[4], coords[5]);
341 glVertex3f(1.0, 1.0, 0.0);
342 glTexCoord2f(coords[6], coords[7]);
343 glVertex3f(0.0, 1.0, 0.0);
344 glEnd();
345
346 font->getTileCoords('a', coords);
347
348 glBegin(GL_QUADS);
349 glTexCoord2f(coords[0], coords[1]);
350 glVertex3f(-1.0, -1.0, 0.0);
351 glTexCoord2f(coords[2], coords[3]);
352 glVertex3f(0.0, -1.0, 0.0);
353 glTexCoord2f(coords[4], coords[5]);
354 glVertex3f(0.0, 0.0, 0.0);
355 glTexCoord2f(coords[6], coords[7]);
356 glVertex3f(-1.0, 0.0, 0.0);
357 glEnd();
358
359 font->getTileCoords('z', coords);
360
361 glBegin(GL_QUADS);
362 glTexCoord2f(coords[0], coords[1]);
363 glVertex3(0.0, -1.0, 0.0);
364 glTexCoord2f(coords[2], coords[3]);
365 glVertex3(1.0, -1.0, 0.0);
366 glTexCoord2f(coords[4], coords[5]);
367 glVertex3(1.0, 0.0, 0.0);
368 glTexCoord2f(coords[6], coords[7]);
369 glVertex3(0.0, 0.0, 0.0);
370 glEnd();
371
372 glEnable(GL_BLEND);
373 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
374 glDisable(GL_DEPTH_TEST);
375
376 glBindTexture(GL_TEXTURE_2D, 0);
377 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
378
379 glBegin(GL_LINES);
380 glVertex2f(0.0f, 0.0f);
381 glVertex2v(interp.getState(alpha).data());
382 glEnd();
383
384 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
385 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
386
387 glDisable(GL_BLEND);
388 glEnable(GL_DEPTH_TEST);*/
389 }
390
391 void YoinkApp::handleEvent(const Mf::Event& event)
392 {
393 switch (event.type)
394 {
395 case SDL_KEYDOWN:
396 if (event.key.keysym.sym == SDLK_ESCAPE)
397 {
398 stop();
399 }
400 else if (event.key.keysym.sym == SDLK_f)
401 {
402 getVideo().toggleFull();
403 }
404 else if (event.key.keysym.sym == SDLK_SPACE)
405 {
406 heroine->getAnimation().startSequence("Punch");
407 punchSound.play();
408 }
409 else if (event.key.keysym.sym == SDLK_r)
410 {
411 testScene->refresh();
412 }
413 else if (event.key.keysym.sym == SDLK_p)
414 {
415 music.togglePlayPause();
416 }
417 else if (event.key.keysym.sym == SDLK_l)
418 {
419 getVideo().toggleCursorGrab();
420 getVideo().toggleCursorVisible();
421 }
422
423 case SDL_KEYUP:
424 heroine->handleEvent(event);
425
426 case SDL_MOUSEMOTION:
427 case SDL_MOUSEBUTTONDOWN:
428 camera.handleEvent(event);
429 break;
430
431 case SDL_QUIT:
432 stop();
433 break;
434
435 case SDL_VIDEORESIZE:
436 glViewport(0, 0, event.resize.w, event.resize.h);
437 hud.resize(event.resize.w, event.resize.h);
438 camera.setProjection(cml::rad(60.0), double(event.resize.w / event.resize.h), 32.0, 2500.0);
439 camera.uploadProjectionToGL();
440 break;
441 }
442 }
443
444
445
446 int main(int argc, char* argv[])
447 {
448 std::cout << PACKAGE_STRING << std::endl
449 << "Compiled " << __TIME__ " " __DATE__ << std::endl
450 << "Send patches and bug reports to <"
451 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
452
453 int status = 0;
454
455 try
456 {
457 YoinkApp app(argc, argv);
458 status = app.run();
459 }
460 catch (Mf::Engine::Exception e)
461 {
462 std::cerr << "Unhandled exception: " << e.what() << std::endl;
463 status = 1;
464 }
465
466 std::cout << "Goodbye..." << std::endl;
467 return status;
468 }
469
470
471 /** vim: set ts=4 sw=4 tw=80: *************************************************/
472
This page took 0.055171 seconds and 4 git commands to generate.