]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
a1c2dbbf8133f273e2aca98efdf2dc3f8db133ef
[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 #include <SDL/SDL_sound.h>
44 #include <AL/al.h>
45 #include <AL/alut.h>
46
47 #if HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51
52 static std::string configFiles()
53 {
54 std::string files;
55
56 char* configFile = getenv("YOINKRC");
57 char* dataDir = getenv("YOINK_DATADIR");
58
59 if (configFile)
60 {
61 // if a config file from the environment variable is specified, we want
62 // to load it first so it has precedence
63 files += configFile;
64 files += ":";
65 }
66
67 // add the colon-delimited paths from configure
68 files += YOINK_CONFIGFILES;
69
70 if (dataDir)
71 {
72 // if another data directory is set in the environment, look for a
73 // config file there
74 files += ":";
75 files += dataDir;
76 files += "/yoinkrc";
77 }
78
79 // look in the configured data directory last of all
80 files += ":";
81 files += (dataDir ? dataDir : YOINK_DATADIR);
82 files += "/yoinkrc";
83
84 return files;
85 }
86
87 static std::string iconFile()
88 {
89 char* dataDir = getenv("YOINK_DATADIR");
90
91 // first set up the search paths so we can find the icon and other resources
92 if (dataDir)
93 {
94 // look first in the data directory specified by the environment
95 Mf::Resource::addSearchPath(dataDir);
96 }
97
98 // then look in the configured data directory
99 Mf::Resource::addSearchPath(YOINK_DATADIR);
100
101 return Mf::Resource::getPath("yoink.png");
102 }
103
104
105 YoinkApp::YoinkApp(int argc, char* argv[]) :
106 Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile()),
107 music("NightFusion"),
108 punchSound("RobotPunch")
109 {
110 Mf::Dispatcher::instance().addHandler("video.context_recreated",
111 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
112 setupGL();
113
114 music.play();
115
116 state = 0.0;
117
118 heroine = CharacterPtr(new Character("RobotTrooper"));
119 heroine->getAnimation().startSequence("Run");
120
121 font = new TilemapFont;
122
123 Mf::Scalar coeffs[4];
124 coeffs[0] = 0.0;
125 coeffs[1] = 1.5;
126 coeffs[2] = -0.5;
127 coeffs[3] = 1.0;
128 interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE);
129
130 Mf::Scalar coeff[2] = {1.0, 0.0};
131 fadeIn.init(coeff, 0.1);
132
133 testScene = new Mf::Scene("Test");
134 heroine->treeNode = testScene->getOctree()->insert(heroine);
135 }
136
137 YoinkApp::~YoinkApp()
138 {
139 //delete heroine;
140 delete font;
141 delete testScene;
142
143 Mf::Dispatcher::instance().removeHandler(this);
144 }
145
146
147 void YoinkApp::setupGL()
148 {
149 glEnable(GL_TEXTURE_2D);
150
151 //glEnable(GL_CULL_FACE);
152 glEnable(GL_DEPTH_TEST);
153
154 glShadeModel(GL_SMOOTH);
155 //glEnable(GL_POLYGON_SMOOTH);
156
157 //int texSize;
158 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
159 //std::cout << "texture size: " << texSize << std::endl;
160
161 //glEnable(GL_BLEND);
162 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
163 glEnable(GL_ALPHA_TEST);
164 glAlphaFunc(GL_GREATER, 0.0);
165
166 glClearColor(1.0, 0.0, 0.0, 1.0);
167
168 //glMatrixMode(GL_PROJECTION);
169 //glLoadIdentity();
170 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
171 camera.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
172 camera.uploadProjectionToGL();
173
174 //glMatrixMode(GL_MODELVIEW);
175
176 //glLineWidth(10.0f);
177 }
178
179 void YoinkApp::contextRecreated(const Mf::Notification& note)
180 {
181 // Whenever the context and a new one created, it probably won't contain our
182 // state so we need to set that up again.
183 setupGL();
184 }
185
186
187 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
188 {
189 //dt *= 0.2;
190
191 fadeIn.update(dt);
192
193 camera.update(t, dt);
194
195 heroine->update(t, dt);
196 heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode);
197
198 //camera.lookAt(heroine->getSphere().point);
199 camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256));
200
201 interp.update(dt);
202 hud.setBar1Progress(interp.getValue());
203 hud.setBar2Progress(1.0 - interp.getValue());
204
205 prevstate = state;
206 state += dt;
207 }
208
209
210 void YoinkApp::draw(Mf::Scalar alpha)
211 {
212 //Mf::Vector4 meh;
213 //meh.random(0.0, 1.0);
214 //static Mf::Vector4 c1(meh);
215
216 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217
218 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
219 //Mf::Scalar sinstate = std::sin(drawstate);
220 //Mf::Scalar cosstate = std::cos(drawstate);
221
222
223 glMatrixMode(GL_MODELVIEW);
224 //glLoadIdentity();
225
226 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
227 //glTranslatef(x, y, z);
228 glLoadMatrix(camera.getModelviewMatrix().data());
229
230 // DRAW THE SCENE
231 Mf::Texture::resetBind();
232 testScene->draw(alpha, camera);
233
234
235
236 //heroine->draw(alpha);
237
238
239 hud.draw();
240
241
242 glEnable(GL_BLEND);
243 glMatrixMode(GL_PROJECTION);
244 glPushMatrix();
245 glLoadIdentity();
246 glMatrixMode(GL_MODELVIEW);
247 glPushMatrix();
248 glLoadIdentity();
249 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
250 Mf::Texture::resetBind();
251
252 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
253 glBegin(GL_QUADS);
254 glVertex3f(-1.0, -1.0, -0.1);
255 glVertex3f(1.0, -1.0, -0.1);
256 glVertex3f(1.0, 1.0, -0.1);
257 glVertex3f(-1.0, 1.0, -0.1);
258 glEnd();
259
260 glDisable(GL_BLEND);
261
262 glMatrixMode(GL_PROJECTION);
263 glPopMatrix();
264 glMatrixMode(GL_MODELVIEW);
265 glPopMatrix();
266
267 /*
268 glLoadIdentity();
269
270 someChar->getTilemap().bind();
271 glColor3f(1.0, 1.0, 1.0);
272
273 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
274
275 Mf::Scalar coords[8];
276 someChar->getTilemap().getTileCoords(heroFrame, coords);
277
278 glBegin(GL_QUADS);
279 glTexCoord2f(coords[0], coords[1]);
280 glVertex3f(-1.0, 0.0, 0.0);
281 glTexCoord2f(coords[2], coords[3]);
282 glVertex3f(0.0, 0.0, 0.0);
283 glTexCoord2f(coords[4], coords[5]);
284 glVertex3f(0.0, 1.0, 0.0);
285 glTexCoord2f(coords[6], coords[7]);
286 glVertex3f(-1.0, 1.0, 0.0);
287 glEnd();
288
289
290 someChar->getTilemap().getTileCoords(heroFrame, coords,
291 Mf::Tilemap::REVERSE);
292
293 glBegin(GL_QUADS);
294 glTexCoord2f(coords[0], coords[1]);
295 glVertex3f(0.0, 0.0, 0.0);
296 glTexCoord2f(coords[2], coords[3]);
297 glVertex3f(1.0, 0.0, 0.0);
298 glTexCoord2f(coords[4], coords[5]);
299 glVertex3f(1.0, 1.0, 0.0);
300 glTexCoord2f(coords[6], coords[7]);
301 glVertex3f(0.0, 1.0, 0.0);
302 glEnd();
303
304 glColor4f(1.0,0.0,0.0,0.5);
305
306 glBindTexture(GL_TEXTURE_2D, 0);
307 glColor4v(c1.data());
308
309 glRectd(-cosstate, -sinstate, sinstate, cosstate);
310 glRectf(0.0f, 0.0f, sinstate, cosstate);
311
312 font->bind();
313
314 font->getTileCoords('c', coords);
315
316 glBegin(GL_QUADS);
317 glTexCoord2f(coords[0], coords[1]);
318 glVertex3f(-1.0, 0.0, 0.0);
319 glTexCoord2f(coords[2], coords[3]);
320 glVertex3f(0.0, 0.0, 0.0);
321 glTexCoord2f(coords[4], coords[5]);
322 glVertex3f(0.0, 1.0, 0.0);
323 glTexCoord2f(coords[6], coords[7]);
324 glVertex3f(-1.0, 1.0, 0.0);
325 glEnd();
326
327 font->getTileCoords('h', coords);
328
329 glBegin(GL_QUADS);
330 glTexCoord2f(coords[0], coords[1]);
331 glVertex3f(0.0, 0.0, 0.0);
332 glTexCoord2f(coords[2], coords[3]);
333 glVertex3f(1.0, 0.0, 0.0);
334 glTexCoord2f(coords[4], coords[5]);
335 glVertex3f(1.0, 1.0, 0.0);
336 glTexCoord2f(coords[6], coords[7]);
337 glVertex3f(0.0, 1.0, 0.0);
338 glEnd();
339
340 font->getTileCoords('a', coords);
341
342 glBegin(GL_QUADS);
343 glTexCoord2f(coords[0], coords[1]);
344 glVertex3f(-1.0, -1.0, 0.0);
345 glTexCoord2f(coords[2], coords[3]);
346 glVertex3f(0.0, -1.0, 0.0);
347 glTexCoord2f(coords[4], coords[5]);
348 glVertex3f(0.0, 0.0, 0.0);
349 glTexCoord2f(coords[6], coords[7]);
350 glVertex3f(-1.0, 0.0, 0.0);
351 glEnd();
352
353 font->getTileCoords('z', coords);
354
355 glBegin(GL_QUADS);
356 glTexCoord2f(coords[0], coords[1]);
357 glVertex3(0.0, -1.0, 0.0);
358 glTexCoord2f(coords[2], coords[3]);
359 glVertex3(1.0, -1.0, 0.0);
360 glTexCoord2f(coords[4], coords[5]);
361 glVertex3(1.0, 0.0, 0.0);
362 glTexCoord2f(coords[6], coords[7]);
363 glVertex3(0.0, 0.0, 0.0);
364 glEnd();
365
366 glEnable(GL_BLEND);
367 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
368 glDisable(GL_DEPTH_TEST);
369
370 glBindTexture(GL_TEXTURE_2D, 0);
371 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
372
373 glBegin(GL_LINES);
374 glVertex2f(0.0f, 0.0f);
375 glVertex2v(interp.getState(alpha).data());
376 glEnd();
377
378 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
379 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
380
381 glDisable(GL_BLEND);
382 glEnable(GL_DEPTH_TEST);*/
383 }
384
385 void YoinkApp::handleEvent(const Mf::Event& event)
386 {
387 switch (event.type)
388 {
389 case SDL_KEYDOWN:
390 if (event.key.keysym.sym == SDLK_ESCAPE)
391 {
392 stop();
393 }
394 else if (event.key.keysym.sym == SDLK_f)
395 {
396 getVideo().toggleFull();
397 }
398 else if (event.key.keysym.sym == SDLK_SPACE)
399 {
400 heroine->getAnimation().startSequence("Punch");
401 punchSound.play();
402 }
403 else if (event.key.keysym.sym == SDLK_r)
404 {
405 testScene->refresh();
406 }
407 else if (event.key.keysym.sym == SDLK_l)
408 {
409 getVideo().toggleCursorGrab();
410 getVideo().toggleCursorVisible();
411 }
412
413 case SDL_KEYUP:
414 heroine->handleEvent(event);
415
416 case SDL_MOUSEMOTION:
417 case SDL_MOUSEBUTTONDOWN:
418 camera.adjustFromInput(event);
419 break;
420
421 case SDL_QUIT:
422 stop();
423 break;
424
425 case SDL_VIDEORESIZE:
426 glViewport(0, 0, event.resize.w, event.resize.h);
427 hud.resize(event.resize.w, event.resize.h);
428 camera.setProjection(cml::rad(60.0), double(event.resize.w / event.resize.h), 32.0, 2500.0);
429 camera.uploadProjectionToGL();
430 break;
431 }
432 }
433
434
435
436 int main(int argc, char* argv[])
437 {
438 std::cout << PACKAGE_STRING << std::endl
439 << "Compiled " << __TIME__ " " __DATE__ << std::endl
440 << "Send patches and bug reports to <"
441 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
442
443 int status = 0;
444
445 try
446 {
447 YoinkApp app(argc, argv);
448 status = app.run();
449 }
450 catch (Mf::Engine::Exception e)
451 {
452 std::cerr << "Unhandled exception: " << e.what() << std::endl;
453 status = 1;
454 }
455
456 std::cout << "Goodbye..." << std::endl;
457 return status;
458 }
459
460
461 /** vim: set ts=4 sw=4 tw=80: *************************************************/
462
This page took 0.055934 seconds and 3 git commands to generate.