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