Debian Codegraph @

dokidoki-support/luaglfw.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 //========================================================================
2 // GLFW - An OpenGL framework
3 // File: luaglfw.c
4 // Platform: Lua 5.0
5 // API version: 2.5
6 // WWW: http://glfw.sourceforge.net
7 //------------------------------------------------------------------------
8 // Copyright (c) 2002-2005 Camilla Berglund
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would
21 // be appreciated but is not required.
22 //
23 // 2. Altered source versions must be plainly marked as such, and must not
24 // be misrepresented as being the original software.
25 //
26 // 3. This notice may not be removed or altered from any source
27 // distribution.
28 //
29 //========================================================================
30
31 #include <lauxlib.h>
32 #include <GL/glfw.h>
33 #include "luaglfw.h"
34
35
36 //************************************************************************
37 //**** Internal type definitions ****
38 //************************************************************************
39
40 struct lua_constant
41 {
42 char *str;
43 int value;
44 };
45
46
47 //************************************************************************
48 //**** Handy functions ****
49 //************************************************************************
50
51 // Check the number of arguments on the Lua stack
52 static int badArgs( lua_State *L, int n, char const *name )
53 {
54 if( lua_gettop(L) != n )
55 {
56 lua_settop( L, 0 );
57 lua_pushstring( L, "Bad arguments passed to function: " );
58 lua_pushstring( L, name );
59 lua_concat( L, 2 );
60 lua_error( L );
61 return 1;
62 }
63 return 0;
64 }
65
66 // Get a numeric value from a Lua table
67 static void getNumber( lua_State *L, int index, char *key, lua_Number *num )
68 {
69 lua_pushstring( L, key );
70 if( index < 0 ) index --;
71 lua_rawget( L, index );
72 if( !lua_isnil( L, -1 ) )
73 {
74 *num = lua_tonumber( L, -1 );
75 }
76 lua_remove( L, -1 );
77 }
78
79 // Add constants to the table on top of the stack
80 static void addConstants( lua_State *L, struct lua_constant *cn )
81 {
82 while( cn->str )
83 {
84 lua_pushstring( L, cn->str );
85 lua_pushnumber( L, cn->value );
86 lua_rawset( L, -3 );
87 ++ cn;
88 }
89 }
90
91 // Report error for unsupported functions
92 static void unsupportedFunction( lua_State *L, char const *name )
93 {
94 lua_settop( L, 0 );
95 lua_pushstring( L, "Unsupported function: " );
96 lua_pushstring( L, name );
97 lua_concat( L, 2 );
98 lua_error( L );
99 }
100
101 static void pushVideoMode( lua_State *L, const GLFWvidmode *mode)
102 {
103 lua_newtable( L );
104 lua_pushstring( L, "Width" );
105 lua_pushnumber( L, mode->Width );
106 lua_rawset( L, -3 );
107 lua_pushstring( L, "Height" );
108 lua_pushnumber( L, mode->Height );
109 lua_rawset( L, -3 );
110 lua_pushstring( L, "RedBits" );
111 lua_pushnumber( L, mode->RedBits );
112 lua_rawset( L, -3 );
113 lua_pushstring( L, "GreenBits" );
114 lua_pushnumber( L, mode->GreenBits );
115 lua_rawset( L, -3 );
116 lua_pushstring( L, "BlueBits" );
117 lua_pushnumber( L, mode->BlueBits );
118 lua_rawset( L, -3 );
119 }
120
121
122 //************************************************************************
123 //*** Callback wrapper functions ****
124 //************************************************************************
125
126 static lua_State * callback_lua_state = (lua_State *) 0;
127
128 static const char * windowsize_name;
129 static const char * windowclose_name;
130 static const char * windowrefresh_name;
131 static const char * mousebutton_name;
132 static const char * mousepos_name;
133 static const char * mousewheel_name;
134 static const char * key_name;
135 static const char * char_name;
136
137
138 void GLFWCALL luaglfw_windowsizefun( int w, int h )
139 {
140 lua_State *L = callback_lua_state;
141 if( L == NULL ) return;
142
143 lua_getglobal( L, windowsize_name );
144 if( lua_isfunction( L, -1 ) )
145 {
146 lua_pushnumber( L, (lua_Number)w );
147 lua_pushnumber( L, (lua_Number)h );
148 lua_pcall( L, 2, 0, 0 );
149 }
150 }
151
152 int GLFWCALL luaglfw_windowclosefun( void )
153 {
154 lua_State *L = callback_lua_state;
155 int do_close = 1;
156 if( L == NULL ) return 1;
157
158 lua_getglobal( L, windowclose_name );
159 if( lua_isfunction( L, -1 ) )
160 {
161 lua_pcall( L, 0, 1, 0 );
162 do_close = (int) lua_tonumber( L, -1 );
163 lua_pop( L, 1 );
164 }
165 return do_close;
166 }
167
168 void GLFWCALL luaglfw_windowrefreshfun( void )
169 {
170 lua_State *L = callback_lua_state;
171 if( L == NULL ) return;
172
173 lua_getglobal( L, windowrefresh_name );
174 if( lua_isfunction( L, -1 ) )
175 {
176 lua_pcall( L, 0, 0, 0 );
177 }
178 }
179
180 void GLFWCALL luaglfw_mousebuttonfun( int button, int action )
181 {
182 lua_State *L = callback_lua_state;
183 if( L == NULL ) return;
184
185 lua_getglobal( L, mousebutton_name );
186 if( lua_isfunction( L, -1 ) )
187 {
188 lua_pushnumber( L, (lua_Number)button );
189 lua_pushnumber( L, (lua_Number)action );
190 lua_pcall( L, 2, 0, 0 );
191 }
192 }
193
194 void GLFWCALL luaglfw_mouseposfun( int x, int y )
195 {
196 lua_State *L = callback_lua_state;
197 if( L == NULL ) return;
198
199 lua_getglobal( L, mousepos_name );
200 if( lua_isfunction( L, -1 ) )
201 {
202 lua_pushnumber( L, (lua_Number)x );
203 lua_pushnumber( L, (lua_Number)y );
204 lua_pcall( L, 2, 0, 0 );
205 }
206 }
207
208 void GLFWCALL luaglfw_mousewheelfun( int wheelpos )
209 {
210 lua_State *L = callback_lua_state;
211 if( L == NULL ) return;
212
213 lua_getglobal( L, mousewheel_name );
214 if( lua_isfunction( L, -1 ) )
215 {
216 lua_pushnumber( L, (lua_Number)wheelpos );
217 lua_pcall( L, 1, 0, 0 );
218 }
219 }
220
221 void GLFWCALL luaglfw_keyfun( int key, int action )
222 {
223 lua_State *L = callback_lua_state;
224 if( L == NULL ) return;
225
226 lua_getglobal( L, key_name );
227 if( lua_isfunction( L, -1 ) )
228 {
229 lua_pushnumber( L, (lua_Number)key );
230 lua_pushnumber( L, (lua_Number)action );
231 lua_pcall( L, 2, 0, 0 );
232 }
233 }
234
235 void GLFWCALL luaglfw_charfun( int key, int action )
236 {
237 lua_State *L = callback_lua_state;
238 if( L == NULL ) return;
239
240 lua_getglobal( L, char_name );
241 if( lua_isfunction( L, -1 ) )
242 {
243 lua_pushnumber( L, (lua_Number)key );
244 lua_pushnumber( L, (lua_Number)action );
245 lua_pcall( L, 2, 0, 0 );
246 }
247 }
248
249
250
251 //************************************************************************
252 //**** GLFW proxy functions ****
253 //************************************************************************
254
255
256 //========================================================================
257 // Init/termination & version info
258 //========================================================================
259
260 static int glfw_Init( lua_State *L )
261 {
262 int res;
263 lua_settop( L, 0 );
264 res = glfwInit();
265 lua_pushnumber( L, (lua_Number)res );
266 return 1;
267 }
268
269 static int glfw_Terminate( lua_State *L )
270 {
271 glfwTerminate();
272 return 0;
273 }
274
275 static int glfw_GetVersion( lua_State *L )
276 {
277 int major, minor, rev;
278 glfwGetVersion( &major, &minor, &rev );
279 lua_pushnumber( L, major );
280 lua_pushnumber( L, minor );
281 lua_pushnumber( L, rev );
282 return 3;
283 }
284
285
286 //========================================================================
287 // Window handling
288 //========================================================================
289
290 static int glfw_OpenWindow( lua_State *L )
291 {
292 lua_Number w, h, r, g, b, a, depth, stencil, mode;
293 int argc, res;
294
295 // Check arguments
296 if( badArgs( L, 9, "OpenWindow" ) ) return 0;
297
298 // Get all arguments to glfwOpenWindow
299 w = lua_tonumber( L, 1 );
300 h = lua_tonumber( L, 2 );
301 r = lua_tonumber( L, 3 );
302 g = lua_tonumber( L, 4 );
303 b = lua_tonumber( L, 5 );
304 a = lua_tonumber( L, 6 );
305 depth = lua_tonumber( L, 7 );
306 stencil = lua_tonumber( L, 8 );
307 mode = lua_tonumber( L, 9 );
308
309 // Call glfwOpenWindow
310 lua_settop( L,0 );
311 res = glfwOpenWindow( (int)w, (int)h, (int)r, (int)g, (int)b,
312 (int)a, (int)depth, (int)stencil,
313 (int)mode );
314
315 // Return result
316 lua_pushnumber( L, (lua_Number)res );
317 return 1;
318 }
319
320 static int glfw_OpenWindowHint( lua_State *L )
321 {
322 lua_Number target, hint;
323 if( badArgs( L, 2, "OpenWindowHint" ) ) return 0;
324 target = lua_tonumber( L, 1 );
325 hint = lua_tonumber( L, 2 );
326 lua_settop( L, 0 );
327 glfwOpenWindowHint( (int)target, (int)hint );
328 return 0;
329 }
330
331 static int glfw_CloseWindow( lua_State *L )
332 {
333 glfwCloseWindow();
334 return 0;
335 }
336
337 static int glfw_SetWindowTitle( lua_State *L )
338 {
339 const char *str;
340 if( badArgs( L, 1, "SetWindowTitle" ) ) return 0;
341 str = lua_tostring( L, 1 );
342 lua_remove( L, 1 );
343 glfwSetWindowTitle( str );
344 return 0;
345 }
346
347 static int glfw_SetWindowSize( lua_State *L )
348 {
349 lua_Number w, h;
350 if( badArgs( L, 2, "SetWindowSize" ) ) return 0;
351 w = lua_tonumber( L, 1 );
352 h = lua_tonumber( L, 2 );
353 lua_settop( L, 0 );
354 glfwSetWindowSize( (int)w, (int)h );
355 return 0;
356 }
357
358 static int glfw_GetWindowSize( lua_State *L )
359 {
360 int w, h;
361 glfwGetWindowSize( &w, &h );
362 lua_settop( L, 0 );
363 lua_pushnumber( L, (lua_Number)w );
364 lua_pushnumber( L, (lua_Number)h );
365 return 2;
366 }
367
368 static int glfw_SetWindowPos( lua_State *L )
369 {
370 lua_Number x, y;
371 if( badArgs( L, 2, "SetWindowPos" ) ) return 0;
372 x = lua_tonumber( L, 1 );
373 y = lua_tonumber( L, 2 );
374 lua_settop( L, 0 );
375 glfwSetWindowPos( (int)x, (int)y );
376 return 0;
377 }
378
379 static int glfw_IconifyWindow( lua_State *L )
380 {
381 glfwIconifyWindow();
382 return 0;
383 }
384
385 static int glfw_RestoreWindow( lua_State *L )
386 {
387 glfwRestoreWindow();
388 return 0;
389 }
390
391 static int glfw_GetWindowParam( lua_State *L )
392 {
393 lua_Number n;
394 if( badArgs( L, 1, "GetWindowParam" ) ) return 0;
395 n = lua_tonumber( L, 1 );
396 lua_settop( L, 0 );
397 n = glfwGetWindowParam( (int)n );
398 lua_pushnumber( L, n );
399 return 1;
400 }
401
402
403
404 //========================================================================
405 // Buffer swapping
406 //========================================================================
407
408 static int glfw_SwapBuffers( lua_State *L )
409 {
410 glfwSwapBuffers();
411 return 0;
412 }
413
414 static int glfw_SwapInterval( lua_State *L )
415 {
416 lua_Number interval;
417 if( badArgs( L, 1, "SwapInterval" ) ) return 0;
418 interval = lua_tonumber( L, 1 );
419 lua_remove( L, 1 );
420 glfwSwapInterval( (int)interval );
421 return 0;
422 }
423
424
425 //========================================================================
426 // Video modes
427 //========================================================================
428
429 #define LUAGLFW_MAX_NUM_MODES 256
430
431 static int glfw_GetVideoModes( lua_State *L )
432 {
433 GLFWvidmode modes[ LUAGLFW_MAX_NUM_MODES ];
434 int modecount, i;
435
436 modecount = glfwGetVideoModes( modes, LUAGLFW_MAX_NUM_MODES );
437
438 lua_newtable( L );
439 for( i = 0; i < modecount; i++ )
440 {
441 pushVideoMode( L, &modes[i] );
442 lua_rawseti( L, -2, i + 1 );
443 }
444
445 return 1;
446 }
447
448 static int glfw_GetDesktopMode( lua_State *L )
449 {
450 GLFWvidmode mode;
451 glfwGetDesktopMode( &mode );
452
453 pushVideoMode( L, &mode );
454
455 return 1;
456 }
457
458
459 //========================================================================
460 // Event polling/waiting
461 //========================================================================
462
463 static int glfw_PollEvents( lua_State *L )
464 {
465 glfwPollEvents();
466 return 0;
467 }
468
469 static int glfw_WaitEvents( lua_State *L )
470 {
471 glfwWaitEvents();
472 return 0;
473 }
474
475
476 //========================================================================
477 // Key and mouse input
478 //========================================================================
479
480 static int glfw_GetKey( lua_State *L )
481 {
482 int n = 0;
483
484 if( badArgs( L, 1, "GetKey" ) ) return 0;
485
486 switch( lua_type( L, 1 ) )
487 {
488 case LUA_TSTRING:
489 n = (lua_tostring(L,1)[0]);
490 break;
491
492 case LUA_TNUMBER:
493 n = lua_tonumber(L,1);
494 break;
495 }
496
497 lua_settop( L, 0 );
498 n = glfwGetKey( n );
499 lua_pushnumber( L, n );
500 return 1;
501 }
502
503 static int glfw_GetMouseButton( lua_State *L )
504 {
505 lua_Number n;
506 if( badArgs( L, 1, "GetMouseButton" ) ) return 0;
507 n = lua_tonumber( L, 1 );
508 lua_settop( L, 0 );
509 lua_pushnumber( L, glfwGetMouseButton( (int)n ) );
510 return 1;
511 }
512
513 static int glfw_GetMousePos( lua_State *L )
514 {
515 int x, y;
516 glfwGetMousePos( &x, &y );
517 lua_settop( L, 0 );
518 lua_pushnumber( L, (lua_Number)x );
519 lua_pushnumber( L, (lua_Number)y );
520 return 2;
521 }
522
523 static int glfw_SetMousePos( lua_State *L )
524 {
525 lua_Number x, y;
526 if( badArgs( L, 2, "SetMousePos" ) ) return 0;
527 x = lua_tonumber( L, 1 );
528 y = lua_tonumber( L, 2 );
529 lua_settop( L, 0 );
530 glfwSetMousePos( (int)x, (int)y );
531 return 0;
532 }
533
534 static int glfw_GetMouseWheel( lua_State *L )
535 {
536 int pos;
537 pos = glfwGetMouseWheel();
538 lua_settop( L, 0 );
539 lua_pushnumber( L, (lua_Number)pos );
540 return 1;
541 }
542
543 static int glfw_SetMouseWheel( lua_State *L )
544 {
545 lua_Number pos;
546 if( badArgs( L, 1, "SetMouseWheel" ) ) return 0;
547 pos = lua_tonumber( L, 1 );
548 lua_settop( L, 0 );
549 glfwSetMouseWheel( (int)pos );
550 return 0;
551 }
552
553
554 //========================================================================
555 // Joystick input
556 //========================================================================
557
558 static int glfw_GetJoystickParam( lua_State *L )
559 {
560 lua_Number joy, param;
561 int res;
562 if( badArgs( L, 2, "GetJoystickParam" ) ) return 0;
563 joy = lua_tonumber( L, 1 );
564 param = lua_tonumber( L, 2 );
565 lua_settop( L, 0 );
566 res = glfwGetJoystickParam( (int)joy, (int)param );
567 lua_pushnumber( L, (lua_Number)res );
568 return 1;
569 }
570
571 #define LUAGLFW_MAX_JOY_AXES 256
572
573 static int glfw_GetJoystickPos( lua_State *L )
574 {
575 int joy, numaxes, res, i;
576 float pos[ LUAGLFW_MAX_JOY_AXES ];
577
578 // Get arguments
579 if( badArgs( L, 2, "GetJoystickPos" ) ) return 0;
580 joy = (int)lua_tonumber( L, 1 );
581 numaxes = (int)lua_tonumber( L, 2 );
582 lua_settop( L, 0 );
583 if( numaxes < 1 ) return 0;
584 if( numaxes > LUAGLFW_MAX_JOY_AXES ) numaxes = LUAGLFW_MAX_JOY_AXES;
585
586 // Call GLFW funciton
587 res = glfwGetJoystickPos( joy, pos, numaxes );
588
589 // Create result array
590 lua_newtable( L );
591 for( i = 0; i < res; ++ i )
592 {
593 lua_pushnumber( L, (lua_Number)(i+1) );
594 lua_pushnumber( L, pos[ i ] );
595 lua_rawset( L, -3 );
596 }
597
598 return 1;
599 }
600
601 #define LUAGLFW_MAX_JOY_BUTTONS 256
602
603 static int glfw_GetJoystickButtons( lua_State *L )
604 {
605 int joy, numbuttons, res, i;
606 unsigned char buttons[ LUAGLFW_MAX_JOY_AXES ];
607
608 // Get arguments
609 if( badArgs( L, 2, "GetJoystickButtons" ) ) return 0;
610 joy = (int)lua_tonumber( L, 1 );
611 numbuttons = (int)lua_tonumber( L, 2 );
612 lua_settop( L, 0 );
613 if( numbuttons < 1 ) return 0;
614 if( numbuttons > LUAGLFW_MAX_JOY_BUTTONS )
615 numbuttons = LUAGLFW_MAX_JOY_BUTTONS;
616
617 // Call GLFW funciton
618 res = glfwGetJoystickButtons( joy, buttons, numbuttons );
619
620 // Create result array
621 lua_newtable( L );
622 for( i = 0; i < res; ++ i )
623 {
624 lua_pushnumber( L, (lua_Number)(i+1) );
625 lua_pushnumber( L, (lua_Number)buttons[ i ] );
626 lua_rawset( L, -3 );
627 }
628
629 return 1;
630 }
631
632
633 //========================================================================
634 // Timing
635 //========================================================================
636
637 static int glfw_GetTime( lua_State *L )
638 {
639 lua_Number t;
640 t = glfwGetTime();
641 lua_settop( L, 0 );
642 lua_pushnumber( L, t );
643 return 1;
644 }
645
646 static int glfw_SetTime( lua_State *L )
647 {
648 lua_Number t;
649 if ( badArgs( L, 1, "SetTime" ) ) return 0;
650 t = lua_tonumber( L, 1 );
651 lua_settop( L, 0 );
652 glfwSetTime( (double)t );
653 return 0;
654 }
655
656 static int glfw_Sleep( lua_State *L )
657 {
658 lua_Number t;
659 if ( badArgs( L, 1, "Sleep" ) ) return 0;
660 t = lua_tonumber( L, 1 );
661 lua_settop( L, 0 );
662 glfwSleep( (double)t );
663 return 0;
664 }
665
666
667 //========================================================================
668 // Threading support (not possible in Lua)
669 //========================================================================
670
671 static int glfw_CreateThread( lua_State *L )
672 {
673 unsupportedFunction( L, "CreateThread" );
674 return 0;
675 }
676
677 static int glfw_DestroyThread( lua_State *L )
678 {
679 unsupportedFunction( L, "DestroyThread" );
680 return 0;
681 }
682
683 static int glfw_WaitThread( lua_State *L )
684 {
685 unsupportedFunction( L, "WaitThread" );
686 return 0;
687 }
688
689 static int glfw_GetThreadID( lua_State *L )
690 {
691 unsupportedFunction( L, "GetThreadID" );
692 return 0;
693 }
694
695 static int glfw_CreateMutex( lua_State *L )
696 {
697 unsupportedFunction( L, "CreateMutex" );
698 return 0;
699 }
700
701 static int glfw_DestroyMutex( lua_State *L )
702 {
703 unsupportedFunction( L, "DestroyMutex" );
704 return 0;
705 }
706
707 static int glfw_LockMutex( lua_State *L )
708 {
709 unsupportedFunction( L, "LockMutex" );
710 return 0;
711 }
712
713 static int glfw_UnlockMutex( lua_State *L )
714 {
715 unsupportedFunction( L, "UnlockMutex" );
716 return 0;
717 }
718
719 static int glfw_CreateCond( lua_State *L )
720 {
721 unsupportedFunction( L, "CreateCond" );
722 return 0;
723 }
724
725 static int glfw_DestroyCond( lua_State *L )
726 {
727 unsupportedFunction( L, "DestroyCond" );
728 return 0;
729 }
730
731 static int glfw_WaitCond( lua_State *L )
732 {
733 unsupportedFunction( L, "WaitCond" );
734 return 0;
735 }
736
737 static int glfw_SignalCond( lua_State *L )
738 {
739 unsupportedFunction( L, "SignalCond" );
740 return 0;
741 }
742
743 static int glfw_BroadcastCond( lua_State *L )
744 {
745 unsupportedFunction( L, "BroadcastCond" );
746 return 0;
747 }
748
749 static int glfw_GetNumberOfProcessors( lua_State *L )
750 {
751 int n;
752 n = glfwGetNumberOfProcessors();
753 lua_settop( L,0 );
754 lua_pushnumber( L, (lua_Number)n );
755 return 1;
756 }
757
758
759 //========================================================================
760 // Extension handling
761 //========================================================================
762
763 static int glfw_GetGLVersion( lua_State *L )
764 {
765 int major, minor, rev;
766 glfwGetGLVersion( &major, &minor, &rev );
767 lua_settop( L,0 );
768 lua_pushnumber( L, (lua_Number)major );
769 lua_pushnumber( L, (lua_Number)minor );
770 lua_pushnumber( L, (lua_Number)rev );
771 return 3;
772 }
773
774 static int glfw_ExtensionSupported( lua_State *L )
775 {
776 const char *str;
777 int res;
778 if( badArgs( L, 1, "ExtensionSupported" ) ) return 0;
779 str = lua_tostring( L, 1 );
780 lua_settop( L, 0 );
781 res = glfwExtensionSupported( str );
782 lua_pushnumber( L, (lua_Number)res );
783 return 1;
784 }
785
786
787 //========================================================================
788 // Enable/Disable
789 //========================================================================
790
791 static int glfw_Enable( lua_State *L )
792 {
793 lua_Number param;
794 if ( badArgs( L, 1, "Enable" ) ) return 0;
795 param = lua_tonumber( L, 1 );
796 lua_settop( L, 0 );
797 glfwEnable( (int)param );
798 return 0;
799 }
800
801 static int glfw_Disable( lua_State *L )
802 {
803 lua_Number param;
804 if ( badArgs( L, 1, "Disable" ) ) return 0;
805 param = lua_tonumber( L, 1 );
806 lua_settop( L, 0 );
807 glfwDisable( (int)param );
808 return 0;
809 }
810
811
812 //========================================================================
813 // Image/texture I/O support
814 //========================================================================
815
816 static int glfw_ReadImage( lua_State *L )
817 {
818 unsupportedFunction( L, "ReadImage" );
819 return 0;
820 }
821
822 static int glfw_FreeImage( lua_State *L )
823 {
824 unsupportedFunction( L, "FreeImage" );
825 return 0;
826 }
827
828 static int glfw_LoadTexture2D( lua_State *L )
829 {
830 const char *name;
831 lua_Number flags;
832 int res;
833 if( badArgs( L, 2, "LoadTexture2D" ) ) return 0;
834 name = lua_tostring( L, 1 );
835 flags = lua_tonumber( L, 2 );
836 lua_settop( L, 0 );
837 res = glfwLoadTexture2D( name, (int)flags );
838 lua_pushnumber( L, (lua_Number)res );
839 return 1;
840 }
841
842
843 //========================================================================
844 // Callback function management
845 //========================================================================
846
847 static int glfw_SetWindowSizeCallback( lua_State *L )
848 {
849 GLFWwindowsizefun fun = NULL;
850 if( lua_isstring( L, 1 ) )
851 {
852 windowsize_name = lua_tostring( L, 1 );
853 fun = luaglfw_windowsizefun;
854 }
855 glfwSetWindowSizeCallback( fun );
856 return 0;
857 }
858
859 static int glfw_SetWindowCloseCallback( lua_State *L )
860 {
861 GLFWwindowclosefun fun = NULL;
862 if( lua_isstring( L, 1 ) )
863 {
864 windowclose_name = lua_tostring( L, 1 );
865 fun = luaglfw_windowclosefun;
866 }
867 glfwSetWindowCloseCallback( fun );
868 return 0;
869 }
870
871 static int glfw_SetWindowRefreshCallback( lua_State *L )
872 {
873 GLFWwindowrefreshfun fun = NULL;
874 if( lua_isstring( L, 1 ) )
875 {
876 windowrefresh_name = lua_tostring( L, 1 );
877 fun = luaglfw_windowrefreshfun;
878 }
879 glfwSetWindowRefreshCallback( fun );
880 return 0;
881 }
882
883 static int glfw_SetMouseButtonCallback( lua_State *L )
884 {
885 GLFWmousebuttonfun fun = NULL;
886 if( lua_isstring( L, 1 ) )
887 {
888 mousebutton_name = lua_tostring( L, 1 );
889 fun = luaglfw_mousebuttonfun;
890 }
891 glfwSetMouseButtonCallback( fun );
892 return 0;
893 }
894
895 static int glfw_SetMousePosCallback( lua_State *L )
896 {
897 GLFWmouseposfun fun = NULL;
898 if( lua_isstring( L, 1 ) )
899 {
900 mousepos_name = lua_tostring( L, 1 );
901 fun = luaglfw_mouseposfun;
902 }
903 glfwSetMousePosCallback( fun );
904 return 0;
905 }
906
907 static int glfw_SetMouseWheelCallback( lua_State *L )
908 {
909 GLFWmousewheelfun fun = NULL;
910 if( lua_isstring( L, 1 ) )
911 {
912 mousewheel_name = lua_tostring( L, 1 );
913 fun = luaglfw_mousewheelfun;
914 }
915 glfwSetMouseWheelCallback( fun );
916 return 0;
917 }
918
919 static int glfw_SetKeyCallback( lua_State *L )
920 {
921 GLFWkeyfun fun = NULL;
922 if( lua_isstring( L, 1 ) )
923 {
924 key_name = lua_tostring( L, 1 );
925 fun = luaglfw_keyfun;
926 }
927 glfwSetKeyCallback( fun );
928 return 0;
929 }
930
931 static int glfw_SetCharCallback( lua_State *L )
932 {
933 GLFWcharfun fun = NULL;
934 if( lua_isstring( L, 1 ) )
935 {
936 char_name = lua_tostring( L, 1 );
937 fun = luaglfw_charfun;
938 }
939 glfwSetCharCallback( fun );
940 return 0;
941 }
942
943
944
945 //************************************************************************
946 //**** LUA Library Registration ****
947 //************************************************************************
948
949 // GLFW constants are stored in the global Lua table "glfw"
950 static struct lua_constant glfw_constants[] =
951 {
952 // GL constants (GL_TRUE/GL_FALSE)
953 { "TRUE", GL_TRUE },
954 { "FALSE", GL_FALSE },
955
956 // GLFW version
957 { "VERSION_MAJOR", GLFW_VERSION_MAJOR },
958 { "VERSION_MINOR", GLFW_VERSION_MINOR },
959 { "VERSION_REVISION", GLFW_VERSION_REVISION },
960
961 // Key and button state/action definitions
962 { "PRESS", GLFW_PRESS },
963 { "RELEASE", GLFW_RELEASE },
964
965 // Keyboard key definitions
966 { "KEY_SPACE", GLFW_KEY_SPACE },
967 { "KEY_SPECIAL", GLFW_KEY_SPECIAL },
968 { "KEY_ESC", GLFW_KEY_ESC },
969 { "KEY_F1", GLFW_KEY_F1 },
970 { "KEY_F2", GLFW_KEY_F2 },
971 { "KEY_F3", GLFW_KEY_F3 },
972 { "KEY_F4", GLFW_KEY_F4 },
973 { "KEY_F5", GLFW_KEY_F5 },
974 { "KEY_F6", GLFW_KEY_F6 },
975 { "KEY_F7", GLFW_KEY_F7 },
976 { "KEY_F8", GLFW_KEY_F8 },
977 { "KEY_F9", GLFW_KEY_F9 },
978 { "KEY_F10", GLFW_KEY_F10 },
979 { "KEY_F11", GLFW_KEY_F11 },
980 { "KEY_F12", GLFW_KEY_F12 },
981 { "KEY_F13", GLFW_KEY_F13 },
982 { "KEY_F14", GLFW_KEY_F14 },
983 { "KEY_F15", GLFW_KEY_F15 },
984 { "KEY_F16", GLFW_KEY_F16 },
985 { "KEY_F17", GLFW_KEY_F17 },
986 { "KEY_F18", GLFW_KEY_F18 },
987 { "KEY_F19", GLFW_KEY_F19 },
988 { "KEY_F20", GLFW_KEY_F20 },
989 { "KEY_F21", GLFW_KEY_F21 },
990 { "KEY_F22", GLFW_KEY_F22 },
991 { "KEY_F23", GLFW_KEY_F23 },
992 { "KEY_F24", GLFW_KEY_F24 },
993 { "KEY_F25", GLFW_KEY_F25 },
994 { "KEY_UP", GLFW_KEY_UP },
995 { "KEY_DOWN", GLFW_KEY_DOWN },
996 { "KEY_LEFT", GLFW_KEY_LEFT },
997 { "KEY_RIGHT", GLFW_KEY_RIGHT },
998 { "KEY_LSHIFT", GLFW_KEY_LSHIFT },
999 { "KEY_RSHIFT", GLFW_KEY_RSHIFT },
1000 { "KEY_LCTRL", GLFW_KEY_LCTRL },
1001 { "KEY_RCTRL", GLFW_KEY_RCTRL },
1002 { "KEY_LALT", GLFW_KEY_LALT },
1003 { "KEY_RALT", GLFW_KEY_RALT },
1004 { "KEY_TAB", GLFW_KEY_TAB },
1005 { "KEY_ENTER", GLFW_KEY_ENTER },
1006 { "KEY_BACKSPACE", GLFW_KEY_BACKSPACE },
1007 { "KEY_INSERT", GLFW_KEY_INSERT },
1008 { "KEY_DEL", GLFW_KEY_DEL },
1009 { "KEY_PAGEUP", GLFW_KEY_PAGEUP },
1010 { "KEY_PAGEDOWN", GLFW_KEY_PAGEDOWN },
1011 { "KEY_HOME", GLFW_KEY_HOME },
1012 { "KEY_END", GLFW_KEY_END },
1013 { "KEY_KP_0", GLFW_KEY_KP_0 },
1014 { "KEY_KP_1", GLFW_KEY_KP_1 },
1015 { "KEY_KP_2", GLFW_KEY_KP_2 },
1016 { "KEY_KP_3", GLFW_KEY_KP_3 },
1017 { "KEY_KP_4", GLFW_KEY_KP_4 },
1018 { "KEY_KP_5", GLFW_KEY_KP_5 },
1019 { "KEY_KP_6", GLFW_KEY_KP_6 },
1020 { "KEY_KP_7", GLFW_KEY_KP_7 },
1021 { "KEY_KP_8", GLFW_KEY_KP_8 },
1022 { "KEY_KP_9", GLFW_KEY_KP_9 },
1023 { "KEY_KP_DIVIDE", GLFW_KEY_KP_DIVIDE },
1024 { "KEY_KP_MULTIPLY", GLFW_KEY_KP_MULTIPLY },
1025 { "KEY_KP_SUBTRACT", GLFW_KEY_KP_SUBTRACT },
1026 { "KEY_KP_ADD", GLFW_KEY_KP_ADD },
1027 { "KEY_KP_DECIMAL", GLFW_KEY_KP_DECIMAL },
1028 { "KEY_KP_EQUAL", GLFW_KEY_KP_EQUAL },
1029 { "KEY_KP_ENTER", GLFW_KEY_KP_ENTER },
1030 { "KEY_LAST", GLFW_KEY_LAST },
1031
1032 // Mouse button definitions
1033 { "MOUSE_BUTTON_1", GLFW_MOUSE_BUTTON_1 },
1034 { "MOUSE_BUTTON_2", GLFW_MOUSE_BUTTON_2 },
1035 { "MOUSE_BUTTON_3", GLFW_MOUSE_BUTTON_3 },
1036 { "MOUSE_BUTTON_4", GLFW_MOUSE_BUTTON_4 },
1037 { "MOUSE_BUTTON_5", GLFW_MOUSE_BUTTON_5 },
1038 { "MOUSE_BUTTON_6", GLFW_MOUSE_BUTTON_6 },
1039 { "MOUSE_BUTTON_7", GLFW_MOUSE_BUTTON_7 },
1040 { "MOUSE_BUTTON_8", GLFW_MOUSE_BUTTON_8 },
1041 { "MOUSE_BUTTON_LAST", GLFW_MOUSE_BUTTON_LAST },
1042
1043 // Mouse button aliases
1044 { "MOUSE_BUTTON_LEFT", GLFW_MOUSE_BUTTON_LEFT },
1045 { "MOUSE_BUTTON_RIGHT", GLFW_MOUSE_BUTTON_RIGHT },
1046 { "MOUSE_BUTTON_MIDDLE", GLFW_MOUSE_BUTTON_MIDDLE },
1047
1048 // Joystick identifiers
1049 { "JOYSTICK_1", GLFW_JOYSTICK_1 },
1050 { "JOYSTICK_2", GLFW_JOYSTICK_2 },
1051 { "JOYSTICK_3", GLFW_JOYSTICK_3 },
1052 { "JOYSTICK_4", GLFW_JOYSTICK_4 },
1053 { "JOYSTICK_5", GLFW_JOYSTICK_5 },
1054 { "JOYSTICK_6", GLFW_JOYSTICK_6 },
1055 { "JOYSTICK_7", GLFW_JOYSTICK_7 },
1056 { "JOYSTICK_8", GLFW_JOYSTICK_8 },
1057 { "JOYSTICK_9", GLFW_JOYSTICK_9 },
1058 { "JOYSTICK_10", GLFW_JOYSTICK_10 },
1059 { "JOYSTICK_11", GLFW_JOYSTICK_11 },
1060 { "JOYSTICK_12", GLFW_JOYSTICK_12 },
1061 { "JOYSTICK_13", GLFW_JOYSTICK_13 },
1062 { "JOYSTICK_14", GLFW_JOYSTICK_14 },
1063 { "JOYSTICK_15", GLFW_JOYSTICK_15 },
1064 { "JOYSTICK_16", GLFW_JOYSTICK_16 },
1065 { "JOYSTICK_LAST", GLFW_JOYSTICK_LAST },
1066
1067 // glfwOpenWindow modes
1068 { "WINDOW", GLFW_WINDOW },
1069 { "FULLSCREEN", GLFW_FULLSCREEN },
1070
1071 // glfwGetWindowParam tokens
1072 { "OPENED", GLFW_OPENED },
1073 { "ACTIVE", GLFW_ACTIVE },
1074 { "ICONIFIED", GLFW_ICONIFIED },
1075 { "ACCELERATED", GLFW_ACCELERATED },
1076 { "RED_BITS", GLFW_RED_BITS },
1077 { "GREEN_BITS", GLFW_GREEN_BITS },
1078 { "BLUE_BITS", GLFW_BLUE_BITS },
1079 { "ALPHA_BITS", GLFW_ALPHA_BITS },
1080 { "DEPTH_BITS", GLFW_DEPTH_BITS },
1081 { "STENCIL_BITS", GLFW_STENCIL_BITS },
1082
1083 // Constants for glfwGetWindowParam and glfwOpenWindowHint
1084 { "REFRESH_RATE", GLFW_REFRESH_RATE },
1085 { "ACCUM_RED_BITS", GLFW_ACCUM_RED_BITS },
1086 { "ACCUM_GREEN_BITS", GLFW_ACCUM_GREEN_BITS },
1087 { "ACCUM_BLUE_BITS", GLFW_ACCUM_BLUE_BITS },
1088 { "ACCUM_ALPHA_BITS", GLFW_ACCUM_ALPHA_BITS },
1089 { "AUX_BUFFERS", GLFW_AUX_BUFFERS },
1090 { "STEREO", GLFW_STEREO },
1091
1092 // glfwEnable/glfwDisable tokens
1093 { "MOUSE_CURSOR", GLFW_MOUSE_CURSOR },
1094 { "STICKY_KEYS", GLFW_STICKY_KEYS },
1095 { "STICKY_MOUSE_BUTTONS", GLFW_STICKY_MOUSE_BUTTONS },
1096 { "SYSTEM_KEYS", GLFW_SYSTEM_KEYS },
1097 { "KEY_REPEAT", GLFW_KEY_REPEAT },
1098 { "AUTO_POLL_EVENTS", GLFW_AUTO_POLL_EVENTS },
1099
1100 // glfwWaitThread wait modes
1101 { "WAIT", GLFW_WAIT },
1102 { "NOWAIT", GLFW_NOWAIT },
1103
1104 // glfwGetJoystickParam tokens
1105 { "PRESENT", GLFW_PRESENT },
1106 { "AXES", GLFW_AXES },
1107 { "BUTTONS", GLFW_BUTTONS },
1108
1109 // glfwReadImage/glfwLoadTexture2D flags
1110 { "NO_RESCALE_BIT", GLFW_NO_RESCALE_BIT },
1111 { "ORIGIN_UL_BIT", GLFW_ORIGIN_UL_BIT },
1112 { "BUILD_MIPMAPS_BIT", GLFW_BUILD_MIPMAPS_BIT },
1113 { "ALPHA_MAP_BIT", GLFW_ALPHA_MAP_BIT },
1114
1115 // Time spans longer than this (seconds) are considered to be infinity
1116 { "INFINITY", GLFW_INFINITY },
1117
1118 { NULL, 0 }
1119 };
1120
1121
1122 // Library functions to register
1123 static const luaL_reg glfwlib[] = {
1124 { "Init", glfw_Init },
1125 { "Terminate", glfw_Terminate },
1126 { "GetVersion", glfw_GetVersion },
1127 { "OpenWindow", glfw_OpenWindow },
1128 { "OpenWindowHint", glfw_OpenWindowHint },
1129 { "CloseWindow", glfw_CloseWindow },
1130 { "SetWindowTitle", glfw_SetWindowTitle },
1131 { "SetWindowSize", glfw_SetWindowSize },
1132 { "GetWindowSize", glfw_GetWindowSize },
1133 { "SetWindowPos", glfw_SetWindowPos },
1134 { "IconifyWindow", glfw_IconifyWindow },
1135 { "RestoreWindow", glfw_RestoreWindow },
1136 { "GetWindowParam", glfw_GetWindowParam },
1137 { "SwapBuffers", glfw_SwapBuffers },
1138 { "SwapInterval", glfw_SwapInterval },
1139 { "GetVideoModes", glfw_GetVideoModes },
1140 { "GetDesktopMode", glfw_GetDesktopMode },
1141 { "PollEvents", glfw_PollEvents },
1142 { "WaitEvents", glfw_WaitEvents },
1143 { "GetKey", glfw_GetKey },
1144 { "GetMouseButton", glfw_GetMouseButton },
1145 { "GetMousePos", glfw_GetMousePos },
1146 { "SetMousePos", glfw_SetMousePos },
1147 { "GetMouseWheel", glfw_GetMouseWheel },
1148 { "SetMouseWheel", glfw_SetMouseWheel },
1149 { "GetJoystickParam", glfw_GetJoystickParam },
1150 { "GetJoystickPos", glfw_GetJoystickPos },
1151 { "GetJoystickButtons", glfw_GetJoystickButtons },
1152 { "GetTime", glfw_GetTime },
1153 { "SetTime", glfw_SetTime },
1154 { "Sleep", glfw_Sleep },
1155 { "GetGLVersion", glfw_GetGLVersion },
1156 { "ExtensionSupported", glfw_ExtensionSupported },
1157 { "CreateThread", glfw_CreateThread },
1158 { "DestroyThread", glfw_DestroyThread },
1159 { "WaitThread", glfw_WaitThread },
1160 { "GetThreadID", glfw_GetThreadID },
1161 { "CreateMutex", glfw_CreateMutex },
1162 { "DestroyMutex", glfw_DestroyMutex },
1163 { "LockMutex", glfw_LockMutex },
1164 { "UnlockMutex", glfw_UnlockMutex },
1165 { "CreateCond", glfw_CreateCond },
1166 { "DestroyCond", glfw_DestroyCond },
1167 { "WaitCond", glfw_WaitCond },
1168 { "SignalCond", glfw_SignalCond },
1169 { "BroadcastCond", glfw_BroadcastCond },
1170 { "GetNumberOfProcessors", glfw_GetNumberOfProcessors },
1171 { "Enable", glfw_Enable },
1172 { "Disable", glfw_Disable },
1173 { "ReadImage", glfw_ReadImage },
1174 { "FreeImage", glfw_FreeImage },
1175 { "LoadTexture2D", glfw_LoadTexture2D },
1176 { "SetWindowSizeCallback", glfw_SetWindowSizeCallback },
1177 { "SetWindowCloseCallback", glfw_SetWindowCloseCallback },
1178 { "SetWindowRefreshCallback", glfw_SetWindowRefreshCallback },
1179 { "SetMouseButtonCallback", glfw_SetMouseButtonCallback },
1180 { "SetMousePosCallback", glfw_SetMousePosCallback },
1181 { "SetMouseWheelCallback", glfw_SetMouseWheelCallback },
1182 { "SetKeyCallback", glfw_SetKeyCallback },
1183 { "SetCharCallback", glfw_SetCharCallback },
1184 { NULL, NULL }
1185 };
1186
1187
1188
1189 int luaopen_glfw( lua_State *L )
1190 {
1191 // Store GLFW functions in "glfw" table
1192 luaL_openlib( L, "glfw", glfwlib, 0 );
1193
1194 // Store GLFW constants in "glfw" table
1195 lua_pushstring( L, "glfw" );
1196 lua_gettable( L, LUA_GLOBALSINDEX );
1197 addConstants( L, glfw_constants );
1198 lua_settop( L, 0 );
1199
1200 // Remember Lua state for callback functions
1201 callback_lua_state = L;
1202
1203 return 0;
1204 }