FANDOM


Example3.c was part of the Zip with commented example program in 3 stages guide that Andrew Gower made for his site around 1998. When you clicked to open the guide this and three other files would download onto your computer. This one read:


//This last program is only very slightly different from example 2
//It has been modified to bounce LOTS of sprites around the screen!
//This one actually looks vaguelly impressive!
 
//We need to include allegro before we can use it.
#include <allegro.h>
 
//Global variables are setup here:
 
BITMAP *mysprite;
PALETTE colours;
 
//setup the double buffering
int n, page=0;
BITMAP *physic, *back, *page1, *page2;
 
main()
{
   //Local variables are setup here
   int num=25, n, x[num], y[num], xd[num], yd[num];
 
   //set the sprites to interesting initial positions and speeds
   for (n=0; n<num; n++) {x[n]=n*20; y[n]=n*10; xd[n]=(n%5)+5; yd[n]=(n/5)+5;}
 
   //Setup the allegro graphics library
   allegro_init();
 
   //Next we want to load in some graphics to mess about with
   mysprite=load_tga("graphic.tga",colours);
   if (mysprite==0)
   {
      printf("Error loading in graphic\n");
      return 1;
   }
 
   //Set the computer to 256 colour mode
   set_color_depth(8);
 
   //Select a resolution of 640x480. 
   set_gfx_mode(GFX_AUTODETECT,320,200,0,0);
   if (set_gfx_mode(GFX_AUTODETECT,640,480,640,480*2)!=0)
   {
      printf("Error setting video mode\n");
      return 1;
   }
 
   //Set up the physic and back screens which work like in STOS
   //we see physic, and write to back
   page1=create_sub_bitmap(screen,0,0,640,480);
   page2=create_sub_bitmap(screen,0,480,640,480);
   page=0; screen_swap();
 
   //Set the colour palette for the screen
   set_palette(colours);
 
   //Main Program Loop Is Here!
   //This loops infintely so use CTRL-C to exit
   while(1==1)
   {
      //Clear the screen
      clear(back);
 
      //Now we want to draw something, so we plot
      //our sprites on the screen using a 'for' loop
      for (n=0; n<num; n++)
      {
        draw_sprite(back,mysprite,x[n],y[n]);
      }
 
      //Now bounce the sprites around the screen a bit
      //again using a 'for' loop to move them all.
      for (n=0; n<num; n++)
      {
         x[n]=x[n]+xd[n]; y[n]=y[n]+yd[n];
         if (x[n]>640-64 || x[n]<0) {xd[n]=-xd[n];}
         if (y[n]>480-64 || y[n]<0) {yd[n]=-yd[n];}
      }
 
      //just like the STOS command!
      screen_swap();
   }
 
   //Shut everything down neatly before we quit
   //This line never actually gets called at the moment,
   //but it's a good idea to put at here anyway
   //in case I ever change the program.
   allegro_exit();
}
 
screen_swap()
{
   page=480-page;
   scroll_screen(0,page);
   if (page==0) {physic=page1; back=page2;}   
   else {physic=page2; back=page1;}
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.