]> Dogcows Code - chaz/yoink/blobdiff - src/GameLayer.cc
pkg-config libs parsing bugfix
[chaz/yoink] / src / GameLayer.cc
index a37f175801e8e38e280dfa857f9798724fd2c3b6..d696f5ebdf851a0276f625f5c886eddb3dfd7594 100644 (file)
 
-/*******************************************************************************
-
- 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 <Moof/Engine.hh>
-#include <Moof/Exception.hh>
-#include <Moof/Log.hh>
-#include <Moof/Math.hh>
-#include <Moof/OpenGL.hh>
-#include <Moof/Video.hh>
+/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+**]  All rights reserved.
+*
+* vi:ts=4 sw=4 tw=75
+*
+* Distributable under the terms and conditions of the 2-clause BSD license;
+* see the file COPYING for a complete text of the license.
+*
+**************************************************************************/
+
+#include "../config.h"
+
+#include <stdexcept>
+
+#include <moof/log.hh>
+#include <moof/math.hh>
+#include <moof/opengl.hh>
+#include <moof/settings.hh>
+#include <moof/video.hh>
 
 #include "GameLayer.hh"
 
-#if HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-
-
-Mf::Scalar GameLayer::getZCoord(const Mf::Vector2& position) const
-{
-       Mf::Scalar z;
-
-       mScript.getGlobalTable().pushField("GetZCoord");
-       mScript.push(position[0]);
-       mScript.push(position[1]);
-       mScript.call(2, 1);
-       mScript.getTop().get(z);
-       mScript.pop();
-
-       return z;
-}
 
 void GameLayer::loadSceneLoader()
 {
-       std::string loaderPath = Scene::getPath("loader");
-       if (loaderPath == "")
+       state_.script.import_standard_libraries();
+       moof::log::import(state_.script);
+
+       std::string path("loader");
+       if (!moof::resource::find(path))
        {
-               throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND, "loader");
+               throw std::runtime_error("cannot find scene loader script");
        }
 
-       Mf::Script::Status status = mScript.doFile(loaderPath);
-       if (status != Mf::Script::SUCCESS)
+       moof::script::status status = state_.script.do_file(path);
+       if (status != moof::script::success)
        {
                std::string str;
-               mScript[-1].get(str);
+               state_.script[-1].get(str);
+               throw std::runtime_error("script error: " + str);
+       }
 
-               Mf::logScript("%s", str.c_str());
-               throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, str.c_str());
+       state_.script.globals().push_field("scenes");
+       state_.script.top().get(state_.sceneList);
+       if (state_.sceneList.size() == 0)
+       {
+               throw std::runtime_error("no variable `scenes' in script loader.");
        }
+}
 
-       mScript.getGlobalTable().pushField("scenes");
-       mScript.getTop().get(mSceneList);
-       if (mSceneList.size() == 0)
+void GameLayer::advanceScene(moof::settings& settings)
+{
+       if (state_.sceneList.size() != 0)
        {
-               Mf::logScript("no variable `scenes' within loader");
-               throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR, "no scenes to load");
+               state_.scene = Scene::alloc(state_.sceneList[0]);
+               state_.sceneList.erase(state_.sceneList.begin());
+
+               moof::script::status status = state_.scene->load(settings,
+                                                                                                          state_.script);
+               if (status != moof::script::success)
+               {
+                       std::string str;
+                       state_.script[-1].get(str);
+                       throw std::runtime_error("script error: " + str);
+               }
+
+               moof::script::slot table = state_.script.globals().push_field("Event");
+               if (table.is_table())
+               {
+                       state_.script.push("Think");
+                       table.push_field("Think");
+                       state_.script.registry().set_field();
+               }
+               state_.script.pop();
        }
 }
 
-void GameLayer::advanceScene()
+
+GameLayer::GameLayer()
 {
-       if (mSceneList.size() != 0)
+       moof::log_info("about to load sound resource...");
+       music_ = moof::resource::load("sounds/NightFusionIntro.ogg");
+       if (music_)
        {
-               mScene = Scene::alloc(mSceneList[0]);
-               mSceneList.erase(mSceneList.begin());
-               mScene->load(mScript);
+               music_->loop(true);
+               music_->enqueue("NightFusionLoop");
        }
+       else moof::log_error("music not loaded");
+
+       //music_->position(moof::vector3(10.0, 5.0, 0.0));
+
+       mThinkTimer.init(boost::bind(&GameLayer::thinkTimer, this),
+                       0.1, moof::timer::repeat);
+
+       state_.heroine = Heroine::alloc();
+       state_.heroine->animation.startSequence("FlyDiagonallyUp");
+
+       state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate);
 }
 
 
