]>
Dogcows Code - chaz/yoink/blob - GameLayer.cc
f61ec3d43adf07da1b7fe0d9884fa713ae0d83d1
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"
45 Mf::Scalar
GameLayer::getZCoord(const Mf::Vector2
& position
) const
49 mState
.script
.getGlobalTable().pushField("GetZCoord");
50 mState
.script
.push(position
[0]);
51 mState
.script
.push(position
[1]);
52 mState
.script
.call(2, 1);
53 mState
.script
.getTop().get(z
);
59 void GameLayer::loadSceneLoader()
61 mState
.script
.importStandardLibraries();
62 importLogPrintFunction(mState
.script
);
64 std::string loaderPath
= Scene::getPath("loader");
67 throw Mf::Exception(Mf::ErrorCode::RESOURCE_NOT_FOUND
, "loader");
70 Mf::Script::Status status
= mState
.script
.doFile(loaderPath
);
71 if (status
!= Mf::Script::SUCCESS
)
74 mState
.script
[-1].get(str
);
76 Mf::logScript("%s", str
.c_str());
77 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, str
);
80 mState
.script
.getGlobalTable().pushField("scenes");
81 mState
.script
.getTop().get(mState
.sceneList
);
82 if (mState
.sceneList
.size() == 0)
84 Mf::logScript("no variable `scenes' within loader");
85 throw Mf::Exception(Mf::ErrorCode::SCRIPT_ERROR
, "no scenes to load");
89 void GameLayer::advanceScene()
91 if (mState
.sceneList
.size() != 0)
93 mState
.scene
= Scene::alloc(mState
.sceneList
[0]);
94 mState
.sceneList
.erase(mState
.sceneList
.begin());
95 mState
.scene
->load(mState
.script
);
100 GameLayer::GameLayer() :
101 mMusic("NightFusionIntro"),
104 mMusic
.setLooping(true);
105 mMusic
.enqueue("NightFusionLoop");
109 advanceScene(); // load the first scene
111 mState
.heroine
= Heroine::alloc();
112 mState
.heroine
->animation
.startSequence("FlyDiagonallyUp");
114 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
115 mState
.interp
.init(a
, 2.0, Mf::Interpolator::OSCILLATE
);
121 void GameLayer::pushed(Mf::Engine
& engine
)
123 engine
.push(Hud::alloc(mState
));
127 void GameLayer::update(Mf::Engine
& engine
, Mf::Scalar t
, Mf::Scalar dt
)
129 mState
.camera
.update(t
, dt
);
130 mState
.heroine
->update(t
, dt
);
132 mState
.scene
->checkForCollision(*mState
.heroine
);
134 mState
.camera
.setPosition(Mf::Vector3(-mState
.heroine
->getState().position
[0],
135 -mState
.heroine
->getState().position
[1], -10));
136 //mState.camera.lookAt(Mf::promote(mState.heroine->getState().position));
138 //Mf::Vector3 heroinePosition = Mf::promote(mState.heroine->getState().position);
139 //Mf::Sound::setListenerPosition(heroinePosition);
143 void GameLayer::draw(Mf::Engine
& engine
, Mf::Scalar alpha
) const
145 mState
.camera
.uploadToGL(alpha
);
148 Mf::Texture::resetBind();
150 glEnableClientState(GL_VERTEX_ARRAY
);
151 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
153 mState
.scene
->drawIfVisible(alpha
, mState
.camera
.getFrustum());
155 mState
.heroine
->setZCoord(getZCoord(mState
.heroine
->getState().position
));
156 mState
.heroine
->draw(alpha
);
159 bool GameLayer::handleEvent(Mf::Engine
& engine
, const Mf::Event
& event
)
164 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
166 mState
.heroine
->animation
.startSequence("Flattened");
167 Mf::logInfo("thump!");
171 else if (event
.key
.keysym
.sym
== SDLK_m
)
176 return mState
.heroine
->handleEvent(event
);
179 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
184 else if (event
.key
.keysym
.sym
== SDLK_h
)
186 engine
.push(Hud::alloc(mState
));
189 return mState
.heroine
->handleEvent(event
);
191 case SDL_MOUSEMOTION
:
192 case SDL_MOUSEBUTTONDOWN
:
193 mState
.camera
.handleEvent(event
);
196 case SDL_VIDEORESIZE
:
197 setProjection(event
.resize
.w
, event
.resize
.h
);
205 void GameLayer::setProjection()
207 Mf::VideoP video
= Mf::Engine::getInstance().getVideo();
208 setProjection(video
->getWidth(), video
->getHeight());
211 void GameLayer::setProjection(Mf::Scalar width
, Mf::Scalar height
)
213 mState
.camera
.setProjection(cml::rad(60.0), width
/ height
, 1.0, 200.0);
217 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.043825 seconds and 3 git commands to generate.