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

  1. //////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 12 - GameWorld
  4. //////////////////////////////////////////////////
  5.  
  6. #include "allegro.h"
  7.  
  8. //define some convenient constants
  9. #define MODE GFX_AUTODETECT_WINDOWED
  10. #define WIDTH 640
  11. #define HEIGHT 480
  12. #define STEP 8
  13. #define TILEW 32
  14. #define TILEH 32
  15. #define TILES 39
  16. #define COLS 10
  17. #define MAP_ACROSS 25
  18. #define MAP_DOWN 25
  19. #define MAPW MAP_ACROSS * TILEW
  20. #define MAPH MAP_DOWN * TILEH
  21.  
  22. int map[MAPW*MAPH] = {
  23.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  24.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  25.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  26.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  27.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  28.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  29.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  30.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  31.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  32.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  33.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  34.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  35.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  36.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  37.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  38.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  39.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  40.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  41.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  42.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  43.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  44.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  45.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  46.     0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0,
  47.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  48. };
  49.  
  50. //temp bitmap
  51. BITMAP *tiles;
  52.  
  53. //virtual background buffer
  54. BITMAP *scroll;
  55.  
  56. //position variables
  57. int x=0, y=0, n;
  58. int tilex, tiley;
  59.  
  60. void drawframe(BITMAP *source, BITMAP *dest,
  61.                int x, int y, int width, int height,
  62.                int startx, int starty, int columns, int frame)
  63. {
  64.     //calculate frame position
  65.     int framex = startx + (frame % columns) * width;
  66.     int framey = starty + (frame / columns) * height;
  67.     //draw frame to destination bitmap
  68.     masked_blit(source,dest,framex,framey,x,y,width,height);
  69. }
  70.  
  71.  
  72. //main function
  73. int main(void)
  74. {
  75.     //initialize allegro
  76.     allegro_init();
  77.     install_keyboard();
  78.     install_timer();
  79.     srand(time(NULL));
  80.     set_color_depth(16);
  81.  
  82.     //set video mode
  83.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  84.  
  85.     //create the virtual background
  86.     scroll = create_bitmap(MAPW, MAPH);
  87.  
  88.     //load the tile bitmap 
  89.     tiles = load_bitmap("tiles.bmp", NULL);
  90.  
  91.     //now draw tiles randomly on virtual background
  92.     for (tiley=0; tiley < scroll->h; tiley+=TILEH)
  93.     {
  94.         for (tilex=0; tilex < scroll->w; tilex+=TILEW)
  95.         {
  96.             //draw the tile
  97.             drawframe(tiles, scroll, tilex, tiley, TILEW+1, TILEH+1, 
  98.                 0, 0, COLS, map[n++]);
  99.         }
  100.     }
  101.  
  102.     //main loop
  103.     while (!key[KEY_ESC])
  104.     {
  105.         //check right arrow
  106.         if (key[KEY_RIGHT])
  107.         {
  108.             x += STEP;
  109.             if (x > scroll->w - WIDTH)
  110.                 x = scroll->w - WIDTH;
  111.         }
  112.  
  113.         //check left arrow
  114.         if (key[KEY_LEFT])
  115.         {
  116.             x -= STEP;
  117.             if (x < 0)
  118.                 x = 0;
  119.         }
  120.  
  121.         //check down arrow
  122.         if (key[KEY_DOWN])
  123.         {
  124.             y += STEP;
  125.             if (y > scroll->h - HEIGHT)
  126.                 y = scroll->h - HEIGHT;
  127.         }
  128.  
  129.         //check up arrow
  130.         if (key[KEY_UP])
  131.         {
  132.             y -= STEP;
  133.             if (y < 0)
  134.                 y = 0;
  135.         }
  136.  
  137.         //draw the scroll window portion of the virtual buffer
  138.         blit(scroll, screen, x, y, 0, 0, WIDTH-1, HEIGHT-1);
  139.  
  140.         //display status info
  141.         textprintf_ex(screen,font,0,0,makecol(0,0,0),-1,
  142.             "Position = (%d,%d)", x, y);
  143.  
  144.         //slow it down
  145.         rest(20);
  146.     }
  147.  
  148.     destroy_bitmap(scroll);
  149.     destroy_bitmap(tiles);
  150.     allegro_exit();
  151.     return 0;
  152. }
  153. END_OF_MAIN()
  154.