]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
extreme refactoring
[chaz/yoink] / src / YoinkApp.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 <cstdlib> // getenv
30 #include <iostream>
31 #include <string>
32
33 #include <boost/bind.hpp>
34
35 #include <Moof/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Settings.hh>
38 #include <Moof/Timer.hh>
39 #include <Moof/Video.hh>
40
41 #include "YoinkApp.hh"
42
43 #if HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47
48 static std::string configFiles()
49 {
50 std::string files;
51
52 char* configFile = getenv("YOINKRC");
53
54 if (configFile)
55 {
56 // if a config file from the environment variable is specified, we want
57 // to load it first so it has precedence
58 files += configFile;
59 files += ":";
60 }
61
62 files += YOINK_CONFIGFILES;
63
64 return files;
65 }
66
67
68 YoinkApp::YoinkApp(int argc, char* argv[]) :
69 Mf::Engine(PACKAGE_STRING, argc, argv, configFiles())
70 {
71 std::cout << PACKAGE_STRING << std::endl
72 << "Compiled " << __TIME__ " " __DATE__ << std::endl
73 << "Send requests, patches, and bug reports to <"
74 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
75
76 char* dataDir = getenv("YOINK_DATADIR");
77
78 if (dataDir)
79 {
80 Mf::Resource::addSearchPath(dataDir);
81 }
82
83 Mf::Resource::addSearchPath(YOINK_DATADIR);
84
85 Mf::Dispatcher::instance().addHandler("video.context_recreated",
86 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
87 setupGL();
88
89 state = 0.0;
90
91 someChar = new Character("RobotTrooper");
92 someChar->getAnimation().startSequence("Run");
93
94 font = new TilemapFont;
95
96 Mf::Vector2 coeffs[4];
97 coeffs[0] = Mf::Vector2(0.0, 0.0);
98 coeffs[1] = Mf::Vector2(0.5, 0.0);
99 coeffs[2] = Mf::Vector2(0.5, 0.0);
100 coeffs[3] = Mf::Vector2(1.0, 0.0);
101 interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE);
102
103 Mf::Scalar coeff[2] = {1.0, 0.0};
104 fadeIn.init(coeff, 0.5f);
105
106 testScene = new Mf::Scene("Test");
107 }
108
109 YoinkApp::~YoinkApp()
110 {
111 delete someChar;
112 delete font;
113
114 Mf::Dispatcher::instance().removeHandler(this);
115
116 std::cout << "Goodbye..." << std::endl;
117 }
118
119
120 void YoinkApp::setupGL()
121 {
122 glEnable(GL_TEXTURE_2D);
123 //glEnable(GL_CULL_FACE);
124 glEnable(GL_DEPTH_TEST);
125
126 glShadeModel(GL_SMOOTH);
127
128 //glEnable(GL_BLEND);
129 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
130 glEnable(GL_ALPHA_TEST);
131 glAlphaFunc(GL_GREATER, 0.0);
132
133 glClearColor(0.0, 0.0, 1.0, 1.0);
134
135 //glLineWidth(10.0f);
136 }
137
138 void YoinkApp::contextRecreated(const Mf::Notification& note)
139 {
140 // Whenever the context and a new one created, it probably won't contain our
141 // state so we need to set that up again.
142 setupGL();
143 }
144
145
146 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
147 {
148 //dt *= 0.2;
149
150 fadeIn.update(dt);
151
152 someChar->getAnimation().update(t, dt);
153 interp.update(dt);
154
155 prevstate = state;
156 state += dt;
157 }
158
159
160 void YoinkApp::draw(Mf::Scalar alpha)
161 {
162 //Mf::vector4 meh;
163 //meh.random(0.0, 1.0);
164 //static Mf::vector4 c1(meh);
165
166 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
167
168 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
169 //Mf::Scalar sinstate = std::sin(drawstate);
170 //Mf::Scalar cosstate = std::cos(drawstate);
171
172
173 glMatrixMode(GL_PROJECTION);
174 glLoadIdentity();
175 gluPerspective(60.0, 1.33333, 1.0, 2000.0);
176
177 glMatrixMode(GL_MODELVIEW);
178 glLoadIdentity();
179
180 glBindTexture(GL_TEXTURE_2D, 0);
181 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
182 glTranslatef(x, y, z);
183
184 // DRAW THE SCENE
185 testScene->draw(alpha);
186
187
188 /*
189 someChar->getTilemap().bind();
190 glColor3f(1.0, 1.0, 1.0);
191
192 unsigned heroFrame = someChar->getAnimation().getFrame();
193
194 float coords[8];
195 someChar->getTilemap().getTileCoords(heroFrame, coords);
196
197 glBegin(GL_QUADS);
198 glTexCoord2f(coords[0], coords[1]);
199 glVertex3f(-1.0, 0.0, 0.0);
200 glTexCoord2f(coords[2], coords[3]);
201 glVertex3f(0.0, 0.0, 0.0);
202 glTexCoord2f(coords[4], coords[5]);
203 glVertex3f(0.0, 1.0, 0.0);
204 glTexCoord2f(coords[6], coords[7]);
205 glVertex3f(-1.0, 1.0, 0.0);
206 glEnd();
207
208
209 someChar->getTilemap().getTileCoords(heroFrame, coords,
210 Mf::tilemap::reverse);
211
212 glBegin(GL_QUADS);
213 glTexCoord2f(coords[0], coords[1]);
214 glVertex3f(0.0, 0.0, 0.0);
215 glTexCoord2f(coords[2], coords[3]);
216 glVertex3f(1.0, 0.0, 0.0);
217 glTexCoord2f(coords[4], coords[5]);
218 glVertex3f(1.0, 1.0, 0.0);
219 glTexCoord2f(coords[6], coords[7]);
220 glVertex3f(0.0, 1.0, 0.0);
221 glEnd();
222
223 glColor4f(1.0,0.0,0.0,0.5);
224
225 glBindTexture(GL_TEXTURE_2D, 0);
226 glColor4fv(c1.data());
227
228 glRectd(-cosstate, -sinstate, sinstate, cosstate);
229 glRectf(0.0f, 0.0f, sinstate, cosstate);
230
231 font->bind();
232
233 font->getTileCoords('c', coords);
234
235 glBegin(GL_QUADS);
236 glTexCoord2f(coords[0], coords[1]);
237 glVertex3f(-1.0, 0.0, 0.0);
238 glTexCoord2f(coords[2], coords[3]);
239 glVertex3f(0.0, 0.0, 0.0);
240 glTexCoord2f(coords[4], coords[5]);
241 glVertex3f(0.0, 1.0, 0.0);
242 glTexCoord2f(coords[6], coords[7]);
243 glVertex3f(-1.0, 1.0, 0.0);
244 glEnd();
245
246 font->getTileCoords('h', coords);
247
248 glBegin(GL_QUADS);
249 glTexCoord2f(coords[0], coords[1]);
250 glVertex3f(0.0, 0.0, 0.0);
251 glTexCoord2f(coords[2], coords[3]);
252 glVertex3f(1.0, 0.0, 0.0);
253 glTexCoord2f(coords[4], coords[5]);
254 glVertex3f(1.0, 1.0, 0.0);
255 glTexCoord2f(coords[6], coords[7]);
256 glVertex3f(0.0, 1.0, 0.0);
257 glEnd();
258
259 font->getTileCoords('a', coords);
260
261 glBegin(GL_QUADS);
262 glTexCoord2f(coords[0], coords[1]);
263 glVertex3f(-1.0, -1.0, 0.0);
264 glTexCoord2f(coords[2], coords[3]);
265 glVertex3f(0.0, -1.0, 0.0);
266 glTexCoord2f(coords[4], coords[5]);
267 glVertex3f(0.0, 0.0, 0.0);
268 glTexCoord2f(coords[6], coords[7]);
269 glVertex3f(-1.0, 0.0, 0.0);
270 glEnd();
271
272 font->getTileCoords('z', coords);
273
274 glBegin(GL_QUADS);
275 glTexCoord2f(coords[0], coords[1]);
276 glVertex3f(0.0, -1.0, 0.0);
277 glTexCoord2f(coords[2], coords[3]);
278 glVertex3f(1.0, -1.0, 0.0);
279 glTexCoord2f(coords[4], coords[5]);
280 glVertex3f(1.0, 0.0, 0.0);
281 glTexCoord2f(coords[6], coords[7]);
282 glVertex3f(0.0, 0.0, 0.0);
283 glEnd();
284
285 glEnable(GL_BLEND);
286 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
287 glDisable(GL_DEPTH_TEST);
288
289 glBindTexture(GL_TEXTURE_2D, 0);
290 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
291
292 glBegin(GL_LINES);
293 glVertex2f(0.0f, 0.0f);
294 glVertex2fv(interp.getState(alpha).data());
295 glEnd();
296
297 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
298 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
299
300 glDisable(GL_BLEND);
301 glEnable(GL_DEPTH_TEST);*/
302 }
303
304 void YoinkApp::handleEvent(const Mf::Event& event)
305 {
306 switch (event.type)
307 {
308 case SDL_KEYDOWN:
309 if (event.key.keysym.sym == SDLK_ESCAPE)
310 {
311 stop();
312 }
313 else if (event.key.keysym.sym == SDLK_f)
314 {
315 getVideo().toggleFull();
316 }
317 else if (event.key.keysym.sym == SDLK_a)
318 {
319 someChar->getAnimation().startSequence("Punch");
320 }
321 else if (event.key.keysym.sym == SDLK_RIGHT)
322 {
323 x -= 50.0;
324 }
325 else if (event.key.keysym.sym == SDLK_LEFT)
326 {
327 x += 50.0;
328 }
329 else if (event.key.keysym.sym == SDLK_UP)
330 {
331 y -= 50.0;
332 }
333 else if (event.key.keysym.sym == SDLK_DOWN)
334 {
335 y += 50.0;
336 }
337 else if (event.key.keysym.sym == SDLK_PAGEUP)
338 {
339 z += 50.0;
340 }
341 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
342 {
343 z -= 50.0;
344 }
345 break;
346
347 case SDL_QUIT:
348 stop();
349 break;
350
351 case SDL_VIDEORESIZE:
352 glViewport(0, 0, event.resize.w, event.resize.h);
353 break;
354 }
355 }
356
357
358
359 int main(int argc, char* argv[])
360 {
361 YoinkApp app(argc, argv);
362 return app.run();
363 }
364
365 /** vim: set ts=4 sw=4 tw=80: *************************************************/
366
This page took 0.050401 seconds and 4 git commands to generate.