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,95 @@
1
+ //========================================================================
2
+ // GLFW - An OpenGL framework
3
+ // File: fullscreen.c
4
+ // Platform: Any
5
+ // API version: 2.6
6
+ // WWW: http://glfw.sourceforge.net
7
+ //------------------------------------------------------------------------
8
+ // Copyright (c) 2002-2006 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 "internal.h"
32
+
33
+
34
+ //************************************************************************
35
+ //**** GLFW user functions ****
36
+ //************************************************************************
37
+
38
+ //========================================================================
39
+ // glfwGetVideoModes() - Get a list of available video modes
40
+ //========================================================================
41
+
42
+ GLFWAPI int GLFWAPIENTRY glfwGetVideoModes( GLFWvidmode *list,
43
+ int maxcount )
44
+ {
45
+ int count, i, swap, res1, res2, depth1, depth2;
46
+ GLFWvidmode vm;
47
+
48
+ if( !_glfwInitialized || maxcount <= 0 || list == (GLFWvidmode*) 0 )
49
+ {
50
+ return 0;
51
+ }
52
+
53
+ // Get list of video modes
54
+ count = _glfwPlatformGetVideoModes( list, maxcount );
55
+
56
+ // Sort list (bubble sort)
57
+ do
58
+ {
59
+ swap = 0;
60
+ for( i = 0; i < count-1; ++ i )
61
+ {
62
+ res1 = list[i].Width*list[i].Height;
63
+ depth1 = list[i].RedBits+list[i].GreenBits+list[i].BlueBits;
64
+ res2 = list[i+1].Width*list[i+1].Height;
65
+ depth2 = list[i+1].RedBits+list[i+1].GreenBits+
66
+ list[i+1].BlueBits;
67
+ if( (depth2<depth1) || ((depth2==depth1) && (res2<res1)) )
68
+ {
69
+ vm = list[i];
70
+ list[i] = list[i+1];
71
+ list[i+1] = vm;
72
+ swap = 1;
73
+ }
74
+ }
75
+ }
76
+ while( swap );
77
+
78
+ return count;
79
+ }
80
+
81
+
82
+ //========================================================================
83
+ // glfwGetDesktopMode() - Get the desktop video mode
84
+ //========================================================================
85
+
86
+ GLFWAPI void GLFWAPIENTRY glfwGetDesktopMode( GLFWvidmode *mode )
87
+ {
88
+ if( !_glfwInitialized || mode == (GLFWvidmode*) 0 )
89
+ {
90
+ return;
91
+ }
92
+
93
+ _glfwPlatformGetDesktopMode( mode );
94
+ }
95
+
@@ -0,0 +1,201 @@
1
+ //========================================================================
2
+ // GLFW - An OpenGL framework
3
+ // File: glext.c
4
+ // Platform: Any
5
+ // API version: 2.6
6
+ // WWW: http://glfw.sourceforge.net
7
+ //------------------------------------------------------------------------
8
+ // Copyright (c) 2002-2006 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 "internal.h"
32
+
33
+
34
+ //************************************************************************
35
+ //**** GLFW internal functions ****
36
+ //************************************************************************
37
+
38
+ //========================================================================
39
+ // _glfwStringInExtensionString() - Check if a string can be found in an
40
+ // OpenGL extension string
41
+ //========================================================================
42
+
43
+ int _glfwStringInExtensionString( const char *string,
44
+ const GLubyte *extensions )
45
+ {
46
+ const GLubyte *start;
47
+ GLubyte *where, *terminator;
48
+
49
+ // It takes a bit of care to be fool-proof about parsing the
50
+ // OpenGL extensions string. Don't be fooled by sub-strings,
51
+ // etc.
52
+ start = extensions;
53
+ while( 1 )
54
+ {
55
+ where = (GLubyte *) strstr( (const char *) start, string );
56
+ if( !where )
57
+ {
58
+ return GL_FALSE;
59
+ }
60
+ terminator = where + strlen( string );
61
+ if( where == start || *(where - 1) == ' ' )
62
+ {
63
+ if( *terminator == ' ' || *terminator == '\0' )
64
+ {
65
+ break;
66
+ }
67
+ }
68
+ start = terminator;
69
+ }
70
+
71
+ return GL_TRUE;
72
+ }
73
+
74
+
75
+
76
+ //************************************************************************
77
+ //**** GLFW user functions ****
78
+ //************************************************************************
79
+
80
+ //========================================================================
81
+ // glfwExtensionSupported() - Check if an OpenGL extension is available
82
+ // at runtime
83
+ //========================================================================
84
+
85
+ GLFWAPI int GLFWAPIENTRY glfwExtensionSupported( const char *extension )
86
+ {
87
+ const GLubyte *extensions;
88
+ GLubyte *where;
89
+
90
+ // Is GLFW initialized?
91
+ if( !_glfwInitialized || !_glfwWin.Opened )
92
+ {
93
+ return GL_FALSE;
94
+ }
95
+
96
+ // Extension names should not have spaces
97
+ where = (GLubyte *) strchr( extension, ' ' );
98
+ if( where || *extension == '\0' )
99
+ {
100
+ return GL_FALSE;
101
+ }
102
+
103
+ // Check if extension is in the standard OpenGL extensions string
104
+ extensions = (GLubyte *) glGetString( GL_EXTENSIONS );
105
+ if( extensions != NULL )
106
+ {
107
+ if( _glfwStringInExtensionString( extension, extensions ) )
108
+ {
109
+ return GL_TRUE;
110
+ }
111
+ }
112
+
113
+ // Additional platform specific extension checking (e.g. WGL)
114
+ if( _glfwPlatformExtensionSupported( extension ) )
115
+ {
116
+ return GL_TRUE;
117
+ }
118
+
119
+ return GL_FALSE;
120
+ }
121
+
122
+
123
+ //========================================================================
124
+ // glfwGetProcAddress() - Get the function pointer to an OpenGL function.
125
+ // This function can be used to get access to extended OpenGL functions.
126
+ //========================================================================
127
+
128
+ GLFWAPI void * GLFWAPIENTRY glfwGetProcAddress( const char *procname )
129
+ {
130
+ // Is GLFW initialized?
131
+ if( !_glfwInitialized || !_glfwWin.Opened )
132
+ {
133
+ return NULL;
134
+ }
135
+
136
+ return _glfwPlatformGetProcAddress( procname );
137
+ }
138
+
139
+
140
+ //========================================================================
141
+ // glfwGetGLVersion() - Get OpenGL version
142
+ //========================================================================
143
+
144
+ GLFWAPI void GLFWAPIENTRY glfwGetGLVersion( int *major, int *minor,
145
+ int *rev )
146
+ {
147
+ GLuint _major, _minor = 0, _rev = 0;
148
+ const GLubyte *version;
149
+ GLubyte *ptr;
150
+
151
+ // Is GLFW initialized?
152
+ if( !_glfwInitialized || !_glfwWin.Opened )
153
+ {
154
+ return;
155
+ }
156
+
157
+ // Get OpenGL version string
158
+ version = glGetString( GL_VERSION );
159
+ if( !version )
160
+ {
161
+ return;
162
+ }
163
+
164
+ // Parse string
165
+ ptr = (GLubyte*) version;
166
+ for( _major = 0; *ptr >= '0' && *ptr <= '9'; ptr ++ )
167
+ {
168
+ _major = 10*_major + (*ptr - '0');
169
+ }
170
+ if( *ptr == '.' )
171
+ {
172
+ ptr ++;
173
+ for( _minor = 0; *ptr >= '0' && *ptr <= '9'; ptr ++ )
174
+ {
175
+ _minor = 10*_minor + (*ptr - '0');
176
+ }
177
+ if( *ptr == '.' )
178
+ {
179
+ ptr ++;
180
+ for( _rev = 0; *ptr >= '0' && *ptr <= '9'; ptr ++ )
181
+ {
182
+ _rev = 10*_rev + (*ptr - '0');
183
+ }
184
+ }
185
+ }
186
+
187
+ // Return parsed values
188
+ if( major != NULL )
189
+ {
190
+ *major = _major;
191
+ }
192
+ if( minor != NULL )
193
+ {
194
+ *minor = _minor;
195
+ }
196
+ if( rev != NULL )
197
+ {
198
+ *rev = _rev;
199
+ }
200
+ }
201
+
@@ -0,0 +1,629 @@
1
+ //========================================================================
2
+ // GLFW - An OpenGL framework
3
+ // File: image.c
4
+ // Platform: Any
5
+ // API version: 2.6
6
+ // WWW: http://glfw.sourceforge.net
7
+ //------------------------------------------------------------------------
8
+ // Copyright (c) 2002-2006 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
+ //========================================================================
32
+ // Description:
33
+ //
34
+ // This module acts as an interface for different image file formats (the
35
+ // image file format is detected automatically).
36
+ //
37
+ // By default the loaded image is rescaled (using bilinear interpolation)
38
+ // to the next higher 2^N x 2^M resolution, unless it has a valid
39
+ // 2^N x 2^M resolution. The interpolation is quite slow, even if the
40
+ // routine has been optimized for speed (a 200x200 RGB image is scaled to
41
+ // 256x256 in ~30 ms on a P3-500).
42
+ //
43
+ // Paletted images are converted to RGB/RGBA images.
44
+ //
45
+ // A convenience function is also included (glfwLoadTexture2D), which
46
+ // loads a texture image from a file directly to OpenGL texture memory,
47
+ // with an option to generate all mipmap levels. GL_SGIS_generate_mipmap
48
+ // is used whenever available, which should give an optimal mipmap
49
+ // generation speed (possibly performed in hardware). A software fallback
50
+ // method is included when GL_SGIS_generate_mipmap is not supported (it
51
+ // generates all mipmaps of a 256x256 RGB texture in ~3 ms on a P3-500).
52
+ //
53
+ //========================================================================
54
+
55
+
56
+ #include "internal.h"
57
+
58
+
59
+ // We want to support automatic mipmap generation
60
+ #ifndef GL_SGIS_generate_mipmap
61
+ #define GL_GENERATE_MIPMAP_SGIS 0x8191
62
+ #define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192
63
+ #define GL_SGIS_generate_mipmap 1
64
+ #endif // GL_SGIS_generate_mipmap
65
+
66
+
67
+ //************************************************************************
68
+ //**** GLFW internal functions ****
69
+ //************************************************************************
70
+
71
+ //========================================================================
72
+ // _glfwUpsampleImage() - Upsample image, from size w1 x h1 to w2 x h2
73
+ //========================================================================
74
+
75
+ static void _glfwUpsampleImage( unsigned char *src, unsigned char *dst,
76
+ int w1, int h1, int w2, int h2, int bpp )
77
+ {
78
+ int m, n, k, x, y, col8;
79
+ float dx, dy, xstep, ystep, col, col1, col2;
80
+ unsigned char *src1, *src2, *src3, *src4;
81
+
82
+ // Calculate scaling factor
83
+ xstep = (float)(w1-1) / (float)(w2-1);
84
+ ystep = (float)(h1-1) / (float)(h2-1);
85
+
86
+ // Copy source data to destination data with bilinear interpolation
87
+ // Note: The rather strange look of this routine is a direct result of
88
+ // my attempts at optimizing it. Improvements are welcome!
89
+ dy = 0.0f;
90
+ y = 0;
91
+ for( n = 0; n < h2; n ++ )
92
+ {
93
+ dx = 0.0f;
94
+ src1 = &src[ y*w1*bpp ];
95
+ src3 = y < (h1-1) ? src1 + w1*bpp : src1;
96
+ src2 = src1 + bpp;
97
+ src4 = src3 + bpp;
98
+ x = 0;
99
+ for( m = 0; m < w2; m ++ )
100
+ {
101
+ for( k = 0; k < bpp; k ++ )
102
+ {
103
+ col1 = *src1 ++;
104
+ col2 = *src2 ++;
105
+ col = col1 + (col2 - col1) * dx;
106
+ col1 = *src3 ++;
107
+ col2 = *src4 ++;
108
+ col2 = col1 + (col2 - col1) * dx;
109
+ col += (col2 - col) * dy;
110
+ col8 = (int) (col + 0.5);
111
+ if( col8 >= 256 ) col8 = 255;
112
+ *dst++ = (unsigned char) col8;
113
+ }
114
+ dx += xstep;
115
+ if( dx >= 1.0f )
116
+ {
117
+ x ++;
118
+ dx -= 1.0f;
119
+ if( x >= (w1-1) )
120
+ {
121
+ src2 = src1;
122
+ src4 = src3;
123
+ }
124
+ }
125
+ else
126
+ {
127
+ src1 -= bpp;
128
+ src2 -= bpp;
129
+ src3 -= bpp;
130
+ src4 -= bpp;
131
+ }
132
+ }
133
+ dy += ystep;
134
+ if( dy >= 1.0f )
135
+ {
136
+ y ++;
137
+ dy -= 1.0f;
138
+ }
139
+ }
140
+ }
141
+
142
+
143
+ //========================================================================
144
+ // _glfwHalveImage() - Build the next mip-map level
145
+ //========================================================================
146
+
147
+ static int _glfwHalveImage( GLubyte *src, int *width, int *height,
148
+ int components )
149
+ {
150
+ int halfwidth, halfheight, m, n, k, idx1, idx2;
151
+ GLubyte *dst;
152
+
153
+ // Last level?
154
+ if( *width <= 1 && *height <= 1 )
155
+ {
156
+ return GL_FALSE;
157
+ }
158
+
159
+ // Calculate new width and height (handle 1D case)
160
+ halfwidth = *width > 1 ? *width / 2 : 1;
161
+ halfheight = *height > 1 ? *height / 2 : 1;
162
+
163
+ // Downsample image with a simple box-filter
164
+ dst = src;
165
+ if( *width == 1 || *height == 1 )
166
+ {
167
+ // 1D case
168
+ for( m = 0; m < halfwidth+halfheight-1; m ++ )
169
+ {
170
+ for( k = 0; k < components; k ++ )
171
+ {
172
+ *dst ++ = (GLubyte) (((int)*src +
173
+ (int)src[components] + 1) >> 1);
174
+ src ++;
175
+ }
176
+ src += components;
177
+ }
178
+ }
179
+ else
180
+ {
181
+ // 2D case
182
+ idx1 = *width*components;
183
+ idx2 = (*width+1)*components;
184
+ for( m = 0; m < halfheight; m ++ )
185
+ {
186
+ for( n = 0; n < halfwidth; n ++ )
187
+ {
188
+ for( k = 0; k < components; k ++ )
189
+ {
190
+ *dst ++ = (GLubyte) (((int)*src +
191
+ (int)src[components] +
192
+ (int)src[idx1] +
193
+ (int)src[idx2] + 2) >> 2);
194
+ src ++;
195
+ }
196
+ src += components;
197
+ }
198
+ src += components * (*width);
199
+ }
200
+ }
201
+
202
+ // Return new width and height
203
+ *width = halfwidth;
204
+ *height = halfheight;
205
+
206
+ return GL_TRUE;
207
+ }
208
+
209
+
210
+ //========================================================================
211
+ // _glfwRescaleImage() - Rescales an image into power-of-two dimensions
212
+ //========================================================================
213
+
214
+ static int _glfwRescaleImage( GLFWimage* image )
215
+ {
216
+ int width, height, log2, newsize;
217
+ unsigned char *data;
218
+
219
+ // Calculate next larger 2^N width
220
+ for( log2 = 0, width = image->Width; width > 1; width >>= 1, log2 ++ )
221
+ ;
222
+ width = (int) 1 << log2;
223
+ if( width < image->Width )
224
+ {
225
+ width <<= 1;
226
+ }
227
+
228
+ // Calculate next larger 2^M height
229
+ for( log2 = 0, height = image->Height; height > 1; height >>= 1, log2 ++ )
230
+ ;
231
+ height = (int) 1 << log2;
232
+ if( height < image->Height )
233
+ {
234
+ height <<= 1;
235
+ }
236
+
237
+ // Do we really need to rescale?
238
+ if( width != image->Width || height != image->Height )
239
+ {
240
+ // Allocate memory for new (upsampled) image data
241
+ newsize = width * height * image->BytesPerPixel;
242
+ data = (unsigned char *) malloc( newsize );
243
+ if( data == NULL )
244
+ {
245
+ free( image->Data );
246
+ return GL_FALSE;
247
+ }
248
+
249
+ // Copy old image data to new image data with interpolation
250
+ _glfwUpsampleImage( image->Data, data, image->Width, image->Height,
251
+ width, height, image->BytesPerPixel );
252
+
253
+ // Free memory for old image data (not needed anymore)
254
+ free( image->Data );
255
+
256
+ // Set pointer to new image data, and set new image dimensions
257
+ image->Data = data;
258
+ image->Width = width;
259
+ image->Height = height;
260
+ }
261
+
262
+ return GL_TRUE;
263
+ }
264
+
265
+
266
+ //************************************************************************
267
+ //**** GLFW user functions ****
268
+ //************************************************************************
269
+
270
+ //========================================================================
271
+ // glfwReadImage() - Read an image from a named file
272
+ //========================================================================
273
+
274
+ GLFWAPI int GLFWAPIENTRY glfwReadImage( const char *name, GLFWimage *img,
275
+ int flags )
276
+ {
277
+ _GLFWstream stream;
278
+
279
+ // Is GLFW initialized?
280
+ if( !_glfwInitialized )
281
+ {
282
+ return GL_FALSE;
283
+ }
284
+
285
+ // Start with an empty image descriptor
286
+ img->Width = 0;
287
+ img->Height = 0;
288
+ img->BytesPerPixel = 0;
289
+ img->Data = NULL;
290
+
291
+ // Open file
292
+ if( !_glfwOpenFileStream( &stream, name, "rb" ) )
293
+ {
294
+ return GL_FALSE;
295
+ }
296
+
297
+ // We only support TGA files at the moment
298
+ if( !_glfwReadTGA( &stream, img, flags ) )
299
+ {
300
+ _glfwCloseStream( &stream );
301
+ return GL_FALSE;
302
+ }
303
+
304
+ // Close stream
305
+ _glfwCloseStream( &stream );
306
+
307
+ // Should we rescale the image to closest 2^N x 2^M resolution?
308
+ if( !(flags & GLFW_NO_RESCALE_BIT) )
309
+ {
310
+ if( !_glfwRescaleImage( img ) )
311
+ {
312
+ return GL_FALSE;
313
+ }
314
+ }
315
+
316
+ // Interpret BytesPerPixel as an OpenGL format
317
+ switch( img->BytesPerPixel )
318
+ {
319
+ default:
320
+ case 1:
321
+ if( flags & GLFW_ALPHA_MAP_BIT )
322
+ {
323
+ img->Format = GL_ALPHA;
324
+ }
325
+ else
326
+ {
327
+ img->Format = GL_LUMINANCE;
328
+ }
329
+ break;
330
+ case 3:
331
+ img->Format = GL_RGB;
332
+ break;
333
+ case 4:
334
+ img->Format = GL_RGBA;
335
+ break;
336
+ }
337
+
338
+ return GL_TRUE;
339
+ }
340
+
341
+
342
+ //========================================================================
343
+ // glfwReadMemoryImage() - Read an image file from a memory buffer
344
+ //========================================================================
345
+
346
+ GLFWAPI int GLFWAPIENTRY glfwReadMemoryImage( const void *data, long size, GLFWimage *img, int flags )
347
+ {
348
+ _GLFWstream stream;
349
+
350
+ // Is GLFW initialized?
351
+ if( !_glfwInitialized )
352
+ {
353
+ return GL_FALSE;
354
+ }
355
+
356
+ // Start with an empty image descriptor
357
+ img->Width = 0;
358
+ img->Height = 0;
359
+ img->BytesPerPixel = 0;
360
+ img->Data = NULL;
361
+
362
+ // Open buffer
363
+ if( !_glfwOpenBufferStream( &stream, (void*) data, size ) )
364
+ {
365
+ return GL_FALSE;
366
+ }
367
+
368
+ // We only support TGA files at the moment
369
+ if( !_glfwReadTGA( &stream, img, flags ) )
370
+ {
371
+ _glfwCloseStream( &stream );
372
+ return GL_FALSE;
373
+ }
374
+
375
+ // Close stream
376
+ _glfwCloseStream( &stream );
377
+
378
+ // Should we rescale the image to closest 2^N x 2^M resolution?
379
+ if( !(flags & GLFW_NO_RESCALE_BIT) )
380
+ {
381
+ if( !_glfwRescaleImage( img ) )
382
+ {
383
+ return GL_FALSE;
384
+ }
385
+ }
386
+
387
+ // Interpret BytesPerPixel as an OpenGL format
388
+ switch( img->BytesPerPixel )
389
+ {
390
+ default:
391
+ case 1:
392
+ if( flags & GLFW_ALPHA_MAP_BIT )
393
+ {
394
+ img->Format = GL_ALPHA;
395
+ }
396
+ else
397
+ {
398
+ img->Format = GL_LUMINANCE;
399
+ }
400
+ break;
401
+ case 3:
402
+ img->Format = GL_RGB;
403
+ break;
404
+ case 4:
405
+ img->Format = GL_RGBA;
406
+ break;
407
+ }
408
+
409
+ return GL_TRUE;
410
+ }
411
+
412
+
413
+ //========================================================================
414
+ // glfwFreeImage() - Free allocated memory for an image
415
+ //========================================================================
416
+
417
+ GLFWAPI void GLFWAPIENTRY glfwFreeImage( GLFWimage *img )
418
+ {
419
+ // Is GLFW initialized?
420
+ if( !_glfwInitialized )
421
+ {
422
+ return;
423
+ }
424
+
425
+ // Free memory
426
+ if( img->Data != NULL )
427
+ {
428
+ free( img->Data );
429
+ img->Data = NULL;
430
+ }
431
+
432
+ // Clear all fields
433
+ img->Width = 0;
434
+ img->Height = 0;
435
+ img->Format = 0;
436
+ img->BytesPerPixel = 0;
437
+ }
438
+
439
+
440
+ //========================================================================
441
+ // glfwLoadTexture2D() - Read an image from a file, and upload it to
442
+ // texture memory
443
+ //========================================================================
444
+
445
+ GLFWAPI int GLFWAPIENTRY glfwLoadTexture2D( const char *name, int flags )
446
+ {
447
+ GLFWimage img;
448
+
449
+ // Is GLFW initialized?
450
+ if( !_glfwInitialized || !_glfwWin.Opened )
451
+ {
452
+ return GL_FALSE;
453
+ }
454
+
455
+ // Force rescaling if necessary
456
+ if( !_glfwWin.Has_GL_ARB_texture_non_power_of_two )
457
+ {
458
+ flags &= (~GLFW_NO_RESCALE_BIT);
459
+ }
460
+
461
+ // Read image from file
462
+ if( !glfwReadImage( name, &img, flags ) )
463
+ {
464
+ return GL_FALSE;
465
+ }
466
+
467
+ if( !glfwLoadTextureImage2D( &img, flags ) )
468
+ {
469
+ return GL_FALSE;
470
+ }
471
+
472
+ // Data buffer is not needed anymore
473
+ glfwFreeImage( &img );
474
+
475
+ return GL_TRUE;
476
+ }
477
+
478
+
479
+ //========================================================================
480
+ // glfwLoadMemoryTexture2D() - Read an image from a buffer, and upload it to
481
+ // texture memory
482
+ //========================================================================
483
+
484
+ GLFWAPI int GLFWAPIENTRY glfwLoadMemoryTexture2D( const void *data, long size, int flags )
485
+ {
486
+ GLFWimage img;
487
+
488
+ // Is GLFW initialized?
489
+ if( !_glfwInitialized || !_glfwWin.Opened )
490
+ {
491
+ return GL_FALSE;
492
+ }
493
+
494
+ // Force rescaling if necessary
495
+ if( !_glfwWin.Has_GL_ARB_texture_non_power_of_two )
496
+ {
497
+ flags &= (~GLFW_NO_RESCALE_BIT);
498
+ }
499
+
500
+ // Read image from file
501
+ if( !glfwReadMemoryImage( data, size, &img, flags ) )
502
+ {
503
+ return GL_FALSE;
504
+ }
505
+
506
+ if( !glfwLoadTextureImage2D( &img, flags ) )
507
+ {
508
+ return GL_FALSE;
509
+ }
510
+
511
+ // Data buffer is not needed anymore
512
+ glfwFreeImage( &img );
513
+
514
+ return GL_TRUE;
515
+ }
516
+
517
+
518
+ //========================================================================
519
+ // glfwLoadTextureImage2D() - Upload an image object to texture memory
520
+ //========================================================================
521
+
522
+ GLFWAPI int GLFWAPIENTRY glfwLoadTextureImage2D( GLFWimage *img, int flags )
523
+ {
524
+ GLint UnpackAlignment, GenMipMap;
525
+ int level, format, AutoGen, newsize, n;
526
+ unsigned char *data, *dataptr;
527
+
528
+ // Is GLFW initialized?
529
+ if( !_glfwInitialized || !_glfwWin.Opened )
530
+ {
531
+ return GL_FALSE;
532
+ }
533
+
534
+ // TODO: Use GL_MAX_TEXTURE_SIZE or GL_PROXY_TEXTURE_2D to determine
535
+ // whether the image size is valid.
536
+ // NOTE: May require box filter downsampling routine.
537
+
538
+ // Do we need to convert the alpha map to RGBA format (OpenGL 1.0)?
539
+ if( (_glfwWin.GLVerMajor == 1) && (_glfwWin.GLVerMinor == 0) &&
540
+ (img->Format == GL_ALPHA) )
541
+ {
542
+ // We go to RGBA representation instead
543
+ img->BytesPerPixel = 4;
544
+
545
+ // Allocate memory for new RGBA image data
546
+ newsize = img->Width * img->Height * img->BytesPerPixel;
547
+ data = (unsigned char *) malloc( newsize );
548
+ if( data == NULL )
549
+ {
550
+ free( img->Data );
551
+ return GL_FALSE;
552
+ }
553
+
554
+ // Convert Alpha map to RGBA
555
+ dataptr = data;
556
+ for( n = 0; n < (img->Width*img->Height); ++ n )
557
+ {
558
+ *dataptr ++ = 255;
559
+ *dataptr ++ = 255;
560
+ *dataptr ++ = 255;
561
+ *dataptr ++ = img->Data[n];
562
+ }
563
+
564
+ // Free memory for old image data (not needed anymore)
565
+ free( img->Data );
566
+
567
+ // Set pointer to new image data
568
+ img->Data = data;
569
+ }
570
+
571
+ // Set unpack alignment to one byte
572
+ glGetIntegerv( GL_UNPACK_ALIGNMENT, &UnpackAlignment );
573
+ glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
574
+
575
+ // Should we use automatic mipmap generation?
576
+ AutoGen = ( flags & GLFW_BUILD_MIPMAPS_BIT ) &&
577
+ _glfwWin.Has_GL_SGIS_generate_mipmap;
578
+
579
+ // Enable automatic mipmap generation
580
+ if( AutoGen )
581
+ {
582
+ glGetTexParameteriv( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
583
+ &GenMipMap );
584
+ glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
585
+ GL_TRUE );
586
+ }
587
+
588
+ // Format specification is different for OpenGL 1.0
589
+ if( _glfwWin.GLVerMajor == 1 && _glfwWin.GLVerMinor == 0 )
590
+ {
591
+ format = img->BytesPerPixel;
592
+ }
593
+ else
594
+ {
595
+ format = img->Format;
596
+ }
597
+
598
+ // Upload to texture memeory
599
+ level = 0;
600
+ do
601
+ {
602
+ // Upload this mipmap level
603
+ glTexImage2D( GL_TEXTURE_2D, level, format,
604
+ img->Width, img->Height, 0, format,
605
+ GL_UNSIGNED_BYTE, (void*) img->Data );
606
+
607
+ // Build next mipmap level manually, if required
608
+ if( ( flags & GLFW_BUILD_MIPMAPS_BIT ) && !AutoGen )
609
+ {
610
+ level = _glfwHalveImage( img->Data, &img->Width,
611
+ &img->Height, img->BytesPerPixel ) ?
612
+ level + 1 : 0;
613
+ }
614
+ }
615
+ while( level != 0 );
616
+
617
+ // Restore old automatic mipmap generation state
618
+ if( AutoGen )
619
+ {
620
+ glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,
621
+ GenMipMap );
622
+ }
623
+
624
+ // Restore old unpack alignment
625
+ glPixelStorei( GL_UNPACK_ALIGNMENT, UnpackAlignment );
626
+
627
+ return GL_TRUE;
628
+ }
629
+