Code Search for Developers
 
 
  

exaccel.c from Allegro game programming library at Krugle


Show exaccel.c syntax highlighted

/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to use an offscreen part of the 
 *    video memory to store source graphics for a hardware accelerated 
 *    graphics driver.
 */


#include "allegro.h"



#define MAX_IMAGES      256


/* structure to hold the current position and velocity of an image */
typedef struct IMAGE
{
   float x, y;
   float dx, dy;
} IMAGE;



/* initialises an image structure to a random position and velocity */
void init_image(IMAGE *image)
{
   image->x = (float)(rand() % 704);
   image->y = (float)(rand() % 568);
   image->dx = (float)((rand() % 255) - 127) / 32.0;
   image->dy = (float)((rand() % 255) - 127) / 32.0;
}



/* called once per frame to bounce an image around the al_screen */
void update_image(IMAGE *image)
{
   image->x += image->dx;
   image->y += image->dy;

   if (((image->x < 0) && (image->dx < 0)) ||
       ((image->x > 703) && (image->dx > 0)))
      image->dx *= -1;

   if (((image->y < 0) && (image->dy < 0)) ||
       ((image->y > 567) && (image->dy > 0)))
      image->dy *= -1;
}



int main(int argc, char *argv[])
{
   char buf[256];
   AL_PALETTE pal;
   AL_BITMAP *image;
   AL_BITMAP *page[2];
   AL_BITMAP *vimage;
   IMAGE images[MAX_IMAGES];
   int num_images = 4;
   int page_num = 1;
   int done = FALSE;
   int i;

   allegro_init(); 
   al_install_keyboard(); 
   al_install_timer();

   /* very high res video mode */
   if (al_set_gfx_mode(AL_GFX_AUTODETECT, 1024, 768, 0, 0) != 0) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Error setting graphics mode\n%s\n", al_error);
      return 1;
   }

   /* read in the source graphic */
   al_replace_filename(buf, argv[0], "mysha.pcx", sizeof(buf));
   image = al_load_bitmap(buf, pal);
   if (!image) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Error reading %s!\n", buf);
      return 1;
   }

   al_set_palette(pal);

   /* initialise the images to random positions */
   for (i=0; i<MAX_IMAGES; i++)
      init_image(images+i);

   /* create two video memory bitmaps for page flipping */
   page[0] = al_create_video_bitmap(AL_SCREEN_W, AL_SCREEN_H);
   page[1] = al_create_video_bitmap(AL_SCREEN_W, AL_SCREEN_H);

   /* create a video memory bitmap to store our picture */
   vimage = al_create_video_bitmap(image->w, image->h);

   if ((!page[0]) || (!page[1]) || (!vimage)) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Not enough video memory (need two 1024x768 pages and a 320x200 image)\n");
      return 1;
   }

   /* copy the picture into offscreen video memory */
   al_blit(image, vimage, 0, 0, 0, 0, image->w, image->h);

   while (!done) {
      al_acquire_bitmap(page[page_num]);

      /* clear the al_screen */
      al_clear_bitmap(page[page_num]);

      /* draw onto it */
      for (i=0; i<num_images; i++)
	 al_blit(vimage, page[page_num], 0, 0, images[i].x, images[i].y, vimage->w, vimage->h);

      al_text_mode(-1);
      al_printf_text(page[page_num], al_font_8x8, 0, 0, 255, "Images: %d (arrow keys to change)", num_images);

      /* tell the user which functions are being done in hardware */
      if (al_gfx_capabilities & AL_GFX_HW_FILL)
	 al_put_text(page[page_num], al_font_8x8, "Clear: hardware accelerated", 0, 16, 255);
      else
	 al_put_text(page[page_num], al_font_8x8, "Clear: software (urgh, this is not good!)", 0, 16, 255);

      if (al_gfx_capabilities & AL_GFX_HW_VRAM_BLIT)
	 al_put_text(page[page_num], al_font_8x8, "Blit: hardware accelerated", 0, 32, 255);
      else
	 al_put_text(page[page_num], al_font_8x8, "Blit: software (urgh, this program will run too sloooooowly without hardware acceleration!)", 0, 32, 255);

      al_release_bitmap(page[page_num]);

      /* page flip */
      al_show_video_bitmap(page[page_num]);
      page_num = 1-page_num;

      /* deal with keyboard input */
      while (al_key_pressed()) {
	 switch (al_read_key()>>8) {

	    case AL_KEY_UP:
	    case AL_KEY_RIGHT:
	       if (num_images < MAX_IMAGES)
		  num_images++;
	       break;

	    case AL_KEY_DOWN:
	    case AL_KEY_LEFT:
	       if (num_images > 0)
		  num_images--;
	       break;

	    case AL_KEY_ESC:
	       done = TRUE;
	       break;
	 }
      }

      /* bounce the images around the al_screen */
      for (i=0; i<num_images; i++)
	 update_image(images+i);
   }

   al_destroy_bitmap(image);
   al_destroy_bitmap(vimage);
   al_destroy_bitmap(page[0]);
   al_destroy_bitmap(page[1]);

   return 0;
}

AL_END_OF_MAIN();




See more files for this project here

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  allegro.pcx
  ex12bit.c
  ex3buf.c
  ex3d.c
  exaccel.c
  exalpha.c
  example.dat
  example.h
  examples.txt
  exbitmap.c
  exblend.c
  excamera.c
  excolmap.c
  excustom.c
  exdata.c
  exdbuf.c
  exdodgy.c
  exexedat.c
  exfixed.c
  exflame.c
  exflip.c
  exgui.c
  exhello.c
  exjoy.c
  exkeys.c
  exlights.c
  exmem.c
  exmidi.c
  exmouse.c
  expal.c
  expat.c
  exquat.c
  exrgbhsv.c
  exsample.c
  exscale.c
  exscn3d.c
  exscroll.c
  exshade.c
  exspline.c
  exsprite.c
  exstars.c
  exstream.c
  exswitch.c
  extimer.c
  extrans.c
  extruec.c
  exunicod.c
  exupdate.c
  exxfade.c
  exzbuf.c
  mysha.pcx
  planet.pcx
  running.dat
  running.h
  unifont.dat