]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
023b3a30ce51837365e60d22136cc8a2e04cdce3
[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/Math.hh>
36 #include <Moof/OpenGL.hh>
37 #include <Moof/Settings.hh>
38 #include <Moof/Timer.hh>
39 #include <Moof/Video.hh>
40
41 #include "YoinkApp.hh"
42
43 #if HAVE_CONFIG_H
44 #include "config.h"
45 #endif
46
47
48 static std::string configFiles()
49 {
50 std::string files;
51
52 char* configFile = getenv("YOINKRC");
53 char* dataDir = getenv("YOINK_DATADIR");
54
55 if (configFile)
56 {
57 // if a config file from the environment variable is specified, we want
58 // to load it first so it has precedence
59 files += configFile;
60 files += ":";
61 }
62
63 // add the colon-delimited paths from configure
64 files += YOINK_CONFIGFILES;
65
66 if (dataDir)
67 {
68 // if another data directory is set in the environment, look for a
69 // config file there
70 files += ":";
71 files += dataDir;
72 files += "/yoinkrc";
73 }
74
75 // look in the configured data directory last of all
76 files += ":";
77 files += (dataDir ? dataDir : YOINK_DATADIR);
78 files += "/yoinkrc";
79
80 return files;
81 }
82
83 static std::string iconFile()
84 {
85 char* dataDir = getenv("YOINK_DATADIR");
86
87 // first set up the search paths so we can find the icon and other resources
88 if (dataDir)
89 {
90 // look first in the data directory specified by the environment
91 Mf::Resource::addSearchPath(dataDir);
92 }
93
94 // then look in the configured data directory
95 Mf::Resource::addSearchPath(YOINK_DATADIR);
96
97 return Mf::Resource::getPathToResource("yoink.png");
98 }
99
100
101 YoinkApp::YoinkApp(int argc, char* argv[]) :
102 Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile())
103 {
104 Mf::Dispatcher::instance().addHandler("video.context_recreated",
105 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
106 setupGL();
107
108 state = 0.0;
109
110 someChar = new Character("RobotTrooper");
111 someChar->getAnimation().startSequence("Run");
112
113 font = new TilemapFont;
114
115 Mf::Vector2 coeffs[4];
116 coeffs[0] = Mf::Vector2(0.0, 0.0);
117 coeffs[1] = Mf::Vector2(0.5, 0.0);
118 coeffs[2] = Mf::Vector2(0.5, 0.0);
119 coeffs[3] = Mf::Vector2(1.0, 0.0);
120 interp.init(coeffs, 1.0, Mf::Interpolator::OSCILLATE);
121
122 Mf::Scalar coeff[2] = {1.0, 0.0};
123 fadeIn.init(coeff, 0.5f);
124
125 testScene = new Mf::Scene("Test");
126
127 x = y = z = 0.0;
128 }
129
130 YoinkApp::~YoinkApp()
131 {
132 delete someChar;
133 delete font;
134
135 Mf::Dispatcher::instance().removeHandler(this);
136 }
137
138
139 void YoinkApp::setupGL()
140 {
141 glEnable(GL_TEXTURE_2D);
142
143 //glEnable(GL_CULL_FACE);
144 glEnable(GL_DEPTH_TEST);
145
146 glShadeModel(GL_SMOOTH);
147 //glEnable(GL_POLYGON_SMOOTH);
148
149 //int texSize;
150 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
151 //std::cout << "texture size: " << texSize << std::endl;
152
153 //glEnable(GL_BLEND);
154 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
155 glEnable(GL_ALPHA_TEST);
156 glAlphaFunc(GL_GREATER, 0.0);
157
158 glClearColor(1.0, 0.0, 0.0, 1.0);
159
160 glMatrixMode(GL_PROJECTION);
161 glLoadIdentity();
162 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
163
164 //glLineWidth(10.0f);
165 }
166
167 void YoinkApp::contextRecreated(const Mf::Notification& note)
168 {
169 // Whenever the context and a new one created, it probably won't contain our
170 // 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.2;
178
179 fadeIn.update(dt);
180
181 camera.update(t, dt);
182
183 someChar->getAnimation().update(t, dt);
184 interp.update(dt);
185
186 prevstate = state;
187 state += dt;
188 }
189
190
191 void YoinkApp::draw(Mf::Scalar alpha)
192 {
193 //Mf::Vector4 meh;
194 //meh.random(0.0, 1.0);
195 //static Mf::Vector4 c1(meh);
196
197 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
198
199 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
200 //Mf::Scalar sinstate = std::sin(drawstate);
201 //Mf::Scalar cosstate = std::cos(drawstate);
202
203
204
205 glMatrixMode(GL_MODELVIEW);
206 //glLoadIdentity();
207
208 glBindTexture(GL_TEXTURE_2D, 0);
209 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
210 //glTranslatef(x, y, z);
211 glLoadMatrix(camera.getTransformation().data());
212
213 // DRAW THE SCENE
214 testScene->draw(alpha);
215
216
217 /*
218 glLoadIdentity();
219
220 someChar->getTilemap().bind();
221 glColor3f(1.0, 1.0, 1.0);
222
223 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
224
225 Mf::Scalar coords[8];
226 someChar->getTilemap().getTileCoords(heroFrame, coords);
227
228 glBegin(GL_QUADS);
229 glTexCoord2f(coords[0], coords[1]);
230 glVertex3f(-1.0, 0.0, 0.0);
231 glTexCoord2f(coords[2], coords[3]);
232 glVertex3f(0.0, 0.0, 0.0);
233 glTexCoord2f(coords[4], coords[5]);
234 glVertex3f(0.0, 1.0, 0.0);
235 glTexCoord2f(coords[6], coords[7]);
236 glVertex3f(-1.0, 1.0, 0.0);
237 glEnd();
238
239
240 someChar->getTilemap().getTileCoords(heroFrame, coords,
241 Mf::Tilemap::REVERSE);
242
243 glBegin(GL_QUADS);
244 glTexCoord2f(coords[0], coords[1]);
245 glVertex3f(0.0, 0.0, 0.0);
246 glTexCoord2f(coords[2], coords[3]);
247 glVertex3f(1.0, 0.0, 0.0);
248 glTexCoord2f(coords[4], coords[5]);
249 glVertex3f(1.0, 1.0, 0.0);
250 glTexCoord2f(coords[6], coords[7]);
251 glVertex3f(0.0, 1.0, 0.0);
252 glEnd();
253
254 glColor4f(1.0,0.0,0.0,0.5);
255
256 glBindTexture(GL_TEXTURE_2D, 0);
257 glColor4v(c1.data());
258
259 glRectd(-cosstate, -sinstate, sinstate, cosstate);
260 glRectf(0.0f, 0.0f, sinstate, cosstate);
261
262 font->bind();
263
264 font->getTileCoords('c', coords);
265
266 glBegin(GL_QUADS);
267 glTexCoord2f(coords[0], coords[1]);
268 glVertex3f(-1.0, 0.0, 0.0);
269 glTexCoord2f(coords[2], coords[3]);
270 glVertex3f(0.0, 0.0, 0.0);
271 glTexCoord2f(coords[4], coords[5]);
272 glVertex3f(0.0, 1.0, 0.0);
273 glTexCoord2f(coords[6], coords[7]);
274 glVertex3f(-1.0, 1.0, 0.0);
275 glEnd();
276
277 font->getTileCoords('h', coords);
278
279 glBegin(GL_QUADS);
280 glTexCoord2f(coords[0], coords[1]);
281 glVertex3f(0.0, 0.0, 0.0);
282 glTexCoord2f(coords[2], coords[3]);
283 glVertex3f(1.0, 0.0, 0.0);
284 glTexCoord2f(coords[4], coords[5]);
285 glVertex3f(1.0, 1.0, 0.0);
286 glTexCoord2f(coords[6], coords[7]);
287 glVertex3f(0.0, 1.0, 0.0);
288 glEnd();
289
290 font->getTileCoords('a', coords);
291
292 glBegin(GL_QUADS);
293 glTexCoord2f(coords[0], coords[1]);
294 glVertex3f(-1.0, -1.0, 0.0);
295 glTexCoord2f(coords[2], coords[3]);
296 glVertex3f(0.0, -1.0, 0.0);
297 glTexCoord2f(coords[4], coords[5]);
298 glVertex3f(0.0, 0.0, 0.0);
299 glTexCoord2f(coords[6], coords[7]);
300 glVertex3f(-1.0, 0.0, 0.0);
301 glEnd();
302
303 font->getTileCoords('z', coords);
304
305 glBegin(GL_QUADS);
306 glTexCoord2f(coords[0], coords[1]);
307 glVertex3(0.0, -1.0, 0.0);
308 glTexCoord2f(coords[2], coords[3]);
309 glVertex3(1.0, -1.0, 0.0);
310 glTexCoord2f(coords[4], coords[5]);
311 glVertex3(1.0, 0.0, 0.0);
312 glTexCoord2f(coords[6], coords[7]);
313 glVertex3(0.0, 0.0, 0.0);
314 glEnd();
315
316 glEnable(GL_BLEND);
317 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
318 glDisable(GL_DEPTH_TEST);
319
320 glBindTexture(GL_TEXTURE_2D, 0);
321 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
322
323 glBegin(GL_LINES);
324 glVertex2f(0.0f, 0.0f);
325 glVertex2v(interp.getState(alpha).data());
326 glEnd();
327
328 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
329 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
330
331 glDisable(GL_BLEND);
332 glEnable(GL_DEPTH_TEST);*/
333 }
334
335 void YoinkApp::handleEvent(const Mf::Event& event)
336 {
337 switch (event.type)
338 {
339 case SDL_KEYDOWN:
340 if (event.key.keysym.sym == SDLK_ESCAPE)
341 {
342 stop();
343 }
344 else if (event.key.keysym.sym == SDLK_f)
345 {
346 getVideo().toggleFull();
347 }
348 else if (event.key.keysym.sym == SDLK_a)
349 {
350 someChar->getAnimation().startSequence("Punch");
351 }
352 else if (event.key.keysym.sym == SDLK_r)
353 {
354 testScene->refresh();
355 }
356 else if (event.key.keysym.sym == SDLK_l)
357 {
358 getVideo().toggleCursorGrab();
359 getVideo().toggleCursorVisible();
360 }
361 //else if (event.key.keysym.sym == SDLK_RIGHT)
362 //{
363 //x -= 50.0;
364 //}
365 //else if (event.key.keysym.sym == SDLK_LEFT)
366 //{
367 //x += 50.0;
368 //}
369 //else if (event.key.keysym.sym == SDLK_UP)
370 //{
371 //y -= 50.0;
372 //}
373 //else if (event.key.keysym.sym == SDLK_DOWN)
374 //{
375 //y += 50.0;
376 //}
377 //else if (event.key.keysym.sym == SDLK_PAGEUP)
378 //{
379 //z += 50.0;
380 //}
381 //else if (event.key.keysym.sym == SDLK_PAGEDOWN)
382 //{
383 //z -= 50.0;
384 //}
385
386 case SDL_MOUSEMOTION:
387 case SDL_MOUSEBUTTONDOWN:
388 camera.adjustFromInput(event);
389 break;
390
391 case SDL_QUIT:
392 stop();
393 break;
394
395 case SDL_VIDEORESIZE:
396 glViewport(0, 0, event.resize.w, event.resize.h);
397
398 glMatrixMode(GL_PROJECTION);
399 glLoadIdentity();
400
401
402 gluPerspective(60.0, double(event.resize.w / event.resize.h), 1.0, 2500.0);
403
404 glMatrixMode(GL_MODELVIEW);
405 break;
406 }
407 }
408
409
410 #include <Moof/Tree.hh>
411
412 int main(int argc, char* argv[])
413 {
414 std::cout << PACKAGE_STRING << std::endl
415 << "Compiled " << __TIME__ " " __DATE__ << std::endl
416 << "Send requests, patches, and bug reports to <"
417 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
418
419 int status = 0;
420
421 //Mf::Tree<Mf::Drawable> myTree;
422 //Mf::Tree<Mf::Drawable>::Ptr prev = myTree.previousSibling();
423 //myTree = *prev;
424
425 try
426 {
427 YoinkApp app(argc, argv);
428 status = app.run();
429 }
430 catch (Mf::Engine::Exception e)
431 {
432 std::cerr << "Unhandled exception: " << e.what() << std::endl;
433 status = 1;
434 }
435
436 std::cout << "Goodbye..." << std::endl;
437 return status;
438 }
439
440 /** vim: set ts=4 sw=4 tw=80: *************************************************/
441
This page took 0.048585 seconds and 3 git commands to generate.