]>
code.delx.au - pong/blob - pong.c
8cf082ba55fe51a67b9c06ddb2daa2beedc1ebf9
2 #pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
4 #elif defined(__APPLE__) || defined(MACOSX)
14 // Amount to increase speed by on x, y axis, and when bouncing
17 #define SPEEDBINC 0.15
18 #define PADDLESPEED 3.0
27 static char* menu
= "";
29 static int p1move
= 0;
30 static int p2move
= 0;
31 static int score1
= 0;
32 static int score2
= 0;
33 static GLdouble paddle1
= 0.0;
34 static GLdouble paddle2
= 0.0;
35 static GLdouble ballX
= 0.0;
36 static GLdouble ballY
= 0.0;
37 static GLdouble ballVecX
= 0.0;
38 static GLdouble ballVecY
= 0.0;
41 static inline double fmax(double a
, double b
) {
45 static void initBall(void) {
55 static void run(void) {
57 if(state
== STATE_1P
) {
58 if(ballY
< paddle2
- PADDLESIZE
)
60 else if(ballY
> paddle2
+ PADDLESIZE
)
67 paddle1
+= p1move
* PADDLESPEED
;
68 paddle2
+= p2move
* PADDLESPEED
;
78 // Check for collisions with paddles
79 if(ballVecX
< 0 && ballX
<= -HEIGHT
+ -ballVecX
* 1.5) {
80 if(ballY
>= paddle1
- PADDLESIZE
&& ballY
<= paddle1
+ PADDLESIZE
) {
81 GLdouble speedChange
= fmax(1.0, fabs(paddle1
- ballY
) * SPEEDBINC
);
82 ballVecX
*= -speedChange
* SPEEDXINC
;
83 ballVecY
*= speedChange
* SPEEDYINC
;
86 if(ballVecX
> 0 && ballX
>= HEIGHT
- ballVecX
* 1.5) {
87 if(ballY
>= paddle2
- PADDLESIZE
&& ballY
<= paddle2
+ PADDLESIZE
) {
88 GLdouble speedChange
= fmax(1.0, fabs(paddle2
- ballY
) * SPEEDBINC
);
89 ballVecX
*= -speedChange
* SPEEDXINC
;
90 ballVecY
*= speedChange
* SPEEDYINC
;
94 // Check if it's past the top or bottom of the screen
95 if(ballY
>= HEIGHT
|| ballY
<= -HEIGHT
) {
99 // Check if it's past the sides of the screen
100 if(ballX
>= HEIGHT
) {
104 if(ballX
<= -HEIGHT
) {
113 // Check scores for winners..
115 menu
= "Player 1 wins!";
120 menu
= "Player 2 wins!";
126 static void timer(int lastTime
) {
127 if(state
== STATE_UI
) {
131 int curTime
= glutGet(GLUT_ELAPSED_TIME
);
135 } while(lastTime
+ FRAME
< curTime
);
137 glutTimerFunc(FRAME
- (curTime
- lastTime
), timer
, curTime
);
140 static void startGame(void) {
141 glutTimerFunc(FRAME
, timer
, glutGet(GLUT_ELAPSED_TIME
));
145 static void drawText(double x
, double y
, char* str
) {
148 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, *str
);
153 static void menuDisplay(void) {
154 drawText(-20, 50, "Awesome PONG!");
155 drawText(-50, -50, "Press 1 for a single player game.");
156 drawText(-50, -60, "Press 2 for a multi player game.");
157 drawText(-50, -70, "Keys: w/s and i/k");
158 drawText(-20, 0, menu
);
161 static void gameDisplay(void) {
165 glVertex2d(-HEIGHT
+ 1, paddle1
- PADDLESIZE
);
166 glVertex2d(-HEIGHT
+ 1, paddle1
+ PADDLESIZE
);
167 glVertex2d( HEIGHT
- 1, paddle2
+ PADDLESIZE
);
168 glVertex2d( HEIGHT
- 1, paddle2
- PADDLESIZE
);
173 glVertex2d(ballX
, ballY
);
174 glVertex2d(ballX
- ballVecX
, ballY
- ballVecY
);
178 glRasterPos2d(-5.0, HEIGHT
- 10.0);
179 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, '0' + score1
);
180 glRasterPos2d( 5.0, HEIGHT
- 10.0);
181 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, '0' + score2
);
184 static void display(void) {
185 glClear(GL_COLOR_BUFFER_BIT
);
186 glColor3d(0.0, 0.0, 0.0);
188 if(state
== STATE_UI
) {
197 static void resize(int w
, int h
) {
198 glViewport(0, 0, w
, h
);
200 glOrtho(-HEIGHT
, HEIGHT
, -HEIGHT
, HEIGHT
, -1.0, 1.0);
203 static void menuKeyboard(unsigned char key
) {
219 static void gameKeyboard(unsigned char key
) {
241 static void keyboard(unsigned char key
, int x
, int y
) {
252 if(state
== STATE_UI
) {
259 static void keyboardUp(unsigned char key
, int x
, int y
) {
276 int main(int argc
, char *argv
[]) {
277 glutInitWindowPosition(100, 100);
278 glutInitWindowSize(640, 480);
279 glutInit(&argc
, argv
);
280 glutInitDisplayMode(GLUT_RGBA
| GLUT_DOUBLE
);
281 glutCreateWindow(argv
[0]);
282 glutDisplayFunc(display
);
283 glutReshapeFunc(resize
);
284 glutKeyboardFunc(keyboard
);
285 glutKeyboardUpFunc(keyboardUp
);
286 glClearColor(1.0, 1.0, 1.0, 0.0);