Code Search for Developers
 
 
  

datitype.c from Allegro game programming library at Krugle


Show datitype.c syntax highlighted

/*         ______   ___    ___ 
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Grabber plugin for converting between different image formats
 *      and color depths.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#include <stdio.h>

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



/* worker function for counting bitmap objects */
static int do_bitmap_check(AL_DATAFILE *dat, int *param, int param2)
{
   if ((dat->type == DAT_BITMAP) || (dat->type == DAT_RLE_SPRITE) ||
       (dat->type == DAT_C_SPRITE) || (dat->type == DAT_XC_SPRITE)) 
      (*param)++;

   return D_O_K;
}



/* checks whether our image changing commands are allowed at the moment */
static int change_query(int popup)
{
   int n, p;

   if (popup) {
      p = 0;
      grabber_foreach_selection(do_bitmap_check, &n, &p, 0);
      return (p > 0);
   }

   return TRUE;
}



/* worker function for changing the type of an image */
static int do_changetype(AL_DATAFILE *dat, int *param, int type)
{
   AL_BITMAP *bmp;
   AL_RLE_SPRITE *spr;
   int x, y, c, r, g, b;

   if ((dat->type != DAT_BITMAP) && (dat->type != DAT_RLE_SPRITE) &&
       (dat->type != DAT_C_SPRITE) && (dat->type != DAT_XC_SPRITE)) {
      (*param)++;
      return D_O_K;
   }

   if (dat->type == type)
      return D_O_K;

   if (dat->type == DAT_RLE_SPRITE) {
      spr = (AL_RLE_SPRITE *)dat->dat;
      bmp = al_create_bitmap_ex(spr->color_depth, spr->w, spr->h);
      al_clear_to_color(bmp, bmp->vtable->mask_color);
      al_draw_rle_sprite(bmp, spr, 0, 0);
      dat->dat = bmp;
      al_destroy_rle_sprite(spr);
   }
   else if (type == DAT_RLE_SPRITE) {
      bmp = (AL_BITMAP *)dat->dat;
      spr = al_create_rle_sprite(bmp);
      dat->dat = spr;
      al_destroy_bitmap(bmp);
   }
   else if ((type == DAT_C_SPRITE) || (type == DAT_XC_SPRITE)) {
      bmp = (AL_BITMAP *)dat->dat;
      if (al_bitmap_color_depth(bmp) == 32) {
	 for (y=0; y<bmp->h; y++) {
	    for (x=0; x<bmp->w; x++) {
	       c = al_get_pixel(bmp, x, y);
	       r = al_get_r32(c);
	       g = al_get_g32(c);
	       b = al_get_b32(c);
	       al_put_pixel(bmp, x, y, al_make_color_32(r, g, b));
	    }
	 }
      }
   }

   dat->type = type;

   return D_REDRAW;
}



/* changes the type of bitmap data */
static int changetype(void)
{
   int type = (int)al_active_menu->dp;
   char buf[80];
   int ret, n;
   int p = 0;

   ret = grabber_foreach_selection(do_changetype, &n, &p, type);

   if (n <= 0) {
      al_show_alert ("Nothing to re-type!", NULL, NULL, "OK", NULL, 13, 0);
   }
   else if (p > 0) {
      sprintf(buf, "%d non-bitmap object%s ignored", p, (p==1) ? " was" : "s were");
      al_show_alert(buf, NULL, NULL, "OK", NULL, 13, 0);
   }

   if (n > p)
      grabber_rebuild_list(NULL, FALSE);

   return ret;
}



