]> Dogcows Code - chaz/yoink/blob - src/MainLayer.cc
fixed layer bugs; generalized octree
[chaz/yoink] / src / MainLayer.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> // atexit, getenv
30 #include <cstring>
31 #include <iostream>
32 #include <string>
33
34 #include <Moof/Dispatcher.hh>
35 #include <Moof/Exception.hh>
36 #include <Moof/Log.hh>
37 #include <Moof/OpenGL.hh>
38 #include <Moof/Resource.hh>
39 #include <Moof/Transition.hh>
40 #include <Moof/Video.hh>
41
42 #include "GameLayer.hh"
43 #include "MainLayer.hh"
44 #include "TitleLayer.hh"
45
46 #if HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49
50
51 MainLayer::MainLayer()
52 {
53 Mf::dispatcher::addHandler("video.context_recreated",
54 boost::bind(&MainLayer::contextRecreated, this, _1), this);
55 setupGL();
56 }
57
58 MainLayer::~MainLayer()
59 {
60 Mf::dispatcher::removeHandler(this);
61 }
62
63
64 void MainLayer::pushed(Mf::Engine& e)
65 {
66 engine = &e;
67
68 //Mf::Scalar coeff[] = {0.0, 1.0};
69 //Mf::Lerp interp(coeff, 0.25);
70
71 //Mf::LayerP gameLayer = GameLayer::alloc();
72 //Mf::Transition<Mf::Lerp>::Ptr transition =
73 //Mf::Transition<Mf::Lerp>::alloc(gameLayer, Mf::LayerP(), interp);
74 //engine->push(transition);
75 //engine->push(GameLayer::alloc());
76 engine->push(TitleLayer::alloc());
77 }
78
79
80 void MainLayer::draw(Mf::Scalar alpha) const
81 {
82 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
83
84 glMatrixMode(GL_PROJECTION);
85 glLoadIdentity();
86
87 glMatrixMode(GL_MODELVIEW);
88 glLoadIdentity();
89 }
90
91 bool MainLayer::handleEvent(const Mf::Event& event)
92 {
93 switch (event.type)
94 {
95 case SDL_KEYDOWN:
96 if (event.key.keysym.sym == SDLK_ESCAPE)
97 {
98 quit();
99 }
100 else if (event.key.keysym.sym == SDLK_f)
101 {
102 engine->getVideo().toggleFull();
103 }
104 else if (event.key.keysym.sym == SDLK_l)
105 {
106 Mf::Video& video = engine->getVideo();
107 video.toggleCursorGrab();
108 video.toggleCursorVisible();
109 }
110 else if (event.key.keysym.sym == SDLK_y)
111 {
112 engine->push(GameLayer::alloc());
113 }
114 break;
115
116 case SDL_VIDEORESIZE:
117 glViewport(0, 0, event.resize.w, event.resize.h);
118 break;
119
120 case SDL_QUIT:
121 quit();
122 break;
123 }
124
125 return false;
126 }
127
128 void MainLayer::quit()
129 {
130 #if NDEBUG
131 // we don't really need to unwind the stack and shut everything down because
132 // the operating system will take care of cleaning up
133 exit(0);
134 #else
135 engine->clear();
136 #endif
137 }
138
139
140 void MainLayer::setupGL()
141 {
142 glEnable(GL_TEXTURE_2D);
143 glEnable(GL_DEPTH_TEST);
144
145 glEnable(GL_LINE_SMOOTH);
146 glEnable(GL_POLYGON_SMOOTH);
147 glShadeModel(GL_SMOOTH);
148
149 //glEnable(GL_BLEND);
150 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
151 glEnable(GL_ALPHA_TEST);
152 glAlphaFunc(GL_GREATER, 0.0);
153
154 glClearColor(0.0, 0.0, 0.0, 1.0);
155
156 glMatrixMode(GL_PROJECTION);
157 glLoadIdentity();
158 gluPerspective(60.0, 1.33333, 1.0, 2500.0);
159
160 glMatrixMode(GL_MODELVIEW);
161 }
162
163 void MainLayer::contextRecreated(const Mf::Notification* note)
164 {
165 // whenever the context is destroyed and a new one created, it probably
166 // won't contain our state so we need to set that up again
167 setupGL();
168 }
169
170
171
172 void printUsage()
173 {
174 std::cout << "Usage: "PACKAGE" [-h|--help] [OPTION=VALUE]..." << std::endl
175 << "The alien-smashing action game." << std::endl
176 << std::endl
177 << "Options:" << std::endl
178 << " -h, --help" << std::endl
179 << " show this help and exit" << std::endl
180 << " detail=1|2|3" << std::endl
181 << " the level of detail of game scenes" << std::endl
182 << " fullscreen=true|false" << std::endl
183 << " if true, uses the entire display" << std::endl
184 << " maxfps=num" << std::endl
185 << " the maximum number of frames per second" << std::endl
186 << std::endl
187 << "See documentation for more options." << std::endl;
188 }
189
190 void goodbye()
191 {
192 std::cout << std::endl << "Goodbye..." << std::endl << std::endl;
193 }
194
195 int main(int argc, char* argv[])
196 {
197 if (argc > 1 &&
198 (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0))
199 {
200 printUsage();
201 return 0;
202 }
203
204 std::cout << std::endl << PACKAGE_STRING << std::endl
205 << "Compiled " << __TIME__ " " __DATE__ << std::endl
206 << "Send patches and bug reports to <"
207 PACKAGE_BUGREPORT << ">." << std::endl << std::endl;
208
209 atexit(goodbye);
210
211
212 #if YOINK_LOGLEVEL >= 4
213 Mf::setLogLevel(Mf::LOG_DEBUG);
214 #elif YOINK_LOGLEVEL >= 3
215 Mf::setLogLevel(Mf::LOG_INFO);
216 #elif YOINK_LOGLEVEL >= 2
217 Mf::setLogLevel(Mf::LOG_SCRIPT);
218 #elif YOINK_LOGLEVEL >= 1
219 Mf::setLogLevel(Mf::LOG_ERROR);
220 #elif YOINK_LOGLEVEL
221 Mf::setLogLevel(Mf::LOG_NONE);
222 #endif
223
224
225 // Add search paths; they should be searched in this order:
226 // 1. YOINK_DATADIR (environment)
227 // 2. YOINK_DATADIR (configure)
228
229 char* dataDir = getenv("YOINK_DATADIR");
230 if (dataDir)
231 {
232 Mf::Resource::addSearchPath(dataDir);
233 }
234
235 Mf::Resource::addSearchPath(YOINK_DATADIR);
236
237 std::string iconFile = Mf::Resource::getPath("yoink.png");
238
239
240 // Build the list of config files to search for, in this order:
241 // 1. YOINK_DATADIR/yoinkrc
242 // 2. /etc/yoinkrc
243 // 3. $HOME/.yoinkrc
244 // 4. YOINKRC (environment)
245
246 std::string configFiles;
247
248 configFiles += Mf::Resource::getPath("yoinkrc");
249 configFiles += ":/etc/yoinkrc:$HOME/.yoinkrc";
250
251 char* configFile = getenv("YOINKRC");
252 if (configFile)
253 {
254 configFiles += ":";
255 configFiles += configFile;
256 }
257
258
259 try
260 {
261 Mf::Engine app(argc, argv, PACKAGE_STRING, iconFile, configFiles);
262 app.push(MainLayer::alloc());
263
264 app.run();
265 }
266 catch (Mf::Exception e)
267 {
268 Mf::logError("unhandled exception: <<%s>>", e.what());
269 Mf::logInfo("it's time to crash now :-(");
270 throw e;
271 }
272
273 return 0;
274 }
275
276
277 /** vim: set ts=4 sw=4 tw=80: *************************************************/
278
This page took 0.044703 seconds and 4 git commands to generate.