< index
< 5. Image toolkit

=====================================
5.10 Blitting an image on a console
=====================================

You can draw an image on a console (by changing the cells background color) :
The first function allows you to specify the floating point coordinates of the center
of the image, and its scale and rotation angle.
The image will be rendered with sub-cell accuracy (no to be confused with sub-cell resolution, which is not yet implemented and should appear only with libtcod 1.5). For example, if you increase x by 0.01 per frame,
you will achieve a smooth scrolling effect.

C++ : void TCODImage::blit(TCODConsole *console, float x, float y, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET, float scalex=1.0f, float scaley=1.0f, float angle=0.0f) const
C   : void TCOD_image_blit(TCOD_image_t image, TCOD_console_t console, int x, int y, TCOD_bkgnd_flag_t bkgnd_flag, float scalex, float scaley, float angle)
Py  : image_blit(image, console, x, y, bkgnd_flag, scalex, scaley, angle)

ParameterDescription
imageIn the C version, the image handler, obtained with the load function.
consoleThe console on which the image will be drawn. In the C version, use NULL for the root console.
x,yCoordinates in the console of the center of the image.
flagThis flag defines how the cell's background color is modified. See TCOD_bkgnd_flag_t.
scaleScale coefficient. Must be > 0.0.
angleRotation angle in radians.
Example :

C++ : TCODImage *pix = TCODImage("mypix.bmp");
      pix->blit(TCODConsole::root,40.0f,25.0f);
C   : TCOD_image_t pix = TCOD_image_new(80,50);
      TCOD_image_blit(pix,NULL,40,25,TCOD_BKGND_SET,1.0f,1.0f,0.0f);
Py  : pix = libtcod.image_new(80,50)
      libtcod.image_blit(pix,0,40,25,libtcod.BKGND_SET,1.0,1.0,0.0)


The second function allows you to easily map an image to a specific part of a console, by
specifying a rectangular part of the console (upper-left corner and size).

C++ : void TCODImage::blitRect(TCODConsole *console, int x, int y, int w=-1, int h=-1, TCOD_bkgnd_flag_t bkgnd_flag = TCOD_BKGND_SET ) const
C   : void TCOD_image_blit_rect(TCOD_image_t image, TCOD_console_t console, int x, int y, int w, int h, TCOD_bkgnd_flag_t bkgnd_flag)
Py  : image_blit_rect(image, console, x, y, w, h, bkgnd_flag)

ParameterDescription
imageIn the C version, the image handler, obtained with the load function.
consoleThe console on which the image will be drawn. In the C version, use NULL for the root console.
x,yCoordinates in the console of the upper-left corner of the image.
w,hDimension of the image on the console. Use -1,-1 to use the image size.
flagThis flag defines how the cell's background color is modified. See TCOD_bkgnd_flag_t.
Example :

C++ : TCODImage *pix = TCODImage("mypix.bmp");
      // blitting the image without scaling it
      pix->blitRect(TCODConsole::root,40,25);
C   : TCOD_image_t pix = TCOD_image_new(10,10);
      /* down-scaling a 10x10 image to a 5x5 zone */
      TCOD_image_blit_rect(pix,NULL,40,25,5,5,TCOD_BKGND_SET);
Py  : pix = libtcod.image_new(10,10)
      # down-scaling a 10x10 image to a 5x5 zone
      libtcod.image_blit_rect(pix,0,40,25,5,5,libtcod.BKGND_SET)


When blitting an image, you can define a key color that will be ignored by the blitting function. This makes it possible to blit non rectangular images or images with transparent pixels.

C++ : void TCODImage::setKeyColor(const TCODColor keyColor)
C   : void TCOD_image_set_key_color(TCOD_image_t image, TCOD_color_t keyColor)
Py  : image_set_key_color(image, keyColor)

ParameterDescription
imageIn the C and python version, the image handler, obtained with the load function.
colorPixels with this color will be skipped by blitting functions.
Example :

C++ : TCODImage *pix = TCODImage("mypix.bmp");
      // set pink as key color
      pix->setKeyColor(TCODColor(255,0,255));
      // blitting the image, omitting pink pixels
      pix->blitRect(TCODConsole::root,40,25);
C   : TCOD_image_t pix = TCOD_image_new(10,10);
      TCOD_image_set_key_color(pix,TCOD_red);
      TCOD_image_blit_rect(pix,NULL,40,25,5,5,TCOD_BKGND_SET);
Py  : pix = libtcod.image_new(10,10)
      libtcod.image_set_key_color(pix,libtcod.red)
      libtcod.image_blit_rect(pix,0,40,25,5,5,libtcod.BKGND_SET)

insert a comment