Code Search for Developers
 
 
  

exshade.c from Allegro game programming library at Krugle


Show exshade.c syntax highlighted

/*
 *    Example program for the Allegro library, by Patrick Hogan
 *
 *    This program demonstrates how to draw gouraud shaded (lit) sprites.
 */


#include <math.h>

#include "allegro.h"



/* AL_RGB -> color mapping table. Not needed, but speeds things up */
AL_RGB_MAP rgb_table;

AL_COLOR_MAP light_table;



int distance(int x1, int y1, int x2, int y2)
{
   int dx = x2 - x1;
   int dy = y2 - y1;
   int temp = sqrt((dx * dx) + (dy * dy));

   temp *= 2;
   if (temp > 255)
      temp = 255;

   return (255 - temp);
}



int main(int argc, char *argv[])
{
   AL_PALETTE pal;
   AL_BITMAP *buffer;
   AL_BITMAP *planet;
   char buf[256];

   allegro_init();
   al_install_keyboard();
   al_install_mouse();
   if (al_set_gfx_mode(AL_GFX_SAFE, 320, 240, 0, 0) != 0) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Unable to set any graphic mode\n%s\n", al_error);
      return 1;
   }
   al_text_mode(-1);

   al_replace_filename(buf, argv[0], "planet.pcx", sizeof(buf));

   planet = al_load_bitmap(buf, pal);
   if (!planet) {
      al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
      al_show_message("Error reading %s\n", buf);
      return 1;
   }

   buffer = al_create_bitmap(AL_SCREEN_W, AL_SCREEN_H);
   al_clear_bitmap(buffer);

   al_set_palette(pal);

   al_create_rgb_table(&rgb_table, pal, NULL);
   al_rgb_map = &rgb_table;

   al_create_light_table(&light_table, pal, 0, 0, 0, NULL);
   al_color_map = &light_table;

   al_set_trans_blender(0, 0, 0, 128);

   do {
      al_poll_mouse();

      al_draw_gouraud_sprite(buffer, planet, AL_SCREEN_W/2, AL_SCREEN_H/2,
			  distance(AL_SCREEN_W/2, AL_SCREEN_H/2, al_mouse_x, al_mouse_y),
			  distance(AL_SCREEN_W/2 + planet->w, AL_SCREEN_H/2, al_mouse_x, al_mouse_y),
			  distance(AL_SCREEN_W/2 + planet->w, AL_SCREEN_H/2 + planet->h, al_mouse_x, al_mouse_y),
			  distance(AL_SCREEN_W/2, AL_SCREEN_H/2 + planet->h, al_mouse_x, al_mouse_y));

      al_put_text(buffer, al_font_8x8, "Gouraud Shaded Sprite Demo", 0, 0, al_palette_color[10]);

      al_show_mouse(buffer);
      al_blit(buffer, al_screen, 0, 0, 0, 0, AL_SCREEN_W, AL_SCREEN_H);
      al_show_mouse(NULL);

   } while (!al_key_pressed());

   al_destroy_bitmap(planet);
   al_destroy_bitmap(buffer);

   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