ruby-glfw 0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (206) hide show
  1. data/README +1 -0
  2. data/README.API +73 -0
  3. data/Rakefile +120 -0
  4. data/examples/boing.rb +519 -0
  5. data/examples/gears.rb +327 -0
  6. data/examples/keytest.rb +117 -0
  7. data/examples/listmodes.rb +20 -0
  8. data/examples/mipmaps.rb +104 -0
  9. data/examples/mipmaps.tga +0 -0
  10. data/examples/particles.rb +837 -0
  11. data/examples/pong3d.rb +741 -0
  12. data/examples/pong3d_field.tga +0 -0
  13. data/examples/pong3d_instr.tga +0 -0
  14. data/examples/pong3d_menu.tga +0 -0
  15. data/examples/pong3d_title.tga +0 -0
  16. data/examples/pong3d_winner1.tga +0 -0
  17. data/examples/pong3d_winner2.tga +0 -0
  18. data/examples/splitview.rb +432 -0
  19. data/examples/triangle.rb +89 -0
  20. data/examples/wave.rb +294 -0
  21. data/ext/glfw/glfw.c +1094 -0
  22. data/ext/glfw/mkrf_conf.rb +70 -0
  23. data/glfw-src/Makefile +220 -0
  24. data/glfw-src/compile.ami +61 -0
  25. data/glfw-src/compile.bat +217 -0
  26. data/glfw-src/compile.sh +607 -0
  27. data/glfw-src/docs/Makefile +57 -0
  28. data/glfw-src/docs/Reference.pdf +0 -0
  29. data/glfw-src/docs/UsersGuide.pdf +0 -0
  30. data/glfw-src/docs/cleanup.bat +22 -0
  31. data/glfw-src/docs/glfwdoc.sty +80 -0
  32. data/glfw-src/docs/glfwrm.tex +3034 -0
  33. data/glfw-src/docs/glfwug.tex +2024 -0
  34. data/glfw-src/docs/readme.txt +80 -0
  35. data/glfw-src/examples/Makefile.amigaos.gcc +70 -0
  36. data/glfw-src/examples/Makefile.amigaos.vbcc +70 -0
  37. data/glfw-src/examples/Makefile.dos.djgpp +71 -0
  38. data/glfw-src/examples/Makefile.macosx.gcc +96 -0
  39. data/glfw-src/examples/Makefile.win32.bcc +75 -0
  40. data/glfw-src/examples/Makefile.win32.cross-mgw +79 -0
  41. data/glfw-src/examples/Makefile.win32.cygwin +79 -0
  42. data/glfw-src/examples/Makefile.win32.lcc +74 -0
  43. data/glfw-src/examples/Makefile.win32.mgw +75 -0
  44. data/glfw-src/examples/Makefile.win32.msvc +74 -0
  45. data/glfw-src/examples/Makefile.win32.ow +74 -0
  46. data/glfw-src/examples/Makefile.win32.pellesc +74 -0
  47. data/glfw-src/examples/Makefile.x11.in +54 -0
  48. data/glfw-src/examples/boing.c +606 -0
  49. data/glfw-src/examples/bundle.sh +46 -0
  50. data/glfw-src/examples/gears.c +382 -0
  51. data/glfw-src/examples/keytest.c +264 -0
  52. data/glfw-src/examples/listmodes.c +48 -0
  53. data/glfw-src/examples/mipmaps.c +126 -0
  54. data/glfw-src/examples/mipmaps.tga +0 -0
  55. data/glfw-src/examples/mtbench.c +301 -0
  56. data/glfw-src/examples/mthello.c +48 -0
  57. data/glfw-src/examples/particles.c +1148 -0
  58. data/glfw-src/examples/pong3d.c +839 -0
  59. data/glfw-src/examples/pong3d_field.tga +0 -0
  60. data/glfw-src/examples/pong3d_instr.tga +0 -0
  61. data/glfw-src/examples/pong3d_menu.tga +0 -0
  62. data/glfw-src/examples/pong3d_title.tga +0 -0
  63. data/glfw-src/examples/pong3d_winner1.tga +0 -0
  64. data/glfw-src/examples/pong3d_winner2.tga +0 -0
  65. data/glfw-src/examples/splitview.c +506 -0
  66. data/glfw-src/examples/triangle.c +108 -0
  67. data/glfw-src/examples/wave.c +397 -0
  68. data/glfw-src/images/opengl.gif +0 -0
  69. data/glfw-src/images/osicert.gif +0 -0
  70. data/glfw-src/include/GL/glfw.h +486 -0
  71. data/glfw-src/lib/amigaos/Makefile.amigaos.gcc +128 -0
  72. data/glfw-src/lib/amigaos/Makefile.amigaos.vbcc +128 -0
  73. data/glfw-src/lib/amigaos/SDI_compiler.h +94 -0
  74. data/glfw-src/lib/amigaos/amigaos_enable.c +51 -0
  75. data/glfw-src/lib/amigaos/amigaos_fullscreen.c +319 -0
  76. data/glfw-src/lib/amigaos/amigaos_glext.c +61 -0
  77. data/glfw-src/lib/amigaos/amigaos_init.c +284 -0
  78. data/glfw-src/lib/amigaos/amigaos_joystick.c +359 -0
  79. data/glfw-src/lib/amigaos/amigaos_thread.c +494 -0
  80. data/glfw-src/lib/amigaos/amigaos_time.c +206 -0
  81. data/glfw-src/lib/amigaos/amigaos_window.c +830 -0
  82. data/glfw-src/lib/amigaos/platform.h +337 -0
  83. data/glfw-src/lib/dos/Makefile.dos.djgpp +146 -0
  84. data/glfw-src/lib/dos/dos_enable.c +51 -0
  85. data/glfw-src/lib/dos/dos_events.c +173 -0
  86. data/glfw-src/lib/dos/dos_fullscreen.c +101 -0
  87. data/glfw-src/lib/dos/dos_glext.c +59 -0
  88. data/glfw-src/lib/dos/dos_init.c +105 -0
  89. data/glfw-src/lib/dos/dos_irq.s +246 -0
  90. data/glfw-src/lib/dos/dos_joystick.c +94 -0
  91. data/glfw-src/lib/dos/dos_keyboard.c +694 -0
  92. data/glfw-src/lib/dos/dos_mouse.c +337 -0
  93. data/glfw-src/lib/dos/dos_thread.c +267 -0
  94. data/glfw-src/lib/dos/dos_time.c +309 -0
  95. data/glfw-src/lib/dos/dos_window.c +563 -0
  96. data/glfw-src/lib/dos/platform.h +341 -0
  97. data/glfw-src/lib/enable.c +295 -0
  98. data/glfw-src/lib/fullscreen.c +95 -0
  99. data/glfw-src/lib/glext.c +201 -0
  100. data/glfw-src/lib/image.c +629 -0
  101. data/glfw-src/lib/init.c +108 -0
  102. data/glfw-src/lib/input.c +280 -0
  103. data/glfw-src/lib/internal.h +210 -0
  104. data/glfw-src/lib/joystick.c +101 -0
  105. data/glfw-src/lib/macosx/Makefile.macosx.gcc +172 -0
  106. data/glfw-src/lib/macosx/Makefile.macosx.gcc.universal +166 -0
  107. data/glfw-src/lib/macosx/libglfw.pc.in +11 -0
  108. data/glfw-src/lib/macosx/macosx_enable.c +42 -0
  109. data/glfw-src/lib/macosx/macosx_fullscreen.c +126 -0
  110. data/glfw-src/lib/macosx/macosx_glext.c +52 -0
  111. data/glfw-src/lib/macosx/macosx_init.c +194 -0
  112. data/glfw-src/lib/macosx/macosx_joystick.c +50 -0
  113. data/glfw-src/lib/macosx/macosx_thread.c +414 -0
  114. data/glfw-src/lib/macosx/macosx_time.c +112 -0
  115. data/glfw-src/lib/macosx/macosx_window.c +1279 -0
  116. data/glfw-src/lib/macosx/platform.h +349 -0
  117. data/glfw-src/lib/stream.c +194 -0
  118. data/glfw-src/lib/tga.c +405 -0
  119. data/glfw-src/lib/thread.c +340 -0
  120. data/glfw-src/lib/time.c +83 -0
  121. data/glfw-src/lib/win32/Makefile.win32.bcc +265 -0
  122. data/glfw-src/lib/win32/Makefile.win32.cross-mgw +274 -0
  123. data/glfw-src/lib/win32/Makefile.win32.cygwin +279 -0
  124. data/glfw-src/lib/win32/Makefile.win32.lcc +246 -0
  125. data/glfw-src/lib/win32/Makefile.win32.mgw +243 -0
  126. data/glfw-src/lib/win32/Makefile.win32.msvc +242 -0
  127. data/glfw-src/lib/win32/Makefile.win32.ow +242 -0
  128. data/glfw-src/lib/win32/Makefile.win32.pellesc +242 -0
  129. data/glfw-src/lib/win32/glfwdll.def +67 -0
  130. data/glfw-src/lib/win32/glfwdll_mgw1.def +67 -0
  131. data/glfw-src/lib/win32/glfwdll_mgw2.def +67 -0
  132. data/glfw-src/lib/win32/glfwdll_pellesc.def +65 -0
  133. data/glfw-src/lib/win32/libglfw.pc.in +11 -0
  134. data/glfw-src/lib/win32/platform.h +474 -0
  135. data/glfw-src/lib/win32/win32_dllmain.c +60 -0
  136. data/glfw-src/lib/win32/win32_enable.c +155 -0
  137. data/glfw-src/lib/win32/win32_fullscreen.c +317 -0
  138. data/glfw-src/lib/win32/win32_glext.c +85 -0
  139. data/glfw-src/lib/win32/win32_init.c +356 -0
  140. data/glfw-src/lib/win32/win32_joystick.c +234 -0
  141. data/glfw-src/lib/win32/win32_thread.c +511 -0
  142. data/glfw-src/lib/win32/win32_time.c +146 -0
  143. data/glfw-src/lib/win32/win32_window.c +1714 -0
  144. data/glfw-src/lib/window.c +727 -0
  145. data/glfw-src/lib/x11/Makefile.x11.in +243 -0
  146. data/glfw-src/lib/x11/platform.h +415 -0
  147. data/glfw-src/lib/x11/x11_enable.c +51 -0
  148. data/glfw-src/lib/x11/x11_fullscreen.c +524 -0
  149. data/glfw-src/lib/x11/x11_glext.c +69 -0
  150. data/glfw-src/lib/x11/x11_init.c +275 -0
  151. data/glfw-src/lib/x11/x11_joystick.c +371 -0
  152. data/glfw-src/lib/x11/x11_keysym2unicode.c +902 -0
  153. data/glfw-src/lib/x11/x11_thread.c +507 -0
  154. data/glfw-src/lib/x11/x11_time.c +154 -0
  155. data/glfw-src/lib/x11/x11_window.c +1746 -0
  156. data/glfw-src/license.txt +21 -0
  157. data/glfw-src/readme.html +927 -0
  158. data/glfw-src/support/d/examples/Makefile +59 -0
  159. data/glfw-src/support/d/examples/boing.d +610 -0
  160. data/glfw-src/support/d/examples/gears.d +379 -0
  161. data/glfw-src/support/d/examples/keytest.d +272 -0
  162. data/glfw-src/support/d/examples/listmodes.d +48 -0
  163. data/glfw-src/support/d/examples/mipmaps.d +126 -0
  164. data/glfw-src/support/d/examples/mtbench.d +304 -0
  165. data/glfw-src/support/d/examples/mthello.d +54 -0
  166. data/glfw-src/support/d/examples/particles.d +1150 -0
  167. data/glfw-src/support/d/examples/pong3d.d +840 -0
  168. data/glfw-src/support/d/examples/splitview.d +486 -0
  169. data/glfw-src/support/d/examples/triangle.d +108 -0
  170. data/glfw-src/support/d/examples/wave.d +400 -0
  171. data/glfw-src/support/d/imports/gl.d +4539 -0
  172. data/glfw-src/support/d/imports/glfw.d +349 -0
  173. data/glfw-src/support/d/imports/glu.d +328 -0
  174. data/glfw-src/support/d/lib/glfwdll.def +64 -0
  175. data/glfw-src/support/d/lib/glu32.def +56 -0
  176. data/glfw-src/support/d/lib/makefile +12 -0
  177. data/glfw-src/support/d/lib/opengl32.def +372 -0
  178. data/glfw-src/support/d/readme.html +83 -0
  179. data/glfw-src/support/delphi/examples/Triangle.dpr +105 -0
  180. data/glfw-src/support/delphi/lib/glfw.pas +437 -0
  181. data/glfw-src/support/delphi/readme.html +97 -0
  182. data/glfw-src/support/lua/examples/gears.lua +383 -0
  183. data/glfw-src/support/lua/examples/test1.lua +68 -0
  184. data/glfw-src/support/lua/readme.html +128 -0
  185. data/glfw-src/support/lua/src/luaglfw.c +1179 -0
  186. data/glfw-src/support/lua/src/luaglfw.h +48 -0
  187. data/glfw-src/support/lua/src/runlua.c +82 -0
  188. data/glfw-src/support/masm/examples/fpc.mac +47 -0
  189. data/glfw-src/support/masm/examples/makeit.bat +66 -0
  190. data/glfw-src/support/masm/examples/triangle.asm +232 -0
  191. data/glfw-src/support/masm/include/glfw.inc +326 -0
  192. data/glfw-src/support/masm/include/glu32.inc +55 -0
  193. data/glfw-src/support/masm/include/opengl32.inc +372 -0
  194. data/glfw-src/support/masm/lib/glfwdll.lib +0 -0
  195. data/glfw-src/support/masm/readme.html +170 -0
  196. data/glfw-src/support/msvc80/GLFW.sln +26 -0
  197. data/glfw-src/support/msvc80/GLFW.vcproj +257 -0
  198. data/glfw-src/support/msvc80/GLFWDLL.vcproj +287 -0
  199. data/glfw-src/support/visualbasic/bindings/glfw.bas +320 -0
  200. data/glfw-src/support/visualbasic/bindings/glu32.bas +284 -0
  201. data/glfw-src/support/visualbasic/bindings/opengl32.bas +999 -0
  202. data/glfw-src/support/visualbasic/examples/Triangle.bas +101 -0
  203. data/glfw-src/support/visualbasic/readme.html +164 -0
  204. data/website/index.html +84 -0
  205. data/website/style.css +110 -0
  206. metadata +301 -0
