]>
Dogcows Code - chaz/yoink/blob - src/yoink.cc
76150362ee2d41cf136470e408a4551816927cd0
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
8 *****************************************************************************/
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>
26 void yoink::load_scene_loader()
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");
33 std::string path
= moof::resource::find_file("scenes/loader.lua");
36 throw std::runtime_error("cannot find scene loader script");
39 moof::script::status status
= state_
.script
.do_file(path
);
40 if (status
!= moof::script::success
)
43 state_
.script
[-1].get(str
);
44 throw std::runtime_error("script error: " + str
);
47 state_
.script
.globals().push_field("scenes");
48 state_
.script
.top().get(state_
.sceneList
);
49 if (state_
.sceneList
.size() == 0)
51 throw std::runtime_error("no variable `scenes' in script loader");
55 void yoink::advance_scene()
57 if (state_
.sceneList
.size() != 0)
59 //state_.scene = Scene::alloc(state_.sceneList[0]);
60 //state_.sceneList.erase(state_.sceneList.begin());
62 //moof::script::status status = state_.scene->load(settings,
65 std::string path
= moof::resource::find_file("scenes/"+state_
.sceneList
[0], "lua");
68 throw "the scene file could not be found";
71 //importSceneBindings(settings, script);
72 moof::script::status status
= state_
.script
.do_file(path
);
73 if (status
!= moof::script::success
)
76 state_
.script
[-1].get(str
);
77 throw std::runtime_error("script error: " + str
);
80 moof::script::slot table
= state_
.script
.globals().push_field("Event");
83 state_
.script
.push("Think");
84 table
.push_field("Think");
85 state_
.script
.registry().set_field();
89 table
= state_
.script
.globals().push_field("Event");
92 table
.push_field("Draw");
93 state_
.script
.registry().set_field("Draw");
102 music_
.queue("NightFusionIntro");
104 music_
.queue("NightFusionLoop");
105 music_
.position(moof::vector3(10.0, 5.0, 0.0));
107 punch_sound_
.sample("Thump");
109 state_
.heroine
= Heroine::alloc();
110 state_
.heroine
->animation
.startSequence("FlyDiagonallyUp");
112 state_
.interp
.init(0.0, 1.0, 4.0, moof::lerp_scalar::oscillate
);
115 //bool isMute = false;
116 //settings().get("nomusic", isMute);
120 advance_scene(); // load the first scene
122 mHud
= Hud::alloc(state_
);
125 mRay
.direction
.set(1.0, 0.0);
130 mCircle
.point
.set(22, 5);
133 //mRayTimer.init(boost::bind(&yoink::rayTimer, this),
134 //1.0, moof::timer::repeat);
140 void yoink::update(moof::scalar t
, moof::scalar dt
)
142 //if (!state_.scene) return;
143 state_
.camera
.update(t
, dt
);
144 state_
.heroine
->update(t
, dt
);
146 //state_.scene->checkForCollision(*state_.heroine);
148 moof::vector3 cam
= -moof::promote(state_
.heroine
->state().position
, 8);
149 state_
.camera
.position(cam
);
151 mRay
.point
= state_
.heroine
->state().position
;
153 state_
.script
.registry().push_field("Think");
154 if (state_
.script
[-1].is_function()) state_
.script
.call();
155 else state_
.script
.pop();
157 //moof::view::update(t, dt);
160 void yoink::rayTimer()
162 moof::ray2::contact meh
;
163 std::list
<moof::ray2::contact
> hits
;
166 bool bam
= mLine
.intersect_ray(mRay
, meh
);
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
;
177 bam
= mCircle
.intersect_ray(mRay
, meh
);
180 meh
.normal
.normalize();
184 //if (state_.scene->castRay(mRay, hits))
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;
195 void yoink::draw(moof::scalar alpha
) const
197 //if (!state_.scene) return;
198 state_
.camera
.upload_to_gl(alpha
);
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
);
205 moof::image::reset_binding();
207 glEnableClientState(GL_VERTEX_ARRAY
);
208 glEnableClientState(GL_TEXTURE_COORD_ARRAY
);
210 //state_.scene->draw_if_visible(alpha, state_.camera.frustum());
213 state_
.script
.push(meh
);
216 state_
.script
.registry().push_field("Draw");
217 if (state_
.script
[-1].is_function())
219 moof::script::status result
= state_
.script
.call();
220 if (result
!= moof::script::success
)
223 state_
.script
[-1].get(str
);
224 throw std::runtime_error("script error: " + str
);
227 else state_
.script
.pop();
229 state_
.heroine
->draw(alpha
);
235 //moof::view::draw(alpha);
238 bool yoink::handle_event(const moof::event
& event
)
240 //if (moof::view::handle_event(event)) return true;
242 static moof::scalar scale
= SCALAR(1.0);
247 if (event
.key
.keysym
.sym
== SDLK_SPACE
)
249 state_
.heroine
->animation
.startSequence("Flattened");
250 moof::log_info("thump!");
254 else if (event
.key
.keysym
.sym
== SDLK_m
)
259 else if (event
.key
.keysym
.sym
== SDLK_PAGEUP
)
261 //mRay.direction = moof::rotate_vector_2D(mRay.direction,
263 scale
+= SCALAR(0.1);
264 moof::timer::default_source().scale(scale
);
267 else if (event
.key
.keysym
.sym
== SDLK_PAGEDOWN
)
269 //mRay.direction = moof::rotate_vector_2D(mRay.direction,
271 scale
-= SCALAR(0.1);
272 moof::timer::default_source().scale(scale
);
275 else if (event
.key
.keysym
.sym
== SDLK_r
)
281 return state_
.heroine
->handle_event(event
);
284 if (event
.key
.keysym
.sym
== SDLK_ESCAPE
)
286 //parent().remove_child(this);
289 else if (event
.key
.keysym
.sym
== SDLK_h
)
294 return state_
.heroine
->handle_event(event
);
296 case SDL_MOUSEMOTION
:
297 case SDL_MOUSEBUTTONDOWN
:
298 state_
.camera
.handle_event(event
);
301 case SDL_VIDEORESIZE
:
302 projection(event
.resize
.w
, event
.resize
.h
);
310 void yoink::projection()
312 moof::video
* video
= moof::video::current();
313 projection(video
->width(), video
->height());
316 void yoink::projection(moof::scalar width
, moof::scalar height
)
318 state_
.camera
.projection(moof::rad(60.0),
320 SCALAR(1.0), SCALAR(200.0));
This page took 0.046897 seconds and 4 git commands to generate.