/******************************************************************************* Copyright (c) 2009, Charles McGarvey All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include // getenv #include #include #include #include #include #include #include #include #include "YoinkApp.hh" #include #include #include #if HAVE_CONFIG_H #include "config.h" #endif static std::string configFiles() { std::string files; char* configFile = getenv("YOINKRC"); char* dataDir = getenv("YOINK_DATADIR"); if (configFile) { // if a config file from the environment variable is specified, we want // to load it first so it has precedence files += configFile; files += ":"; } // add the colon-delimited paths from configure files += YOINK_CONFIGFILES; if (dataDir) { // if another data directory is set in the environment, look for a // config file there files += ":"; files += dataDir; files += "/yoinkrc"; } // look in the configured data directory last of all files += ":"; files += (dataDir ? dataDir : YOINK_DATADIR); files += "/yoinkrc"; return files; } static std::string iconFile() { char* dataDir = getenv("YOINK_DATADIR"); // first set up the search paths so we can find the icon and other resources if (dataDir) { // look first in the data directory specified by the environment Mf::Resource::addSearchPath(dataDir); } // then look in the configured data directory Mf::Resource::addSearchPath(YOINK_DATADIR); return Mf::Resource::getPath("yoink.png"); } YoinkApp::YoinkApp(int argc, char* argv[]) : Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile()), music("NightFusion"), punchSound("RobotPunch") { Mf::Dispatcher::getInstance().addHandler("video.context_recreated", boost::bind(&YoinkApp::contextRecreated, this, _1), this); setupGL(); Mf::Scalar fade[2] = {0.0, 1.0}; musicFade.init(fade, 0.0); music.play(); state = 0.0; heroine = CharacterPtr(new Character("RobotTrooper")); heroine->getAnimation().startSequence("Run"); font = new TilemapFont; Mf::Scalar coeffs[4]; coeffs[0] = 0.0; coeffs[1] = 1.5; coeffs[2] = -0.5; coeffs[3] = 1.0; interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE); Mf::Scalar coeff[2] = {1.0, 0.0}; fadeIn.init(coeff, 0.1); testScene = new Mf::Scene("Test"); heroine->treeNode = testScene->getOctree()->insert(heroine); } YoinkApp::~YoinkApp() { std::cerr << "yoinkapp destructor" << std::endl; //delete heroine; delete font; delete testScene; Mf::Dispatcher::getInstance().removeHandler(this); } void YoinkApp::setupGL() { glEnable(GL_TEXTURE_2D); //glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); //glEnable(GL_POLYGON_SMOOTH); //int texSize; //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize); //std::cout << "texture size: " << texSize << std::endl; //glEnable(GL_BLEND); //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.0); glClearColor(1.0, 0.0, 0.0, 1.0); //glMatrixMode(GL_PROJECTION); //glLoadIdentity(); //gluPerspective(60.0, 1.33333, 1.0, 2500.0); camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0); camera.uploadProjectionToGL(); //glMatrixMode(GL_MODELVIEW); //glLineWidth(10.0f); } void YoinkApp::contextRecreated(const Mf::Notification* note) { // Whenever the context and a new one created, it probably won't contain our // state so we need to set that up again. setupGL(); } void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt) { //dt *= 0.2; musicFade.update(dt); music.update(t, dt); music.setGain(musicFade.getValue()); fadeIn.update(dt); camera.update(t, dt); heroine->update(t, dt); heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode); //camera.lookAt(heroine->getSphere().point); camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256)); interp.update(dt); hud.setBar1Progress(interp.getValue()); hud.setBar2Progress(1.0 - interp.getValue()); prevstate = state; state += dt; } void YoinkApp::draw(Mf::Scalar alpha) { //Mf::Vector4 meh; //meh.random(0.0, 1.0); //static Mf::Vector4 c1(meh); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha); //Mf::Scalar sinstate = std::sin(drawstate); //Mf::Scalar cosstate = std::cos(drawstate); glMatrixMode(GL_MODELVIEW); //glLoadIdentity(); //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0); //glTranslatef(x, y, z); glLoadMatrix(camera.getModelviewMatrix().data()); // DRAW THE SCENE Mf::Texture::resetBind(); testScene->draw(alpha, camera); //heroine->draw(alpha); hud.draw(); glEnable(GL_BLEND); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha)); Mf::Texture::resetBind(); //glRectf(-1.0f, -1.0f, 1.0f, 1.0f); glBegin(GL_QUADS); glVertex3f(-1.0, -1.0, -0.1); glVertex3f(1.0, -1.0, -0.1); glVertex3f(1.0, 1.0, -0.1); glVertex3f(-1.0, 1.0, -0.1); glEnd(); glDisable(GL_BLEND); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); /* glLoadIdentity(); someChar->getTilemap().bind(); glColor3f(1.0, 1.0, 1.0); Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame(); Mf::Scalar coords[8]; someChar->getTilemap().getTileCoords(heroFrame, coords); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3f(-1.0, 0.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3f(-1.0, 1.0, 0.0); glEnd(); someChar->getTilemap().getTileCoords(heroFrame, coords, Mf::Tilemap::REVERSE); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3f(1.0, 0.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3f(1.0, 1.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3f(0.0, 1.0, 0.0); glEnd(); glColor4f(1.0,0.0,0.0,0.5); glBindTexture(GL_TEXTURE_2D, 0); glColor4v(c1.data()); glRectd(-cosstate, -sinstate, sinstate, cosstate); glRectf(0.0f, 0.0f, sinstate, cosstate); font->bind(); font->getTileCoords('c', coords); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3f(-1.0, 0.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3f(-1.0, 1.0, 0.0); glEnd(); font->getTileCoords('h', coords); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3f(1.0, 0.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3f(1.0, 1.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3f(0.0, 1.0, 0.0); glEnd(); font->getTileCoords('a', coords); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3f(-1.0, -1.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3f(0.0, -1.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3f(0.0, 0.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3f(-1.0, 0.0, 0.0); glEnd(); font->getTileCoords('z', coords); glBegin(GL_QUADS); glTexCoord2f(coords[0], coords[1]); glVertex3(0.0, -1.0, 0.0); glTexCoord2f(coords[2], coords[3]); glVertex3(1.0, -1.0, 0.0); glTexCoord2f(coords[4], coords[5]); glVertex3(1.0, 0.0, 0.0); glTexCoord2f(coords[6], coords[7]); glVertex3(0.0, 0.0, 0.0); glEnd(); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 0); glColor4f(0.0f, 0.0f, 0.0f, 1.0f); glBegin(GL_LINES); glVertex2f(0.0f, 0.0f); glVertex2v(interp.getState(alpha).data()); glEnd(); glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha)); glRectf(-1.0f, -1.0f, 1.0f, 1.0f); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST);*/ } void YoinkApp::handleEvent(const Mf::Event& event) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) { stop(); break; } else if (event.key.keysym.sym == SDLK_f) { getVideo().toggleFull(); break; } else if (event.key.keysym.sym == SDLK_SPACE) { heroine->getAnimation().startSequence("Punch"); punchSound.play(); break; } else if (event.key.keysym.sym == SDLK_r) { testScene->refresh(); break; } else if (event.key.keysym.sym == SDLK_t) { Mf::Dispatcher::getInstance().dispatch("video.context_recreated"); break; } else if (event.key.keysym.sym == SDLK_p) { music.togglePlayPause(); break; } else if (event.key.keysym.sym == SDLK_l) { getVideo().toggleCursorGrab(); getVideo().toggleCursorVisible(); break; } case SDL_KEYUP: heroine->handleEvent(event); case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: camera.handleEvent(event); break; case SDL_QUIT: stop(); break; case SDL_VIDEORESIZE: glViewport(0, 0, event.resize.w, event.resize.h); hud.resize(event.resize.w, event.resize.h); camera.setProjection(cml::rad(60.0), double(event.resize.w / event.resize.h), 32.0, 2500.0); camera.uploadProjectionToGL(); break; } } int main(int argc, char* argv[]) { std::cout << PACKAGE_STRING << std::endl << "Compiled " << __TIME__ " " __DATE__ << std::endl << "Send patches and bug reports to <" PACKAGE_BUGREPORT << ">." << std::endl << std::endl; int status = 0; try { YoinkApp* app = new YoinkApp(argc, argv); status = app->run(); delete app; } catch (Mf::Engine::Exception e) { std::cerr << "Unhandled exception: " << e.what() << std::endl; status = 1; } std::cout << "Goodbye..." << std::endl; return status; } /** vim: set ts=4 sw=4 tw=80: *************************************************/