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

  1. /////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 16 - PlatformScroller
  4. /////////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <allegro.h>
  8. #include "mappyal.h"
  9.  
  10. #define MODE GFX_AUTODETECT_WINDOWED
  11. #define WIDTH 640
  12. #define HEIGHT 480
  13. #define JUMPIT 1600
  14.  
  15. //define the sprite structure
  16. typedef struct SPRITE
  17. {
  18.     int dir, alive;
  19.     int x,y;
  20.     int width,height;
  21.     int xspeed,yspeed;
  22.     int xdelay,ydelay;
  23.     int xcount,ycount;
  24.     int curframe,maxframe,animdir;
  25.     int framecount,framedelay;
  26. }SPRITE;
  27.  
  28. //declare the bitmaps and sprites
  29. BITMAP *player_image[8];
  30. SPRITE *player;
  31. BITMAP *buffer;    
  32. BITMAP *temp;
  33.  
  34.  
  35. //tile grabber
  36. BITMAP *grabframe(BITMAP *source, 
  37.                   int width, int height, 
  38.                   int startx, int starty, 
  39.                   int columns, int frame)
  40. {
  41.     BITMAP *temp = create_bitmap(width,height);
  42.     int x = startx + (frame % columns) * width;
  43.     int y = starty + (frame / columns) * height;
  44.     blit(source,temp,x,y,0,0,width,height);
  45.     return temp;
  46. }
  47.  
  48.  
  49. int collided(int x, int y)
  50. {
  51.     BLKSTR *blockdata;
  52.     blockdata = MapGetBlock(x/mapblockwidth, y/mapblockheight);
  53.     return blockdata->tl;
  54. }
  55.  
  56.  
  57. int main (void)
  58. {
  59.     int mapxoff, mapyoff;
  60.     int oldpy, oldpx;
  61.     int facing = 0;
  62.     int jump = JUMPIT;
  63.     int n;
  64.  
  65.     allegro_init();    
  66.     install_timer();
  67.     install_keyboard();
  68.     set_color_depth(16);
  69.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  70.  
  71.     temp = load_bitmap("guy.bmp", NULL);
  72.     for (n=0; n<8; n++)
  73.         player_image[n] = grabframe(temp,50,64,0,0,8,n);
  74.     destroy_bitmap(temp);
  75.  
  76.     player = malloc(sizeof(SPRITE));
  77.     player->x = 80;
  78.     player->y = 100;
  79.     player->curframe=0;
  80.     player->framecount=0;
  81.     player->framedelay=6;
  82.     player->maxframe=7;
  83.     player->width=player_image[0]->w;
  84.     player->height=player_image[0]->h;
  85.  
  86.     //load the map
  87.     MapLoad("sample.fmp");
  88.  
  89.     //create the double buffer
  90.     buffer = create_bitmap (WIDTH, HEIGHT);
  91.     clear(buffer);
  92.  
  93.     //main loop
  94.     while (!key[KEY_ESC])
  95.     {
  96.  
  97.         oldpy = player->y; 
  98.         oldpx = player->x;
  99.  
  100.         if (key[KEY_RIGHT]) 
  101.         { 
  102.             facing = 1; 
  103.             player->x+=2; 
  104.             if (++player->framecount > player->framedelay)
  105.             {
  106.                 player->framecount=0;
  107.                 if (++player->curframe > player->maxframe)
  108.                     player->curframe=1;
  109.             }
  110.         }
  111.         else if (key[KEY_LEFT]) 
  112.         { 
  113.             facing = 0; 
  114.             player->x-=2; 
  115.             if (++player->framecount > player->framedelay)
  116.             {
  117.                 player->framecount=0;
  118.                 if (++player->curframe > player->maxframe)
  119.                     player->curframe=1;
  120.             }
  121.         }
  122.         else player->curframe=0;
  123.  
  124.         //handle jumping
  125.         if (jump==JUMPIT)
  126.         { 
  127.             if (!collided(player->x + player->width/2, 
  128.                 player->y + player->height + 5))
  129.                 jump = 0; 
  130.  
  131.             if (key[KEY_SPACE]) 
  132.                 jump = 30;
  133.         }
  134.         else
  135.         {
  136.             player->y -= jump/3; 
  137.             jump--; 
  138.         }
  139.  
  140.         if (jump<0) 
  141.         { 
  142.             if (collided(player->x + player->width/2, 
  143.                 player->y + player->height))
  144.             { 
  145.                 jump = JUMPIT; 
  146.                 while (collided(player->x + player->width/2, 
  147.                     player->y + player->height))
  148.                     player->y -= 2; 
  149.             } 
  150.         }
  151.  
  152.         //check for collided with foreground tiles
  153.         if (!facing) 
  154.         { 
  155.             if (collided(player->x, player->y + player->height)) 
  156.                 player->x = oldpx; 
  157.         }
  158.         else 
  159.         { 
  160.             if (collided(player->x + player->width, 
  161.                 player->y + player->height)) 
  162.                 player->x = oldpx; 
  163.         }
  164.         
  165.         //update the map scroll position
  166.         mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
  167.         mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;
  168.  
  169.  
  170.         //avoid moving beyond the map edge
  171.         if (mapxoff < 0) mapxoff = 0;
  172.         if (mapxoff > (mapwidth * mapblockwidth - WIDTH))
  173.             mapxoff = mapwidth * mapblockwidth - WIDTH;
  174.         if (mapyoff < 0) 
  175.             mapyoff = 0;
  176.         if (mapyoff > (mapheight * mapblockheight - HEIGHT)) 
  177.             mapyoff = mapheight * mapblockheight - HEIGHT;
  178.  
  179.         //draw the background tiles
  180.         MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1);
  181.  
  182.         //draw foreground tiles
  183.         MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 0);
  184.  
  185.         //draw the player's sprite
  186.         if (facing) 
  187.             draw_sprite(buffer, player_image[player->curframe], 
  188.                 (player->x-mapxoff), (player->y-mapyoff+1));
  189.         else 
  190.             draw_sprite_h_flip(buffer, player_image[player->curframe], 
  191.                 (player->x-mapxoff), (player->y-mapyoff));
  192.  
  193.         //blit the double buffer 
  194.         vsync();
  195.         acquire_screen();
  196.         blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
  197.         release_screen();
  198.  
  199.     } //while
  200.  
  201.     for (n=0; n<8; n++)
  202.         destroy_bitmap(player_image[n]);
  203.     free(player);
  204.     destroy_bitmap(buffer);
  205.     MapFreeMem ();
  206.     allegro_exit();
  207.     return 0;
  208. }
  209. END_OF_MAIN()
  210.