Code Search for Developers
 
 
  

datpal.c from Allegro game programming library at Krugle


Show datpal.c syntax highlighted

/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Grabber plugin for managing palette objects.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#include <stdio.h>
#include <string.h>

#include "allegro.h"
#include "allegro/internal/aintern.h"
#include "../datedit.h"



/* creates a new palette object */
static void *makenew_palette(long *size)
{
   AL_RGB *pal = _al_malloc(sizeof(AL_PALETTE));

   memcpy(pal, al_default_palette, sizeof(AL_PALETTE));
   *size = sizeof(AL_PALETTE);

   return pal;
}



/* tests whether two palettes are identical */
static int compare_palettes(AL_RGB *p1, AL_RGB *p2)
{
   int c;

   for (c=0; c<PAL_SIZE; c++) {
      if ((p1[c].r != p2[c].r) || 
	  (p1[c].g != p2[c].g) || 
	  (p1[c].b != p2[c].b))
	 return TRUE;
   }

   return FALSE;
}



/* draws a palette onto the grabber object view window */
static void plot_palette(AL_CONST AL_DATAFILE *dat, int x, int y)
{
   int c;

   if (compare_palettes(dat->dat, datedit_current_palette)) {
      if (al_bitmap_color_depth(al_screen) > 8) {
	 al_select_palette(dat->dat);
	 for (c=0; c<PAL_SIZE; c++)
	    al_draw_rect_fill(al_screen, x+(c&15)*8, y+(c/16)*8+32, 
		     x+(c&15)*8+7, y+(c/16)*8+39, al_palette_color[c]);
	 al_unselect_palette();
	 al_put_text(al_screen, al_font_8x8, "A different palette", x+160, y+32, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "is currently in use.", x+160, y+40, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "To select this one,", x+160, y+48, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "double-click on it", x+160, y+56, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "in the item list.", x+160, y+64, al_gui_fg_color);
      }
      else {
	 al_put_text(al_screen, al_font_8x8, "A different palette is currently in use.", x, y+32, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "To select this one, double-click on it", x, y+40, al_gui_fg_color);
	 al_put_text(al_screen, al_font_8x8, "in the item list.", x, y+48, al_gui_fg_color);
      }
   }
   else {
      for (c=0; c<PAL_SIZE; c++)
	 al_draw_rect_fill(al_screen, x+(c&15)*8, y+(c/16)*8+32, 
		  x+(c&15)*8+7, y+(c/16)*8+39, al_palette_color[c]);
   }
}



/* handles double-clicking on a palette object in the grabber */
static int use_palette(AL_DATAFILE *dat)
{
   grabber_sel_palette(dat->dat);
   return D_REDRAW;
}



/* exports a palette into an external file */
static int export_palette(AL_CONST AL_DATAFILE *dat, AL_CONST char *filename)
{
   AL_BITMAP *bmp;
   int ret;

   bmp = al_create_bitmap_ex(8, 32, 8);
   al_clear_bitmap(bmp);
   al_text_mode(0);
   al_put_text(bmp, al_font_8x8, "PAL.", 0, 0, 255);

   ret = (al_save_bitmap(filename, bmp, (AL_RGB *)dat->dat) == 0);

   al_destroy_bitmap(bmp);
   return ret;
}



/* grabs a palette from an external file */
static void *grab_palette(AL_CONST char *filename, long *size, int x, int y, int w, int h, int depth)
{
   int oldcolordepth = _color_depth;
   AL_RGB *pal;
   AL_BITMAP *bmp;

   _color_depth = 8;
   al_set_color_conversion(AL_COLORCONV_TOTAL);

   pal = _al_malloc(sizeof(AL_PALETTE));
   bmp = al_load_bitmap(filename, pal);

   _color_depth = oldcolordepth;
   al_set_color_conversion(AL_COLORCONV_NONE);

   if (!bmp) {
      _al_free(pal);
      return NULL;
   }

   al_destroy_bitmap(bmp);

   *size = sizeof(AL_PALETTE);
   return pal;
}



/* checks whether our grab command is allowed at the moment */
static int grabber_query(int popup)
{
   AL_DATAFILE *dat = grabber_single_selection();

   return ((dat) && (dat->type == DAT_PALETTE) && (grabber_graphic));
}



/* menu hook for our special grab command */
static int grabber(void)
{
   AL_DATAFILE *dat = grabber_single_selection();

   if ((!dat) || (dat->type != DAT_PALETTE) || (!grabber_graphic))
      return D_O_K;

   memcpy(dat->dat, grabber_palette, sizeof(AL_PALETTE));

   datedit_set_property(dat, DAT_ORIG, grabber_graphic_origin);
   datedit_set_property(dat, DAT_DATE, grabber_graphic_date);

   al_show_alert("Palette data grabbed from the bitmap file",
	 NULL, NULL, "OK", NULL, 13, 0);

   grabber_sel_palette(dat->dat);

   datedit_sort_properties(dat->prop);
   grabber_select_property(DAT_NAME);

   return D_REDRAW;
}



/* hook ourselves into the grabber menu system */
static AL_MENU grabber_menu =
{
   "Grab",
   grabber,
   NULL,
   0,
   NULL
};



DATEDIT_MENU_INFO datpal_grabber_menu =
{
   &grabber_menu,
   grabber_query,
   DATEDIT_MENU_OBJECT,
   0
};



/* plugin interface header */
DATEDIT_OBJECT_INFO datpal_info =
{
   DAT_PALETTE, 
   "Palette", 
   NULL,
   makenew_palette,
   NULL,
   plot_palette,
   use_palette,
   NULL
};



DATEDIT_GRABBER_INFO datpal_grabber =
{
   DAT_PALETTE, 
   "bmp;lbm;pcx;tga",
   "bmp;pcx;tga",
   grab_palette,
   export_palette
};





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

  datalpha.c
  datalpha.inc
  datfli.c
  datfli.inc
  datfont.c
  datfont.inc
  datgrab.c
  datgrab.inc
  datgrid.c
  datgrid.inc
  datimage.c
  datimage.inc
  datitype.c
  datitype.inc
  datmidi.c
  datmidi.inc
  datpal.c
  datpal.inc
  datsamp.c
  datsamp.inc
  datworms.c
  datworms.inc
  plugins.txt