/* worker function for changing the color depth of bitmap data */
static int do_changedepth(AL_DATAFILE *dat, int *param, int depth)
{
   AL_BITMAP *bmp, *bmp2;
   AL_RLE_SPRITE *spr;
   AL_RGB tmprgb = datedit_current_palette[0];

   if ((dat->type != DAT_BITMAP) && (dat->type != DAT_RLE_SPRITE) &&
       (dat->type != DAT_C_SPRITE) && (dat->type != DAT_XC_SPRITE)) {
      (*param)++;
      return D_O_K;
   }

   if (dat->type == DAT_RLE_SPRITE) {
      spr = (AL_RLE_SPRITE *)dat->dat;
      if (spr->color_depth == depth)
	 return D_O_K;

      bmp = al_create_bitmap_ex(spr->color_depth, spr->w, spr->h);
      al_clear_to_color(bmp, bmp->vtable->mask_color);
      al_draw_rle_sprite(bmp, spr, 0, 0);
      bmp2 = al_create_bitmap_ex(depth, bmp->w, bmp->h);

      datedit_current_palette[0].r = 63;
      datedit_current_palette[0].g = 0;
      datedit_current_palette[0].b = 63;
      al_select_palette(datedit_current_palette);

      al_blit(bmp, bmp2, 0, 0, 0, 0, bmp->w, bmp->h);

      al_unselect_palette();
      datedit_current_palette[0] = tmprgb;

      dat->dat = al_create_rle_sprite(bmp2);
      al_destroy_bitmap(bmp);
      al_destroy_bitmap(bmp2);
      al_destroy_rle_sprite(spr);
   }
   else {
      bmp = (AL_BITMAP *)dat->dat;
      if (al_bitmap_color_depth(bmp) == depth)
	 return D_O_K;
      bmp2 = al_create_bitmap_ex(depth, bmp->w, bmp->h);

      if ((dat->type == DAT_C_SPRITE) || (dat->type == DAT_XC_SPRITE)) {
	 datedit_current_palette[0].r = 63;
	 datedit_current_palette[0].g = 0;
	 datedit_current_palette[0].b = 63;
      }
      al_select_palette(datedit_current_palette);

      al_blit(bmp, bmp2, 0, 0, 0, 0, bmp->w, bmp->h);

      al_unselect_palette();
      datedit_current_palette[0] = tmprgb;

      dat->dat = bmp2;
      al_destroy_bitmap(bmp);
   }

   return D_REDRAW;
}



/* changes the color depth of bitmap data */
static int changedepth(void)
{
   int depth = (int)al_active_menu->dp;
   char buf[80];
   int ret, n;
   int p = 0;

   grabber_busy_mouse(TRUE);

   ret = grabber_foreach_selection(do_changedepth, &n, &p, depth);

   grabber_busy_mouse(FALSE);

   if (n <= 0) {
      al_show_alert ("Nothing to re-format!", NULL, NULL, "OK", NULL, 13, 0);
   }
   else if (p > 0) {
      sprintf(buf, "%d non-bitmap object%s ignored", p, (p==1) ? " was" : "s were");
      al_show_alert(buf, NULL, NULL, "OK", NULL, 13, 0);
   }

   return ret;
}



static AL_MENU type_menu[] =
{
   { "To &Bitmap",               changetype,    NULL,    0,    (void *)DAT_BITMAP      },
   { "To &RLE Sprite",           changetype,    NULL,    0,    (void *)DAT_RLE_SPRITE  },
   { "To &Compiled Sprite",      changetype,    NULL,    0,    (void *)DAT_C_SPRITE    },
   { "To &X-Compiled Sprite",    changetype,    NULL,    0,    (void *)DAT_XC_SPRITE   },
   { NULL,                       NULL,          NULL,    0,    NULL                    }
};



static AL_MENU depth_menu[] =
{
   { "&256 color palette",       changedepth,   NULL,    0,    (void *)8   },
   { "1&5 bit hicolor",          changedepth,   NULL,    0,    (void *)15  },
   { "1&6 bit hicolor",          changedepth,   NULL,    0,    (void *)16  },
   { "2&4 bit truecolor",        changedepth,   NULL,    0,    (void *)24  },
   { "&32 bit truecolor",        changedepth,   NULL,    0,    (void *)32  },
   { NULL,                       NULL,          NULL,    0,    NULL        }
};



/* hook ourselves into the grabber menu system */
static AL_MENU change_type_menu =
{
   "Change Type",
   NULL,
   type_menu,
   0,
   NULL
};



DATEDIT_MENU_INFO datitype_type_menu =
{
   &change_type_menu,
   change_query,
   DATEDIT_MENU_OBJECT | DATEDIT_MENU_POPUP,
   0
};



static AL_MENU change_depth_menu =
{
   "Color Depth",
   NULL,
   depth_menu,
   0,
   NULL
};



DATEDIT_MENU_INFO datitype_depth_menu =
{
   &change_depth_menu,
   change_query,
   DATEDIT_MENU_OBJECT | DATEDIT_MENU_POPUP,
   0
};






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