]>
code.delx.au - pong/blob - pong.c
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 void initBall(void) {
51 static void run(void) {
53 if(state
== STATE_1P
) {
54 if(ballY
< paddle2
- PADDLESIZE
)
56 else if(ballY
> paddle2
+ PADDLESIZE
)
63 paddle1
+= p1move
* PADDLESPEED
;
64 paddle2
+= p2move
* PADDLESPEED
;
74 // Check for collisions with paddles
75 if(ballVecX
< 0 && ballX
<= -HEIGHT
+ -ballVecX
* 1.5) {
76 if(ballY
>= paddle1
- PADDLESIZE
&& ballY
<= paddle1
+ PADDLESIZE
) {
77 GLdouble speedChange
= fmax(1.0, fabs(paddle1
- ballY
) * SPEEDBINC
);
78 ballVecX
*= -speedChange
* SPEEDXINC
;
79 ballVecY
*= speedChange
* SPEEDYINC
;
82 if(ballVecX
> 0 && ballX
>= HEIGHT
- ballVecX
* 1.5) {
83 if(ballY
>= paddle2
- PADDLESIZE
&& ballY
<= paddle2
+ PADDLESIZE
) {
84 GLdouble speedChange
= fmax(1.0, fabs(paddle2
- ballY
) * SPEEDBINC
);
85 ballVecX
*= -speedChange
* SPEEDXINC
;
86 ballVecY
*= speedChange
* SPEEDYINC
;
90 // Check if it's past the top or bottom of the screen
91 if(ballY
>= HEIGHT
|| ballY
<= -HEIGHT
) {
95 // Check if it's past the sides of the screen
100 if(ballX
<= -HEIGHT
) {
109 // Check scores for winners..
111 menu
= "Player 1 wins!";
116 menu
= "Player 2 wins!";
122 static void timer(int lastTime
) {
123 if(state
== STATE_UI
) {
127 int curTime
= glutGet(GLUT_ELAPSED_TIME
);
131 } while(lastTime
+ FRAME
< curTime
);
133 glutTimerFunc(FRAME
- (curTime
- lastTime
), timer
, curTime
);
136 static void startGame(void) {
137 glutTimerFunc(FRAME
, timer
, glutGet(GLUT_ELAPSED_TIME
));
141 static void drawText(double x
, double y
, char* str
) {
144 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, *str
);
149 static void menuDisplay(void) {
150 drawText(-20, 50, "Awesome PONG!");
151 drawText(-50, -50, "Press 1 for a single player game.");
152 drawText(-50, -60, "Press 2 for a multi player game.");
153 drawText(-50, -70, "Keys: w/s and i/k");
154 drawText(-20, 0, menu
);
157 static void gameDisplay(void) {
161 glVertex2d(-HEIGHT
+ 1, paddle1
- PADDLESIZE
);
162 glVertex2d(-HEIGHT
+ 1, paddle1
+ PADDLESIZE
);
163 glVertex2d( HEIGHT
- 1, paddle2
+ PADDLESIZE
);
164 glVertex2d( HEIGHT
- 1, paddle2
- PADDLESIZE
);
169 glVertex2d(ballX
, ballY
);
170 glVertex2d(ballX
- ballVecX
, ballY
- ballVecY
);
174 glRasterPos2d(-5.0, HEIGHT
- 10.0);
175 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, '0' + score1
);
176 glRasterPos2d( 5.0, HEIGHT
- 10.0);
177 glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18
, '0' + score2
);
180 static void display(void) {
181 glClear(GL_COLOR_BUFFER_BIT
);
182 glColor3d(0.0, 0.0, 0.0);
184 if(state
== STATE_UI
) {
193 static void resize(int w
, int h
) {
194 glViewport(0, 0, w
, h
);
196 glOrtho(-HEIGHT
, HEIGHT
, -HEIGHT
, HEIGHT
, -1.0, 1.0);
199 static void menuKeyboard(unsigned char key
) {
215 static void gameKeyboard(unsigned char key
) {
237 static void keyboard(unsigned char key
, int x
, int y
) {
248 if(state
== STATE_UI
) {
255 static void keyboardUp(unsigned char key
, int x
, int y
) {
272 int main(int argc
, char *argv
[]) {
273 glutInitWindowPosition(100, 100);
274 glutInitWindowSize(640, 480);
275 glutInit(&argc
, argv
);
276 glutInitDisplayMode(GLUT_RGBA
| GLUT_DOUBLE
);
277 glutCreateWindow(argv
[0]);
278 glutDisplayFunc(display
);
279 glutReshapeFunc(resize
);
280 glutKeyboardFunc(keyboard
);
281 glutKeyboardUpFunc(keyboardUp
);
282 glClearColor(1.0, 1.0, 1.0, 0.0);