]>
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
34 #include <Moof/Exception.hh>
35 #include <Moof/Log.hh>
36 #include <Moof/Math.hh>
37 #include <Moof/OpenGL.hh>
38 #include <Moof/Settings.hh>
39 #include <Moof/Thread.hh>
40 #include <Moof/Timer.hh>
41 #include <Moof/Video.hh>
43 #include "YoinkApp.hh"
50 static std::string
configFiles()
54 // look in the configured data directory last of all
55 char* dataDir
= getenv("YOINK_DATADIR");
56 files
+= (dataDir
? dataDir
: YOINK_DATADIR
);
59 // add the colon-delimited paths from configure
61 files
+= YOINK_CONFIGFILES
;
63 char* configFile
= getenv("YOINKRC");
66 // if a config file from the environment variable is specified, we want
67 // to load it first so it has precedence
75 static std::string
iconFile()
77 char* dataDir
= getenv("YOINK_DATADIR");
79 // first set up the search paths so we can find the icon and other resources
82 // look first in the data directory specified by the environment
83 Mf::Resource::addSearchPath(dataDir
);
86 // then look in the configured data directory
87 Mf::Resource::addSearchPath(YOINK_DATADIR
);
89 return Mf::Resource::getPath("yoink.png");
93 YoinkApp::YoinkApp(int argc
, char* argv
[]) :
94 Mf::Engine(argc
, argv
, configFiles(), PACKAGE_STRING
, iconFile()),
95 music("NightFusionIntro"),
96 punchSound("RobotPunch")
98 Mf::dispatcher::addHandler("video.context_recreated",
99 boost::bind(&YoinkApp::contextRecreated
, this, _1
), this);
102 music
.setLooping(true);
103 music
.enqueue("NightFusionLoop");
106 heroine
= Character::alloc("RobotTrooper");
107 heroine
->getAnimation().startSequence("Run");
109 Mf::Scalar a
[6] = {0.0, 1.5, -0.5, 3.0, -2.0, 1.0};
110 interp
.init(a
, 2.0, Mf::Interpolator::OSCILLATE
);
112 Mf::Scalar b
[2] = {1.0, 0.0};
115 octree
= Mf::loadScene("Classic");
116 heroine
->treeNode
= octree
->insert(heroine
);
119 YoinkApp::~YoinkApp()
121 Mf::dispatcher::removeHandler(this);
125 void YoinkApp::setupGL()
127 glEnable(GL_TEXTURE_2D
);
129 //glEnable(GL_CULL_FACE);
130 glEnable(GL_DEPTH_TEST
);
132 glShadeModel(GL_SMOOTH
);
133 //glEnable(GL_POLYGON_SMOOTH);
136 //glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
137 //std::cout << "texture size: " << texSize << std::endl;
139 //glEnable(GL_BLEND);
140 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
141 glEnable(GL_ALPHA_TEST
);
142 glAlphaFunc(GL_GREATER
, 0.0);
144 glClearColor(1.0, 0.0, 0.0, 1.0);
146 //glMatrixMode(GL_PROJECTION);
148 //gluPerspective(60.0, 1.33333, 1.0, 2500.0);
149 camera
.setProjection(cml::rad(60.0), 1.33333, 32.0, 2500.0);
150 camera
.uploadProjectionToGL();
152 //glMatrixMode(GL_MODELVIEW);
154 //glLineWidth(10.0f);
157 void YoinkApp::contextRecreated(const Mf::Notification
* note
)
159 // Whenever the context is destroyed and a new one created, it probably
160 // won't contain our state so we need to set that up again.
165 void YoinkApp::update(Mf::Scalar t
, Mf::Scalar dt
)
170 camera
.update(t
, dt
);
171 heroine
->update(t
, dt
);
174 heroine
->treeNode
= octree
->reinsert(heroine
, heroine
->treeNode
);
175 octree
->print(heroine
->treeNode
);
177 //camera.lookAt(heroine->getSphere().point);
178 camera
.setPosition(Mf::Vector3(-heroine
->current
.position
[0],
179 -heroine
->current
.position
[1], -256));
181 Mf::Vector3 heroinePosition
;
182 Mf::promoteVector(heroinePosition
, heroine
->current
.position
);
183 Mf::Sound::setListenerPosition(heroinePosition
);
186 hud
.setBar1Progress(interp
.getState(dt
));
187 hud
.setBar2Progress(1.0 - interp
.getState(dt
));
191 void YoinkApp::draw(Mf::Scalar alpha
)
193 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
195 glMatrixMode(GL_MODELVIEW
);
196 glLoadMatrix(camera
.getModelviewMatrix().data());
199 Mf::Texture::resetBind();
201 glEnableClientState(GL_VERTEX_ARRAY
);
202 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
204 octree
->drawIfVisible(alpha
, camera
.getFrustum());
206 //heroine->draw(alpha);
207 heroine
->getAabb().draw();
213 glMatrixMode(GL_PROJECTION
);
216 glMatrixMode(GL_MODELVIEW
);
219 glColor4f(0.0f
, 0.0f
, 0.0f
, fadeIn
.getState(alpha
));
220 Mf::Texture::resetBind();
222 //glRectf(-1.0f, -1.0f, 1.0f, 1.0f);
224 glVertex3f(-1.0, -1.0, -0.1);
225 glVertex3f(1.0, -1.0, -0.1);
226 glVertex3f(1.0, 1.0, -0.1);
227 glVertex3f(-1.0, 1.0, -0.1);
232 glMatrixMode(GL_PROJECTION
);
234 glMatrixMode(GL_MODELVIEW
);
238 void YoinkApp::handleEvent(const Mf::Event
& event
)
243 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
248 else if (event
.key
.keysym
.sym
== SDLK_f
)
250 getVideo().toggleFull();
253 else if (event
.key
.keysym
.sym
== SDLK_SPACE
)
255 heroine
->getAnimation().startSequence("Punch");
259 else if (event
.key
.keysym
.sym
== SDLK_t
)
261 Mf::dispatcher::dispatch("video.context_recreated");
264 else if (event
.key
.keysym
.sym
== SDLK_p
)
269 else if (event
.key
.keysym
.sym
== SDLK_l
)
271 getVideo().toggleCursorGrab();
272 getVideo().toggleCursorVisible();
277 heroine
->handleEvent(event
);
279 case SDL_MOUSEMOTION
:
280 case SDL_MOUSEBUTTONDOWN
:
281 camera
.handleEvent(event
);
288 case SDL_VIDEORESIZE
:
289 glViewport(0, 0, event
.resize
.w
, event
.resize
.h
);
290 hud
.resize(event
.resize
.w
, event
.resize
.h
);
291 camera
.setProjection(cml::rad(60.0),
292 double(event
.resize
.w
) / double(event
.resize
.h
), 32.0, 2500.0);
293 camera
.uploadProjectionToGL();
301 std::cout
<< "Usage: "PACKAGE
" [-h|--help] [OPTION=VALUE]..." << std::endl
302 << "The alien-smashing action game." << std::endl
304 << "Options:" << std::endl
305 << " -h, --help" << std::endl
306 << " show this help and exit" << std::endl
307 << " detail=1|2|3" << std::endl
308 << " the level of detail of game scenes" << std::endl
309 << " fullscreen=true|false" << std::endl
310 << " if true, uses the entire display" << std::endl
311 << " maxfps=num" << std::endl
312 << " the maximum number of frames per second" << std::endl
314 << "See documentation for more options." << std::endl
;
317 int main(int argc
, char* argv
[])
320 (strcmp(argv
[1], "-h") == 0 || strcmp(argv
[1], "--help") == 0))
326 std::cout
<< std::endl
<< PACKAGE_STRING
<< std::endl
327 << "Compiled " << __TIME__
" " __DATE__
<< std::endl
328 << "Send patches and bug reports to <"
329 PACKAGE_BUGREPORT
<< ">." << std::endl
<< std::endl
;
331 #if YOINK_LOGLEVEL >= 4
332 Mf::setLogLevel(Mf::LOG_DEBUG
);
333 #elif YOINK_LOGLEVEL >= 3
334 Mf::setLogLevel(Mf::LOG_INFO
);
335 #elif YOINK_LOGLEVEL >= 2
336 Mf::setLogLevel(Mf::LOG_SCRIPT
);
337 #elif YOINK_LOGLEVEL >= 1
338 Mf::setLogLevel(Mf::LOG_ERROR
);
340 Mf::setLogLevel(Mf::LOG_NONE
);
347 YoinkApp
app(argc
, argv
);
350 catch (Mf::Exception e
)
352 Mf::logError("unhandled exception: <<%s>>", e
.what());
353 Mf::logInfo("it's time to crash now :-(");
358 std::cout
<< std::endl
<< "Goodbye..." << std::endl
<< std::endl
;
363 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.045194 seconds and 5 git commands to generate.