]> Dogcows Code - chaz/yoink/blob - src/yoink.cc
initial runloop implementation
[chaz/yoink] / src / yoink.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include "config.h"
13
14 #include <stdexcept>
15
16 #include <moof/log.hh>
17 #include <moof/math.hh>
18 #include <moof/mesh.hh>
19 #include <moof/opengl.hh>
20 //#include <moof/settings.hh>
21 #include <moof/video.hh>
22
23 #include "yoink.hh"
24
25
26 void yoink::load_scene_loader()
27 {
28 state_.script.import_safe_standard_libraries();
29 moof::log::import(state_.script);
30 moof::mesh::import(state_.script, "yoink");
31 moof::sound::import(state_.script, "yoink");
32
33 std::string path = moof::resource::find_file("scenes/loader.lua");
34 if (path.empty())
35 {
36 throw std::runtime_error("cannot find scene loader script");
37 }
38
39 moof::script::status status = state_.script.do_file(path);
40 if (status != moof::script::success)
41 {
42 std::string str;
43 state_.script[-1].get(str);
44 throw std::runtime_error("script error: " + str);
45 }
46
47 state_.script.globals().push_field("scenes");
48 state_.script.top().get(state_.sceneList);
49 if (state_.sceneList.size() == 0)
50 {
51 throw std::runtime_error("no variable `scenes' in script loader");
52 }
53 }
54
55 void yoink::advance_scene()
56 {
57 if (state_.sceneList.size() != 0)
58 {
59 //state_.scene = Scene::alloc(state_.sceneList[0]);
60 //state_.sceneList.erase(state_.sceneList.begin());
61
62 //moof::script::status status = state_.scene->load(settings,
63 //state_.script);
64
65 std::string path = moof::resource::find_file("scenes/"+state_.sceneList[0], "lua");
66 if (path.empty())
67 {
68 throw "the scene file could not be found";
69 }
70
71 //importSceneBindings(settings, script);
72 moof::script::status status = state_.script.do_file(path);
73 if (status != moof::script::success)
74 {
75 std::string str;
76 state_.script[-1].get(str);
77 throw std::runtime_error("script error: " + str);
78 }
79
80 moof::script::slot table = state_.script.globals().push_field("Event");
81 if (table.is_table())
82 {
83 state_.script.push("Think");
84 table.push_field("Think");
85 state_.script.registry().set_field();
86 }
87 state_.script.pop();
88
89 table = state_.script.globals().push_field("Event");
90 if (table.is_table())
91 {
92 table.push_field("Draw");
93 state_.script.registry().set_field("Draw");
94 }
95 state_.script.pop();
96 }
97 }
98
99
100 yoink::yoink()
101 {
102 music_.queue("NightFusionIntro");
103 music_.loop(true);
104 music_.queue("NightFusionLoop");
105 music_.position(moof::vector3(10.0, 5.0, 0.0));
106
107 punch_sound_.sample("Thump");
108
109 state_.heroine = Heroine::alloc();
110 state_.heroine->animation.startSequence("FlyDiagonallyUp");
111
112 state_.interp.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate);
113
114
115 //bool isMute = false;
116 //settings().get("nomusic", isMute);
117 //music_.play();
118
119 load_scene_loader();
120 advance_scene(); // load the first scene
121
122 mHud = Hud::alloc(state_);
123 //add_child(mHud);
124
125 mRay.direction.set(1.0, 0.0);
126
127 mLine.a.set(20, 10);
128 mLine.b.set(19, 14);
129
130 mCircle.point.set(22, 5);
131 mCircle.radius = 2;
132
133 //mRayTimer.init(boost::bind(&yoink::rayTimer, this),
134 //1.0, moof::timer::repeat);
135
136 projection();
137 }
138
139
140 void yoink::update(moof::scalar t, moof::scalar dt)
141 {
142 //if (!state_.scene) return;
143 state_.camera.update(t, dt);
144 state_.heroine->update(t, dt);
145
146 //state_.scene->checkForCollision(*state_.heroine);
147
148 moof::vector3 cam= -moof::promote(state_.heroine->state().position, 8);
149 state_.camera.position(cam);
150
151 mRay.point = state_.heroine->state().position;
152
153 state_.script.registry().push_field("Think");
154 if (state_.script[-1].is_function()) state_.script.call();
155 else state_.script.pop();
156
157 //moof::view::update(t, dt);
158 }
159
160 void yoink::rayTimer()
161 {
162 moof::ray2::contact meh;
163 std::list<moof::ray2::contact> hits;
164 moof::vector2 point;
165
166 bool bam = mLine.intersect_ray(mRay, meh);
167 if (bam)
168 {
169 //meh.normal.normalize();
170 //hits.push_back(meh);
171 mRay.solve(point, meh.distance);
172 moof::log_info << "line: d = " << meh.distance << std::endl;
173 moof::log_info << " P = " << point << std::endl;
174 moof::log_info << " n = " << meh.normal << std::endl;
175 }
176
177 bam = mCircle.intersect_ray(mRay, meh);
178 if (bam)
179 {
180 meh.normal.normalize();
181 hits.push_back(meh);
182 }
183
184 //if (state_.scene->castRay(mRay, hits))
185 //{
186 //hits.front().normal.normalize();
187 //mRay.solve(point, hits.front().distance);
188 //moof::log_info << "scene: d = " << hits.front().distance << std::endl;
189 //moof::log_info << " P = " << point << std::endl;
190 //moof::log_info << " n = " << hits.front().normal << std::endl;
191 //}
192 }
193
194
195 void yoink::draw(moof::scalar alpha) const
196 {
197 //if (!state_.scene) return;
198 state_.camera.upload_to_gl(alpha);
199
200 float pos[] = {state_.heroine->state().position[0],
201 state_.heroine->state().position[1], 0.0f, 1.0f};
202 glLightfv(GL_LIGHT0, GL_POSITION, pos);
203
204 // DRAW THE SCENE
205 moof::image::reset_binding();
206
207 glEnableClientState(GL_VERTEX_ARRAY);
208 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
209
210 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
211
212 size_t meh = 23;
213 state_.script.push(meh);
214 state_.script.pop();
215
216 state_.script.registry().push_field("Draw");
217 if (state_.script[-1].is_function())
218 {
219 moof::script::status result = state_.script.call();
220 if (result != moof::script::success)
221 {
222 std::string str;
223 state_.script[-1].get(str);
224 throw std::runtime_error("script error: " + str);
225 }
226 }
227 else state_.script.pop();
228
229 state_.heroine->draw(alpha);
230
231 //mRay.draw();
232 mLine.draw();
233 mCircle.draw();
234
235 //moof::view::draw(alpha);
236 }
237
238 bool yoink::handle_event(const moof::event& event)
239 {
240 //if (moof::view::handle_event(event)) return true;
241
242 static moof::scalar scale = SCALAR(1.0);
243
244 switch (event.type)
245 {
246 case SDL_KEYDOWN:
247 if (event.key.keysym.sym == SDLK_SPACE)
248 {
249 state_.heroine->animation.startSequence("Flattened");
250 moof::log_info("thump!");
251 punch_sound_.play();
252 return true;
253 }
254 else if (event.key.keysym.sym == SDLK_m)
255 {
256 music_.toggle();
257 return true;
258 }
259 else if (event.key.keysym.sym == SDLK_PAGEUP)
260 {
261 //mRay.direction = moof::rotate_vector_2D(mRay.direction,
262 //moof::rad(10.0));
263 scale += SCALAR(0.1);
264 moof::timer::default_source().scale(scale);
265 return true;
266 }
267 else if (event.key.keysym.sym == SDLK_PAGEDOWN)
268 {
269 //mRay.direction = moof::rotate_vector_2D(mRay.direction,
270 //moof::rad(-10.0));
271 scale -= SCALAR(0.1);
272 moof::timer::default_source().scale(scale);
273 return true;
274 }
275 else if (event.key.keysym.sym == SDLK_r)
276 {
277 load_scene_loader();
278 advance_scene();
279 return true;
280 }
281 return state_.heroine->handle_event(event);
282
283 case SDL_KEYUP:
284 if (event.key.keysym.sym == SDLK_ESCAPE)
285 {
286 //parent().remove_child(this);
287 return false;
288 }
289 else if (event.key.keysym.sym == SDLK_h)
290 {
291 //add_child(mHud);
292 return true;
293 }
294 return state_.heroine->handle_event(event);
295
296 case SDL_MOUSEMOTION:
297 case SDL_MOUSEBUTTONDOWN:
298 state_.camera.handle_event(event);
299 return true;
300
301 case SDL_VIDEORESIZE:
302 projection(event.resize.w, event.resize.h);
303 break;
304 }
305
306 return false;
307 }
308
309
310 void yoink::projection()
311 {
312 moof::video* video = moof::video::current();
313 projection(video->width(), video->height());
314 }
315
316 void yoink::projection(moof::scalar width, moof::scalar height)
317 {
318 state_.camera.projection(moof::rad(60.0),
319 width / height,
320 SCALAR(1.0), SCALAR(200.0));
321 }
322
This page took 0.043314 seconds and 4 git commands to generate.