]>
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 <Moof/Exception.hh>
34 #include <Moof/Log.hh>
35 #include <Moof/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Settings.hh>
38 #include <Moof/Thread.hh>
39 #include <Moof/Timer.hh>
40 #include <Moof/Video.hh>
42 #include "YoinkApp.hh"
49 static std::string
configFiles()
53 char* configFile
= getenv("YOINKRC");
54 char* dataDir
= getenv("YOINK_DATADIR");
58 // if a config file from the environment variable is specified, we want
59 // to load it first so it has precedence
64 // add the colon-delimited paths from configure
65 files
+= YOINK_CONFIGFILES
;
69 // if another data directory is set in the environment, look for a
76 // look in the configured data directory last of all
78 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
84 static std::string
iconFile()
86 char* dataDir
= getenv("YOINK_DATADIR");
88 // first set up the search paths so we can find the icon and other resources
91 // look first in the data directory specified by the environment
92 Mf::Resource::addSearchPath(dataDir
);
95 // then look in the configured data directory
96 Mf::Resource::addSearchPath(YOINK_DATADIR
);
98 return Mf::Resource::getPath("yoink.png");
102 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
103 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
104 music("NightFusionIntro"),
105 punchSound("RobotPunch")
107 Mf::dispatcher::addHandler("video.context_recreated",
108 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
111 music
.setLooping(true);
112 music
.enqueue("NightFusionLoop");
115 heroine
= Character::alloc("RobotTrooper");
116 heroine
->getAnimation().startSequence("Run");
118 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
119 interp
.init(a
, 2.0, Mf::Interpolator::OSCILLATE
);
121 Mf::Scalar b
[2] = {1.0, 0.0};
124 octree
= Mf::loadScene("Classic");
125 heroine
->treeNode
= octree
->insert(heroine
);
128 YoinkApp::~YoinkApp()
130 Mf::dispatcher::removeHandler(this);
134 void YoinkApp::setupGL()
136 glEnable(GL_TEXTURE_2D
);
138 //glEnable(GL_CULL_FACE);
139 glEnable(GL_DEPTH_TEST
);
141 glShadeModel(GL_SMOOTH
);
142 //glEnable(GL_POLYGON_SMOOTH);
145 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
146 //std::cout << "texture size: " << texSize << std::endl;
148 //glEnable(GL_BLEND);
149 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
150 glEnable(GL_ALPHA_TEST
);
151 glAlphaFunc(GL_GREATER
, 0.0);
153 glClearColor(1.0, 0.0, 0.0, 1.0);
155 //glMatrixMode(GL_PROJECTION);
157 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
158 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
159 camera
.uploadProjectionToGL();
161 //glMatrixMode(GL_MODELVIEW);
163 //glLineWidth(10.0f);
166 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
168 // Whenever the context is destroyed and a new one created, it probably
169 // won't contain our state so we need to set that up again.
174 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
179 camera
.update(t
, dt
);
180 heroine
->update(t
, dt
);
183 heroine
->treeNode
= octree
->reinsert(heroine
, heroine
->treeNode
);
184 octree
->print(heroine
->treeNode
);
186 //camera.lookAt(heroine->getSphere().point);
187 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0],
188 -heroine
->current
.position
[1], -256));
191 hud
.setBar1Progress(interp
.getState(dt
));
192 hud
.setBar2Progress(1.0 - interp
.getState(dt
));
196 void YoinkApp::draw(Mf::Scalar alpha
)
198 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
200 glMatrixMode(GL_MODELVIEW
);
201 glLoadMatrix(camera
.getModelviewMatrix().data());
204 Mf::Texture::resetBind();
206 glEnableClientState(GL_VERTEX_ARRAY
);
207 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
209 octree
->drawIfVisible(alpha
, camera
.getFrustum());
211 //heroine->draw(alpha);
212 heroine
->getAabb().draw();
218 glMatrixMode(GL_PROJECTION
);
221 glMatrixMode(GL_MODELVIEW
);
224 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
225 Mf::Texture::resetBind();
227 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
229 glVertex3f(-1.0, -1.0, -0.1);
230 glVertex3f(1.0, -1.0, -0.1);
231 glVertex3f(1.0, 1.0, -0.1);
232 glVertex3f(-1.0, 1.0, -0.1);
237 glMatrixMode(GL_PROJECTION
);
239 glMatrixMode(GL_MODELVIEW
);
243 void YoinkApp::handleEvent(const Mf::Event
& event
)
248 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
253 else if (event
.key
.keysym
.sym
== SDLK_f
)
255 getVideo().toggleFull();
258 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
260 heroine
->getAnimation().startSequence("Punch");
264 else if (event
.key
.keysym
.sym
== SDLK_t
)
266 Mf::dispatcher::dispatch("video.context_recreated");
269 else if (event
.key
.keysym
.sym
== SDLK_p
)
274 else if (event
.key
.keysym
.sym
== SDLK_l
)
276 getVideo().toggleCursorGrab();
277 getVideo().toggleCursorVisible();
282 heroine
->handleEvent(event
);
284 case SDL_MOUSEMOTION
:
285 case SDL_MOUSEBUTTONDOWN
:
286 camera
.handleEvent(event
);
293 case SDL_VIDEORESIZE
:
294 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
295 hud
.resize(event
.resize
.w
, event
.resize
.h
);
296 camera
.setProjection(cml::rad(60.0),
297 double(event
.resize
.w
) / double(event
.resize
.h
), 32.0, 2500.0);
298 camera
.uploadProjectionToGL();
305 int main(int argc
, char* argv
[])
307 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
308 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
309 << "Send patches and bug reports to <"
310 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
312 #if YOINK_LOGLEVEL >= 4
313 Mf::setLogLevel(Mf::LOG_DEBUG
);
314 #elif YOINK_LOGLEVEL >= 3
315 Mf::setLogLevel(Mf::LOG_INFO
);
316 #elif YOINK_LOGLEVEL >= 2
317 Mf::setLogLevel(Mf::LOG_WARNING
);
318 #elif YOINK_LOGLEVEL >= 1
319 Mf::setLogLevel(Mf::LOG_ERROR
);
321 Mf::setLogLevel(Mf::LOG_NONE
);
328 YoinkApp
app(argc
, argv
);
331 catch (Mf::Exception e
)
333 Mf::logError("unhandled exception: <<%s>>", e
.what());
334 Mf::logInfo("it's time to crash now :-(");
339 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
344 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.047674 seconds and 4 git commands to generate.