]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
7ec807bdb22de89f4c3a7d461e12b38063afec8d
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #include <cstdlib> // getenv
33 #include <boost/bind.hpp>
35 #include <Moof/Exception.hh>
36 #include <Moof/Log.hh>
37 #include <Moof/Math.hh>
38 #include <Moof/OpenGL.hh>
39 #include <Moof/Settings.hh>
40 #include <Moof/Timer.hh>
41 #include <Moof/Video.hh>
43 #include "YoinkApp.hh"
45 #include <SDL/SDL_sound.h>
54 static std::string
configFiles()
58 char* configFile
= getenv("YOINKRC");
59 char* dataDir
= getenv("YOINK_DATADIR");
63 // if a config file from the environment variable is specified, we want
64 // to load it first so it has precedence
69 // add the colon-delimited paths from configure
70 files
+= YOINK_CONFIGFILES
;
74 // if another data directory is set in the environment, look for a
81 // look in the configured data directory last of all
83 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
89 static std::string
iconFile()
91 char* dataDir
= getenv("YOINK_DATADIR");
93 // first set up the search paths so we can find the icon and other resources
96 // look first in the data directory specified by the environment
97 Mf::Resource::addSearchPath(dataDir
);
100 // then look in the configured data directory
101 Mf::Resource::addSearchPath(YOINK_DATADIR
);
103 return Mf::Resource::getPath("yoink.png");
107 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
108 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
109 music("NightFusion"),
110 punchSound("RobotPunch")
112 Mf::Dispatcher::getInstance().addHandler("video.context_recreated",
113 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
116 Mf::Scalar fade
[2] = {0.0, 1.0};
117 musicFade
.init(fade
, 0.0);
122 heroine
= Character::alloc("RobotTrooper");
123 heroine
->getAnimation().startSequence("Run");
125 font
= new TilemapFont
;
127 Mf::Scalar coeffs
[4];
132 interp
.init(coeffs
, 1.0, Mf::Interpolator::OSCILLATE
);
134 Mf::Scalar coeff
[2] = {1.0, 0.0};
135 fadeIn
.init(coeff
, 0.1);
137 testScene
= new Mf::Scene("Test");
138 heroine
->treeNode
= testScene
->getOctree()->insert(heroine
);
141 YoinkApp::~YoinkApp()
147 Mf::Dispatcher::getInstance().removeHandler(this);
151 void YoinkApp::setupGL()
153 glEnable(GL_TEXTURE_2D
);
155 //glEnable(GL_CULL_FACE);
156 glEnable(GL_DEPTH_TEST
);
158 glShadeModel(GL_SMOOTH
);
159 //glEnable(GL_POLYGON_SMOOTH);
162 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
163 //std::cout << "texture size: " << texSize << std::endl;
165 //glEnable(GL_BLEND);
166 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
167 glEnable(GL_ALPHA_TEST
);
168 glAlphaFunc(GL_GREATER
, 0.0);
170 glClearColor(1.0, 0.0, 0.0, 1.0);
172 //glMatrixMode(GL_PROJECTION);
174 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
175 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
176 camera
.uploadProjectionToGL();
178 //glMatrixMode(GL_MODELVIEW);
180 //glLineWidth(10.0f);
183 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
185 // Whenever the context and a new one created, it probably won't contain our
186 // state so we need to set that up again.
191 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
195 musicFade
.update(dt
);
197 music
.setGain(musicFade
.getValue());
201 camera
.update(t
, dt
);
203 heroine
->update(t
, dt
);
204 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
206 //camera.lookAt(heroine->getSphere().point);
207 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
210 hud
.setBar1Progress(interp
.getValue());
211 hud
.setBar2Progress(1.0 - interp
.getValue());
218 void YoinkApp::draw(Mf::Scalar alpha
)
221 //meh.random(0.0, 1.0);
222 //static Mf::Vector4 c1(meh);
224 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
226 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
227 //Mf::Scalar sinstate = std::sin(drawstate);
228 //Mf::Scalar cosstate = std::cos(drawstate);
231 glMatrixMode(GL_MODELVIEW
);
234 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
235 //glTranslatef(x, y, z);
236 glLoadMatrix(camera
.getModelviewMatrix().data());
239 Mf::Texture::resetBind();
240 testScene
->draw(alpha
, camera
);
244 //heroine->draw(alpha);
245 heroine
->getAabb().draw();
252 glMatrixMode(GL_PROJECTION
);
255 glMatrixMode(GL_MODELVIEW
);
258 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
259 Mf::Texture::resetBind();
261 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
263 glVertex3f(-1.0, -1.0, -0.1);
264 glVertex3f(1.0, -1.0, -0.1);
265 glVertex3f(1.0, 1.0, -0.1);
266 glVertex3f(-1.0, 1.0, -0.1);
271 glMatrixMode(GL_PROJECTION
);
273 glMatrixMode(GL_MODELVIEW
);
279 someChar->getTilemap().bind();
280 glColor3f(1.0, 1.0, 1.0);
282 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
284 Mf::Scalar coords[8];
285 someChar->getTilemap().getTileCoords(heroFrame, coords);
288 glTexCoord2f(coords[0], coords[1]);
289 glVertex3f(-1.0, 0.0, 0.0);
290 glTexCoord2f(coords[2], coords[3]);
291 glVertex3f(0.0, 0.0, 0.0);
292 glTexCoord2f(coords[4], coords[5]);
293 glVertex3f(0.0, 1.0, 0.0);
294 glTexCoord2f(coords[6], coords[7]);
295 glVertex3f(-1.0, 1.0, 0.0);
299 someChar->getTilemap().getTileCoords(heroFrame, coords,
300 Mf::Tilemap::REVERSE);
303 glTexCoord2f(coords[0], coords[1]);
304 glVertex3f(0.0, 0.0, 0.0);
305 glTexCoord2f(coords[2], coords[3]);
306 glVertex3f(1.0, 0.0, 0.0);
307 glTexCoord2f(coords[4], coords[5]);
308 glVertex3f(1.0, 1.0, 0.0);
309 glTexCoord2f(coords[6], coords[7]);
310 glVertex3f(0.0, 1.0, 0.0);
313 glColor4f(1.0,0.0,0.0,0.5);
315 glBindTexture(GL_TEXTURE_2D, 0);
316 glColor4v(c1.data());
318 glRectd(-cosstate, -sinstate, sinstate, cosstate);
319 glRectf(0.0f, 0.0f, sinstate, cosstate);
323 font->getTileCoords('c', coords);
326 glTexCoord2f(coords[0], coords[1]);
327 glVertex3f(-1.0, 0.0, 0.0);
328 glTexCoord2f(coords[2], coords[3]);
329 glVertex3f(0.0, 0.0, 0.0);
330 glTexCoord2f(coords[4], coords[5]);
331 glVertex3f(0.0, 1.0, 0.0);
332 glTexCoord2f(coords[6], coords[7]);
333 glVertex3f(-1.0, 1.0, 0.0);
336 font->getTileCoords('h', coords);
339 glTexCoord2f(coords[0], coords[1]);
340 glVertex3f(0.0, 0.0, 0.0);
341 glTexCoord2f(coords[2], coords[3]);
342 glVertex3f(1.0, 0.0, 0.0);
343 glTexCoord2f(coords[4], coords[5]);
344 glVertex3f(1.0, 1.0, 0.0);
345 glTexCoord2f(coords[6], coords[7]);
346 glVertex3f(0.0, 1.0, 0.0);
349 font->getTileCoords('a', coords);
352 glTexCoord2f(coords[0], coords[1]);
353 glVertex3f(-1.0, -1.0, 0.0);
354 glTexCoord2f(coords[2], coords[3]);
355 glVertex3f(0.0, -1.0, 0.0);
356 glTexCoord2f(coords[4], coords[5]);
357 glVertex3f(0.0, 0.0, 0.0);
358 glTexCoord2f(coords[6], coords[7]);
359 glVertex3f(-1.0, 0.0, 0.0);
362 font->getTileCoords('z', coords);
365 glTexCoord2f(coords[0], coords[1]);
366 glVertex3(0.0, -1.0, 0.0);
367 glTexCoord2f(coords[2], coords[3]);
368 glVertex3(1.0, -1.0, 0.0);
369 glTexCoord2f(coords[4], coords[5]);
370 glVertex3(1.0, 0.0, 0.0);
371 glTexCoord2f(coords[6], coords[7]);
372 glVertex3(0.0, 0.0, 0.0);
376 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
377 glDisable(GL_DEPTH_TEST);
379 glBindTexture(GL_TEXTURE_2D, 0);
380 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
383 glVertex2f(0.0f, 0.0f);
384 glVertex2v(interp.getState(alpha).data());
387 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
388 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
391 glEnable(GL_DEPTH_TEST);*/
394 void YoinkApp::handleEvent(const Mf::Event
& event
)
399 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
404 else if (event
.key
.keysym
.sym
== SDLK_f
)
406 getVideo().toggleFull();
409 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
411 heroine
->getAnimation().startSequence("Punch");
415 else if (event
.key
.keysym
.sym
== SDLK_r
)
417 testScene
->refresh();
420 else if (event
.key
.keysym
.sym
== SDLK_t
)
422 Mf::Dispatcher::getInstance().dispatch("video.context_recreated");
425 else if (event
.key
.keysym
.sym
== SDLK_p
)
427 music
.togglePlayPause();
430 else if (event
.key
.keysym
.sym
== SDLK_l
)
432 getVideo().toggleCursorGrab();
433 getVideo().toggleCursorVisible();
438 heroine
->handleEvent(event
);
440 case SDL_MOUSEMOTION
:
441 case SDL_MOUSEBUTTONDOWN
:
442 camera
.handleEvent(event
);
449 case SDL_VIDEORESIZE
:
450 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
451 hud
.resize(event
.resize
.w
, event
.resize
.h
);
452 camera
.setProjection(cml::rad(60.0), double(event
.resize
.w
/ event
.resize
.h
), 32.0, 2500.0);
453 camera
.uploadProjectionToGL();
460 int main(int argc
, char* argv
[])
462 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
463 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
464 << "Send patches and bug reports to <"
465 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
468 Mf::setLogLevel(Mf::DEBUGGING
);
475 YoinkApp
app(argc
, argv
);
478 catch (Mf::Exception e
)
480 Mf::logError("unhandled exception: <<%s>>", e
.what());
481 Mf::logInfo("it's time to crash now :-(");
485 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
490 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.062387 seconds and 4 git commands to generate.