]> Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
beginnings of scene rendering
[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 <iostream>
30 #include <string>
31
32 #include <cstdlib> // getenv
33
34 #include <boost/bind.hpp>
35
36 #include "math.hh"
37 #include "opengl.hh"
38 #include "settings.hh"
39 #include "timer.hh"
40 #include "video.hh"
41
42 #include "YoinkApp.hh"
43
44 #if HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48
49 static std::string configFiles()
50 {
51 std::string files;
52
53 char* configFile = getenv("YOINKRC");
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 files += YOINK_CONFIGFILES;
64
65 return files;
66 }
67
68
69 YoinkApp::YoinkApp(int argc, char* argv[]) :
70 dc::engine(PACKAGE_STRING, argc, argv, configFiles())
71 {
72 std::cout << PACKAGE_STRING << std::endl
73 << "Compiled " << __TIME__ " " __DATE__ << std::endl
74 << "Send requests, patches, and bug reports to <"
75 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
76
77 char* dataDir = getenv("YOINK_DATADIR");
78
79 if (dataDir)
80 {
81 dc::resource::addSearchPath(dataDir);
82 }
83
84 dc::resource::addSearchPath(YOINK_DATADIR);
85
86 dc::dispatcher::instance().addHandler("video.context_recreated",
87 boost::bind(&YoinkApp::contextRecreated, this, _1), this);
88 setupGL();
89
90 state = 0.0;
91
92 someChar = new Character("RobotTrooper");
93 someChar->getAnimation().startSequence("Run");
94
95 font = new TilemapFont;
96
97 dc::vector2 coeffs[4];
98 coeffs[0] = dc::vector2(0.0, 0.0);
99 coeffs[1] = dc::vector2(0.5, 0.0);
100 coeffs[2] = dc::vector2(0.5, 0.0);
101 coeffs[3] = dc::vector2(1.0, 0.0);
102 interp.init(coeffs, 1.0, dc::interpolator::oscillate);
103
104 dc::scalar coeff[2] = {1.0, 0.0};
105 fadeIn.init(coeff, 0.5f);
106
107 testScene = new dc::scene("Test");
108 }
109
110 YoinkApp::~YoinkApp()
111 {
112 delete someChar;
113 delete font;
114
115 dc::dispatcher::instance().removeHandler(this);
116
117 std::cout << "Goodbye..." << std::endl;
118 }
119
120
121 void YoinkApp::setupGL()
122 {
123 glEnable(GL_TEXTURE_2D);
124 //glEnable(GL_CULL_FACE);
125 glEnable(GL_DEPTH_TEST);
126
127 glShadeModel(GL_SMOOTH);
128
129 //glEnable(GL_BLEND);
130 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
131 glEnable(GL_ALPHA_TEST);
132 glAlphaFunc(GL_GREATER, 0.0);
133
134 glClearColor(0.0, 0.0, 1.0, 1.0);
135
136 //glLineWidth(10.0f);
137 }
138
139 void YoinkApp::contextRecreated(const dc::notification& note)
140 {
141 // Whenever the context and a new one created, it probably won't contain our
142 // state so we need to set that up again.
143 setupGL();
144 }
145
146
147 void YoinkApp::update(dc::scalar t, dc::scalar dt)
148 {
149 //dt *= 0.2;
150
151 fadeIn.update(dt);
152
153 someChar->getAnimation().update(t, dt);
154 interp.update(dt);
155
156 prevstate = state;
157 state += dt;
158 }
159
160
161 void YoinkApp::draw(dc::scalar alpha)
162 {
163 //dc::vector4 meh;
164 //meh.random(0.0, 1.0);
165 //static dc::vector4 c1(meh);
166
167 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
168
169 //dc::scalar drawstate = cml::lerp(prevstate, state, alpha);
170 //dc::scalar sinstate = std::sin(drawstate);
171 //dc::scalar cosstate = std::cos(drawstate);
172
173
174 glMatrixMode(GL_PROJECTION);
175 glLoadIdentity();
176 gluPerspective(60.0, 1.33333, 1.0, 2000.0);
177
178 glMatrixMode(GL_MODELVIEW);
179 glLoadIdentity();
180
181 glBindTexture(GL_TEXTURE_2D, 0);
182 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
183 glTranslatef(x, y, z);
184
185 // DRAW THE SCENE
186 testScene->draw(alpha);
187
188
189 /*
190 someChar->getTilemap().bind();
191 glColor3f(1.0, 1.0, 1.0);
192
193 unsigned heroFrame = someChar->getAnimation().getFrame();
194
195 float coords[8];
196 someChar->getTilemap().getTileCoords(heroFrame, coords);
197
198 glBegin(GL_QUADS);
199 glTexCoord2f(coords[0], coords[1]);
200 glVertex3f(-1.0, 0.0, 0.0);
201 glTexCoord2f(coords[2], coords[3]);
202 glVertex3f(0.0, 0.0, 0.0);
203 glTexCoord2f(coords[4], coords[5]);
204 glVertex3f(0.0, 1.0, 0.0);
205 glTexCoord2f(coords[6], coords[7]);
206 glVertex3f(-1.0, 1.0, 0.0);
207 glEnd();
208
209
210 someChar->getTilemap().getTileCoords(heroFrame, coords,
211 dc::tilemap::reverse);
212
213 glBegin(GL_QUADS);
214 glTexCoord2f(coords[0], coords[1]);
215 glVertex3f(0.0, 0.0, 0.0);
216 glTexCoord2f(coords[2], coords[3]);
217 glVertex3f(1.0, 0.0, 0.0);
218 glTexCoord2f(coords[4], coords[5]);
219 glVertex3f(1.0, 1.0, 0.0);
220 glTexCoord2f(coords[6], coords[7]);
221 glVertex3f(0.0, 1.0, 0.0);
222 glEnd();
223
224 glColor4f(1.0,0.0,0.0,0.5);
225
226 glBindTexture(GL_TEXTURE_2D, 0);
227 glColor4fv(c1.data());
228
229 glRectd(-cosstate, -sinstate, sinstate, cosstate);
230 glRectf(0.0f, 0.0f, sinstate, cosstate);
231
232 font->bind();
233
234 font->getTileCoords('c', coords);
235
236 glBegin(GL_QUADS);
237 glTexCoord2f(coords[0], coords[1]);
238 glVertex3f(-1.0, 0.0, 0.0);
239 glTexCoord2f(coords[2], coords[3]);
240 glVertex3f(0.0, 0.0, 0.0);
241 glTexCoord2f(coords[4], coords[5]);
242 glVertex3f(0.0, 1.0, 0.0);
243 glTexCoord2f(coords[6], coords[7]);
244 glVertex3f(-1.0, 1.0, 0.0);
245 glEnd();
246
247 font->getTileCoords('h', coords);
248
249 glBegin(GL_QUADS);
250 glTexCoord2f(coords[0], coords[1]);
251 glVertex3f(0.0, 0.0, 0.0);
252 glTexCoord2f(coords[2], coords[3]);
253 glVertex3f(1.0, 0.0, 0.0);
254 glTexCoord2f(coords[4], coords[5]);
255 glVertex3f(1.0, 1.0, 0.0);
256 glTexCoord2f(coords[6], coords[7]);
257 glVertex3f(0.0, 1.0, 0.0);
258 glEnd();
259
260 font->getTileCoords('a', coords);
261
262 glBegin(GL_QUADS);
263 glTexCoord2f(coords[0], coords[1]);
264 glVertex3f(-1.0, -1.0, 0.0);
265 glTexCoord2f(coords[2], coords[3]);
266 glVertex3f(0.0, -1.0, 0.0);
267 glTexCoord2f(coords[4], coords[5]);
268 glVertex3f(0.0, 0.0, 0.0);
269 glTexCoord2f(coords[6], coords[7]);
270 glVertex3f(-1.0, 0.0, 0.0);
271 glEnd();
272
273 font->getTileCoords('z', coords);
274
275 glBegin(GL_QUADS);
276 glTexCoord2f(coords[0], coords[1]);
277 glVertex3f(0.0, -1.0, 0.0);
278 glTexCoord2f(coords[2], coords[3]);
279 glVertex3f(1.0, -1.0, 0.0);
280 glTexCoord2f(coords[4], coords[5]);
281 glVertex3f(1.0, 0.0, 0.0);
282 glTexCoord2f(coords[6], coords[7]);
283 glVertex3f(0.0, 0.0, 0.0);
284 glEnd();
285
286 glEnable(GL_BLEND);
287 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
288 glDisable(GL_DEPTH_TEST);
289
290 glBindTexture(GL_TEXTURE_2D, 0);
291 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
292
293 glBegin(GL_LINES);
294 glVertex2f(0.0f, 0.0f);
295 glVertex2fv(interp.getState(alpha).data());
296 glEnd();
297
298 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
299 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
300
301 glDisable(GL_BLEND);
302 glEnable(GL_DEPTH_TEST);*/
303 }
304
305 void YoinkApp::handleEvent(const dc::event& e)
306 {
307 switch (e.type)
308 {
309 case SDL_KEYDOWN:
310 if (e.key.keysym.sym == SDLK_ESCAPE)
311 {
312 stop();
313 }
314 else if (e.key.keysym.sym == SDLK_f)
315 {
316 getVideo().toggleFull();
317 }
318 else if (e.key.keysym.sym == SDLK_a)
319 {
320 someChar->getAnimation().startSequence("Punch");
321 }
322 else if (e.key.keysym.sym == SDLK_RIGHT)
323 {
324 x -= 50.0;
325 }
326 else if (e.key.keysym.sym == SDLK_LEFT)
327 {
328 x += 50.0;
329 }
330 else if (e.key.keysym.sym == SDLK_UP)
331 {
332 y -= 50.0;
333 }
334 else if (e.key.keysym.sym == SDLK_DOWN)
335 {
336 y += 50.0;
337 }
338 else if (e.key.keysym.sym == SDLK_PAGEUP)
339 {
340 z += 50.0;
341 }
342 else if (e.key.keysym.sym == SDLK_PAGEDOWN)
343 {
344 z -= 50.0;
345 }
346 break;
347
348 case SDL_QUIT:
349 stop();
350 break;
351
352 case SDL_VIDEORESIZE:
353 glViewport(0, 0, e.resize.w, e.resize.h);
354 break;
355 }
356 }
357
358
359
360 int main(int argc, char* argv[])
361 {
362 YoinkApp app(argc, argv);
363 return app.run();
364 }
365
366 /** vim: set ts=4 sw=4 tw=80: *************************************************/
367
This page took 0.044728 seconds and 4 git commands to generate.