]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
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::instance().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()
145 Mf::Dispatcher::instance().removeHandler(this);
149 void YoinkApp::setupGL()
151 glEnable(GL_TEXTURE_2D
);
153 //glEnable(GL_CULL_FACE);
154 glEnable(GL_DEPTH_TEST
);
156 glShadeModel(GL_SMOOTH
);
157 //glEnable(GL_POLYGON_SMOOTH);
160 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
161 //std::cout << "texture size: " << texSize << std::endl;
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);
168 glClearColor(1.0, 0.0, 0.0, 1.0);
170 //glMatrixMode(GL_PROJECTION);
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();
176 //glMatrixMode(GL_MODELVIEW);
178 //glLineWidth(10.0f);
181 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
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.
189 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
193 musicFade
.update(dt
);
195 music
.setGain(musicFade
.getValue());
199 camera
.update(t
, dt
);
201 heroine
->update(t
, dt
);
202 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
204 //camera.lookAt(heroine->getSphere().point);
205 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
208 hud
.setBar1Progress(interp
.getValue());
209 hud
.setBar2Progress(1.0 - interp
.getValue());
216 void YoinkApp::draw(Mf::Scalar alpha
)
219 //meh.random(0.0, 1.0);
220 //static Mf::Vector4 c1(meh);
222 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
224 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
225 //Mf::Scalar sinstate = std::sin(drawstate);
226 //Mf::Scalar cosstate = std::cos(drawstate);
229 glMatrixMode(GL_MODELVIEW
);
232 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
233 //glTranslatef(x, y, z);
234 glLoadMatrix(camera
.getModelviewMatrix().data());
237 Mf::Texture::resetBind();
238 testScene
->draw(alpha
, camera
);
242 //heroine->draw(alpha);
249 glMatrixMode(GL_PROJECTION
);
252 glMatrixMode(GL_MODELVIEW
);
255 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
256 Mf::Texture::resetBind();
258 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
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);
268 glMatrixMode(GL_PROJECTION
);
270 glMatrixMode(GL_MODELVIEW
);
276 someChar->getTilemap().bind();
277 glColor3f(1.0, 1.0, 1.0);
279 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
281 Mf::Scalar coords[8];
282 someChar->getTilemap().getTileCoords(heroFrame, coords);
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);
296 someChar->getTilemap().getTileCoords(heroFrame, coords,
297 Mf::Tilemap::REVERSE);
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);
310 glColor4f(1.0,0.0,0.0,0.5);
312 glBindTexture(GL_TEXTURE_2D, 0);
313 glColor4v(c1.data());
315 glRectd(-cosstate, -sinstate, sinstate, cosstate);
316 glRectf(0.0f, 0.0f, sinstate, cosstate);
320 font->getTileCoords('c', coords);
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);
333 font->getTileCoords('h', coords);
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);
346 font->getTileCoords('a', coords);
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);
359 font->getTileCoords('z', coords);
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);
373 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
374 glDisable(GL_DEPTH_TEST);
376 glBindTexture(GL_TEXTURE_2D, 0);
377 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
380 glVertex2f(0.0f, 0.0f);
381 glVertex2v(interp.getState(alpha).data());
384 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
385 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
388 glEnable(GL_DEPTH_TEST);*/
391 void YoinkApp::handleEvent(const Mf::Event
& event
)
396 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
400 else if (event
.key
.keysym
.sym
== SDLK_f
)
402 getVideo().toggleFull();
404 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
406 heroine
->getAnimation().startSequence("Punch");
409 else if (event
.key
.keysym
.sym
== SDLK_r
)
411 testScene
->refresh();
413 else if (event
.key
.keysym
.sym
== SDLK_p
)
415 music
.togglePlayPause();
417 else if (event
.key
.keysym
.sym
== SDLK_l
)
419 getVideo().toggleCursorGrab();
420 getVideo().toggleCursorVisible();
424 heroine
->handleEvent(event
);
426 case SDL_MOUSEMOTION
:
427 case SDL_MOUSEBUTTONDOWN
:
428 camera
.handleEvent(event
);
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();
446 int main(int argc
, char* argv
[])
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
;
457 YoinkApp
app(argc
, argv
);
460 catch (Mf::Engine::Exception e
)
462 std::cerr
<< "Unhandled exception: " << e
.what() << std::endl
;
466 std::cout
<< "Goodbye..." << std::endl
;
471 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.056229 seconds and 4 git commands to generate.