c763b0fbbf04edce32967a867d13973780f57646
[chaz/yoink] / src / GameLayer.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include <Moof/Engine.hh>
30 #include <Moof/Log.hh>
31 #include <Moof/Math.hh>
32 #include <Moof/OpenGL.hh>
33 #include <Moof/Video.hh>
34
35 #include "GameLayer.hh"
36
37 #if HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41
42 GameLayer::GameLayer() :
43 music("BeatTheCube"),
44 punchSound("Thump")
45 {
46 music.setLooping(true);
47 music.enqueue("NightFusionLoop");
48 music.stream();
49
50 heroine = Heroine::alloc();
51 heroine->animation.startSequence("FlyDiagonallyUp");
52
53 Mf::Scalar a[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
54 interp.init(a, 2.0, Mf::Interpolator::OSCILLATE);
55
56 scene = Scene::alloc("Classic");
57
58 setProjection();
59
60 hud = Hud::alloc();
61 }
62
63
64 void GameLayer::pushed(Mf::Engine& engine)
65 {
66 engine.push(hud);
67 }
68
69
70 void GameLayer::update(Mf::Scalar t, Mf::Scalar dt)
71 {
72 camera.update(t, dt);
73 heroine->update(t, dt);
74
75 //camera.lookAt(heroine->getSphere().point);
76 camera.setPosition(Mf::Vector3(-heroine->current.position[0],
77 -heroine->current.position[1], -256));
78
79 //Mf::Vector3 heroinePosition = Mf::promote(heroine->current.position);
80 //Mf::Sound::setListenerPosition(heroinePosition);
81
82 interp.update(t, dt);
83 hud->setBar1Progress(interp.getState(dt));
84 hud->setBar2Progress(1.0 - interp.getState(dt));
85 }
86
87
88 void GameLayer::draw(Mf::Scalar alpha) const
89 {
90 camera.uploadToGL();
91
92 // DRAW THE SCENE
93 Mf::Texture::resetBind();
94
95 glEnableClientState(GL_VERTEX_ARRAY);
96 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
97
98 scene->drawIfVisible(alpha, camera.getFrustum());
99
100 heroine->draw(alpha);
101 }
102
103 bool GameLayer::handleEvent(const Mf::Event& event)
104 {
105 switch (event.type)
106 {
107 case SDL_KEYDOWN:
108 if (event.key.keysym.sym == SDLK_SPACE)
109 {
110 heroine->animation.startSequence("Flattened");
111 Mf::logInfo("thump!");
112 punchSound.play();
113 return true;
114 }
115 else if (event.key.keysym.sym == SDLK_p)
116 {
117 music.toggle();
118 return true;
119 }
120 else if (event.key.keysym.sym == SDLK_y)
121 {
122 Mf::Engine::getInstance().pop();
123 return true;
124 }
125
126 case SDL_KEYUP:
127 heroine->handleEvent(event);
128 break;
129
130 case SDL_MOUSEMOTION:
131 case SDL_MOUSEBUTTONDOWN:
132 camera.handleEvent(event);
133 return true;
134
135 case SDL_VIDEORESIZE:
136 setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
137 break;
138 }
139
140 return false;
141 }
142
143
144 void GameLayer::setProjection()
145 {
146 Mf::Video& video = Mf::Engine::getInstance().getVideo();
147 setProjection(video.getWidth(), video.getHeight());
148 }
149
150 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
151 {
152 camera.setProjection(cml::rad(60.0), width / height, 32.0, 2500.0);
153 }
154
155
156 /** vim: set ts=4 sw=4 tw=80: *************************************************/
157
This page took 0.042122 seconds and 3 git commands to generate.