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