]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
no more useless singleton class
[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::getInstance().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 std::cerr << "yoinkapp destructor" << std::endl;
142 //delete heroine;
143 delete font;
144 delete testScene;
145
146 Mf::Dispatcher::getInstance().removeHandler(this);
147 }
148
149
150 void YoinkApp::setupGL()
151 {
152 glEnable(GL_TEXTURE_2D);
153
154 //glEnable(GL_CULL_FACE);
155 glEnable(GL_DEPTH_TEST);
156
157 glShadeModel(GL_SMOOTH);
158 //glEnable(GL_POLYGON_SMOOTH);
159
160 //int texSize;
161 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
162 //std::cout << "texture size: " << texSize << std::endl;
163
164 //glEnable(GL_BLEND);
165 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166 glEnable(GL_ALPHA_TEST);
167 glAlphaFunc(GL_GREATER, 0.0);
168
169 glClearColor(1.0, 0.0, 0.0, 1.0);
170
171 //glMatrixMode(GL_PROJECTION);
172 //glLoadIdentity();
173 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
174 camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
175 camera.uploadProjectionToGL();
176
177 //glMatrixMode(GL_MODELVIEW);
178
179 //glLineWidth(10.0f);
180 }
181
182 void YoinkApp::contextRecreated(const Mf::Notification* note)
183 {
184 // Whenever the context and a new one created, it probably won't contain our
185 // state so we need to set that up again.
186 setupGL();
187 }
188
189
190 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
191 {
192 //dt *= 0.2;
193
194 musicFade.update(dt);
195 music.update(t, dt);
196 music.setGain(musicFade.getValue());
197
198 fadeIn.update(dt);
199
200 camera.update(t, dt);
201
202 heroine->update(t, dt);
203 heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode);
204
205 //camera.lookAt(heroine->getSphere().point);
206 camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256));
207
208 interp.update(dt);
209 hud.setBar1Progress(interp.getValue());
210 hud.setBar2Progress(1.0 - interp.getValue());
211
212 prevstate = state;
213 state += dt;
214 }
215
216
217 void YoinkApp::draw(Mf::Scalar alpha)
218 {
219 //Mf::Vector4 meh;
220 //meh.random(0.0, 1.0);
221 //static Mf::Vector4 c1(meh);
222
223 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
224
225 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
226 //Mf::Scalar sinstate = std::sin(drawstate);
227 //Mf::Scalar cosstate = std::cos(drawstate);
228
229
230 glMatrixMode(GL_MODELVIEW);
231 //glLoadIdentity();
232
233 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
234 //glTranslatef(x, y, z);
235 glLoadMatrix(camera.getModelviewMatrix().data());
236
237 // DRAW THE SCENE
238 Mf::Texture::resetBind();
239 testScene->draw(alpha, camera);
240
241
242
243 //heroine->draw(alpha);
244
245
246 hud.draw();
247
248
249 glEnable(GL_BLEND);
250 glMatrixMode(GL_PROJECTION);
251 glPushMatrix();
252 glLoadIdentity();
253 glMatrixMode(GL_MODELVIEW);
254 glPushMatrix();
255 glLoadIdentity();
256 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
257 Mf::Texture::resetBind();
258
259 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
260 glBegin(GL_QUADS);
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 glVertex3f(-1.0, 1.0, -0.1);
265 glEnd();
266
267 glDisable(GL_BLEND);
268
269 glMatrixMode(GL_PROJECTION);
270 glPopMatrix();
271 glMatrixMode(GL_MODELVIEW);
272 glPopMatrix();
273
274 /*
275 glLoadIdentity();
276
277 someChar->getTilemap().bind();
278 glColor3f(1.0, 1.0, 1.0);
279
280 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
281
282 Mf::Scalar coords[8];
283 someChar->getTilemap().getTileCoords(heroFrame, coords);
284
285 glBegin(GL_QUADS);
286 glTexCoord2f(coords[0], coords[1]);
287 glVertex3f(-1.0, 0.0, 0.0);
288 glTexCoord2f(coords[2], coords[3]);
289 glVertex3f(0.0, 0.0, 0.0);
290 glTexCoord2f(coords[4], coords[5]);
291 glVertex3f(0.0, 1.0, 0.0);
292 glTexCoord2f(coords[6], coords[7]);
293 glVertex3f(-1.0, 1.0, 0.0);
294 glEnd();
295
296
297 someChar->getTilemap().getTileCoords(heroFrame, coords,
298 Mf::Tilemap::REVERSE);
299
300 glBegin(GL_QUADS);
301 glTexCoord2f(coords[0], coords[1]);
302 glVertex3f(0.0, 0.0, 0.0);
303 glTexCoord2f(coords[2], coords[3]);
304 glVertex3f(1.0, 0.0, 0.0);
305 glTexCoord2f(coords[4], coords[5]);
306 glVertex3f(1.0, 1.0, 0.0);
307 glTexCoord2f(coords[6], coords[7]);
308 glVertex3f(0.0, 1.0, 0.0);
309 glEnd();
310
311 glColor4f(1.0,0.0,0.0,0.5);
312
313 glBindTexture(GL_TEXTURE_2D, 0);
314 glColor4v(c1.data());
315
316 glRectd(-cosstate, -sinstate, sinstate, cosstate);
317 glRectf(0.0f, 0.0f, sinstate, cosstate);
318
319 font->bind();
320
321 font->getTileCoords('c', coords);
322
323 glBegin(GL_QUADS);
324 glTexCoord2f(coords[0], coords[1]);
325 glVertex3f(-1.0, 0.0, 0.0);
326 glTexCoord2f(coords[2], coords[3]);
327 glVertex3f(0.0, 0.0, 0.0);
328 glTexCoord2f(coords[4], coords[5]);
329 glVertex3f(0.0, 1.0, 0.0);
330 glTexCoord2f(coords[6], coords[7]);
331 glVertex3f(-1.0, 1.0, 0.0);
332 glEnd();
333
334 font->getTileCoords('h', coords);
335
336 glBegin(GL_QUADS);
337 glTexCoord2f(coords[0], coords[1]);
338 glVertex3f(0.0, 0.0, 0.0);
339 glTexCoord2f(coords[2], coords[3]);
340 glVertex3f(1.0, 0.0, 0.0);
341 glTexCoord2f(coords[4], coords[5]);
342 glVertex3f(1.0, 1.0, 0.0);
343 glTexCoord2f(coords[6], coords[7]);
344 glVertex3f(0.0, 1.0, 0.0);
345 glEnd();
346
347 font->getTileCoords('a', coords);
348
349 glBegin(GL_QUADS);
350 glTexCoord2f(coords[0], coords[1]);
351 glVertex3f(-1.0, -1.0, 0.0);
352 glTexCoord2f(coords[2], coords[3]);
353 glVertex3f(0.0, -1.0, 0.0);
354 glTexCoord2f(coords[4], coords[5]);
355 glVertex3f(0.0, 0.0, 0.0);
356 glTexCoord2f(coords[6], coords[7]);
357 glVertex3f(-1.0, 0.0, 0.0);
358 glEnd();
359
360 font->getTileCoords('z', coords);
361
362 glBegin(GL_QUADS);
363 glTexCoord2f(coords[0], coords[1]);
364 glVertex3(0.0, -1.0, 0.0);
365 glTexCoord2f(coords[2], coords[3]);
366 glVertex3(1.0, -1.0, 0.0);
367 glTexCoord2f(coords[4], coords[5]);
368 glVertex3(1.0, 0.0, 0.0);
369 glTexCoord2f(coords[6], coords[7]);
370 glVertex3(0.0, 0.0, 0.0);
371 glEnd();
372
373 glEnable(GL_BLEND);
374 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
375 glDisable(GL_DEPTH_TEST);
376
377 glBindTexture(GL_TEXTURE_2D, 0);
378 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
379
380 glBegin(GL_LINES);
381 glVertex2f(0.0f, 0.0f);
382 glVertex2v(interp.getState(alpha).data());
383 glEnd();
384
385 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
386 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
387
388 glDisable(GL_BLEND);
389 glEnable(GL_DEPTH_TEST);*/
390 }
391
392 void YoinkApp::handleEvent(const Mf::Event& event)
393 {
394 switch (event.type)
395 {
396 case SDL_KEYDOWN:
397 if (event.key.keysym.sym == SDLK_ESCAPE)
398 {
399 stop();
400 break;
401 }
402 else if (event.key.keysym.sym == SDLK_f)
403 {
404 getVideo().toggleFull();
405 break;
406 }
407 else if (event.key.keysym.sym == SDLK_SPACE)
408 {
409 heroine->getAnimation().startSequence("Punch");
410 punchSound.play();
411 break;
412 }
413 else if (event.key.keysym.sym == SDLK_r)
414 {
415 testScene->refresh();
416 break;
417 }
418 else if (event.key.keysym.sym == SDLK_t)
419 {
420 Mf::Dispatcher::getInstance().dispatch("video.context_recreated");
421 break;
422 }
423 else if (event.key.keysym.sym == SDLK_p)
424 {
425 music.togglePlayPause();
426 break;
427 }
428 else if (event.key.keysym.sym == SDLK_l)
429 {
430 getVideo().toggleCursorGrab();
431 getVideo().toggleCursorVisible();
432 break;
433 }
434
435 case SDL_KEYUP:
436 heroine->handleEvent(event);
437
438 case SDL_MOUSEMOTION:
439 case SDL_MOUSEBUTTONDOWN:
440 camera.handleEvent(event);
441 break;
442
443 case SDL_QUIT:
444 stop();
445 break;
446
447 case SDL_VIDEORESIZE:
448 glViewport(0, 0, event.resize.w, event.resize.h);
449 hud.resize(event.resize.w, event.resize.h);
450 camera.setProjection(cml::rad(60.0), double(event.resize.w / event.resize.h), 32.0, 2500.0);
451 camera.uploadProjectionToGL();
452 break;
453 }
454 }
455
456
457
458 int main(int argc, char* argv[])
459 {
460 std::cout << PACKAGE_STRING << std::endl
461 << "Compiled " << __TIME__ " " __DATE__ << std::endl
462 << "Send patches and bug reports to <"
463 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
464
465 int status = 0;
466
467 try
468 {
469 YoinkApp* app = new YoinkApp(argc, argv);
470 status = app->run();
471 delete app;
472 }
473 catch (Mf::Engine::Exception e)
474 {
475 std::cerr << "Unhandled exception: " << e.what() << std::endl;
476 status = 1;
477 }
478
479 std::cout << "Goodbye..." << std::endl;
480 return status;
481 }
482
483
484 /** vim: set ts=4 sw=4 tw=80: *************************************************/
485
This page took 0.057277 seconds and 4 git commands to generate.