home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter05 / testjoystick / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-10  |  3.7 KB  |  150 lines

  1. ////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 5, TestJoystick Program
  4. ////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. #define WHITE makecol(255,255,255)
  9. #define BLACK makecol(0,0,0)
  10.  
  11. BITMAP *back;
  12. BITMAP *paddle;
  13. BITMAP *ball;
  14.  
  15. int score = 0, paddlex, paddley = 430;
  16. int ballx = 100, bally = 100;
  17. int dirx = 1, diry = 2;
  18.  
  19. void updateball()
  20. {
  21.     //update ball x
  22.     ballx += dirx;
  23.     
  24.     //hit left?
  25.     if (ballx < 0)
  26.     {
  27.         ballx = 1;
  28.         dirx = rand() % 2 + 4;
  29.     }
  30.     
  31.     //hit right?
  32.     if (ballx > SCREEN_W - ball->w - 1)
  33.     {
  34.         ballx = SCREEN_W - ball->w - 1;
  35.         dirx = rand() % 2 - 6;
  36.     }
  37.  
  38.     //update ball y
  39.     bally += diry;
  40.     
  41.     //hit top?
  42.     if (bally < 0)
  43.     {
  44.         bally = 1;
  45.         diry = rand() % 2 + 4;
  46.     }
  47.     
  48.     //hit bottom?
  49.     if (bally > SCREEN_H - ball->h - 1)
  50.     {
  51.         score--;
  52.         bally = SCREEN_H - ball->h - 1;
  53.         diry = rand() % 2 - 6;
  54.     }
  55.     
  56.     //hit the paddle?
  57.     if (ballx > paddlex && ballx < paddlex+paddle->w &&
  58.         bally > paddley && bally < paddley+paddle->h)
  59.     {
  60.         score++;
  61.         bally = paddley - ball->h - 1;
  62.         diry = rand() % 2 - 6;
  63.     }
  64.  
  65.     //draw ball
  66.     masked_blit(ball, screen, 0, 0, ballx, bally, ball->w, ball->h);
  67. }
  68.  
  69. int main(void)
  70. {
  71.     int d1, d2, pos, startpos;
  72.  
  73.     //initialize program    
  74.     allegro_init();
  75.     set_color_depth(16);
  76.     set_gfx_mode(GFX_AUTODETECT_FULLSCREEN , 640, 480, 0, 0);
  77.     srand(time(NULL));
  78.     install_keyboard();
  79.  
  80.     //install the joystick handler
  81.     install_joystick(JOY_TYPE_AUTODETECT);
  82.     poll_joystick();
  83.  
  84.     //look for a joystick
  85.     if (num_joysticks == 0)
  86.     {
  87.         textout_ex(screen,font,"No joystick could be found",0,20,WHITE,0);
  88.         while(!keypressed());
  89.         return;
  90.     }
  91.     
  92.     //store starting stick position
  93.     startpos = joy[0].stick[0].axis[0].pos;
  94.  
  95.     //load the background image
  96.     back = load_bitmap("background.bmp", NULL);
  97.     
  98.     //load the paddle image and position it
  99.     paddle = load_bitmap("paddle.bmp", NULL);
  100.     paddlex = SCREEN_W/2 - paddle->w/2;
  101.     
  102.     //load the ball image
  103.     ball = load_bitmap("ball.bmp", NULL);
  104.     
  105.     //main loop
  106.     while (!key[KEY_ESC])
  107.     {
  108.         //clear screen the slow way (redraw background)
  109.         blit(back, screen, 0, 0, 0, 0, back->w, back->h);
  110.  
  111.         //update ball position
  112.         updateball();
  113.  
  114.         //read the joystick
  115.         poll_joystick();
  116.         d1 = joy[0].stick[0].axis[0].d1;
  117.         d2 = joy[0].stick[0].axis[0].d2;
  118.         pos = joy[0].stick[0].axis[0].pos;
  119.         
  120.         //see if stick moved left
  121.         if (d1 || pos < startpos+10) paddlex -= 10;
  122.         if (paddlex < 2) paddlex = 2;
  123.         
  124.         //see if stick moved right
  125.         if (d2 || pos > startpos-10) paddlex += 10;
  126.         if (paddlex > SCREEN_W - paddle->w - 2) 
  127.             paddlex = SCREEN_W - paddle->w - 2;
  128.         
  129.         //display text messages
  130.         textout_ex(screen, font, "TestJoystick Program (ESC to quit)",
  131.             2, 2, BLACK,0);
  132.         textprintf_ex(screen, font, 2, 20, BLACK,0,
  133.             "Stick d1,d2,pos: %d,%d,%d", d1, d2, pos);
  134.         textprintf_right_ex(screen, font, SCREEN_W - 2, 2, BLACK,0,
  135.             "SCORE: %d", score);
  136.         
  137.         //draw the paddle
  138.         blit(paddle,screen,0,0,paddlex,paddley,paddle->w,paddle->h);
  139.         
  140.         rest(20);
  141.     }
  142.     
  143.     destroy_bitmap(back);
  144.     destroy_bitmap(paddle);
  145.     destroy_bitmap(ball);
  146.     allegro_exit();
  147.     return 0;
  148. }
  149. END_OF_MAIN()
  150.