]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
a1c2dbbf8133f273e2aca98efdf2dc3f8db133ef
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 <cstdlib> // getenv
33 #include <boost/bind.hpp>
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>
41 #include "YoinkApp.hh"
43 #include <SDL/SDL_sound.h>
52 static std::string
configFiles()
56 char* configFile
= getenv("YOINKRC");
57 char* dataDir
= getenv("YOINK_DATADIR");
61 // if a config file from the environment variable is specified, we want
62 // to load it first so it has precedence
67 // add the colon-delimited paths from configure
68 files
+= YOINK_CONFIGFILES
;
72 // if another data directory is set in the environment, look for a
79 // look in the configured data directory last of all
81 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
87 static std::string
iconFile()
89 char* dataDir
= getenv("YOINK_DATADIR");
91 // first set up the search paths so we can find the icon and other resources
94 // look first in the data directory specified by the environment
95 Mf::Resource::addSearchPath(dataDir
);
98 // then look in the configured data directory
99 Mf::Resource::addSearchPath(YOINK_DATADIR
);
101 return Mf::Resource::getPath("yoink.png");
105 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
106 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
107 music("NightFusion"),
108 punchSound("RobotPunch")
110 Mf::Dispatcher::instance().addHandler("video.context_recreated",
111 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
118 heroine
= CharacterPtr(new Character("RobotTrooper"));
119 heroine
->getAnimation().startSequence("Run");
121 font
= new TilemapFont
;
123 Mf::Scalar coeffs
[4];
128 interp
.init(coeffs
, 1.0, Mf::Interpolator::OSCILLATE
);
130 Mf::Scalar coeff
[2] = {1.0, 0.0};
131 fadeIn
.init(coeff
, 0.1);
133 testScene
= new Mf::Scene("Test");
134 heroine
->treeNode
= testScene
->getOctree()->insert(heroine
);
137 YoinkApp::~YoinkApp()
143 Mf::Dispatcher::instance().removeHandler(this);
147 void YoinkApp::setupGL()
149 glEnable(GL_TEXTURE_2D
);
151 //glEnable(GL_CULL_FACE);
152 glEnable(GL_DEPTH_TEST
);
154 glShadeModel(GL_SMOOTH
);
155 //glEnable(GL_POLYGON_SMOOTH);
158 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
159 //std::cout << "texture size: " << texSize << std::endl;
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);
166 glClearColor(1.0, 0.0, 0.0, 1.0);
168 //glMatrixMode(GL_PROJECTION);
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();
174 //glMatrixMode(GL_MODELVIEW);
176 //glLineWidth(10.0f);
179 void YoinkApp::contextRecreated(const Mf::Notification
& note
)
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.
187 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
193 camera
.update(t
, dt
);
195 heroine
->update(t
, dt
);
196 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
198 //camera.lookAt(heroine->getSphere().point);
199 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
202 hud
.setBar1Progress(interp
.getValue());
203 hud
.setBar2Progress(1.0 - interp
.getValue());
210 void YoinkApp::draw(Mf::Scalar alpha
)
213 //meh.random(0.0, 1.0);
214 //static Mf::Vector4 c1(meh);
216 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
218 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
219 //Mf::Scalar sinstate = std::sin(drawstate);
220 //Mf::Scalar cosstate = std::cos(drawstate);
223 glMatrixMode(GL_MODELVIEW
);
226 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
227 //glTranslatef(x, y, z);
228 glLoadMatrix(camera
.getModelviewMatrix().data());
231 Mf::Texture::resetBind();
232 testScene
->draw(alpha
, camera
);
236 //heroine->draw(alpha);
243 glMatrixMode(GL_PROJECTION
);
246 glMatrixMode(GL_MODELVIEW
);
249 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
250 Mf::Texture::resetBind();
252 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
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);
262 glMatrixMode(GL_PROJECTION
);
264 glMatrixMode(GL_MODELVIEW
);
270 someChar->getTilemap().bind();
271 glColor3f(1.0, 1.0, 1.0);
273 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
275 Mf::Scalar coords[8];
276 someChar->getTilemap().getTileCoords(heroFrame, coords);
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);
290 someChar->getTilemap().getTileCoords(heroFrame, coords,
291 Mf::Tilemap::REVERSE);
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);
304 glColor4f(1.0,0.0,0.0,0.5);
306 glBindTexture(GL_TEXTURE_2D, 0);
307 glColor4v(c1.data());
309 glRectd(-cosstate, -sinstate, sinstate, cosstate);
310 glRectf(0.0f, 0.0f, sinstate, cosstate);
314 font->getTileCoords('c', coords);
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);
327 font->getTileCoords('h', coords);
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);
340 font->getTileCoords('a', coords);
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);
353 font->getTileCoords('z', coords);
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);
367 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
368 glDisable(GL_DEPTH_TEST);
370 glBindTexture(GL_TEXTURE_2D, 0);
371 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
374 glVertex2f(0.0f, 0.0f);
375 glVertex2v(interp.getState(alpha).data());
378 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
379 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
382 glEnable(GL_DEPTH_TEST);*/
385 void YoinkApp::handleEvent(const Mf::Event
& event
)
390 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
394 else if (event
.key
.keysym
.sym
== SDLK_f
)
396 getVideo().toggleFull();
398 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
400 heroine
->getAnimation().startSequence("Punch");
403 else if (event
.key
.keysym
.sym
== SDLK_r
)
405 testScene
->refresh();
407 else if (event
.key
.keysym
.sym
== SDLK_l
)
409 getVideo().toggleCursorGrab();
410 getVideo().toggleCursorVisible();
414 heroine
->handleEvent(event
);
416 case SDL_MOUSEMOTION
:
417 case SDL_MOUSEBUTTONDOWN
:
418 camera
.adjustFromInput(event
);
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();
436 int main(int argc
, char* argv
[])
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
;
447 YoinkApp
app(argc
, argv
);
450 catch (Mf::Engine::Exception e
)
452 std::cerr
<< "Unhandled exception: " << e
.what() << std::endl
;
456 std::cout
<< "Goodbye..." << std::endl
;
461 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.054239 seconds and 4 git commands to generate.