From 44d403c7823e58abb4993229b56e81de117e9c1b Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 23 Jun 2007 22:00:46 +1000 Subject: [PATCH] Welcome screen Added welcome screen with instructions Can select multi/single player game from welcome screen Winners are announced on the welcome screen --- pong.c | 135 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 39 deletions(-) diff --git a/pong.c b/pong.c index f09d718..09bf9d4 100644 --- a/pong.c +++ b/pong.c @@ -20,7 +20,12 @@ #define PADDLESIZE 10 #define HEIGHT 100 -static int AI = 0; +#define STATE_UI 0 +#define STATE_1P 1 +#define STATE_2P 2 + +static char* menu = ""; +static int state = 0; static int p1move = 0; static int p2move = 0; static int score1 = 0; @@ -33,7 +38,7 @@ static GLdouble ballVecX = 0.0; static GLdouble ballVecY = 0.0; -static void initball(void) { +static void initBall(void) { ballX = 0.0; ballY = 0.0; if(ballVecX < 0) @@ -44,8 +49,8 @@ static void initball(void) { } static void run(void) { - // AI - if(AI) { + // AI is on + if(state == STATE_1P) { if(ballY < paddle2 - PADDLESIZE) p2move = -1; else if(ballY > paddle2 + PADDLESIZE) @@ -90,11 +95,11 @@ static void run(void) { // Check if it's past the sides of the screen if(ballX >= HEIGHT) { ++score1; - initball(); + initBall(); } if(ballX <= -HEIGHT) { ++score2; - initball(); + initBall(); } // Move the ball @@ -103,19 +108,53 @@ static void run(void) { // Check scores for winners.. if(score1 == 9) { - // Player 1 wins + menu = "Player 1 wins!"; + state = STATE_UI; score1 = score2 = 0; } if(score2 == 9) { - // Player 2 wins + menu = "Player 2 wins!"; + state = STATE_UI; score1 = score2 = 0; } } -static void display(void) { - glClear(GL_COLOR_BUFFER_BIT); - glColor3d(0.0, 0.0, 0.0); +static void timer(int lastTime) { + if(state == STATE_UI) { + return; + } + + int curTime = glutGet(GLUT_ELAPSED_TIME); + do { + lastTime += FRAME; + run(); + } while(lastTime + FRAME < curTime); + glutPostRedisplay(); + glutTimerFunc(FRAME - (curTime - lastTime), timer, curTime); +} +static void startGame(void) { + glutTimerFunc(FRAME, timer, glutGet(GLUT_ELAPSED_TIME)); + initBall(); +} + +static void drawText(double x, double y, char* str) { + glRasterPos2d(x, y); + while(*str) { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *str); + ++str; + } +} + +static void menuDisplay(void) { + drawText(-20, 50, "Awesome PONG!"); + drawText(-50, -50, "Press 1 for a single player game."); + drawText(-50, -60, "Press 2 for a multi player game."); + drawText(-50, -70, "Keys: w/s and i/k"); + drawText(-20, 0, menu); +} + +static void gameDisplay(void) { // Draw the paddles glLineWidth(2.0); glBegin(GL_LINES); @@ -136,6 +175,17 @@ static void display(void) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + score1); glRasterPos2d( 5.0, HEIGHT - 10.0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, '0' + score2); +} + +static void display(void) { + glClear(GL_COLOR_BUFFER_BIT); + glColor3d(0.0, 0.0, 0.0); + + if(state == STATE_UI) { + menuDisplay(); + } else { + gameDisplay(); + } glutSwapBuffers(); } @@ -146,15 +196,24 @@ static void resize(int w, int h) { glOrtho(-HEIGHT, HEIGHT, -HEIGHT, HEIGHT, -1.0, 1.0); } -static void keyboard(unsigned char key, int x, int y) { - (void)x;(void)y; - +static void menuKeyboard(unsigned char key) { switch(key) { - case 'q': - case 'Q': - case '\033': - exit(0); + case '1': + state = STATE_1P; + break; + case '2': + state = STATE_2P; + break; + + default: + return; + } + startGame(); +} + +static void gameKeyboard(unsigned char key) { + switch(key) { case 'w': p1move = 1; break; @@ -175,6 +234,24 @@ static void keyboard(unsigned char key, int x, int y) { glutPostRedisplay(); } +static void keyboard(unsigned char key, int x, int y) { + (void)x;(void)y; + + switch(key) { + case 'q': + case 'Q': + case '\033': + exit(0); + } + + + if(state == STATE_UI) { + menuKeyboard(key); + } else { + gameKeyboard(key); + } +} + static void keyboardUp(unsigned char key, int x, int y) { (void)x;(void)y; @@ -192,26 +269,7 @@ static void keyboardUp(unsigned char key, int x, int y) { } } -static void timer(int lastTime) { - int curTime = glutGet(GLUT_ELAPSED_TIME); - do { - lastTime += FRAME; - run(); - } while(lastTime + FRAME < curTime); - glutPostRedisplay(); - glutTimerFunc(FRAME - (curTime - lastTime), timer, curTime); -} - -static void init(void) { - glClearColor(1.0, 1.0, 1.0, 0.0); - initball(); -} - int main(int argc, char *argv[]) { - if(argc == 2 && *argv[1] == '1') { - AI = 1; - } - glutInitWindowPosition(100, 100); glutInitWindowSize(640, 480); glutInit(&argc, argv); @@ -221,8 +279,7 @@ int main(int argc, char *argv[]) { glutReshapeFunc(resize); glutKeyboardFunc(keyboard); glutKeyboardUpFunc(keyboardUp); - glutTimerFunc(FRAME, timer, glutGet(GLUT_ELAPSED_TIME)); - init(); + glClearColor(1.0, 1.0, 1.0, 0.0); glutMainLoop(); return 0; } -- 2.39.2