beginning CD implementation
[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 scene->checkForCollision(*heroine);
76
77 //camera.lookAt(heroine->getSphere().point);
78 camera.setPosition(Mf::Vector3(-heroine->current.position[0],
79 -heroine->current.position[1], -256));
80
81 //Mf::Vector3 heroinePosition = Mf::promote(heroine->current.position);
82 //Mf::Sound::setListenerPosition(heroinePosition);
83
84 interp.update(t, dt);
85 hud->setBar1Progress(interp.getState(dt));
86 hud->setBar2Progress(1.0 - interp.getState(dt));
87 }
88
89
90 void GameLayer::draw(Mf::Scalar alpha) const
91 {
92 camera.uploadToGL();
93
94 // DRAW THE SCENE
95 Mf::Texture::resetBind();
96
97 glEnableClientState(GL_VERTEX_ARRAY);
98 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
99
100 scene->drawIfVisible(alpha, camera.getFrustum());
101
102 heroine->draw(alpha);
103 }
104
105 bool GameLayer::handleEvent(const Mf::Event& event)
106 {
107 switch (event.type)
108 {
109 case SDL_KEYDOWN:
110 if (event.key.keysym.sym == SDLK_SPACE)
111 {
112 heroine->animation.startSequence("Flattened");
113 Mf::logInfo("thump!");
114 punchSound.play();
115 return true;
116 }
117 else if (event.key.keysym.sym == SDLK_p)
118 {
119 music.toggle();
120 return true;
121 }
122 else if (event.key.keysym.sym == SDLK_y)
123 {
124 Mf::Engine::getInstance().pop();
125 return true;
126 }
127
128 case SDL_KEYUP:
129 return heroine->handleEvent(event);
130
131 case SDL_MOUSEMOTION:
132 case SDL_MOUSEBUTTONDOWN:
133 camera.handleEvent(event);
134 return true;
135
136 case SDL_VIDEORESIZE:
137 setProjection(Mf::Scalar(event.resize.w), Mf::Scalar(event.resize.h));
138 break;
139 }
140
141 return false;
142 }
143
144
145 void GameLayer::setProjection()
146 {
147 Mf::Video& video = Mf::Engine::getInstance().getVideo();
148 setProjection(video.getWidth(), video.getHeight());
149 }
150
151 void GameLayer::setProjection(Mf::Scalar width, Mf::Scalar height)
152 {
153 camera.setProjection(cml::rad(60.0), width / height, 32.0, 2500.0);
154 }
155
156
157 /** vim: set ts=4 sw=4 tw=80: *************************************************/
158
This page took 0.037685 seconds and 4 git commands to generate.