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

  1. //////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 12 - ScrollScreen
  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.  
  14. //virtual buffer variable
  15. BITMAP *scroll;
  16.  
  17. //position variables
  18. int x=0, y=0;
  19.  
  20. //main function
  21. int main(void)
  22. {
  23.     //initialize allegro
  24.     allegro_init();
  25.     install_keyboard();
  26.     install_timer();
  27.     set_color_depth(16);
  28.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  29.  
  30.     //load the large bitmap image from disk
  31.     scroll = load_bitmap("bigbg.bmp", NULL);
  32.  
  33.     //main loop
  34.     while (!key[KEY_ESC])
  35.     {
  36.         //check right arrow
  37.         if (key[KEY_RIGHT])
  38.         {
  39.             x += STEP;
  40.             if (x > scroll->w - WIDTH)
  41.                 x = scroll->w - WIDTH;
  42.         }
  43.  
  44.         //check left arrow
  45.         if (key[KEY_LEFT])
  46.         {
  47.             x -= STEP;
  48.             if (x < 0)
  49.                 x = 0;
  50.         }
  51.  
  52.         //check down arrow
  53.         if (key[KEY_DOWN])
  54.         {
  55.             y += STEP;
  56.             if (y > scroll->h - HEIGHT)
  57.                 y = scroll->h - HEIGHT;
  58.         }
  59.  
  60.         //check up arrow
  61.         if (key[KEY_UP])
  62.         {
  63.             y -= STEP;
  64.             if (y < 0)
  65.                 y = 0;
  66.         }
  67.  
  68.         //draw the scroll window portion of the virtual buffer
  69.         blit(scroll, screen, x, y, 0, 0, WIDTH-1, HEIGHT-1);
  70.  
  71.         //slow it down
  72.         rest(20);
  73.     }
  74.  
  75.     allegro_exit();
  76.     return 0;
  77. }
  78. END_OF_MAIN()
  79.