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

  1. /////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 14 - TestMappy
  4. /////////////////////////////////////////////////////////
  5.  
  6. #include "allegro.h"
  7. #include "mappyal.h"
  8.  
  9. #define MODE GFX_AUTODETECT_WINDOWED
  10. #define WIDTH 640
  11. #define HEIGHT 480
  12. #define WHITE makecol(255,255,255)
  13.  
  14. //x, y offset in pixels 
  15. int xoffset = 0;
  16. int yoffset = 0;
  17.  
  18. //double buffer
  19. BITMAP *buffer;    
  20.  
  21. int main (void)
  22. {
  23.     //initialize program
  24.     allegro_init();    
  25.     install_timer();
  26.     install_keyboard();
  27.     set_color_depth(16);
  28.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  29.  
  30.     //create the double buffer and clear it
  31.     buffer = create_bitmap(SCREEN_W, SCREEN_H);    
  32.     clear(buffer);
  33.  
  34.     //load the Mappy file
  35.     if (MapLoad("map1.fmp") != 0)
  36.     {
  37.         allegro_message ("Can't find map1.fmp");
  38.         return 1;
  39.     }
  40.  
  41.     //main loop
  42.     while (!key[KEY_ESC])
  43.     {
  44.         //check for keyboard input
  45.         if (key[KEY_RIGHT]) xoffset+=4; 
  46.         if (key[KEY_LEFT])  xoffset-=4; 
  47.         if (key[KEY_UP])    yoffset-=4; 
  48.         if (key[KEY_DOWN])  yoffset+=4; 
  49.  
  50.         //make sure it doesn't scroll beyond map edge
  51.         if (xoffset < 0) xoffset = 0;
  52.  
  53.         if (xoffset > mapwidth*mapblockwidth-SCREEN_W) 
  54.             xoffset = mapwidth*mapblockwidth-SCREEN_W;
  55.  
  56.         if (yoffset < 0) yoffset = 0;
  57.         if (yoffset > mapheight*mapblockheight-SCREEN_H) 
  58.             yoffset = mapheight*mapblockheight-SCREEN_H;
  59.  
  60.         //draw map with single layer
  61.         MapDrawBG(buffer, xoffset, yoffset, 0, 0, SCREEN_W-1, SCREEN_H-1);
  62.  
  63.         //print scroll position
  64.         textprintf_ex(buffer,font,0,0,WHITE,-1,
  65.             "Position = %d,%d", xoffset, yoffset);
  66.         textprintf_ex(buffer,font,200,0,WHITE,-1,
  67.             "Map size = %d,%d, Tiles = %d,%d", mapwidth, mapheight, mapblockwidth, mapblockheight);
  68.  
  69.         //blit the double buffer
  70.         blit (buffer, screen, 0, 0, 0, 0, SCREEN_W-1, SCREEN_H-1);
  71.  
  72.     }
  73.  
  74.     //delete double buffer
  75.     destroy_bitmap(buffer);
  76.  
  77.     //delete the Mappy level
  78.     MapFreeMem();
  79.  
  80.     allegro_exit();
  81.     return 0;
  82. }
  83. END_OF_MAIN()
  84.