]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
626fe1aea9ab18e823c49635573a19dad9dfac8c
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/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Settings.hh>
38 #include <Moof/Timer.hh>
39 #include <Moof/Video.hh>
41 #include "YoinkApp.hh"
43 #include <SDL/SDL_sound.h>
52 static std::string
configFiles()
56 char* configFile
= getenv("YOINKRC");
57 char* dataDir
= getenv("YOINK_DATADIR");
61 // if a config file from the environment variable is specified, we want
62 // to load it first so it has precedence
67 // add the colon-delimited paths from configure
68 files
+= YOINK_CONFIGFILES
;
72 // if another data directory is set in the environment, look for a
79 // look in the configured data directory last of all
81 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
87 static std::string
iconFile()
89 char* dataDir
= getenv("YOINK_DATADIR");
91 // first set up the search paths so we can find the icon and other resources
94 // look first in the data directory specified by the environment
95 Mf::Resource::addSearchPath(dataDir
);
98 // then look in the configured data directory
99 Mf::Resource::addSearchPath(YOINK_DATADIR
);
101 return Mf::Resource::getPath("yoink.png");
105 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
106 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
107 music("NightFusion"),
108 punchSound("RobotPunch")
110 Mf::Dispatcher::getInstance().addHandler("video.context_recreated",
111 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
114 Mf::Scalar fade
[2] = {0.0, 1.0};
115 musicFade
.init(fade
, 0.0);
120 heroine
= CharacterPtr(new Character("RobotTrooper"));
121 heroine
->getAnimation().startSequence("Run");
123 font
= new TilemapFont
;
125 Mf::Scalar coeffs
[4];
130 interp
.init(coeffs
, 1.0, Mf::Interpolator::OSCILLATE
);
132 Mf::Scalar coeff
[2] = {1.0, 0.0};
133 fadeIn
.init(coeff
, 0.1);
135 testScene
= new Mf::Scene("Test");
136 heroine
->treeNode
= testScene
->getOctree()->insert(heroine
);
139 YoinkApp::~YoinkApp()
141 std::cerr
<< "yoinkapp destructor" << std::endl
;
146 Mf::Dispatcher::getInstance().removeHandler(this);
150 void YoinkApp::setupGL()
152 glEnable(GL_TEXTURE_2D
);
154 //glEnable(GL_CULL_FACE);
155 glEnable(GL_DEPTH_TEST
);
157 glShadeModel(GL_SMOOTH
);
158 //glEnable(GL_POLYGON_SMOOTH);
161 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
162 //std::cout << "texture size: " << texSize << std::endl;
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);
169 glClearColor(1.0, 0.0, 0.0, 1.0);
171 //glMatrixMode(GL_PROJECTION);
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();
177 //glMatrixMode(GL_MODELVIEW);
179 //glLineWidth(10.0f);
182 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
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.
190 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
194 musicFade
.update(dt
);
196 music
.setGain(musicFade
.getValue());
200 camera
.update(t
, dt
);
202 heroine
->update(t
, dt
);
203 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
205 //camera.lookAt(heroine->getSphere().point);
206 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
209 hud
.setBar1Progress(interp
.getValue());
210 hud
.setBar2Progress(1.0 - interp
.getValue());
217 void YoinkApp::draw(Mf::Scalar alpha
)
220 //meh.random(0.0, 1.0);
221 //static Mf::Vector4 c1(meh);
223 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
225 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
226 //Mf::Scalar sinstate = std::sin(drawstate);
227 //Mf::Scalar cosstate = std::cos(drawstate);
230 glMatrixMode(GL_MODELVIEW
);
233 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
234 //glTranslatef(x, y, z);
235 glLoadMatrix(camera
.getModelviewMatrix().data());
238 Mf::Texture::resetBind();
239 testScene
->draw(alpha
, camera
);
243 //heroine->draw(alpha);
250 glMatrixMode(GL_PROJECTION
);
253 glMatrixMode(GL_MODELVIEW
);
256 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
257 Mf::Texture::resetBind();
259 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
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);
269 glMatrixMode(GL_PROJECTION
);
271 glMatrixMode(GL_MODELVIEW
);
277 someChar->getTilemap().bind();
278 glColor3f(1.0, 1.0, 1.0);
280 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
282 Mf::Scalar coords[8];
283 someChar->getTilemap().getTileCoords(heroFrame, coords);
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);
297 someChar->getTilemap().getTileCoords(heroFrame, coords,
298 Mf::Tilemap::REVERSE);
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);
311 glColor4f(1.0,0.0,0.0,0.5);
313 glBindTexture(GL_TEXTURE_2D, 0);
314 glColor4v(c1.data());
316 glRectd(-cosstate, -sinstate, sinstate, cosstate);
317 glRectf(0.0f, 0.0f, sinstate, cosstate);
321 font->getTileCoords('c', coords);
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);
334 font->getTileCoords('h', coords);
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);
347 font->getTileCoords('a', coords);
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);
360 font->getTileCoords('z', coords);
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);
374 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
375 glDisable(GL_DEPTH_TEST);
377 glBindTexture(GL_TEXTURE_2D, 0);
378 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
381 glVertex2f(0.0f, 0.0f);
382 glVertex2v(interp.getState(alpha).data());
385 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
386 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
389 glEnable(GL_DEPTH_TEST);*/
392 void YoinkApp::handleEvent(const Mf::Event
& event
)
397 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
402 else if (event
.key
.keysym
.sym
== SDLK_f
)
404 getVideo().toggleFull();
407 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
409 heroine
->getAnimation().startSequence("Punch");
413 else if (event
.key
.keysym
.sym
== SDLK_r
)
415 testScene
->refresh();
418 else if (event
.key
.keysym
.sym
== SDLK_t
)
420 Mf::Dispatcher::getInstance().dispatch("video.context_recreated");
423 else if (event
.key
.keysym
.sym
== SDLK_p
)
425 music
.togglePlayPause();
428 else if (event
.key
.keysym
.sym
== SDLK_l
)
430 getVideo().toggleCursorGrab();
431 getVideo().toggleCursorVisible();
436 heroine
->handleEvent(event
);
438 case SDL_MOUSEMOTION
:
439 case SDL_MOUSEBUTTONDOWN
:
440 camera
.handleEvent(event
);
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();
458 int main(int argc
, char* argv
[])
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
;
469 YoinkApp
* app
= new YoinkApp(argc
, argv
);
473 catch (Mf::Engine::Exception e
)
475 std::cerr
<< "Unhandled exception: " << e
.what() << std::endl
;
479 std::cout
<< "Goodbye..." << std::endl
;
484 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.058322 seconds and 4 git commands to generate.