]>
Dogcows Code - chaz/yoink/blob - src/GameLayer.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 <Moof/Engine.hh>
30 #include <Moof/Exception.hh>
31 #include <Moof/Log.hh>
32 #include <Moof/Math.hh>
33 #include <Moof/OpenGL.hh>
34 #include <Moof/Video.hh>
36 #include "GameLayer.hh"
44 Mf::Scalar
GameLayer::getZCoord(const Mf::Vector2
& position
) const
48 mScript
.getGlobalTable().pushField("GetZCoord");
49 mScript
.push(position
[0]);
50 mScript
.push(position
[1]);
52 mScript
.getTop().get(z
);
58 void GameLayer::loadSceneLoader()
60 std::string loaderPath
= Scene::getPath("loader");
63 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND
, "loader");
66 Mf::Script::Status status
= mScript
.doFile(loaderPath
);
67 if (status
!= Mf::Script::SUCCESS
)
72 Mf::logScript("%s", str
.c_str());
73 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, str
.c_str());
76 mScript
.getGlobalTable().pushField("scenes");
77 mScript
.getTop().get(mSceneList
);
78 if (mSceneList
.size() == 0)
80 Mf::logScript("no variable `scenes' within loader");
81 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, "no scenes to load");
85 void GameLayer::advanceScene()
87 if (mSceneList
.size() != 0)
89 mScene
= Scene::alloc(mSceneList
[0]);
90 mSceneList
.erase(mSceneList
.begin());
91 mScene
->load(mScript
);
96 GameLayer::GameLayer() :
97 mMusic("NightFusionIntro"),
100 mMusic
.setLooping(true);
101 mMusic
.enqueue("NightFusionLoop");
105 advanceScene(); // load the first scene
107 mHeroine
= Heroine::alloc();
108 mHeroine
->animation
.startSequence("FlyDiagonallyUp");
110 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
111 mInterp
.init(a
, 2.0, Mf::Interpolator::OSCILLATE
);
119 void GameLayer::pushed(Mf::Engine
& engine
)
125 void GameLayer::update(Mf::Scalar t
, Mf::Scalar dt
)
127 mCamera
.update(t
, dt
);
128 mHeroine
->update(t
, dt
);
130 mScene
->checkForCollision(*mHeroine
);
132 mCamera
.setPosition(Mf::Vector3(-mHeroine
->getState().position
[0],
133 -mHeroine
->getState().position
[1], -10));
134 //mCamera.lookAt(Mf::promote(mHeroine->getState().position));
136 //Mf::Vector3 heroinePosition = Mf::promote(mHeroine->getState().position);
137 //Mf::Sound::setListenerPosition(heroinePosition);
139 mInterp
.update(t
, dt
);
140 mHud
->setBar1Progress(mInterp
.getState(dt
));
141 mHud
->setBar2Progress(1.0 - mInterp
.getState(dt
));
145 void GameLayer::draw(Mf::Scalar alpha
) const
147 mCamera
.uploadToGL(alpha
);
150 Mf::Texture::resetBind();
152 glEnableClientState(GL_VERTEX_ARRAY
);
153 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
155 mScene
->drawIfVisible(alpha
, mCamera
.getFrustum());
157 mHeroine
->setZCoord(getZCoord(mHeroine
->getState().position
));
158 mHeroine
->draw(alpha
);
161 bool GameLayer::handleEvent(const Mf::Event
& event
)
166 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
168 mHeroine
->animation
.startSequence("Flattened");
169 Mf::logInfo("thump!");
173 else if (event
.key
.keysym
.sym
== SDLK_p
)
178 else if (event
.key
.keysym
.sym
== SDLK_y
)
180 Mf::Engine::getInstance().pop();
185 return mHeroine
->handleEvent(event
);
187 case SDL_MOUSEMOTION
:
188 case SDL_MOUSEBUTTONDOWN
:
189 mCamera
.handleEvent(event
);
192 case SDL_VIDEORESIZE
:
193 setProjection(Mf::Scalar(event
.resize
.w
), Mf::Scalar(event
.resize
.h
));
201 void GameLayer::setProjection()
203 Mf::Video
& video
= Mf::Engine::getInstance().getVideo();
204 setProjection(video
.getWidth(), video
.getHeight());
207 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
209 mCamera
.setProjection(cml::rad(60.0), width
/ height
, 1.0, 200.0);
213 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.040374 seconds and 4 git commands to generate.