1 // morse code - gluck keyboard event demo
2 // june 2004 Philip Davidson ( philipd@alumni.princeton.edu )
9 "gluck.ckx" => (:gluck:);
13 Shakers shake1 => JCRev r => Echo a => Echo b => Echo c => dac;
36 0.02 => float puckrad;
50 0.1 => float padspace;
51 -boardw + padspace => float pad1x;
52 boardw - padspace => float pad2x;
76 function void keycode ( uint k ) {
78 if ( k == gluck.KEY_q ) {
82 if ( k == gluck.KEY_t ) {
83 1 - oldskool => oldskool;
85 -boardw + padspace => pad1x;
86 boardw - padspace => pad2x;
90 if ( k == gluck.KEY_w ) {
91 math.max ( 0.2, math.min ( 1.6 , pad1vy + 0.2) ) => pad1vy;
93 if ( k == gluck.KEY_s ) {
96 if ( k == gluck.KEY_x ) {
97 math.min ( -0.2 , math.max (-1.6 , pad1vy - 0.2) ) => pad1vy;
100 if ( k == gluck.KEY_i ) {
101 math.max ( 0.2, math.min ( 1.6 , pad2vy + 0.2) ) => pad2vy;
103 if ( k == gluck.KEY_j ) {
106 if ( k == gluck.KEY_n ) {
107 math.min ( -0.2, math.max (-1.6 , pad2vy - 0.2) ) => pad2vy;
109 if ( k == gluck.KEY_SPACE ) {
110 std.rand2f( 0.0, 100.0 ) => float deg;
111 std.rand2f( 1.0, 2.0 ) => float mag;
112 mag * math.cos(deg) => puckvx;
113 mag * math.sin(deg) => puckvy;
117 function void motion ( int x, int y ) {
123 function void drag ( int x, int y ) {
126 if ( x > gluck.GetViewDims(0) / 2 ) {
127 -0.6 * (float)dy => pad2vy;
128 0.6 * (float)dx => pad2vx;
131 -0.6 * (float)dy => pad1vy;
132 0.6 * (float)dx => pad1vx;
137 function void theeventpoll() {
138 while ( gluck.HasEvents() && running == 1 ) {
140 gluck.GetNextEvent() => int curid;
141 gluck.GetEventType(curid) => uint curtype;
143 if ( curtype == gluck.EVENT_KEY ) { //keyboard
144 gluck.GetEventKey(curid) => uint k;
147 if ( curtype == gluck.EVENT_PMOTION ) { //keyboard
148 gluck.GetEventX(curid) => int x;
149 gluck.GetEventY(curid) => int y;
152 if ( curtype == gluck.EVENT_MOTION ) { //keyboard
153 gluck.GetEventX(curid) => int x;
154 gluck.GetEventY(curid) => int y;
161 function void drawWindow() {
163 gl.ClearColor( 0.4, 0.4, 0.4 , 0.0);
164 gl.Clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT );
170 gl.Color4f ( 0.5, 1.0, 0.5, 0.5 ) ;
172 gl.Translatef( -0.1 , -boardh, 0.0 );
173 gl.Scalef( 0.1, 0.1, 1.0 );
174 for ( 0 => ix; ix < p1score ; ix + 1 => ix ) {
176 gl.Vertex2f( 0.0, 0.0 );
177 gl.Vertex2f( 0.0, 0.9 );
178 gl.Vertex2f( 0.9, 0.9 );
179 gl.Vertex2f( 0.9, 0.0 );
181 gl.Translatef ( 0.0, 1.0 , 0.0 );
186 gl.Translatef( 0.01 , -boardh, 0.0 );
187 gl.Scalef( 0.1, 0.1, 1.0 );
189 for ( 0 => ix; ix < p2score ; ix + 1 => ix ) {
191 gl.Vertex2f( 0.0, 0.0 );
192 gl.Vertex2f( 0.0, 0.9 );
193 gl.Vertex2f( 0.9, 0.9 );
194 gl.Vertex2f( 0.9, 0.0 );
196 gl.Translatef ( 0.0, 1.0 , 0.0 );
201 gl.Color4f ( 0.0, 1.0, 0.0, 0.5 ) ;
203 gl.Begin(gl.LINE_LOOP);
204 gl.Vertex2f(-boardw, -boardh);
205 gl.Vertex2f( boardw, -boardh);
206 gl.Vertex2f( boardw, boardh);
207 gl.Vertex2f(-boardw, boardh);
211 gl.Translatef ( pad1x, pad1y, 0.0 );
213 gl.Vertex3f ( -padw, -padh, 0.0 );
214 gl.Vertex3f ( padw, -padh, 0.0 );
215 gl.Vertex3f ( padw, padh, 0.0 );
216 gl.Vertex3f ( -padw, padh, 0.0 );
221 gl.Translatef ( pad2x , pad2y, 0.0 );
223 gl.Vertex3f ( -padw, -padh, 0.0 );
224 gl.Vertex3f ( padw, -padh, 0.0 );
225 gl.Vertex3f ( padw, padh, 0.0 );
226 gl.Vertex3f ( -padw, padh, 0.0 );
231 gl.Translatef ( puckx , pucky , 0.1);
233 gl.Vertex2f ( -puckrad, -puckrad );
234 gl.Vertex2f ( puckrad, -puckrad );
235 gl.Vertex2f ( puckrad, puckrad );
236 gl.Vertex2f ( -puckrad, puckrad );
241 gl.Vertex2f(0.0, boardh );
242 gl.Vertex2f(0.0, -boardh );
251 function void sndtrigger ( int which ) {
254 1.5 => shake1.noteOn;
256 else if ( which == 2 ) {
257 1.5 => shake2.noteOn;
259 else if ( which == 3 ) {
261 0.9 + math.max ( 0.0, math.min ( 1.0 , std.abs( puckvy * 0.3 ))) => t.noteOn;
264 else if ( which == 4 ) {
266 0.7 + math.max ( 0.0, math.min ( 1.0 , std.abs( puckvy * 0.3 ))) => t.noteOn;
272 function void score ( int which ) {
274 p1score + 1 => p1score;
275 p1score => shake1.which;
278 else if ( which == 2 ) {
279 p2score + 1 => p2score;
280 p2score => shake2.which;
288 function void testcollisions() {
291 if ( pad1y + padh > boardh ) {
292 pad1vy * -0.1 => pad1vy;
293 boardh + ( boardh - ( pad1y + padh ) ) - padh => pad1y;
295 if ( pad1y - padh < -boardh ) {
296 pad1vy * -0.1 => pad1vy;
297 -boardh + ( -boardh - (pad1y-padh) ) + padh => pad1y;
300 math.max ( -boardw , math.min ( -boardw * 0.5 , pad1x ) ) => pad1x;
301 math.max ( boardw * 0.5 , math.min ( boardw , pad2x ) ) => pad2x;
304 if ( pad2y + padh > boardh ) {
305 pad2vy * -0.1 => pad2vy;
306 boardh + ( boardh - ( pad2y + padh ) ) - padh => pad2y;
308 if ( pad2y - padh < -boardh ) {
309 pad2vy * -0.1 => pad2vy;
310 -boardh + ( -boardh - (pad2y-padh) ) + padh => pad2y;
316 if ( pucky < -boardh ) {
318 puckvy * -1.0 => puckvy;
320 if ( boardh < pucky) {
322 puckvy * -1.0 => puckvy;
328 if ( puckx < -boardw ) {
331 else if ( puckx > boardw ) {
336 if ( puckx - puckrad < p1f && pucky < pad1y + padh && pucky > pad1y - padh ) {
337 if ( puckx - puckrad + puckvx * -dt * 5.0 > p1f + pad1vx * -dt * 5.0 ) {
338 p1f + ( p1f - ( puckx - puckrad ) ) + puckrad => puckx;
339 puckvy + 0.3 * ( pad1vy - puckvy ) => puckvy; //friction transfer...
340 if ( oldskool ) { puckvx * -1.0 => puckvx; }
342 pad1vx + -0.3 * ( puckvx - pad1vx ) => puckvx; //ricochet
347 else if ( puckx + puckrad > p2f && pucky < pad2y + padh && pucky > pad2y - padh ) {
348 if ( puckx + puckrad + puckvx * -dt * 5.0 < p2f + pad2vx * -dt * 5.0 ) {
349 p2f + ( p2f - ( puckx + puckrad ) ) - puckrad => puckx;
350 puckvy + 0.3 * ( pad2vy - puckvy ) => puckvy; //friction transfer...
351 if ( oldskool ) { puckvx * -1.0 => puckvx; }
353 pad2vx + -0.3 * ( puckvx - pad2vx ) => puckvx; //ricochet
361 function void simulshred() {
363 while ( running == 1 ) {
368 //drag on the paddles.
369 std.sgn( pad1vy ) * math.max( 0.0, std.abs(pad1vy) - 0.2 * dt ) => pad1vy;
370 std.sgn( pad2vy ) * math.max( 0.0, std.abs(pad2vy) - 0.2 * dt ) => pad2vy;
371 std.sgn( pad1vx ) * math.max( 0.0, std.abs(pad1vx) - 0.2 * dt ) => pad1vx;
372 std.sgn( pad2vx ) * math.max( 0.0, std.abs(pad2vx) - 0.2 * dt ) => pad2vx;
378 if ( !oldskool ) puckay + grav => puckay;
380 pad1y + pad1vy * dt => pad1y;
381 pad2y + pad2vy * dt => pad2y;
384 pad1x + pad1vx * dt => pad1x;
385 pad2x + pad2vx * dt => pad2x;
388 puckvx + puckax * dt => puckvx;
389 puckvy + puckay * dt => puckvy;
391 puckx + puckvx * dt => puckx;
392 pucky + puckvy * dt => pucky;
399 function void eventshred() {
401 //handle events that were buffered by glut's calls.
402 //once we have events, this will change.
404 while ( running == 1 ) {
411 function void gluckinitcall() {
414 gluck.Init(); //MUST be called before any other gluck function
416 gluck.InitBasicWindow("ponnng?"); //request a window...
418 gluck.InitCallbacks(0, 1, 1); //register callbacks for the window
419 //arguments monitor ( mouse, motion, keyboard );
421 //set up a simple view matrix
423 gl.MatrixMode(gl.PROJECTION);
425 gl.Ortho(-1.0, 1.0, -1.0 , 1.0, -4.0 , 4.0 );
427 gl.MatrixMode(gl.MODELVIEW);
431 gl.ClearColor ( 0.0 , 0.0, 0.3, 0.0 );
434 function void eventloop( ) {
436 gluck.MainLoopEvent(); // let glut do its bizness
437 if ( gluck.NeedDraw() && running == 1 ) { drawWindow(); }
438 gluck.PostRedisplay(); // request that we draw again;
442 function void gluckshred() {
445 spork ~ eventshred();
446 while ( running == 1 ) {
448 33::ms => now; //30 fps
452 spork ~ gluckshred();
453 spork ~ simulshred();
455 while ( running == 1 ) {