ruby-glfw 0.9

Sign up to get free protection for your applications and to get access to all the features.
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,839 @@
1
+ //========================================================================
2
+ // This is a small test application for GLFW.
3
+ // This is an OpenGL port of the famous "PONG" game (the first computer
4
+ // game ever?). It is very simple, and could be improved alot. It was
5
+ // created in order to show off the gaming capabilities of GLFW.
6
+ //========================================================================
7
+
8
+ #include <GL/glfw.h>
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <math.h>
12
+
13
+
14
+ //========================================================================
15
+ // Constants
16
+ //========================================================================
17
+
18
+ // Screen resolution
19
+ #define WIDTH 640
20
+ #define HEIGHT 480
21
+
22
+ // Player size (units)
23
+ #define PLAYER_XSIZE 0.05
24
+ #define PLAYER_YSIZE 0.15
25
+
26
+ // Ball size (units)
27
+ #define BALL_SIZE 0.02
28
+
29
+ // Maximum player movement speed (units / second)
30
+ #define MAX_SPEED 1.5
31
+
32
+ // Player movement acceleration (units / seconds^2)
33
+ #define ACCELERATION 4.0
34
+
35
+ // Player movement deceleration (units / seconds^2)
36
+ #define DECELERATION 2.0
37
+
38
+ // Ball movement speed (units / second)
39
+ #define BALL_SPEED 0.4
40
+
41
+ // Menu options
42
+ #define MENU_NONE 0
43
+ #define MENU_PLAY 1
44
+ #define MENU_QUIT 2
45
+
46
+ // Game events
47
+ #define NOBODY_WINS 0
48
+ #define PLAYER1_WINS 1
49
+ #define PLAYER2_WINS 2
50
+
51
+ // Winner ID
52
+ #define NOBODY 0
53
+ #define PLAYER1 1
54
+ #define PLAYER2 2
55
+
56
+ // Camera positions
57
+ #define CAMERA_CLASSIC 0
58
+ #define CAMERA_ABOVE 1
59
+ #define CAMERA_SPECTATOR 2
60
+ #define CAMERA_DEFAULT CAMERA_CLASSIC
61
+
62
+
63
+ //========================================================================
64
+ // Textures
65
+ //========================================================================
66
+
67
+ #define TEX_TITLE 0
68
+ #define TEX_MENU 1
69
+ #define TEX_INSTR 2
70
+ #define TEX_WINNER1 3
71
+ #define TEX_WINNER2 4
72
+ #define TEX_FIELD 5
73
+ #define NUM_TEXTURES 6
74
+
75
+ // Texture names
76
+ char * tex_name[ NUM_TEXTURES ] = {
77
+ "pong3d_title.tga",
78
+ "pong3d_menu.tga",
79
+ "pong3d_instr.tga",
80
+ "pong3d_winner1.tga",
81
+ "pong3d_winner2.tga",
82
+ "pong3d_field.tga"
83
+ };
84
+
85
+ // OpenGL texture object IDs
86
+ GLuint tex_id[ NUM_TEXTURES ];
87
+
88
+
89
+ //========================================================================
90
+ // Global variables
91
+ //========================================================================
92
+
93
+ // Display information
94
+ int width, height;
95
+
96
+ // Frame information
97
+ double thistime, oldtime, dt, starttime;
98
+
99
+ // Camera information
100
+ int camerapos;
101
+
102
+ // Player information
103
+ struct {
104
+ double ypos; // -1.0 to +1.0
105
+ double yspeed; // -MAX_SPEED to +MAX_SPEED
106
+ } player1, player2;
107
+
108
+ // Ball information
109
+ struct {
110
+ double xpos, ypos;
111
+ double xspeed, yspeed;
112
+ } ball;
113
+
114
+ // And the winner is...
115
+ int winner;
116
+
117
+ // Lighting configuration
118
+ const GLfloat env_ambient[4] = {1.0f,1.0f,1.0f,1.0f};
119
+ const GLfloat light1_position[4] = {-3.0f,3.0f,2.0f,1.0f};
120
+ const GLfloat light1_diffuse[4] = {1.0f,1.0f,1.0f,0.0f};
121
+ const GLfloat light1_ambient[4] = {0.0f,0.0f,0.0f,0.0f};
122
+
123
+ // Object material properties
124
+ const GLfloat player1_diffuse[4] = {1.0f,0.3f,0.3f,1.0f};
125
+ const GLfloat player1_ambient[4] = {0.3f,0.1f,0.0f,1.0f};
126
+ const GLfloat player2_diffuse[4] = {0.3f,1.0f,0.3f,1.0f};
127
+ const GLfloat player2_ambient[4] = {0.1f,0.3f,0.1f,1.0f};
128
+ const GLfloat ball_diffuse[4] = {1.0f,1.0f,0.5f,1.0f};
129
+ const GLfloat ball_ambient[4] = {0.3f,0.3f,0.1f,1.0f};
130
+ const GLfloat border_diffuse[4] = {0.3f,0.3f,1.0f,1.0f};
131
+ const GLfloat border_ambient[4] = {0.1f,0.1f,0.3f,1.0f};
132
+ const GLfloat floor_diffuse[4] = {1.0f,1.0f,1.0f,1.0f};
133
+ const GLfloat floor_ambient[4] = {0.3f,0.3f,0.3f,1.0f};
134
+
135
+
136
+ //========================================================================
137
+ // LoadTextures() - Load textures from disk and upload to OpenGL card
138
+ //========================================================================
139
+
140
+ void LoadTextures( void )
141
+ {
142
+ int i;
143
+
144
+ // Generate texture objects
145
+ glGenTextures( NUM_TEXTURES, tex_id );
146
+
147
+ // Load textures
148
+ for( i = 0; i < NUM_TEXTURES; i ++ )
149
+ {
150
+ // Select texture object
151
+ glBindTexture( GL_TEXTURE_2D, tex_id[ i ] );
152
+
153
+ // Set texture parameters
154
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
155
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
156
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
157
+ glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
158
+
159
+ // Upload texture from file to texture memory
160
+ glfwLoadTexture2D( tex_name[ i ], 0 );
161
+ }
162
+ }
163
+
164
+
165
+ //========================================================================
166
+ // DrawImage() - Draw a 2D image as a texture
167
+ //========================================================================
168
+
169
+ void DrawImage( int texnum, float x1, float x2, float y1, float y2 )
170
+ {
171
+ glEnable( GL_TEXTURE_2D );
172
+ glBindTexture( GL_TEXTURE_2D, tex_id[ texnum ] );
173
+ glBegin( GL_QUADS );
174
+ glTexCoord2f( 0.0f, 1.0f );
175
+ glVertex2f( x1, y1 );
176
+ glTexCoord2f( 1.0f, 1.0f );
177
+ glVertex2f( x2, y1 );
178
+ glTexCoord2f( 1.0f, 0.0f );
179
+ glVertex2f( x2, y2 );
180
+ glTexCoord2f( 0.0f, 0.0f );
181
+ glVertex2f( x1, y2 );
182
+ glEnd();
183
+ glDisable( GL_TEXTURE_2D );
184
+ }
185
+
186
+
187
+ //========================================================================
188
+ // GameMenu() - Game menu (returns menu option)
189
+ //========================================================================
190
+
191
+ int GameMenu( void )
192
+ {
193
+ int option;
194
+
195
+ // Enable sticky keys
196
+ glfwEnable( GLFW_STICKY_KEYS );
197
+
198
+ // Wait for a game menu key to be pressed
199
+ do
200
+ {
201
+ // Get window size
202
+ glfwGetWindowSize( &width, &height );
203
+
204
+ // Set viewport
205
+ glViewport( 0, 0, width, height );
206
+
207
+ // Clear display
208
+ glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
209
+ glClear( GL_COLOR_BUFFER_BIT );
210
+
211
+ // Setup projection matrix
212
+ glMatrixMode( GL_PROJECTION );
213
+ glLoadIdentity();
214
+ glOrtho( 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f );
215
+
216
+ // Setup modelview matrix
217
+ glMatrixMode( GL_MODELVIEW );
218
+ glLoadIdentity();
219
+
220
+ // Display title
221
+ glColor3f( 1.0f, 1.0f, 1.0f );
222
+ DrawImage( TEX_TITLE, 0.1f, 0.9f, 0.0f, 0.3f );
223
+
224
+ // Display menu
225
+ glColor3f( 1.0f, 1.0f, 0.0f );
226
+ DrawImage( TEX_MENU, 0.38f, 0.62f, 0.35f, 0.5f );
227
+
228
+ // Display instructions
229
+ glColor3f( 0.0f, 1.0f, 1.0f );
230
+ DrawImage( TEX_INSTR, 0.32f, 0.68f, 0.65f, 0.85f );
231
+
232
+ // Swap buffers
233
+ glfwSwapBuffers();
234
+
235
+ // Check for keys
236
+ if( glfwGetKey( 'Q' ) || !glfwGetWindowParam( GLFW_OPENED ) )
237
+ {
238
+ option = MENU_QUIT;
239
+ }
240
+ else if( glfwGetKey( GLFW_KEY_F1 ) )
241
+ {
242
+ option = MENU_PLAY;
243
+ }
244
+ else
245
+ {
246
+ option = MENU_NONE;
247
+ }
248
+
249
+ // To avoid horrible busy waiting, sleep for at least 20 ms
250
+ glfwSleep( 0.02 );
251
+ }
252
+ while( option == MENU_NONE );
253
+
254
+ // Disable sticky keys
255
+ glfwDisable( GLFW_STICKY_KEYS );
256
+
257
+ return option;
258
+ }
259
+
260
+
261
+ //========================================================================
262
+ // NewGame() - Initialize a new game
263
+ //========================================================================
264
+
265
+ void NewGame( void )
266
+ {
267
+ // Frame information
268
+ starttime = thistime = glfwGetTime();
269
+
270
+ // Camera information
271
+ camerapos = CAMERA_DEFAULT;
272
+
273
+ // Player 1 information
274
+ player1.ypos = 0.0;
275
+ player1.yspeed = 0.0;
276
+
277
+ // Player 2 information
278
+ player2.ypos = 0.0;
279
+ player2.yspeed = 0.0;
280
+
281
+ // Ball information
282
+ ball.xpos = -1.0 + PLAYER_XSIZE;
283
+ ball.ypos = player1.ypos;
284
+ ball.xspeed = 1.0;
285
+ ball.yspeed = 1.0;
286
+ }
287
+
288
+
289
+ //========================================================================
290
+ // PlayerControl() - Player control
291
+ //========================================================================
292
+
293
+ void PlayerControl( void )
294
+ {
295
+ float joy1pos[ 2 ], joy2pos[ 2 ];
296
+
297
+ // Get joystick X & Y axis positions
298
+ glfwGetJoystickPos( GLFW_JOYSTICK_1, joy1pos, 2 );
299
+ glfwGetJoystickPos( GLFW_JOYSTICK_2, joy2pos, 2 );
300
+
301
+ // Player 1 control
302
+ if( glfwGetKey( 'A' ) || joy1pos[ 1 ] > 0.2f )
303
+ {
304
+ player1.yspeed += dt * ACCELERATION;
305
+ if( player1.yspeed > MAX_SPEED )
306
+ {
307
+ player1.yspeed = MAX_SPEED;
308
+ }
309
+ }
310
+ else if( glfwGetKey( 'Z' ) || joy1pos[ 1 ] < -0.2f )
311
+ {
312
+ player1.yspeed -= dt * ACCELERATION;
313
+ if( player1.yspeed < -MAX_SPEED )
314
+ {
315
+ player1.yspeed = -MAX_SPEED;
316
+ }
317
+ }
318
+ else
319
+ {
320
+ player1.yspeed /= exp( DECELERATION * dt );
321
+ }
322
+
323
+ // Player 2 control
324
+ if( glfwGetKey( 'K' ) || joy2pos[ 1 ] > 0.2f )
325
+ {
326
+ player2.yspeed += dt * ACCELERATION;
327
+ if( player2.yspeed > MAX_SPEED )
328
+ {
329
+ player2.yspeed = MAX_SPEED;
330
+ }
331
+ }
332
+ else if( glfwGetKey( 'M' ) || joy2pos[ 1 ] < -0.2f )
333
+ {
334
+ player2.yspeed -= dt * ACCELERATION;
335
+ if( player2.yspeed < -MAX_SPEED )
336
+ {
337
+ player2.yspeed = -MAX_SPEED;
338
+ }
339
+ }
340
+ else
341
+ {
342
+ player2.yspeed /= exp( DECELERATION * dt );
343
+ }
344
+
345
+ // Update player 1 position
346
+ player1.ypos += dt * player1.yspeed;
347
+ if( player1.ypos > 1.0 - PLAYER_YSIZE )
348
+ {
349
+ player1.ypos = 1.0 - PLAYER_YSIZE;
350
+ player1.yspeed = 0.0;
351
+ }
352
+ else if( player1.ypos < -1.0 + PLAYER_YSIZE )
353
+ {
354
+ player1.ypos = -1.0 + PLAYER_YSIZE;
355
+ player1.yspeed = 0.0;
356
+ }
357
+
358
+ // Update player 2 position
359
+ player2.ypos += dt * player2.yspeed;
360
+ if( player2.ypos > 1.0 - PLAYER_YSIZE )
361
+ {
362
+ player2.ypos = 1.0 - PLAYER_YSIZE;
363
+ player2.yspeed = 0.0;
364
+ }
365
+ else if( player2.ypos < -1.0 + PLAYER_YSIZE )
366
+ {
367
+ player2.ypos = -1.0 + PLAYER_YSIZE;
368
+ player2.yspeed = 0.0;
369
+ }
370
+ }
371
+
372
+
373
+ //========================================================================
374
+ // BallControl() - Ball control
375
+ //========================================================================
376
+
377
+ int BallControl( void )
378
+ {
379
+ int event;
380
+ double ballspeed;
381
+
382
+ // Calculate new ball speed
383
+ ballspeed = BALL_SPEED * (1.0 + 0.02*(thistime-starttime));
384
+ ball.xspeed = ball.xspeed > 0 ? ballspeed : -ballspeed;
385
+ ball.yspeed = ball.yspeed > 0 ? ballspeed : -ballspeed;
386
+ ball.yspeed *= 0.74321;
387
+
388
+ // Update ball position
389
+ ball.xpos += dt * ball.xspeed;
390
+ ball.ypos += dt * ball.yspeed;
391
+
392
+ // Did the ball hit a top/bottom wall?
393
+ if( ball.ypos >= 1.0 )
394
+ {
395
+ ball.ypos = 2.0 - ball.ypos;
396
+ ball.yspeed = -ball.yspeed;
397
+ }
398
+ else if( ball.ypos <= -1.0 )
399
+ {
400
+ ball.ypos = -2.0 - ball.ypos;
401
+ ball.yspeed = -ball.yspeed;
402
+ }
403
+
404
+ // Did the ball hit/miss a player?
405
+ event = NOBODY_WINS;
406
+
407
+ // Is the ball entering the player 1 goal?
408
+ if( ball.xpos < -1.0 + PLAYER_XSIZE )
409
+ {
410
+ // Did player 1 catch the ball?
411
+ if( ball.ypos > (player1.ypos-PLAYER_YSIZE) &&
412
+ ball.ypos < (player1.ypos+PLAYER_YSIZE) )
413
+ {
414
+ ball.xpos = -2.0 + 2.0*PLAYER_XSIZE - ball.xpos;
415
+ ball.xspeed = -ball.xspeed;
416
+ }
417
+ else
418
+ {
419
+ event = PLAYER2_WINS;
420
+ }
421
+ }
422
+
423
+ // Is the ball entering the player 2 goal?
424
+ if( ball.xpos > 1.0 - PLAYER_XSIZE )
425
+ {
426
+ // Did player 2 catch the ball?
427
+ if( ball.ypos > (player2.ypos-PLAYER_YSIZE) &&
428
+ ball.ypos < (player2.ypos+PLAYER_YSIZE) )
429
+ {
430
+ ball.xpos = 2.0 - 2.0*PLAYER_XSIZE - ball.xpos;
431
+ ball.xspeed = -ball.xspeed;
432
+ }
433
+ else
434
+ {
435
+ event = PLAYER1_WINS;
436
+ }
437
+ }
438
+
439
+ return event;
440
+ }
441
+
442
+
443
+ //========================================================================
444
+ // DrawBox() - Draw a 3D box
445
+ //========================================================================
446
+
447
+ #define TEX_SCALE 4.0f
448
+
449
+
450
+ void DrawBox( float x1, float y1, float z1, float x2, float y2, float z2 )
451
+ {
452
+ // Draw six sides of a cube
453
+ glBegin( GL_QUADS );
454
+ // Side 1 (down)
455
+ glNormal3f( 0.0f, 0.0f, -1.0f );
456
+ glTexCoord2f( 0.0f, 0.0f );
457
+ glVertex3f( x1,y2,z1 );
458
+ glTexCoord2f( TEX_SCALE, 0.0f );
459
+ glVertex3f( x2,y2,z1 );
460
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
461
+ glVertex3f( x2,y1,z1 );
462
+ glTexCoord2f( 0.0f, TEX_SCALE );
463
+ glVertex3f( x1,y1,z1 );
464
+ // Side 2 (up)
465
+ glNormal3f( 0.0f, 0.0f, 1.0f );
466
+ glTexCoord2f( 0.0f, 0.0f );
467
+ glVertex3f( x1,y1,z2 );
468
+ glTexCoord2f( TEX_SCALE, 0.0f );
469
+ glVertex3f( x2,y1,z2 );
470
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
471
+ glVertex3f( x2,y2,z2 );
472
+ glTexCoord2f( 0.0f, TEX_SCALE );
473
+ glVertex3f( x1,y2,z2 );
474
+ // Side 3 (backward)
475
+ glNormal3f( 0.0f, -1.0f, 0.0f );
476
+ glTexCoord2f( 0.0f, 0.0f );
477
+ glVertex3f( x1,y1,z1 );
478
+ glTexCoord2f( TEX_SCALE, 0.0f );
479
+ glVertex3f( x2,y1,z1 );
480
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
481
+ glVertex3f( x2,y1,z2 );
482
+ glTexCoord2f( 0.0f, TEX_SCALE );
483
+ glVertex3f( x1,y1,z2 );
484
+ // Side 4 (forward)
485
+ glNormal3f( 0.0f, 1.0f, 0.0f );
486
+ glTexCoord2f( 0.0f, 0.0f );
487
+ glVertex3f( x1,y2,z2 );
488
+ glTexCoord2f( TEX_SCALE, 0.0f );
489
+ glVertex3f( x2,y2,z2 );
490
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
491
+ glVertex3f( x2,y2,z1 );
492
+ glTexCoord2f( 0.0f, TEX_SCALE );
493
+ glVertex3f( x1,y2,z1 );
494
+ // Side 5 (left)
495
+ glNormal3f( -1.0f, 0.0f, 0.0f );
496
+ glTexCoord2f( 0.0f, 0.0f );
497
+ glVertex3f( x1,y1,z2 );
498
+ glTexCoord2f( TEX_SCALE, 0.0f );
499
+ glVertex3f( x1,y2,z2 );
500
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
501
+ glVertex3f( x1,y2,z1 );
502
+ glTexCoord2f( 0.0f, TEX_SCALE );
503
+ glVertex3f( x1,y1,z1 );
504
+ // Side 6 (right)
505
+ glNormal3f( 1.0f, 0.0f, 0.0f );
506
+ glTexCoord2f( 0.0f, 0.0f );
507
+ glVertex3f( x2,y1,z1 );
508
+ glTexCoord2f( TEX_SCALE, 0.0f );
509
+ glVertex3f( x2,y2,z1 );
510
+ glTexCoord2f( TEX_SCALE, TEX_SCALE );
511
+ glVertex3f( x2,y2,z2 );
512
+ glTexCoord2f( 0.0f, TEX_SCALE );
513
+ glVertex3f( x2,y1,z2 );
514
+ glEnd();
515
+ }
516
+
517
+
518
+ //========================================================================
519
+ // UpdateDisplay() - Draw graphics (all game related OpenGL stuff goes
520
+ // here)
521
+ //========================================================================
522
+
523
+ void UpdateDisplay( void )
524
+ {
525
+ // Get window size
526
+ glfwGetWindowSize( &width, &height );
527
+
528
+ // Set viewport
529
+ glViewport( 0, 0, width, height );
530
+
531
+ // Clear display
532
+ glClearColor( 0.02f, 0.02f, 0.02f, 0.0f );
533
+ glClearDepth( 1.0f );
534
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
535
+
536
+ // Setup projection matrix
537
+ glMatrixMode( GL_PROJECTION );
538
+ glLoadIdentity();
539
+ gluPerspective(
540
+ 55.0f, // Angle of view
541
+ (GLfloat)width/(GLfloat)height, // Aspect
542
+ 1.0f, // Near Z
543
+ 100.0f // Far Z
544
+ );
545
+
546
+ // Setup modelview matrix
547
+ glMatrixMode( GL_MODELVIEW );
548
+ glLoadIdentity();
549
+ switch( camerapos )
550
+ {
551
+ default:
552
+ case CAMERA_CLASSIC:
553
+ gluLookAt(
554
+ 0.0f, 0.0f, 2.5f,
555
+ 0.0f, 0.0f, 0.0f,
556
+ 0.0f, 1.0f, 0.0f
557
+ );
558
+ break;
559
+ case CAMERA_ABOVE:
560
+ gluLookAt(
561
+ 0.0f, 0.0f, 2.5f,
562
+ (float)ball.xpos, (float)ball.ypos, 0.0f,
563
+ 0.0f, 1.0f, 0.0f
564
+ );
565
+ break;
566
+ case CAMERA_SPECTATOR:
567
+ gluLookAt(
568
+ 0.0f, -2.0, 1.2f,
569
+ (float)ball.xpos, (float)ball.ypos, 0.0f,
570
+ 0.0f, 0.0f, 1.0f
571
+ );
572
+ break;
573
+ }
574
+
575
+ // Enable depth testing
576
+ glEnable( GL_DEPTH_TEST );
577
+ glDepthFunc( GL_LEQUAL );
578
+
579
+ // Enable lighting
580
+ glEnable( GL_LIGHTING );
581
+ glLightModelfv( GL_LIGHT_MODEL_AMBIENT, env_ambient );
582
+ glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );
583
+ glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE );
584
+ glLightfv( GL_LIGHT1, GL_POSITION, light1_position );
585
+ glLightfv( GL_LIGHT1, GL_DIFFUSE, light1_diffuse );
586
+ glLightfv( GL_LIGHT1, GL_AMBIENT, light1_ambient );
587
+ glEnable( GL_LIGHT1 );
588
+
589
+ // Front face is counter-clock-wise
590
+ glFrontFace( GL_CCW );
591
+
592
+ // Enable face culling (not necessary, but speeds up rendering)
593
+ glCullFace( GL_BACK );
594
+ glEnable( GL_CULL_FACE );
595
+
596
+ // Draw Player 1
597
+ glMaterialfv( GL_FRONT, GL_DIFFUSE, player1_diffuse );
598
+ glMaterialfv( GL_FRONT, GL_AMBIENT, player1_ambient );
599
+ DrawBox( -1.0, (GLfloat)player1.ypos-PLAYER_YSIZE, 0.0,
600
+ -1.0+PLAYER_XSIZE, (GLfloat)player1.ypos+PLAYER_YSIZE, 0.1 );
601
+
602
+ // Draw Player 2
603
+ glMaterialfv( GL_FRONT, GL_DIFFUSE, player2_diffuse );
604
+ glMaterialfv( GL_FRONT, GL_AMBIENT, player2_ambient );
605
+ DrawBox( 1.0-PLAYER_XSIZE, (GLfloat)player2.ypos-PLAYER_YSIZE, 0.0,
606
+ 1.0, (GLfloat)player2.ypos+PLAYER_YSIZE, 0.1 );
607
+
608
+ // Draw Ball
609
+ glMaterialfv( GL_FRONT, GL_DIFFUSE, ball_diffuse );
610
+ glMaterialfv( GL_FRONT, GL_AMBIENT, ball_ambient );
611
+ DrawBox( (GLfloat)ball.xpos-BALL_SIZE, (GLfloat)ball.ypos-BALL_SIZE, 0.0,
612
+ (GLfloat)ball.xpos+BALL_SIZE, (GLfloat)ball.ypos+BALL_SIZE, BALL_SIZE*2 );
613
+
614
+ // Top game field border
615
+ glMaterialfv( GL_FRONT, GL_DIFFUSE, border_diffuse );
616
+ glMaterialfv( GL_FRONT, GL_AMBIENT, border_ambient );
617
+ DrawBox( -1.1f, 1.0f, 0.0f, 1.1f, 1.1f, 0.1f );
618
+ // Bottom game field border
619
+ glColor3f( 0.0f, 0.0f, 0.7f );
620
+ DrawBox( -1.1f, -1.1f, 0.0f, 1.1f, -1.0f, 0.1f );
621
+ // Left game field border
622
+ DrawBox( -1.1f, -1.0f, 0.0f, -1.0f, 1.0f, 0.1f );
623
+ // Left game field border
624
+ DrawBox( 1.0f, -1.0f, 0.0f, 1.1f, 1.0f, 0.1f );
625
+
626
+ // Enable texturing
627
+ glEnable( GL_TEXTURE_2D );
628
+ glBindTexture( GL_TEXTURE_2D, tex_id[ TEX_FIELD ] );
629
+
630
+ // Game field floor
631
+ glMaterialfv( GL_FRONT, GL_DIFFUSE, floor_diffuse );
632
+ glMaterialfv( GL_FRONT, GL_AMBIENT, floor_ambient );
633
+ DrawBox( -1.01f, -1.01f, -0.01f, 1.01f, 1.01f, 0.0f );
634
+
635
+ // Disable texturing
636
+ glDisable( GL_TEXTURE_2D );
637
+
638
+ // Disable face culling
639
+ glDisable( GL_CULL_FACE );
640
+
641
+ // Disable lighting
642
+ glDisable( GL_LIGHTING );
643
+
644
+ // Disable depth testing
645
+ glDisable( GL_DEPTH_TEST );
646
+ }
647
+
648
+
649
+ //========================================================================
650
+ // GameOver()
651
+ //========================================================================
652
+
653
+ void GameOver( void )
654
+ {
655
+ // Enable sticky keys
656
+ glfwEnable( GLFW_STICKY_KEYS );
657
+
658
+ // Until the user presses ESC or SPACE
659
+ while( !glfwGetKey( GLFW_KEY_ESC ) && !glfwGetKey( ' ' ) &&
660
+ glfwGetWindowParam( GLFW_OPENED ) )
661
+ {
662
+ // Draw display
663
+ UpdateDisplay();
664
+
665
+ // Setup projection matrix
666
+ glMatrixMode( GL_PROJECTION );
667
+ glLoadIdentity();
668
+ glOrtho( 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 1.0f );
669
+
670
+ // Setup modelview matrix
671
+ glMatrixMode( GL_MODELVIEW );
672
+ glLoadIdentity();
673
+
674
+ // Enable blending
675
+ glEnable( GL_BLEND );
676
+
677
+ // Dim background
678
+ glBlendFunc( GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA );
679
+ glColor4f( 0.3f, 0.3f, 0.3f, 0.3f );
680
+ glBegin( GL_QUADS );
681
+ glVertex2f( 0.0f, 0.0f );
682
+ glVertex2f( 1.0f, 0.0f );
683
+ glVertex2f( 1.0f, 1.0f );
684
+ glVertex2f( 0.0f, 1.0f );
685
+ glEnd();
686
+
687
+ // Display winner text
688
+ glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_COLOR );
689
+ if( winner == PLAYER1 )
690
+ {
691
+ glColor4f( 1.0f, 0.5f, 0.5f, 1.0f );
692
+ DrawImage( TEX_WINNER1, 0.35f, 0.65f, 0.46f, 0.54f );
693
+ }
694
+ else if( winner == PLAYER2 )
695
+ {
696
+ glColor4f( 0.5f, 1.0f, 0.5f, 1.0f );
697
+ DrawImage( TEX_WINNER2, 0.35f, 0.65f, 0.46f, 0.54f );
698
+ }
699
+
700
+ // Disable blending
701
+ glDisable( GL_BLEND );
702
+
703
+ // Swap buffers
704
+ glfwSwapBuffers();
705
+ }
706
+
707
+ // Disable sticky keys
708
+ glfwDisable( GLFW_STICKY_KEYS );
709
+ }
710
+
711
+
712
+ //========================================================================
713
+ // GameLoop() - Game loop
714
+ //========================================================================
715
+
716
+ void GameLoop( void )
717
+ {
718
+ int playing, event;
719
+
720
+ // Initialize a new game
721
+ NewGame();
722
+
723
+ // Enable sticky keys
724
+ glfwEnable( GLFW_STICKY_KEYS );
725
+
726
+ // Loop until the game ends
727
+ playing = GL_TRUE;
728
+ while( playing && glfwGetWindowParam( GLFW_OPENED ) )
729
+ {
730
+ // Frame timer
731
+ oldtime = thistime;
732
+ thistime = glfwGetTime();
733
+ dt = thistime - oldtime;
734
+
735
+ // Get user input and update player positions
736
+ PlayerControl();
737
+
738
+ // Move the ball, and check if a player hits/misses the ball
739
+ event = BallControl();
740
+
741
+ // Did we have a winner?
742
+ switch( event )
743
+ {
744
+ case PLAYER1_WINS:
745
+ winner = PLAYER1;
746
+ playing = GL_FALSE;
747
+ break;
748
+ case PLAYER2_WINS:
749
+ winner = PLAYER2;
750
+ playing = GL_FALSE;
751
+ break;
752
+ default:
753
+ break;
754
+ }
755
+
756
+ // Did the user press ESC?
757
+ if( glfwGetKey( GLFW_KEY_ESC ) )
758
+ {
759
+ playing = GL_FALSE;
760
+ }
761
+
762
+ // Did the user change camera view?
763
+ if( glfwGetKey( '1' ) )
764
+ {
765
+ camerapos = CAMERA_CLASSIC;
766
+ }
767
+ else if( glfwGetKey( '2' ) )
768
+ {
769
+ camerapos = CAMERA_ABOVE;
770
+ }
771
+ else if( glfwGetKey( '3' ) )
772
+ {
773
+ camerapos = CAMERA_SPECTATOR;
774
+ }
775
+
776
+ // Draw display
777
+ UpdateDisplay();
778
+
779
+ // Swap buffers
780
+ glfwSwapBuffers();
781
+ }
782
+
783
+ // Disable sticky keys
784
+ glfwDisable( GLFW_STICKY_KEYS );
785
+
786
+ // Show winner
787
+ GameOver();
788
+ }
789
+
790
+
791
+ //========================================================================
792
+ // main() - Program entry point
793
+ //========================================================================
794
+
795
+ int main( void )
796
+ {
797
+ int menuoption;
798
+
799
+ // Initialize GLFW
800
+ if( !glfwInit() )
801
+ {
802
+ exit( 0 );
803
+ }
804
+
805
+ // Open OpenGL window
806
+ if( !glfwOpenWindow( WIDTH, HEIGHT, 0,0,0,0, 16,0, GLFW_FULLSCREEN ) )
807
+ {
808
+ glfwTerminate();
809
+ exit( 0 );
810
+ }
811
+
812
+ // Load all textures
813
+ LoadTextures();
814
+
815
+ // Main loop
816
+ do
817
+ {
818
+ // Get menu option
819
+ menuoption = GameMenu();
820
+
821
+ // If the user wants to play, let him...
822
+ if( menuoption == MENU_PLAY )
823
+ {
824
+ GameLoop();
825
+ }
826
+ }
827
+ while( menuoption != MENU_QUIT );
828
+
829
+ // Unload all textures
830
+ if( glfwGetWindowParam( GLFW_OPENED ) )
831
+ {
832
+ glDeleteTextures( NUM_TEXTURES, tex_id );
833
+ }
834
+
835
+ // Terminate GLFW
836
+ glfwTerminate();
837
+
838
+ return 0;
839
+ }