Example #1
0
/****************************************************************************
  Create a new sprite by cropping and taking only the given portion of
  the image.

  source gives the sprite that is to be cropped.

  x,y, width, height gives the rectangle to be cropped.  The pixel at
  position of the source sprite will be at (0,0) in the new sprite, and
  the new sprite will have dimensions (width, height).

  mask gives an additional mask to be used for clipping the new
  sprite. Only the transparency value of the mask is used in
  crop_sprite. The formula is: dest_trans = src_trans *
  mask_trans. Note that because the transparency is expressed as an
  integer it is common to divide it by 256 afterwards.

  mask_offset_x, mask_offset_y is the offset of the mask relative to the
  origin of the source image.  The pixel at (mask_offset_x,mask_offset_y)
  in the mask image will be used to clip pixel (0,0) in the source image
  which is pixel (-x,-y) in the new image.
****************************************************************************/
struct sprite *crop_sprite(struct sprite *source,
			   int x, int y, int width, int height,
			   struct sprite *mask,
			   int mask_offset_x, int mask_offset_y)
{
  SDL_Rect src_rect = {(Sint16) x, (Sint16) y, (Uint16) width, (Uint16) height};
  SDL_Surface *pSrc = crop_rect_from_surface(GET_SURF(source), &src_rect);
  SDL_Surface *pDest = NULL;

  if (mask) {
    pDest = mask_surface(pSrc, mask->psurface, x - mask_offset_x, y - mask_offset_y);
    FREESURFACE(pSrc);    
    return ctor_sprite(pDest);
  }

  return ctor_sprite(pSrc);
}
Example #2
0
/****************************************************************************
  Load the given graphics file into a sprite.  This function loads an
  entire image file, which may later be broken up into individual sprites
  with crop_sprite.
****************************************************************************/
struct sprite *load_gfxfile(const char *filename)
{
  GdkPixbuf *im;

  if (!(im = gdk_pixbuf_new_from_file(filename, NULL))) {
    freelog(LOG_FATAL, "Failed reading graphics file: \"%s\"", filename);
    exit(EXIT_FAILURE);
  }

  return ctor_sprite(im);
}
Example #3
0
/****************************************************************************
  Load the given graphics file into a sprite.  This function loads an
  entire image file, which may later be broken up into individual sprites
  with crop_sprite.
****************************************************************************/
struct sprite *load_gfxfile(const char *filename)
{
  GdkPixbuf *im;
  GError *err = NULL;

  if (!(im = gdk_pixbuf_new_from_file(filename, &err))) {
    log_fatal("Failed reading graphics file \"%s\": \"%s\"", filename, err->message);
    exit(EXIT_FAILURE);
  }

  return ctor_sprite(im);
}
Example #4
0
/***************************************************************************
...
***************************************************************************/
void load_tile_gfx(void)
{
  int i, x, y, ntiles, a;
  struct Sprite *big_sprite;
  big_sprite=load_xpmfile(datafilename("tiles.xpm"));
    
  ntiles=2*(big_sprite->width/30)*(big_sprite->height/30);

  if(!(tile_sprites=malloc(ntiles*sizeof(struct Sprite *)))) {
    log(LOG_FATAL, "couldn't malloc tile_sprites array");
    exit(1);
  }
  
  i=0;
  for(y=0, a=0; a<22 && y<big_sprite->height; a++, y+=30)
    for(x=0; x<big_sprite->width; x+=30) {
      GC plane_gc;
      Pixmap mypixmap, mask;
      
      mypixmap=XCreatePixmap(display, root_window, 30, 30, 
			     display_depth);
      XCopyArea(display, big_sprite->pixmap, mypixmap, civ_gc, 
		x, y, 30, 30, 0 ,0);

      mask=XCreatePixmap(display, root_window, 30, 30, 1);

      plane_gc = XCreateGC(display, mask, 0, NULL);

      XCopyArea(display, big_sprite->mask, mask, plane_gc, 
		x, y, 30, 30, 0 ,0);

      tile_sprites[i++]=ctor_sprite_mask(mypixmap, mask, 30, 30);

      XFreeGC(display, plane_gc);
    }

  for(x=0; x<big_sprite->width; x+=15) {
    Pixmap mypixmap;
    
    mypixmap=XCreatePixmap(display, root_window, 15, 20, 
			   display_depth);
    XCopyArea(display, big_sprite->pixmap, mypixmap, civ_gc, 
	      x, y, 15, 20, 0 ,0);
    
    tile_sprites[i++]=ctor_sprite(mypixmap, 15, 20);
 }
  

  tile_sprite_width=30;
  tile_sprite_height=30;

}
Example #5
0
/****************************************************************************
  Create a sprite with the given height, width and color.
****************************************************************************/
struct sprite *create_sprite(int width, int height, struct color *pcolor)
{
  GdkPixbuf *mypixbuf = NULL;
  GdkColor *color = NULL;

