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