-GameLayer::GameLayer() :
-       mMusic("NightFusionIntro"),
-       mPunchSound("Thump")
+void GameLayer::did_add_to_view()
 {
-       mMusic.setLooping(true);
-       mMusic.enqueue("NightFusionLoop");
-       mMusic.stream();
+       bool isMute = false;
+       settings().get("nomusic", isMute);
+       if (!isMute) music_->play();
 
        loadSceneLoader();
-       advanceScene();                         // load the first scene
+       advanceScene(settings());               // load the first scene
+
+       mHud = Hud::alloc(state_);
+       add_child(mHud);
 
-       mHeroine = Heroine::alloc();
-       mHeroine->animation.startSequence("FlyDiagonallyUp");
+       mRay.direction.set(1.0, 0.0);
 
-       Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
-       mInterp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
+       mLine.a.set(20, 10);
+       mLine.b.set(19, 14);
 
-       setProjection();
+       mCircle.point.set(22, 5);
+       mCircle.radius = 2;
 
-       mHud = Hud::alloc();
+       mRayTimer.init(boost::bind(&GameLayer::rayTimer, this),
+                                  1.0, moof::timer::repeat);
+
+       projection();
 }
 
 
-void GameLayer::pushed(Mf::Engine& engine)
+void GameLayer::update(moof::scalar t, moof::scalar dt)
+{
+       if (!state_.scene) return;
+       state_.camera.update(t, dt);
+       state_.heroine->update(t, dt);
+
+       state_.scene->checkForCollision(*state_.heroine);
+
+       moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8);
+       state_.camera.position(cam);
+
+       mRay.point = state_.heroine->state().position;
+
+       moof::view::update(t, dt);
+}
+
+void GameLayer::thinkTimer()
 {
-       engine.push(mHud);
+       state_.script.registry().push_field("Think");
+       if (state_.script[-1].is_function()) state_.script.call();
+       else                                state_.script.pop();
 }
 
 
-void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
+void GameLayer::rayTimer()
 {
-       mCamera.update(t, dt);
-       mHeroine->update(t, dt);
+       moof::ray2::contact meh;
+       std::list<moof::ray2::contact> hits;
+       moof::vector2 point;
 
-       mScene->checkForCollision(*mHeroine);
+       bool bam = mLine.intersect_ray(mRay, meh);
+       if (bam)
+       {
+               //meh.normal.normalize();
+               //hits.push_back(meh);
+               mRay.solve(point, meh.distance);
+               moof::log_info << "line: d = " << meh.distance << std::endl;
+               moof::log_info << "      P = " << point << std::endl;
+               moof::log_info << "      n = " << meh.normal << std::endl;
+       }
 
-       mCamera.setPosition(Mf::Vector3(-mHeroine->getState().position[0],
-                               -mHeroine->getState().position[1], -10));
-       //mCamera.lookAt(Mf::promote(mHeroine->getState().position));
+       bam = mCircle.intersect_ray(mRay, meh);
+       if (bam)
+       {
+               meh.normal.normalize();
+               hits.push_back(meh);
+       }
 
-       //Mf::Vector3 heroinePosition = Mf::promote(mHeroine->getState().position);
-       //Mf::Sound::setListenerPosition(heroinePosition);
-       
-       mInterp.update(t, dt);
-       mHud->setBar1Progress(mInterp.getState(dt));
-       mHud->setBar2Progress(1.0 - mInterp.getState(dt));
+       if (state_.scene->castRay(mRay, hits))
+       {
+               hits.front().normal.normalize();
+               mRay.solve(point, hits.front().distance);
+               moof::log_info << "scene: d = " << hits.front().distance << std::endl;
+               moof::log_info << "       P = " << point << std::endl;
+               moof::log_info << "       n = " << hits.front().normal << std::endl;
+       }
 }
 
 