@@ -0,0 +1,48 @@
1
+ //========================================================================
2
+ // This is a small test application for GLFW.
3
+ // The program lists all available fullscreen video modes.
4
+ //========================================================================
5
+
6
+ import glfw;
7
+
8
+ // Maximum number of modes that we want to list
9
+ const int MAX_NUM_MODES = 400;
10
+
11
+
12
+ //========================================================================
13
+ // main()
14
+ //========================================================================
15
+
16
+ int main()
17
+ {
18
+ GLFWvidmode dtmode;
19
+ GLFWvidmode modes[ MAX_NUM_MODES ];
20
+ int modecount, i;
21
+
22
+ // Initialize GLFW
23
+ if( !glfwInit() )
24
+ {
25
+ return 0;
26
+ }
27
+
28
+ // Show desktop video mode
29
+ glfwGetDesktopMode( &dtmode );
30
+ printf( "Desktop mode: %d x %d x %d\n\n",
31
+ dtmode.Width, dtmode.Height, dtmode.RedBits +
32
+ dtmode.GreenBits + dtmode.BlueBits );
33
+
34
+ // List available video modes
35
+ modecount = glfwGetVideoModes( modes, MAX_NUM_MODES );
36
+ printf( "Available modes:\n" );
37
+ for( i = 0; i < modecount; i ++ )
38
+ {
39
+ printf( "%3d: %d x %d x %d\n", i,
40
+ modes[i].Width, modes[i].Height, modes[i].RedBits +
41
+ modes[i].GreenBits + modes[i].BlueBits );
42
+ }
43
+
44
+ // Terminate GLFW
45
+ glfwTerminate();
46
+
47
+ return 0;
48
+ }
@@ -0,0 +1,126 @@
1
+ //========================================================================
2
+ // This is a small test application for GLFW.
3
+ // The program shows texture loading with mipmap generation and trilienar
4
+ // filtering.
5
+ // Note: For OpenGL 1.0 compability, we do not use texture objects (this
6
+ // is no issue, since we only have one texture).
7
+ //========================================================================
8
+
9
+ import glfw;
10
+ import std.c.stdio;
11
+
12
+
13
+ //========================================================================
14
+ // main()
15
+ //========================================================================
16
+
17
+ int main( )
18
+ {
19
+ int width, height, running, frames, x, y;
20
+ double t, t0, fps;
21
+ char titlestr[ 200 ];
22
+
23
+ // Initialise GLFW
24
+ glfwInit();
25
+
26
+ // Open OpenGL window
27
+ if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) )
28
+ {
29
+ glfwTerminate();
30
+ return 0;
31
+ }
32
+
33
+ // Enable sticky keys
34
+ glfwEnable( GLFW_STICKY_KEYS );
35
+
36
+ // Disable vertical sync (on cards that support it)
37
+ glfwSwapInterval( 0 );
38
+
39
+ // Load texture from file, and build all mipmap levels. The
40
+ // texture is automatically uploaded to texture memory.
41
+ if( !glfwLoadTexture2D( "mipmaps.tga", GLFW_BUILD_MIPMAPS_BIT ) )
42
+ {
43
+ glfwTerminate();
44
+ return 0;
45
+ }
46
+
47
+ // Use trilinear interpolation (GL_LINEAR_MIPMAP_LINEAR)
48
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
49
+ GL_LINEAR_MIPMAP_LINEAR );
50
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
51
+ GL_LINEAR );
52
+
53
+ // Enable texturing
54
+ glEnable( GL_TEXTURE_2D );
55
+
56
+ // Main loop
57
+ running = GL_TRUE;
58
+ frames = 0;
59
+ t0 = glfwGetTime();
60
+ while( running )
61
+ {
62
+ // Get time and mouse position
63
+ t = glfwGetTime();
64
+ glfwGetMousePos( &x, &y );
65
+
66
+ // Calculate and display FPS (frames per second)
67
+ if( (t-t0) > 1.0 || frames == 0 )
68
+ {
69
+ fps = cast(double)frames / (t-t0);
70
+ sprintf( titlestr, "Trilinear interpolation (%.1f FPS)", fps );
71
+ glfwSetWindowTitle( titlestr );
72
+ t0 = t;
73
+ frames = 0;
74
+ }
75
+ frames ++;
76
+
77
+ // Get window size (may be different than the requested size)
78
+ glfwGetWindowSize( &width, &height );
79
+ height = height > 0 ? height : 1;
80
+
81
+ // Set viewport
82
+ glViewport( 0, 0, width, height );
83
+
84
+ // Clear color buffer
85
+ glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);
86
+ glClear( GL_COLOR_BUFFER_BIT );
87
+
88
+ // Select and setup the projection matrix
89
+ glMatrixMode( GL_PROJECTION );
90
+ glLoadIdentity();
91
+ gluPerspective( 65.0f, cast(GLfloat)width/cast(GLfloat)height, 1.0f,
92
+ 50.0f );
93
+
94
+ // Select and setup the modelview matrix
95
+ glMatrixMode( GL_MODELVIEW );
96
+ glLoadIdentity();
97
+ gluLookAt( 0.0f, 3.0f, -20.0f, // Eye-position
98
+ 0.0f, -4.0f, -11.0f, // View-point
99
+ 0.0f, 1.0f, 0.0f ); // Up-vector
100
+
101
+ // Draw a textured quad
102
+ glRotatef( 0.05*cast(GLfloat)x + cast(GLfloat)t*5.0f, 0.0f, 1.0f, 0.0f );
103
+ glBegin( GL_QUADS );
104
+ glTexCoord2f( -20.0f, 20.0f );
105
+ glVertex3f( -50.0f, 0.0f, -50.0f );
106
+ glTexCoord2f( 20.0f, 20.0f );
107
+ glVertex3f( 50.0f, 0.0f, -50.0f );
108
+ glTexCoord2f( 20.0f, -20.0f );
109
+ glVertex3f( 50.0f, 0.0f, 50.0f );
110
+ glTexCoord2f( -20.0f, -20.0f );
111
+ glVertex3f( -50.0f, 0.0f, 50.0f );
112
+ glEnd();
113
+
114
+ // Swap buffers
115
+ glfwSwapBuffers();
116
+
117
+ // Check if the ESC key was pressed or the window was closed
118
+ running = !glfwGetKey( GLFW_KEY_ESC ) &&
119
+ glfwGetWindowParam( GLFW_OPENED );
120
+ }
121
+
122
+ // Close OpenGL window and terminate GLFW
123
+ glfwTerminate();
124
+
125
+ return 0;
126
+ }
@@ -0,0 +1,304 @@
1
+ //========================================================================
2
+ // Multithreading benchmark program, based on the GLFW multi threading
3
+ // support.
4
+ //
5
+ // This program can be used to get an idea of what to expect in terms of
6
+ // multithreading granularity performance.
7
+ //
8
+ // As a "bonus", this program demonstrates how to create a signal
9
+ // primitive using the GLFW mutex and condition variable primitives.
10
+ //
11
+ // Here are some benchmark results:
12
+ // (Note: these are not exact measurments, since they are subject to
13
+ // varying CPU-loads etc. Some tested systems are multi-user systems
14
+ // which were running under anything but optimal conditions)
15
+ //
16
+ // +------------+-------+-------------+-------------------+------------+
17
+ // | Processor | CPUs | OS | Context switches | Mean sleep |
18
+ // | | | | per second | time (ms) |
19
+ // +------------+-------+-------------+-------------------+------------+
20
+ // |Athlon | 1 | Linux | 161942 | 20.000 |
21
+ // |710 MHz | | 2.4.3 | | |
22
+ // +------------+-------+-------------+-------------------+------------+
23
+ // |Athlon | 1 | MS Win2k | 525230 | 10.014 |
24
+ // |710 MHz | | | | |
25
+ // +------------+-------+-------------+-------------------+------------+
26
+ // |Athlon | 1 | MS Win 98 | 23564 | 4.947 |
27
+ // |710 MHz | | | | |
28
+ // +------------+-------+-------------+-------------------+------------+
29
+ // |Pentium III | 1 | MS NT 4.0 | 304694 | 10.014 |
30
+ // |500 MHz | | | | |
31
+ // +------------+-------+-------------+-------------------+------------+
32
+ // |UltraSPARC2 | 6 | SunOS 5.6 | 120867 | 19.355 |
33
+ // |400 MHz | | | | |
34
+ // +------------+-------+-------------+-------------------+------------+
35
+ // |Alpha 21264 | 1 | OSF1 | 131993 | 3.097 |
36
+ // |500 MHz | | | | |
37
+ // +------------+-------+-------------+-------------------+------------+
38
+ // |Alpha 21264 | 2 | OSF1 | 40836 | 1.397 |
39
+ // |500 MHz | | | | |
40
+ // +------------+-------+-------------+-------------------+------------+
41
+ // |68020 (emu) | 1 | AmigaOS 3.1 | 50425 | 40.060 |
42
+ // |~200 MHz | | (WinUAE) | | |
43
+ // +------------+-------+-------------+-------------------+------------+
44
+ //
45
+ //========================================================================
46
+
47
+ import glfw;
48
+ import std.c.stdio;
49
+
50
+ struct signal_t {
51
+ GLFWcond cond;
52
+ GLFWmutex mutex;
53
+ int flag;
54
+ }
55
+
56
+ signal_t gotoA, gotoB;
57
+
58
+ GLFWcond threadDone;
59
+ GLFWmutex doneMutex;
60
+ int doneCount = 0;
61
+ int gotoACount = 0;
62
+ int gotoBCount = 0;
63
+
64
+ const int MAX_COUNT = 10000;
65
+
66
+
67
+ //------------------------------------------------------------------------
68
+ // InitSignal()
69
+ //------------------------------------------------------------------------
70
+
71
+ void InitSignal( signal_t *s )
72
+ {
73
+ s.cond = glfwCreateCond();
74
+ s.mutex = glfwCreateMutex();
75
+ s.flag = 0;
76
+ }
77
+
78
+
79
+ //------------------------------------------------------------------------
80
+ // KillSignal()
81
+ //------------------------------------------------------------------------
82
+
83
+ void KillSignal( signal_t *s )
84
+ {
85
+ glfwDestroyCond( s.cond );
86
+ glfwDestroyMutex( s.mutex );
87
+ s.flag = 0;
88
+ }
89
+
90
+
91
+ //------------------------------------------------------------------------
92
+ // WaitSignal()
93
+ //------------------------------------------------------------------------
94
+
95
+ void WaitSignal( signal_t *s )
96
+ {
97
+ glfwLockMutex( s.mutex );
98
+ while( !s.flag )
99
+ {
100
+ glfwWaitCond( s.cond, s.mutex, GLFW_INFINITY );
101
+ }
102
+ s.flag = 0;
103
+ glfwUnlockMutex( s.mutex );
104
+ }
105
+
106
+
107
+ //------------------------------------------------------------------------
108
+ // SetSignal()
109
+ //------------------------------------------------------------------------
110
+
111
+ void SetSignal( signal_t *s )
112
+ {
113
+ glfwLockMutex( s.mutex );
114
+ s.flag = 1;
115
+ glfwUnlockMutex( s.mutex );
116
+ glfwSignalCond( s.cond );
117
+ }
118
+
119
+
120
+ //------------------------------------------------------------------------
121
+ // main()
122
+ //------------------------------------------------------------------------
123
+
124
+ int main( )
125
+ {
126
+ GLFWthread threadA, threadB;
127
+ double t1, t2, csps;
128
+ int done, count, i;
129
+
130
+ gotoACount = gotoBCount = doneCount = 0;
131
+
132
+ // Initialize GLFW
133
+ if( !glfwInit() )
134
+ {
135
+ return 0;
136
+ }
137
+
138
+ // Print some program information
139
+ printf( "\nMultithreading benchmarking program\n" );
140
+ printf( "-----------------------------------\n\n" );
141
+ printf( "This program consists of two tests. In the first test " );
142
+ printf( "two threads are created,\n" );
143
+ printf( "which continously signal/wait each other. This forces " );
144
+ printf( "the execution to\n" );
145
+ printf( "alternate between the two threads, and gives a measure " );
146
+ printf( "of the thread\n" );
147
+ printf( "synchronization granularity. In the second test, the " );
148
+ printf( "main thread is repeatedly\n" );
149
+ printf( "put to sleep for a very short interval using glfwSleep. " );
150
+ printf( "The average sleep time\n" );
151
+ printf( "is measured, which tells the minimum supported sleep " );
152
+ printf( "interval.\n\n" );
153
+ printf( "Results:\n" );
154
+ printf( "--------\n\n" );
155
+ printf( "Number of CPUs: %d\n\n", glfwGetNumberOfProcessors() );
156
+ fflush( stdout );
157
+
158
+
159
+ //------------------------------------------------------------------------
160
+ // 1) Benchmark thread synchronization granularity
161
+ //------------------------------------------------------------------------
162
+
163
+ // Init mutexes and conditions
164
+ doneMutex = glfwCreateMutex();
165
+ threadDone = glfwCreateCond();
166
+ InitSignal( &gotoA );
167
+ InitSignal( &gotoB );
168
+
169
+ // Create threads A & B
170
+ threadA = glfwCreateThread( &threadAfun, null );
171
+ threadB = glfwCreateThread( &threadBfun, null );
172
+ if( threadA == -1 || threadB == -1 )
173
+ {
174
+ glfwLockMutex( doneMutex );
175
+ doneCount = 2;
176
+ glfwUnlockMutex( doneMutex );
177
+ }
178
+
179
+ // Wait for both threads to be done
180
+ t1 = glfwGetTime();
181
+ glfwLockMutex( doneMutex );
182
+ do
183
+ {
184
+ done = (doneCount == 2);
185
+ if( !done )
186
+ {
187
+ glfwWaitCond( threadDone, doneMutex, GLFW_INFINITY );
188
+ }
189
+ }
190
+ while( !done );
191
+ glfwUnlockMutex( doneMutex );
192
+ t2 = glfwGetTime();
193
+
194
+ // Display results
195
+ count = gotoACount + gotoBCount;
196
+ csps = cast(double)count / (t2-t1);
197
+ printf( "Test 1: %.0f context switches / second (%.3f us/switch)\n",
198
+ csps, 1e6/csps );
199
+ fflush( stdout );
200
+
201
+ // Wait for threads to die
202
+ glfwWaitThread( threadA, GLFW_WAIT );
203
+ glfwWaitThread( threadB, GLFW_WAIT );
204
+
205
+ // Destroy mutexes and conditions
206
+ glfwDestroyMutex( doneMutex );
207
+ glfwDestroyCond( threadDone );
208
+ KillSignal( &gotoA );
209
+ KillSignal( &gotoB );
210
+
211
+
212
+ //------------------------------------------------------------------------
213
+ // 2) Benchmark thread sleep granularity
214
+ //------------------------------------------------------------------------
215
+
216
+ // Find an initial estimate
217
+ t1 = glfwGetTime();
218
+ for( i = 0; i < 10; i ++ )
219
+ {
220
+ glfwSleep( 0.0001 );
221
+ }
222
+ t2 = glfwGetTime();
223
+
224
+ // Sleep for roughly 1 s
225
+ count = cast(int)(1.0 / ((t2-t1)/10.0));
226
+ t1 = glfwGetTime();
227
+ for( i = 0; i < count; i ++ )
228
+ {
229
+ glfwSleep( 0.0001 );
230
+ }
231
+ t2 = glfwGetTime();
232
+
233
+ // Display results
234
+ printf( "Test 2: %.3f ms / sleep (mean)\n\n",
235
+ 1000.0 * (t2-t1) / cast(double)count );
236
+
237
+ // Terminate GLFW
238
+ glfwTerminate();
239
+
240
+ return 0;
241
+ }
242
+
243
+
244
+ version(Windows) {
245
+ extern(Windows):
246
+ } else {
247
+ extern(C):
248
+ }
249
+ //------------------------------------------------------------------------
250
+ // threadAfun()
251
+ //------------------------------------------------------------------------
252
+
253
+ void threadAfun( void * arg )
254
+ {
255
+ int done;
256
+
257
+ do
258
+ {
259
+ done = (gotoACount >= MAX_COUNT);
260
+ if( !done )
261
+ {
262
+ gotoACount ++;
263
+ SetSignal( &gotoB );
264
+ WaitSignal( &gotoA );
265
+ }
266
+ }
267
+ while( !done );
268
+
269
+ SetSignal( &gotoB );
270
+
271
+ glfwLockMutex( doneMutex );
272
+ doneCount ++;
273
+ glfwUnlockMutex( doneMutex );
274
+ glfwSignalCond( threadDone );
275
+ }
276
+
277
+
278
+ //------------------------------------------------------------------------
279
+ // threadBfun()
280
+ //------------------------------------------------------------------------
281
+
282
+ void threadBfun( void * arg )
283
+ {
284
+ int done;
285
+
286
+ do
287
+ {
288
+ done = (gotoBCount >= MAX_COUNT);
289
+ if( !done )
290
+ {
291
+ gotoBCount ++;
292
+ SetSignal( &gotoA );
293
+ WaitSignal( &gotoB );
294
+ }
295
+ }
296
+ while( !done );
297
+
298
+ SetSignal( &gotoA );
299
+
300
+ glfwLockMutex( doneMutex );
301
+ doneCount ++;
302
+ glfwUnlockMutex( doneMutex );
303
+ glfwSignalCond( threadDone );
304
+ }