]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
minor cleanups
[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/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>
42
43 #include "YoinkApp.hh"
44
45 #if HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49
50 static std::string configFiles()
51 {
52 std::string files;
53
54 char* configFile = getenv("YOINKRC");
55 char* dataDir = getenv("YOINK_DATADIR");
56
57 if (configFile)
58 {
59 // if a config file from the environment variable is specified, we want
60 // to load it first so it has precedence
61 files += configFile;
62 files += ":";
63 }
64
65 // add the colon-delimited paths from configure
66 files += YOINK_CONFIGFILES;
67
68 if (dataDir)
69 {
70 // if another data directory is set in the environment, look for a
71 // config file there
72 files += ":";
73 files += dataDir;
74 files += "/yoinkrc";
75 }
76
77 // look in the configured data directory last of all
78 files += ":";
79 files += (dataDir ? dataDir : YOINK_DATADIR);
80 files += "/yoinkrc";
81
82 return files;
83 }
84
85 static std::string iconFile()
86 {
87 char* dataDir = getenv("YOINK_DATADIR");
88
89 // first set up the search paths so we can find the icon and other resources
90 if (dataDir)
91 {
92 // look first in the data directory specified by the environment
93 Mf::Resource::addSearchPath(dataDir);
94 }
95
96 // then look in the configured data directory
97 Mf::Resource::addSearchPath(YOINK_DATADIR);
98
99 return Mf::Resource::getPath("yoink.png");
100 }
101
102
103 YoinkApp::YoinkApp(int argc, char* argv[]) :
104 Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile()),
105 music("NightFusionIntro"),
106 punchSound("RobotPunch")
107 {
108 Mf::dispatcher::addHandler("video.context_recreated",
109 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
110 setupGL();
111
112 music.setLooping(true);
113 music.enqueue("NightFusionLoop");
114 music.stream();
115
116 heroine = Character::alloc("RobotTrooper");
117 heroine->getAnimation().startSequence("Run");
118
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);
121
122 Mf::Scalar b[2] = {1.0, 0.0};
123 fadeIn.init(b, 1.0);
124
125 testScene = Mf::Scene::alloc("Test");
126 heroine->treeNode = testScene->getOctree()->insert(heroine);
127 }
128
129 YoinkApp::~YoinkApp()
130 {
131 Mf::dispatcher::removeHandler(this);
132 }
133
134
135 void YoinkApp::setupGL()
136 {
137 glEnable(GL_TEXTURE_2D);
138
139 //glEnable(GL_CULL_FACE);
140 glEnable(GL_DEPTH_TEST);
141
142 glShadeModel(GL_SMOOTH);
143 //glEnable(GL_POLYGON_SMOOTH);
144
145 //int texSize;
146 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
147 //std::cout << "texture size: " << texSize << std::endl;
148
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);
153
154 glClearColor(1.0, 0.0, 0.0, 1.0);
155
156 //glMatrixMode(GL_PROJECTION);
157 //glLoadIdentity();
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();
161
162 //glMatrixMode(GL_MODELVIEW);
163
164 //glLineWidth(10.0f);
165 }
166
167 void YoinkApp::contextRecreated(const Mf::Notification* note)
168 {
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.
171 setupGL();
172 }
173
174
175 void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt)
176 {
177 //dt *= 0.1;
178
179 music.update(t, dt);
180 fadeIn.update(dt);
181 camera.update(t, dt);
182 heroine->update(t, dt);
183
184 // reinsert heroine
185 heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode);
186 testScene->getOctree()->print(heroine->treeNode);
187
188 //camera.lookAt(heroine->getSphere().point);
189 camera.setPosition(Mf::Vector3(-heroine->current.position[0], -heroine->current.position[1], -256));
190
191 interp.update(dt);
192 hud.setBar1Progress(interp.getState(dt));
193 hud.setBar2Progress(1.0 - interp.getState(dt));
194 }
195
196
197 void YoinkApp::draw(Mf::Scalar alpha)
198 {
199 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
200
201 glMatrixMode(GL_MODELVIEW);
202 glLoadMatrix(camera.getModelviewMatrix().data());
203
204 // DRAW THE SCENE
205 Mf::Texture::resetBind();
206 testScene->draw(alpha, camera);
207
208 //heroine->draw(alpha);
209 heroine->getAabb().draw();
210
211 hud.draw();
212
213 // DRAW FADE
214 glEnable(GL_BLEND);
215 glMatrixMode(GL_PROJECTION);
216 glPushMatrix();
217 glLoadIdentity();
218 glMatrixMode(GL_MODELVIEW);
219 glPushMatrix();
220 glLoadIdentity();
221 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
222 Mf::Texture::resetBind();
223
224 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
225 glBegin(GL_QUADS);
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);
230 glEnd();
231
232 glDisable(GL_BLEND);
233
234 glMatrixMode(GL_PROJECTION);
235 glPopMatrix();
236 glMatrixMode(GL_MODELVIEW);
237 glPopMatrix();
238 }
239
240 void YoinkApp::handleEvent(const Mf::Event& event)
241 {
242 switch (event.type)
243 {
244 case SDL_KEYDOWN:
245 if (event.key.keysym.sym == SDLK_ESCAPE)
246 {
247 stop();
248 break;
249 }
250 else if (event.key.keysym.sym == SDLK_f)
251 {
252 getVideo().toggleFull();
253 break;
254 }
255 else if (event.key.keysym.sym == SDLK_SPACE)
256 {
257 heroine->getAnimation().startSequence("Punch");
258 punchSound.play();
259 break;
260 }
261 else if (event.key.keysym.sym == SDLK_r)
262 {
263 testScene->refresh();
264 break;
265 }
266 else if (event.key.keysym.sym == SDLK_t)
267 {
268 Mf::dispatcher::dispatch("video.context_recreated");
269 break;
270 }
271 else if (event.key.keysym.sym == SDLK_p)
272 {
273 music.toggle();
274 break;
275 }
276 else if (event.key.keysym.sym == SDLK_l)
277 {
278 getVideo().toggleCursorGrab();
279 getVideo().toggleCursorVisible();
280 break;
281 }
282
283 case SDL_KEYUP:
284 heroine->handleEvent(event);
285
286 case SDL_MOUSEMOTION:
287 case SDL_MOUSEBUTTONDOWN:
288 camera.handleEvent(event);
289 break;
290
291 case SDL_QUIT:
292 stop();
293 break;
294
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();
300 break;
301 }
302 }
303
304
305
306 int main(int argc, char* argv[])
307 {
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;
312
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);
321 #elif YOINK_LOGLEVEL
322 Mf::setLogLevel(Mf::LOG_NONE);
323 #endif
324
325 int status = 0;
326
327 try
328 {
329 YoinkApp app(argc, argv);
330 status = app.run();
331 }
332 catch (Mf::Exception e)
333 {
334 Mf::logError("unhandled exception: <<%s>>", e.what());
335 Mf::logInfo("it's time to crash now :-(");
336 //status = 1;
337 throw e;
338 }
339
340 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
341 return status;
342 }
343
344
345 /** vim: set ts=4 sw=4 tw=80: *************************************************/
346
This page took 0.046297 seconds and 4 git commands to generate.