21
|
1
|
|
2 // written in the D programming language
|
|
3
|
16
|
4 /++
|
|
5 + Authors: Stephan Dilly, www.extrawurst.org
|
|
6 +/
|
|
7
|
|
8 module gameApp;
|
|
9
|
|
10 import framework;
|
|
11
|
|
12 import drawSpace;
|
|
13
|
|
14 import chipmunkd.chipmunk;
|
|
15
|
|
16 import samples.LogoSmash;
|
|
17 import samples.Simple;
|
|
18 import samples.PyramidStack;
|
|
19 import samples.ChipmunkDemo;
|
|
20 import samples.Plink;
|
|
21 import samples.Tumble;
|
|
22 import samples.PyramidTopple;
|
|
23 import samples.Planet;
|
|
24 import samples.Query;
|
|
25 import samples.OneWay;
|
|
26 import samples.Sensors;
|
|
27 import samples.Bounce;
|
|
28 import samples.Springies;
|
|
29 import samples.Joints;
|
|
30 import samples.MagnetsElectric;
|
|
31 import samples.Player;
|
|
32 import samples.Tank;
|
|
33 import samples.Pump;
|
|
34 import samples.TheoJansen;
|
|
35 import samples.UnsafeOps;
|
26
|
36 import samples.Nukeit;
|
16
|
37
|
21
|
38 // using derelict bindings for sdl/opengl
|
16
|
39 import derelict.opengl.gl;
|
|
40 import derelict.opengl.glu;
|
|
41 import derelict.sdl.sdl;
|
|
42
|
|
43 import std.stdio;
|
21
|
44 import core.thread;
|
16
|
45
|
26
|
46 version = TIME_TRIAL;
|
16
|
47
|
|
48 cpVect mousePos;
|
|
49 cpVect arrowDirection;
|
|
50
|
21
|
51 bool key_up = false;
|
|
52 bool key_down = false;
|
|
53 bool key_left = false;
|
|
54 bool key_right = false;
|
|
55 bool key_space = false;
|
|
56
|
16
|
57 ///
|
|
58 final class GameApp {
|
|
59
|
|
60 private:
|
|
61
|
|
62 chipmunkDemo*[] demos;
|
|
63 chipmunkDemo* currentDemo;
|
|
64 cpSpace* space;
|
|
65 int ticks;
|
|
66 cpBody* mouseBody;
|
|
67 cpConstraint* mouseJoint;
|
|
68 cpVect mousePos_last;
|
|
69
|
|
70 bool m_running = true;
|
|
71
|
|
72 drawSpaceOptions options = {
|
|
73 0, // drawHash
|
|
74 0, // drawBB
|
|
75 1, // drawShapes
|
|
76 4.0f, // collisionPointSize
|
|
77 0.0f, // bodyPointSize
|
|
78 1.5f, // lineThickness
|
|
79 };
|
|
80
|
|
81 enum width = 1024;
|
|
82 enum height = 780;
|
|
83
|
|
84 version(TIME_TRIAL)
|
|
85 {
|
21
|
86 ulong time_trial(int index, int count)
|
16
|
87 {
|
|
88 currentDemo = demos[index];
|
|
89 space = currentDemo.initFunc();
|
|
90
|
21
|
91 auto start = .tickCount();
|
16
|
92
|
|
93 foreach(i; 0..count)
|
|
94 currentDemo.updateFunc(i);
|
|
95
|
21
|
96 auto end = .tickCount();
|
16
|
97 auto duration = (end - start);
|
|
98
|
|
99 currentDemo.destroyFunc();
|
|
100 currentDemo = null;
|
|
101
|
|
102 writefln("Time(%s) = %s", cast(char)(index + 'a'), duration);
|
21
|
103
|
|
104 return duration;
|
16
|
105 }
|
|
106 }
|
|
107
|
|
108 ///
|
|
109 public void boot() {
|
|
110
|
|
111 demos = [
|
|
112 &LogoSmash,
|
26
|
113 //&Simple,
|
16
|
114 &PyramidStack,
|
|
115 &Plink,
|
|
116 &Tumble,
|
|
117 &PyramidTopple,
|
|
118 &Bounce,
|
|
119 &Planet,
|
|
120 &Springies,
|
|
121 &Pump,
|
|
122 &TheoJansen,
|
|
123 &MagnetsElectric,
|
|
124 &UnsafeOps,
|
|
125 &Query,
|
|
126 &OneWay,
|
|
127 &Player,
|
|
128 &Sensors,
|
|
129 &Joints,
|
|
130 &Tank,
|
|
131 ];
|
|
132
|
|
133 cpInitChipmunk();
|
|
134
|
|
135 cp_collision_slop = 0.2f;
|
|
136
|
|
137 version(TIME_TRIAL)
|
|
138 {
|
26
|
139 Thread.sleep(2_000_000);
|
21
|
140 ulong duration;
|
16
|
141 foreach(i; 0..demos.length)
|
|
142 {
|
21
|
143 if(i == 'l'-'a') continue;
|
|
144 duration+=time_trial(i, 1000);
|
16
|
145 }
|
|
146
|
21
|
147 writefln("Time accum = %s", duration);
|
|
148
|
16
|
149 m_running = false;
|
|
150
|
|
151 return;
|
|
152 }//TIME_TRIAL
|
|
153
|
|
154 //setup framework
|
21
|
155 framework.startup("chipmunk'd by Stephan Dilly",width,height,false);
|
16
|
156
|
|
157 reshape(width,height);
|
|
158
|
|
159 glEnableClientState(GL_VERTEX_ARRAY);
|
|
160
|
21
|
161 runDemo(&LogoSmash);
|
16
|
162
|
|
163 mouseBody = cpBodyNew(INFINITY, INFINITY);
|
|
164 }
|
|
165
|
|
166 ///
|
|
167 void runDemo(chipmunkDemo *demo)
|
|
168 {
|
|
169 if(currentDemo)
|
|
170 currentDemo.destroyFunc();
|
|
171
|
|
172 currentDemo = demo;
|
|
173 ticks = 0;
|
|
174 mouseJoint = null;
|
|
175
|
|
176 //maxArbiters = 0;
|
|
177 //maxPoints = 0;
|
|
178 //maxConstraints = 0;
|
|
179 space = currentDemo.initFunc();
|
|
180 }
|
|
181
|
|
182 ///
|
|
183 void reshape(int width, int height)
|
|
184 {
|
|
185 glViewport(0, 0, width, height);
|
|
186
|
|
187 double rx = width / 2.0;
|
|
188 double ry = height / 2.0;
|
|
189
|
|
190 glMatrixMode(GL_PROJECTION);
|
|
191 glLoadIdentity();
|
|
192 glOrtho(-rx, rx, -ry, ry, -1.0, 1.0);
|
|
193 glTranslated(0.5, 0.5, 0.0);
|
|
194 }
|
|
195
|
|
196 ///
|
|
197 public bool update() {
|
|
198
|
|
199 if(!m_running) return m_running;
|
|
200
|
|
201 if(!framework.processEvents(&keyEvent,&mouseMove,&mouseButtonEvent))
|
|
202 return false;
|
|
203
|
|
204 cpVect newPoint = cpvlerp(mousePos_last, mousePos, 0.25f);
|
|
205 mouseBody.p = newPoint;
|
|
206 mouseBody.v = cpvmult(cpvsub(newPoint, mousePos_last), 60.0f);
|
|
207 mousePos_last = newPoint;
|
|
208
|
|
209 currentDemo.updateFunc(ticks++);
|
|
210
|
|
211 // render
|
|
212
|
|
213 glClearColor(1,1,1,1);
|
|
214
|
|
215 glClear(GL_COLOR_BUFFER_BIT);
|
|
216
|
|
217 DrawSpace(space,currentDemo.drawOptions ? currentDemo.drawOptions : &options);
|
|
218
|
|
219 SDL_GL_SwapBuffers();
|
|
220
|
|
221 return m_running;
|
|
222 }
|
|
223
|
|
224 ///
|
|
225 public void shutdown() {
|
|
226
|
|
227 version(TIME_TRIAL){}else
|
|
228 {
|
|
229 currentDemo.destroyFunc();
|
|
230
|
|
231 framework.shutdown();
|
|
232 }
|
|
233 }
|
|
234
|
|
235 cpVect mouseToSpace(int x, int y)
|
|
236 {
|
|
237 GLdouble model[16];
|
|
238 glGetDoublev(GL_MODELVIEW_MATRIX, model.ptr);
|
|
239
|
|
240 GLdouble proj[16];
|
|
241 glGetDoublev(GL_PROJECTION_MATRIX, proj.ptr);
|
|
242
|
|
243 GLint view[4];
|
|
244 glGetIntegerv(GL_VIEWPORT, view.ptr);
|
|
245
|
|
246 GLdouble mx, my, mz;
|
|
247 gluUnProject(x, height - y, 0.0f, model.ptr, proj.ptr, view.ptr, &mx, &my, &mz);
|
|
248
|
|
249 return cpv(mx, my);
|
|
250 }
|
|
251
|
|
252 ///
|
|
253 private void mouseMove(int x,int y)
|
|
254 {
|
|
255 mousePos = mouseToSpace(x,y);
|
|
256 }
|
|
257
|
|
258 ///
|
|
259 private void mouseButtonEvent(int x,int y,bool _down)
|
|
260 {
|
|
261 if(_down){
|
|
262 cpVect point = mouseToSpace(x,y);
|
|
263
|
|
264 cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, CP_NO_GROUP);
|
|
265 if(shape){
|
|
266 cpBody *_body = shape._body;
|
|
267 mouseJoint = cpPivotJointNew2(mouseBody, _body, cpvzero, cpBodyWorld2Local(_body, point));
|
|
268 mouseJoint.maxForce = 50000.0f;
|
|
269 mouseJoint.biasCoef = 0.15f;
|
|
270 cpSpaceAddConstraint(space, mouseJoint);
|
|
271 }
|
|
272 } else if(mouseJoint){
|
|
273 cpSpaceRemoveConstraint(space, mouseJoint);
|
|
274 cpConstraintFree(mouseJoint);
|
|
275 mouseJoint = null;
|
|
276 }
|
|
277 }
|
|
278
|
|
279 ///
|
|
280 private void set_arrowDirection()
|
|
281 {
|
|
282 int x = 0, y = 0;
|
|
283
|
|
284 if(key_up) y += 1;
|
|
285 if(key_down) y -= 1;
|
|
286 if(key_right) x += 1;
|
|
287 if(key_left) x -= 1;
|
|
288
|
|
289 arrowDirection = cpv(x, y);
|
|
290 }
|
|
291
|
|
292 ///
|
|
293 private void keyEvent(int _key,bool _down)
|
|
294 {
|
|
295 int key = _key;
|
|
296
|
|
297 if(_down)
|
|
298 {
|
|
299 int index = key - 'a';
|
|
300
|
|
301 if(0 <= index && index < demos.length){
|
|
302 runDemo(demos[index]);
|
|
303 } else if(key == '\r'){
|
|
304 runDemo(currentDemo);
|
|
305 } else if(key == 47){
|
|
306 options.drawHash = !options.drawHash;
|
|
307 } else if(key == 92){
|
|
308 options.drawBBs = !options.drawBBs;
|
21
|
309 } else if(key == 27){
|
|
310 m_running = false;
|
16
|
311 } else if(key == 93){
|
|
312 glEnable(GL_LINE_SMOOTH);
|
|
313 glEnable(GL_POINT_SMOOTH);
|
|
314 glEnable(GL_BLEND);
|
|
315 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
316 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
|
|
317 glHint(GL_POINT_SMOOTH_HINT, GL_DONT_CARE);
|
|
318 }
|
|
319 else
|
|
320 {
|
|
321 if(key == SDLK_UP) key_up = true;
|
|
322 else if(key == SDLK_DOWN) key_down = true;
|
|
323 else if(key == SDLK_LEFT) key_left = true;
|
|
324 else if(key == SDLK_RIGHT) key_right = true;
|
21
|
325 else if(key == SDLK_SPACE) key_space = true;
|
16
|
326
|
|
327 set_arrowDirection();
|
|
328 }
|
|
329 }
|
|
330 else
|
|
331 {
|
|
332 if(key == SDLK_UP) key_up = false;
|
|
333 else if(key == SDLK_DOWN) key_down = false;
|
|
334 else if(key == SDLK_LEFT) key_left = false;
|
|
335 else if(key == SDLK_RIGHT) key_right = false;
|
21
|
336 else if(key == SDLK_SPACE) key_space = false;
|
16
|
337
|
|
338 set_arrowDirection();
|
|
339 }
|
|
340 }
|
|
341 }
|
|
342
|