home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter10 / AngularMotion / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-14  |  4.1 KB  |  158 lines

  1. ///////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 10 - AngularMotion
  4. ///////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include "allegro.h"
  8. #include "math.h"
  9. #include "sprite.h"
  10. #include "spritehandler.h"
  11.  
  12. #define PI 3.1415926535
  13. #define ACCELERATION 0.1f
  14. #define STEP 1
  15.  
  16. #define MODE GFX_AUTODETECT_WINDOWED
  17. #define WIDTH 800
  18. #define HEIGHT 600
  19. #define BLACK makecol(0,0,0)
  20. #define WHITE makecol(255,255,255)
  21.  
  22. //calculate X movement value based on direction angle
  23. double calcAngleMoveX(int angle) {
  24.     return (double) cos(angle * PI / 180);
  25. }
  26.  
  27. //calculate Y movement value based on direction angle
  28. double calcAngleMoveY(int angle) {
  29.     return (double) sin(angle * PI / 180);
  30. }
  31.  
  32. void erasesprite(BITMAP *dest, sprite *spr)
  33. {
  34.     //erase the sprite using BLACK color fill
  35.     rectfill(dest, (int)spr->x, (int)spr->y, (int)spr->x + spr->width, 
  36.         (int)spr->y + spr->height, BLACK);
  37. }
  38.  
  39. void warpsprite(sprite *spr)
  40. {
  41.     //simple screen warping behavior
  42.     int w = spr->width;
  43.     int h = spr->height;
  44.     
  45.     if (spr->x < 0-w)
  46.     {
  47.         spr->x = SCREEN_W;
  48.     }
  49.  
  50.     else if (spr->x > SCREEN_W)
  51.     {
  52.         spr->x = 0-w;
  53.     }
  54.  
  55.     if (spr->y < 0-h)
  56.     {
  57.         spr->y = SCREEN_H;
  58.     }
  59.  
  60.     else if (spr->y > SCREEN_H)
  61.     {
  62.         spr->y = 0-h;
  63.     }
  64.  
  65. }
  66.  
  67. int main(void)
  68. {
  69.     //initialize
  70.     allegro_init();
  71.     set_color_depth(16);
  72.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  73.     install_keyboard();
  74.     install_timer();
  75.     srand(time(NULL));
  76.     
  77.     //create a back buffer
  78.     BITMAP *buffer = create_bitmap(WIDTH,HEIGHT);
  79.  
  80.     //create the ship sprite
  81.     sprite *ship = new sprite();
  82.     ship->load("ship.bmp");
  83.     ship->width = 60;
  84.     ship->height = 60;
  85.     ship->xdelay = 0;
  86.     ship->ydelay = 0;
  87.     ship->x = SCREEN_W / 2;
  88.     ship->y = SCREEN_H / 2;
  89.     ship->moveAngle = 0;
  90.     ship->faceAngle = 0;
  91.  
  92.     //game loop
  93.     while (!key[KEY_ESC])
  94.     {
  95.         rectfill(buffer, 0, 0, WIDTH-1, HEIGHT-1, BLACK);
  96.         erasesprite(buffer, ship);
  97.  
  98.         if (key[KEY_LEFT]) {
  99.             ship->faceAngle -= STEP;
  100.             if (ship->faceAngle < 0) ship->faceAngle = 359;
  101.         }
  102.  
  103.         if (key[KEY_RIGHT]) {
  104.             ship->faceAngle += STEP;
  105.             if (ship->faceAngle > 359) ship->faceAngle = 0;
  106.         }
  107.  
  108.         if (key[KEY_UP]) {
  109.             //shift 0-degree orientation from right-face to up-face
  110.             ship->moveAngle = ship->faceAngle - 90;
  111.             //convert negative angle to wraparound
  112.             if (ship->moveAngle < 0) ship->moveAngle = 359 + ship->moveAngle;
  113.  
  114.             //adjust velocity based on angle
  115.             ship->velx += calcAngleMoveX(ship->moveAngle) * ACCELERATION;
  116.             ship->vely += calcAngleMoveY(ship->moveAngle) * ACCELERATION;
  117.             
  118.             //keep velocity down to a reasonable speed
  119.             if (ship->velx > 4.0) ship->velx = 4.0;
  120.             if (ship->velx < -4.0) ship->velx = -4.0;
  121.             if (ship->vely > 4.0) ship->vely = 4.0;
  122.             if (ship->vely < -4.0) ship->vely = -4.0;
  123.         }
  124.  
  125.         //rotate sprite with adjust for Allegro's 16.16 fixed trig
  126.         //(256 / 360 = 0.7), then divide by 2 radians
  127.         rotate_sprite(buffer, ship->image, (int)ship->x, (int)ship->y, 
  128.             itofix((int)(ship->faceAngle / 0.7f / 2.0f)));
  129.  
  130.         textout_ex(buffer, font, "AngularMotion Program (ESC to quit)", 0, 0, WHITE, -1);
  131.         textprintf_ex(buffer, font, 0, 10, WHITE, -1, "Face angle: %d", ship->faceAngle);
  132.         textprintf_ex(buffer, font, 0, 20, WHITE, -1, "Move angle: %d", ship->moveAngle);
  133.         textprintf_ex(buffer, font, 0, 30, WHITE, -1, "Velocity X: %.2f", ship->velx);
  134.         textprintf_ex(buffer, font, 0, 40, WHITE, -1, "Velocity Y: %.2f", ship->vely);
  135.         textprintf_ex(buffer, font, 0, 50, WHITE, -1, "Position X: %.2f", ship->x);
  136.         textprintf_ex(buffer, font, 0, 60, WHITE, -1, "Position Y: %.2f", ship->y);
  137.  
  138.  
  139.         //move the ship
  140.         ship->updatePosition();
  141.         warpsprite(ship);
  142.  
  143.         //refresh the screen
  144.         acquire_screen();
  145.         blit(buffer, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
  146.         release_screen();
  147.         rest(10);
  148.     }
  149.  
  150.     delete ship;
  151.     destroy_bitmap(buffer);
  152.     allegro_exit();
  153.     return 1;
  154. }
  155.  
  156. END_OF_MAIN()
  157.  
  158.