]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
6cddf1a4d528578f4da50ec622bc217854cd9e81
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/Exception.hh>
36 #include <Moof/Log.hh>
37 #include <Moof/Math.hh>
38 #include <Moof/OpenGL.hh>
39 #include <Moof/Settings.hh>
40 #include <Moof/Timer.hh>
41 #include <Moof/Video.hh>
43 #include "YoinkApp.hh"
50 static std::string
configFiles()
54 char* configFile
= getenv("YOINKRC");
55 char* dataDir
= getenv("YOINK_DATADIR");
59 // if a config file from the environment variable is specified, we want
60 // to load it first so it has precedence
65 // add the colon-delimited paths from configure
66 files
+= YOINK_CONFIGFILES
;
70 // if another data directory is set in the environment, look for a
77 // look in the configured data directory last of all
79 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
85 static std::string
iconFile()
87 char* dataDir
= getenv("YOINK_DATADIR");
89 // first set up the search paths so we can find the icon and other resources
92 // look first in the data directory specified by the environment
93 Mf::Resource::addSearchPath(dataDir
);
96 // then look in the configured data directory
97 Mf::Resource::addSearchPath(YOINK_DATADIR
);
99 return Mf::Resource::getPath("yoink.png");
103 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
104 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
105 music("BeatTheCube"),
106 punchSound("RobotPunch")
108 Mf::dispatcher::addHandler("video.context_recreated",
109 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
112 music
.setLooping(true);
113 music
.enqueue("NightFusionLoop");
116 heroine
= Character::alloc("RobotTrooper");
117 heroine
->getAnimation().startSequence("Run");
119 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -1.5, 1.0};
120 interp
.init(a
, 2.0, Mf::Interpolator::OSCILLATE
);
122 Mf::Scalar b
[2] = {1.0, 0.0};
125 testScene
= Mf::Scene::alloc("Test");
126 heroine
->treeNode
= testScene
->getOctree()->insert(heroine
);
129 YoinkApp::~YoinkApp()
131 Mf::dispatcher::removeHandler(this);
135 void YoinkApp::setupGL()
137 glEnable(GL_TEXTURE_2D
);
139 //glEnable(GL_CULL_FACE);
140 glEnable(GL_DEPTH_TEST
);
142 glShadeModel(GL_SMOOTH
);
143 //glEnable(GL_POLYGON_SMOOTH);
146 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
147 //std::cout << "texture size: " << texSize << std::endl;
149 //glEnable(GL_BLEND);
150 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
151 glEnable(GL_ALPHA_TEST
);
152 glAlphaFunc(GL_GREATER
, 0.0);
154 glClearColor(1.0, 0.0, 0.0, 1.0);
156 //glMatrixMode(GL_PROJECTION);
158 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
159 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
160 camera
.uploadProjectionToGL();
162 //glMatrixMode(GL_MODELVIEW);
164 //glLineWidth(10.0f);
167 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
169 // Whenever the context is destroyed and a new one created, it probably
170 // won't contain our state so we need to set that up again.
175 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
181 camera
.update(t
, dt
);
182 heroine
->update(t
, dt
);
185 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
186 testScene
->getOctree()->print(heroine
->treeNode
);
188 //camera.lookAt(heroine->getSphere().point);
189 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
192 hud
.setBar1Progress(interp
.getState(dt
));
193 hud
.setBar2Progress(1.0 - interp
.getState(dt
));
197 void YoinkApp::draw(Mf::Scalar alpha
)
199 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
201 glMatrixMode(GL_MODELVIEW
);
202 glLoadMatrix(camera
.getModelviewMatrix().data());
205 Mf::Texture::resetBind();
206 testScene
->draw(alpha
, camera
);
208 //heroine->draw(alpha);
209 heroine
->getAabb().draw();
215 glMatrixMode(GL_PROJECTION
);
218 glMatrixMode(GL_MODELVIEW
);
221 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
222 Mf::Texture::resetBind();
224 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
226 glVertex3f(-1.0, -1.0, -0.1);
227 glVertex3f(1.0, -1.0, -0.1);
228 glVertex3f(1.0, 1.0, -0.1);
229 glVertex3f(-1.0, 1.0, -0.1);
234 glMatrixMode(GL_PROJECTION
);
236 glMatrixMode(GL_MODELVIEW
);
240 void YoinkApp::handleEvent(const Mf::Event
& event
)
245 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
250 else if (event
.key
.keysym
.sym
== SDLK_f
)
252 getVideo().toggleFull();
255 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
257 heroine
->getAnimation().startSequence("Punch");
261 else if (event
.key
.keysym
.sym
== SDLK_r
)
263 testScene
->refresh();
266 else if (event
.key
.keysym
.sym
== SDLK_t
)
268 Mf::dispatcher::dispatch("video.context_recreated");
271 else if (event
.key
.keysym
.sym
== SDLK_p
)
276 else if (event
.key
.keysym
.sym
== SDLK_l
)
278 getVideo().toggleCursorGrab();
279 getVideo().toggleCursorVisible();
284 heroine
->handleEvent(event
);
286 case SDL_MOUSEMOTION
:
287 case SDL_MOUSEBUTTONDOWN
:
288 camera
.handleEvent(event
);
295 case SDL_VIDEORESIZE
:
296 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
297 hud
.resize(event
.resize
.w
, event
.resize
.h
);
298 camera
.setProjection(cml::rad(60.0), double(event
.resize
.w
/ event
.resize
.h
), 32.0, 2500.0);
299 camera
.uploadProjectionToGL();
306 int main(int argc
, char* argv
[])
308 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
309 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
310 << "Send patches and bug reports to <"
311 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
313 #if YOINK_LOGLEVEL >= 4
314 Mf::setLogLevel(Mf::LOG_DEBUG
);
315 #elif YOINK_LOGLEVEL >= 3
316 Mf::setLogLevel(Mf::LOG_INFO
);
317 #elif YOINK_LOGLEVEL >= 2
318 Mf::setLogLevel(Mf::LOG_WARNING
);
319 #elif YOINK_LOGLEVEL >= 1
320 Mf::setLogLevel(Mf::LOG_ERROR
);
322 Mf::setLogLevel(Mf::LOG_NONE
);
329 YoinkApp
app(argc
, argv
);
332 catch (Mf::Exception e
)
334 Mf::logError("unhandled exception: <<%s>>", e
.what());
335 Mf::logInfo("it's time to crash now :-(");
340 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
345 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.05253 seconds and 4 git commands to generate.