]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
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("NightFusionIntro"),
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 font
= new TilemapFont
;
121 Mf::Scalar coeffs
[4];
126 interp
.init(coeffs
, 1.0, Mf::Interpolator::OSCILLATE
);
128 Mf::Scalar coeff
[2] = {1.0, 0.0};
129 fadeIn
.init(coeff
, 0.1);
131 testScene
= Mf::Scene::alloc("Test");
132 heroine
->treeNode
= testScene
->getOctree()->insert(heroine
);
135 YoinkApp::~YoinkApp()
140 Mf::dispatcher::removeHandler(this);
144 void YoinkApp::setupGL()
146 glEnable(GL_TEXTURE_2D
);
148 //glEnable(GL_CULL_FACE);
149 glEnable(GL_DEPTH_TEST
);
151 glShadeModel(GL_SMOOTH
);
152 //glEnable(GL_POLYGON_SMOOTH);
155 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
156 //std::cout << "texture size: " << texSize << std::endl;
158 //glEnable(GL_BLEND);
159 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
160 glEnable(GL_ALPHA_TEST
);
161 glAlphaFunc(GL_GREATER
, 0.0);
163 glClearColor(1.0, 0.0, 0.0, 1.0);
165 //glMatrixMode(GL_PROJECTION);
167 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
168 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
169 camera
.uploadProjectionToGL();
171 //glMatrixMode(GL_MODELVIEW);
173 //glLineWidth(10.0f);
176 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
178 // Whenever the context and a new one created, it probably won't contain our
179 // state so we need to set that up again.
184 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
190 camera
.update(t
, dt
);
191 heroine
->update(t
, dt
);
194 heroine
->treeNode
= testScene
->getOctree()->reinsert(heroine
, heroine
->treeNode
);
195 testScene
->getOctree()->print(heroine
->treeNode
);
197 //camera.lookAt(heroine->getSphere().point);
198 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0], -heroine
->current
.position
[1], -256));
201 hud
.setBar1Progress(interp
.getValue());
202 hud
.setBar2Progress(1.0 - interp
.getValue());
206 void YoinkApp::draw(Mf::Scalar alpha
)
208 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
210 glMatrixMode(GL_MODELVIEW
);
211 glLoadMatrix(camera
.getModelviewMatrix().data());
214 Mf::Texture::resetBind();
215 testScene
->draw(alpha
, camera
);
217 //heroine->draw(alpha);
218 heroine
->getAabb().draw();
224 glMatrixMode(GL_PROJECTION
);
227 glMatrixMode(GL_MODELVIEW
);
230 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
231 Mf::Texture::resetBind();
233 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
235 glVertex3f(-1.0, -1.0, -0.1);
236 glVertex3f(1.0, -1.0, -0.1);
237 glVertex3f(1.0, 1.0, -0.1);
238 glVertex3f(-1.0, 1.0, -0.1);
243 glMatrixMode(GL_PROJECTION
);
245 glMatrixMode(GL_MODELVIEW
);
249 void YoinkApp::handleEvent(const Mf::Event
& event
)
254 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
259 else if (event
.key
.keysym
.sym
== SDLK_f
)
261 getVideo().toggleFull();
264 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
266 heroine
->getAnimation().startSequence("Punch");
270 else if (event
.key
.keysym
.sym
== SDLK_r
)
272 testScene
->refresh();
275 else if (event
.key
.keysym
.sym
== SDLK_t
)
277 Mf::dispatcher::dispatch("video.context_recreated");
280 else if (event
.key
.keysym
.sym
== SDLK_p
)
285 else if (event
.key
.keysym
.sym
== SDLK_l
)
287 getVideo().toggleCursorGrab();
288 getVideo().toggleCursorVisible();
293 heroine
->handleEvent(event
);
295 case SDL_MOUSEMOTION
:
296 case SDL_MOUSEBUTTONDOWN
:
297 camera
.handleEvent(event
);
304 case SDL_VIDEORESIZE
:
305 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
306 hud
.resize(event
.resize
.w
, event
.resize
.h
);
307 camera
.setProjection(cml::rad(60.0), double(event
.resize
.w
/ event
.resize
.h
), 32.0, 2500.0);
308 camera
.uploadProjectionToGL();
315 int main(int argc
, char* argv
[])
317 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
318 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
319 << "Send patches and bug reports to <"
320 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
322 #if YOINK_LOGLEVEL >= 4
323 Mf::setLogLevel(Mf::LOG_DEBUG
);
324 #elif YOINK_LOGLEVEL >= 3
325 Mf::setLogLevel(Mf::LOG_INFO
);
326 #elif YOINK_LOGLEVEL >= 2
327 Mf::setLogLevel(Mf::LOG_WARNING
);
328 #elif YOINK_LOGLEVEL >= 1
329 Mf::setLogLevel(Mf::LOG_ERROR
);
331 Mf::setLogLevel(Mf::LOG_NONE
);
338 YoinkApp
app(argc
, argv
);
341 catch (Mf::Exception e
)
343 Mf::logError("unhandled exception: <<%s>>", e
.what());
344 Mf::logInfo("it's time to crash now :-(");
348 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
353 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.043948 seconds and 4 git commands to generate.