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