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

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