  fc_assert_ret_val(width > 0, NULL);
  fc_assert_ret_val(height > 0, NULL);
  fc_assert_ret_val(pcolor != NULL, NULL);

  color = &pcolor->color;
  mypixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
  gdk_pixbuf_fill(mypixbuf, ((guint32)(color->red & 0xff00) << 16)
                            | ((color->green & 0xff00) << 8)
                            | (color->blue & 0xff00) | 0xff);

  return ctor_sprite(mypixbuf);
}
Example #6
0
/****************************************************************************
  Create a sprite with the given height, width and color.
****************************************************************************/
struct sprite *create_sprite(int width, int height, struct color *pcolor)
{
  SDL_Surface *mypixbuf = NULL;
  SDL_Surface *pmask = NULL;

  fc_assert_ret_val(width > 0, NULL);
  fc_assert_ret_val(height > 0, NULL);
  fc_assert_ret_val(pcolor != NULL, NULL);

  mypixbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
                                  0x00ff0000, 0x0000ff00, 0x000000ff,
                                  0xff000000);
  pmask = SDL_DisplayFormatAlpha(mypixbuf);
  SDL_FillRect(mypixbuf, NULL, map_rgba(pmask->format, *pcolor->color));

  return ctor_sprite(mypixbuf);
}
Example #7
0
/****************************************************************************
  Create a new sprite by cropping and taking only the given portion of
  the image.
****************************************************************************/
struct sprite *crop_sprite(struct sprite *source,
			   int x, int y, int width, int height,
			   struct sprite *mask,
			   int mask_offset_x, int mask_offset_y)
{
  Pixmap mypixmap, mymask;
  GC plane_gc;

  mypixmap = XCreatePixmap(display, root_window,
			   width, height, display_depth);
  XCopyArea(display, source->pixmap, mypixmap, civ_gc, 
	    x, y, width, height, 0, 0);

  if (source->has_mask) {
    mymask = XCreatePixmap(display, root_window, width, height, 1);

    plane_gc = XCreateGC(display, mymask, 0, NULL);
    XCopyArea(display, source->mask, mymask, plane_gc, 
	      x, y, width, height, 0, 0);
    XFreeGC(display, plane_gc);

    if (mask) {
      XGCValues values;

      values.function = GXand;

      plane_gc = XCreateGC(display, mymask, GCFunction, &values);
      XCopyArea(display, mask->mask, mymask, plane_gc,
		x - mask_offset_x, y - mask_offset_y, width, height, 0, 0);
      XFreeGC(display, plane_gc);
    }

    return ctor_sprite_mask(mypixmap, mymask, width, height);
  } else if (mask) {
    mymask = XCreatePixmap(display, root_window, width, height, 1);

    plane_gc = XCreateGC(display, mymask, 0, NULL);
    XCopyArea(display, source->mask, mymask, plane_gc, 
	      x, y, width, height, 0, 0);
    XFreeGC(display, plane_gc);
    return ctor_sprite_mask(mypixmap, mymask, width, height);
  } else {
    return ctor_sprite(mypixmap, width, height);
  }
}
Example #8
0
/****************************************************************************
  Load the given graphics file into a sprite.  This function loads an
  entire image file, which may later be broken up into individual sprites
  with crop_sprite.
****************************************************************************/
struct sprite * load_gfxfile(const char *filename)
{
  SDL_Surface *pNew = NULL;
  SDL_Surface *pBuf = NULL;

  if ((pBuf = IMG_Load(filename)) == NULL) {
    log_error(_("load_gfxfile: Unable to load graphic file %s!"), filename);
    return NULL;		/* Should I use abotr() ? */
  }