-void GameLayer::draw(Mf::Scalar alpha) const
+void GameLayer::draw(moof::scalar alpha) const
 {
-       mCamera.uploadToGL(alpha);
+       if (!state_.scene) return;
+       state_.camera.upload_to_gl(alpha);
 
        // DRAW THE SCENE
-       Mf::Texture::resetBind();
+       moof::texture::reset_binding();
 
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
-       mScene->drawIfVisible(alpha, mCamera.getFrustum());
+       state_.scene->draw_if_visible(alpha, state_.camera.frustum());
+       state_.heroine->draw(alpha);
+
+       mRay.draw();
+       mLine.draw();
+       mCircle.draw();
 
-       mHeroine->setZCoord(getZCoord(mHeroine->getState().position));
-       mHeroine->draw(alpha);
+       moof::view::draw(alpha);
 }
 
-bool GameLayer::handleEvent(const Mf::Event& event)
+bool GameLayer::handle_event(const moof::event& event)
 {
+       if (moof::view::handle_event(event)) return true;
+
        switch (event.type)
        {
                case SDL_KEYDOWN:
                        if (event.key.keysym.sym == SDLK_SPACE)
                        {
-                               mHeroine->animation.startSequence("Flattened");
-                               Mf::logInfo("thump!");
-                               mPunchSound.play();
+                               state_.heroine->animation.startSequence("Flattened");
+                               moof::log_info("thump!");
+                               //mPunchSound.play();
+                               return true;
+                       }
+                       else if (event.key.keysym.sym == SDLK_m)
+                       {
+                               music_->toggle();
+                               return true;
+                       }
+                       else if (event.key.keysym.sym == SDLK_PAGEUP)
+                       {
+                               mRay.direction = moof::rotate_vector_2D(mRay.direction,
+                                                                                                               moof::rad(10.0));
                                return true;
                        }
-                       else if (event.key.keysym.sym == SDLK_p)
+                       else if (event.key.keysym.sym == SDLK_PAGEDOWN)
                        {
-                               mMusic.toggle();
+                               mRay.direction = moof::rotate_vector_2D(mRay.direction,
+                                                                                                               moof::rad(-10.0));
                                return true;
                        }
-                       else if (event.key.keysym.sym == SDLK_y)
+                       else if (event.key.keysym.sym == SDLK_r)
                        {
-                               Mf::Engine::getInstance().pop();
+                               loadSceneLoader();
+                               advanceScene(settings());
                                return true;
                        }
+                       return state_.heroine->handle_event(event);
 
                case SDL_KEYUP:
-                       return mHeroine->handleEvent(event);
+                       if (event.key.keysym.sym == SDLK_ESCAPE)
+                       {
+                               parent().remove_child(this);
+                               return true;
+                       }
+                       else if (event.key.keysym.sym == SDLK_h)
+                       {
+                               add_child(mHud);
+                               return true;
+                       }
+                       return state_.heroine->handle_event(event);
 
                case SDL_MOUSEMOTION:
                case SDL_MOUSEBUTTONDOWN:
-                       mCamera.handleEvent(event);
+                       state_.camera.handle_event(event);
                        return true;
 
                case SDL_VIDEORESIZE:
-                       setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
+                       projection(event.resize.w, event.resize.h);
                        break;
        }
 
@@ -198,17 +273,15 @@ bool GameLayer::handleEvent(const Mf::Event& event)
 }
 
 
-void GameLayer::setProjection()
+void GameLayer::projection()
 {
-       Mf::Video& video = Mf::Engine::getInstance().getVideo();
-       setProjection(video.getWidth(), video.getHeight());
+       projection(video().width(), video().height());
 }
 
-void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
+void GameLayer::projection(moof::scalar width, moof::scalar height)
 {
-       mCamera.setProjection(cml::rad(60.0), width / height, 1.0, 200.0);
+       state_.camera.projection(moof::rad(60.0),
+                                                        width / height,
+                                                        SCALAR(1.0), SCALAR(200.0));
 }
 
-
-/** vim: set ts=4 sw=4 tw=80: *************************************************/
-
This page took 0.0289 seconds and 4 git commands to generate.