FANDOM


Example.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:


//An example C program to do some graphics
//Also illustrates a few other things.
 
//****************************************************************************
//IMPORTANT compile this program using gcc example.c -oexample.exe -lalleg -O2
//We need the extra switch because we are using allegro now
//****************************************************************************
 
//Uses lots and lots of allegro commands. Fortunately allegro
//is very well documented.
 
//we need this include for the getch() command at the end.
#include <stdio.h>
//We need to include allegro before we can use it.
#include <allegro.h>
 
//Global variables are setup here:
 
//First we need a pointer which will keep track of where our sprite
//is stored in memory. Allegro seems to provide a new type of C pointer
//called bitmap especially for the job of pointing to graphics - which is nice!!!
BITMAP *mysprite;
 
//We also need somewhere to store the colour palette. Again there is a new
//type of C variable called PALETTE. This isn't a standard C variable, but is
//something Allegro has added for us. Note this time it isn't a pointer
//So the palette must be stored within the variable itself.
PALETTE colours;
 
main()
{
   //Setup the allegro graphics library
   //We can't use the library until we do this
   allegro_init();
 
   //Next we want to load in some graphics to mess about with.
   //do do this we use the load_tga command which needs 2 pieces of information.
   //The name of the file, and the place to store the palette in.
   //It returns a pointer to the graphic itself which
   //we store in our own pointer mysprite.
   mysprite=load_tga("graphic.tga",colours);
 
   //if mysprite is still equal to zero it obviously isn't pointing to our
   //graphic. Something must have gone wrong so we quit the program.
 
   //This is the first introduction to the if command in C
   //the condition for the 'if' is placed between the brackets, and the
   //instructions to execute if successful, between the curly brackets following
   //the 'if' instruction.
   if (mysprite==0)
   {
      printf("Error loading in graphic\n");
 
      //The return command (which we have seen earlier)
      //returns from the current function, as well as giving information back 
      //to the calling function. Being as this function is main, the calling
      //function is DOS itself! so the program quits
      return 1;
   }
 
   //Set the computer to 256 colour mode - another allegro instruction!
   set_color_depth(8);
 
   //Select a resolution of 640x480. I've tried to choose fairly stable
   //options here so it should work on just about any computer. (hopefully!)
   //although on some computers univbe might be needed, I don't know.
   //You will notice that I am setting the screen to 320x200 mode first.
   //This helps the program work on my dodgy video card! Most computers
   //wouldn't need this line, but mine does for some reason!
   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 the colour palette for the screen. This uses the colour palette we
   //stored earlier when we loaded the graphic. This is yet another allegro function.
   set_palette(colours);
 
   //Clear the screen. This is another allegro command
   //It requires one variable, which is the graphic to clear.
   //screen is a special variable provided by allegro which points to the
   //monitor itself. If we used clear(mysprite); here it would erase our
   //sprite instead!
   clear(screen);
 
   //Now we want to draw something, so we plot
   //our sprite on the screen using ANOTHER allegro function :-)
   draw_sprite(screen,mysprite,50,50);  
 
   //This line waits for keypress so we can actually see what happens
   getch();
 
   //Shut everything down neatly before we quit
   allegro_exit();
}

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.