  if (pBuf->flags & SDL_SRCCOLORKEY) {
    /* convert colorkey to alpha */
    SDL_SetColorKey(pBuf, SDL_SRCCOLORKEY, pBuf->format->colorkey);
    pNew = SDL_DisplayFormatAlpha(pBuf);
    FREESURFACE(pBuf);
    pBuf = pNew;
  }

  pNew = pBuf;
  
  return ctor_sprite(pNew);
}
Example #9
0
/****************************************************************************
  Create a new sprite by cropping and taking only the given portion of
  the image.

  source gives the sprite that is to be cropped.

  x,y, width, height gives the rectangle to be cropped.  The pixel at
  position of the source sprite will be at (0,0) in the new sprite, and
  the new sprite will have dimensions (width, height).

  mask gives an additional mask to be used for clipping the new sprite.

  mask_offset_x, mask_offset_y is the offset of the mask relative to the
  origin of the source image.  The pixel at (mask_offset_x,mask_offset_y)
  in the mask image will be used to clip pixel (0,0) in the source image
  which is pixel (-x,-y) in the new image.
****************************************************************************/
struct sprite *crop_sprite(struct sprite *source,
			   int x, int y,
			   int width, int height,
			   struct sprite *mask,
			   int mask_offset_x, int mask_offset_y)
{
  GdkPixbuf *mypixbuf, *sub, *mask_pixbuf;

  /* First just crop the image. */
  if (x < 0) {
    width += x;
    x = 0;
  }
  if (y < 0) {
    height += y;
    y = 0;
  }
  width = CLIP(0, width, source->width - x);
  height = CLIP(0, height, source->height - y);
  sub = gdk_pixbuf_new_subpixbuf(sprite_get_pixbuf(source), x, y,
				 width, height);
  mypixbuf = gdk_pixbuf_copy(sub);
  g_object_unref(sub);

  /* Now mask.  This reduces the alpha of the final image proportional to the
   * alpha of the mask.  Thus if the mask has 50% alpha the final image will
   * be reduced by 50% alpha.  Note that the mask offset is in coordinates
   * relative to the clipped image not the final image. */
  if (mask
      && (mask_pixbuf = sprite_get_pixbuf(mask))
      && gdk_pixbuf_get_has_alpha(mask_pixbuf)) {
    int x1, y1;

    /* The mask offset is the offset of the mask relative to the origin
     * of the original source image.  For instance when cropping with
     * blending sprites the offset is always 0.  Here we convert the
     * coordinates so that they are relative to the origin of the new
     * (cropped) image. */
    mask_offset_x -= x;
    mask_offset_y -= y;

    width = CLIP(0, width, mask->width + mask_offset_x);
    height = CLIP(0, height, mask->height + mask_offset_y);

    if (!gdk_pixbuf_get_has_alpha(mypixbuf)) {
      GdkPixbuf *p2 = mypixbuf;

      mypixbuf = gdk_pixbuf_add_alpha(mypixbuf, FALSE, 0, 0, 0);
      g_object_unref(p2);
    }

    for (x1 = 0; x1 < width; x1++) {
      for (y1 = 0; y1 < height; y1++) {
	int mask_x = x1 - mask_offset_x, mask_y = y1 - mask_offset_y;
	guchar *alpha = gdk_pixbuf_get_pixels(mypixbuf)
	  + y1 * gdk_pixbuf_get_rowstride(mypixbuf)
	  + x1 * gdk_pixbuf_get_n_channels(mypixbuf)
	  + 3;
	guchar *mask_alpha = gdk_pixbuf_get_pixels(mask_pixbuf)
	  + mask_y * gdk_pixbuf_get_rowstride(mask_pixbuf)
	  + mask_x * gdk_pixbuf_get_n_channels(mask_pixbuf)
	  + 3;

	*alpha = (*alpha) * (*mask_alpha) / 255;
      }
    }
  }

  return ctor_sprite(mypixbuf);
}
Example #10
0
/****************************************************************************
  Scales a sprite. If the sprite contains a mask, the mask is scaled
  as as well.
****************************************************************************/
struct sprite *sprite_scale(struct sprite *src, int new_w, int new_h)
{
  return ctor_sprite(gdk_pixbuf_scale_simple(sprite_get_pixbuf(src),
					     new_w, new_h,
					     GDK_INTERP_BILINEAR));
}