]>
Dogcows Code - chaz/yoink/blob - src/YoinkApp.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #include <cstdlib> // getenv
33 #include <boost/bind.hpp>
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>
41 #include "YoinkApp.hh"
48 static std::string
configFiles()
52 char* configFile
= getenv("YOINKRC");
53 char* dataDir
= getenv("YOINK_DATADIR");
57 // if a config file from the environment variable is specified, we want
58 // to load it first so it has precedence
63 // add the colon-delimited paths from configure
64 files
+= YOINK_CONFIGFILES
;
68 // if another data directory is set in the environment, look for a
75 // look in the configured data directory last of all
77 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
83 static std::string
iconFile()
85 char* dataDir
= getenv("YOINK_DATADIR");
87 // first set up the search paths so we can find the icon and other resources
90 // look first in the data directory specified by the environment
91 Mf::Resource::addSearchPath(dataDir
);
94 // then look in the configured data directory
95 Mf::Resource::addSearchPath(YOINK_DATADIR
);
97 return Mf::Resource::getPathToResource("yoink.png");
101 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
102 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile())
104 Mf::Dispatcher::instance().addHandler("video.context_recreated",
105 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
110 someChar
= new Character("RobotTrooper");
111 someChar
->getAnimation().startSequence("Run");
113 font
= new TilemapFont
;
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
);
122 Mf::Scalar coeff
[2] = {1.0, 0.0};
123 fadeIn
.init(coeff
, 0.5f
);
125 testScene
= new Mf::Scene("Test");
130 YoinkApp::~YoinkApp()
136 Mf::Dispatcher::instance().removeHandler(this);
140 void YoinkApp::setupGL()
142 glEnable(GL_TEXTURE_2D
);
144 //glEnable(GL_CULL_FACE);
145 glEnable(GL_DEPTH_TEST
);
147 glShadeModel(GL_SMOOTH
);
148 //glEnable(GL_POLYGON_SMOOTH);
151 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
152 //std::cout << "texture size: " << texSize << std::endl;
154 //glEnable(GL_BLEND);
155 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
156 glEnable(GL_ALPHA_TEST
);
157 glAlphaFunc(GL_GREATER
, 0.0);
159 glClearColor(1.0, 0.0, 0.0, 1.0);
161 //glMatrixMode(GL_PROJECTION);
163 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
164 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
165 camera
.uploadProjectionToGL();
167 //glMatrixMode(GL_MODELVIEW);
169 //glLineWidth(10.0f);
172 void YoinkApp::contextRecreated(const Mf::Notification
& note
)
174 // Whenever the context and a new one created, it probably won't contain our
175 // state so we need to set that up again.
180 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
186 camera
.update(t
, dt
);
188 someChar
->getAnimation().update(t
, dt
);
196 void YoinkApp::draw(Mf::Scalar alpha
)
199 //meh.random(0.0, 1.0);
200 //static Mf::Vector4 c1(meh);
202 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
204 //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha);
205 //Mf::Scalar sinstate = std::sin(drawstate);
206 //Mf::Scalar cosstate = std::cos(drawstate);
210 glMatrixMode(GL_MODELVIEW
);
213 glBindTexture(GL_TEXTURE_2D
, 0);
214 //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0);
215 //glTranslatef(x, y, z);
216 glLoadMatrix(camera
.getModelviewMatrix().data());
219 testScene
->draw(alpha
, camera
);
225 someChar->getTilemap().bind();
226 glColor3f(1.0, 1.0, 1.0);
228 Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame();
230 Mf::Scalar coords[8];
231 someChar->getTilemap().getTileCoords(heroFrame, coords);
234 glTexCoord2f(coords[0], coords[1]);
235 glVertex3f(-1.0, 0.0, 0.0);
236 glTexCoord2f(coords[2], coords[3]);
237 glVertex3f(0.0, 0.0, 0.0);
238 glTexCoord2f(coords[4], coords[5]);
239 glVertex3f(0.0, 1.0, 0.0);
240 glTexCoord2f(coords[6], coords[7]);
241 glVertex3f(-1.0, 1.0, 0.0);
245 someChar->getTilemap().getTileCoords(heroFrame, coords,
246 Mf::Tilemap::REVERSE);
249 glTexCoord2f(coords[0], coords[1]);
250 glVertex3f(0.0, 0.0, 0.0);
251 glTexCoord2f(coords[2], coords[3]);
252 glVertex3f(1.0, 0.0, 0.0);
253 glTexCoord2f(coords[4], coords[5]);
254 glVertex3f(1.0, 1.0, 0.0);
255 glTexCoord2f(coords[6], coords[7]);
256 glVertex3f(0.0, 1.0, 0.0);
259 glColor4f(1.0,0.0,0.0,0.5);
261 glBindTexture(GL_TEXTURE_2D, 0);
262 glColor4v(c1.data());
264 glRectd(-cosstate, -sinstate, sinstate, cosstate);
265 glRectf(0.0f, 0.0f, sinstate, cosstate);
269 font->getTileCoords('c', coords);
272 glTexCoord2f(coords[0], coords[1]);
273 glVertex3f(-1.0, 0.0, 0.0);
274 glTexCoord2f(coords[2], coords[3]);
275 glVertex3f(0.0, 0.0, 0.0);
276 glTexCoord2f(coords[4], coords[5]);
277 glVertex3f(0.0, 1.0, 0.0);
278 glTexCoord2f(coords[6], coords[7]);
279 glVertex3f(-1.0, 1.0, 0.0);
282 font->getTileCoords('h', coords);
285 glTexCoord2f(coords[0], coords[1]);
286 glVertex3f(0.0, 0.0, 0.0);
287 glTexCoord2f(coords[2], coords[3]);
288 glVertex3f(1.0, 0.0, 0.0);
289 glTexCoord2f(coords[4], coords[5]);
290 glVertex3f(1.0, 1.0, 0.0);
291 glTexCoord2f(coords[6], coords[7]);
292 glVertex3f(0.0, 1.0, 0.0);
295 font->getTileCoords('a', coords);
298 glTexCoord2f(coords[0], coords[1]);
299 glVertex3f(-1.0, -1.0, 0.0);
300 glTexCoord2f(coords[2], coords[3]);
301 glVertex3f(0.0, -1.0, 0.0);
302 glTexCoord2f(coords[4], coords[5]);
303 glVertex3f(0.0, 0.0, 0.0);
304 glTexCoord2f(coords[6], coords[7]);
305 glVertex3f(-1.0, 0.0, 0.0);
308 font->getTileCoords('z', coords);
311 glTexCoord2f(coords[0], coords[1]);
312 glVertex3(0.0, -1.0, 0.0);
313 glTexCoord2f(coords[2], coords[3]);
314 glVertex3(1.0, -1.0, 0.0);
315 glTexCoord2f(coords[4], coords[5]);
316 glVertex3(1.0, 0.0, 0.0);
317 glTexCoord2f(coords[6], coords[7]);
318 glVertex3(0.0, 0.0, 0.0);
322 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
323 glDisable(GL_DEPTH_TEST);
325 glBindTexture(GL_TEXTURE_2D, 0);
326 glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
329 glVertex2f(0.0f, 0.0f);
330 glVertex2v(interp.getState(alpha).data());
333 glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha));
334 glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
337 glEnable(GL_DEPTH_TEST);*/
340 void YoinkApp::handleEvent(const Mf::Event
& event
)
345 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
349 else if (event
.key
.keysym
.sym
== SDLK_f
)
351 getVideo().toggleFull();
353 else if (event
.key
.keysym
.sym
== SDLK_a
)
355 someChar
->getAnimation().startSequence("Punch");
357 else if (event
.key
.keysym
.sym
== SDLK_r
)
359 testScene
->refresh();
361 else if (event
.key
.keysym
.sym
== SDLK_l
)
363 getVideo().toggleCursorGrab();
364 getVideo().toggleCursorVisible();
366 //else if (event.key.keysym.sym == SDLK_RIGHT)
370 //else if (event.key.keysym.sym == SDLK_LEFT)
374 //else if (event.key.keysym.sym == SDLK_UP)
378 //else if (event.key.keysym.sym == SDLK_DOWN)
382 //else if (event.key.keysym.sym == SDLK_PAGEUP)
386 //else if (event.key.keysym.sym == SDLK_PAGEDOWN)
391 case SDL_MOUSEMOTION
:
392 case SDL_MOUSEBUTTONDOWN
:
393 camera
.adjustFromInput(event
);
400 case SDL_VIDEORESIZE
:
401 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
402 camera
.setProjection(cml::rad(60.0), double(event
.resize
.w
/ event
.resize
.h
), 32.0, 2500.0);
403 camera
.uploadProjectionToGL();
410 int main(int argc
, char* argv
[])
412 std::cout
<< PACKAGE_STRING
<< std::endl
413 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
414 << "Send requests, patches, and bug reports to <"
415 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
421 YoinkApp
app(argc
, argv
);
424 catch (Mf::Engine::Exception e
)
426 std::cerr
<< "Unhandled exception: " << e
.what() << std::endl
;
430 std::cout
<< "Goodbye..." << std::endl
;
435 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.053515 seconds and 4 git commands to generate.