Advertisement
Guest User

Untitled

a guest
May 29th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.76 KB | None | 0 0
  1. namespace RPG {
  2.     /*! \brief %Image buffer with 16 bit color depth, used as canvas for multiple 8-bit images
  3.         \sa RPG::Screen::canvas
  4.         \sa RPG::Image
  5.     */
  6.     class Canvas {
  7.         public:
  8.             void **vTable;
  9.             DBitmap *bitmap; //!< Underlying 16-bit bitmap
  10.             /*! \brief Number of bytes you have to add to a pixel address to
  11.                 get the address of the pixel in the same column of the next
  12.                 pixel row (see details)
  13.                
  14.                 Normally, this number is negative because bitmaps are stored
  15.                 from bottom to top.
  16.             */
  17.             int lineSize;
  18.             /*! \brief Brightness of the screen (see details)
  19.                
  20.                 Supported values are from \c 0 (completely black) to \c 200
  21.                 (completely white). \c 100 is the normal value. This value is
  22.                 changed during "fading" screen transitions.
  23.                
  24.                 \note Values higher than \c 200 will cause the colors to go
  25.                 crazy. Of course, this can also be used as an intentional
  26.                 effect, for example the value \c 204 will only use the colors
  27.                 blue, white and black.
  28.             */
  29.             int brightness;
  30.            
  31.             /*! \brief Returns a reference to a pixel
  32.                 \param x X coordinate
  33.                 \param y Y coordinate
  34.                 \return Reference to the pixel
  35.             */
  36.             unsigned short &pixel(int x, int y);
  37.            
  38.             /*! \brief Returns a pointer to a row of pixel data
  39.                 \param y Y coordinate
  40.                 \return Pointer to the first pixel of the row
  41.                 \note If you loop through rows, it is way faster to use
  42.                 <tt>getScanline(0)</tt> once and then always add \c lineSize to
  43.                 get to the next row instead of calling \c getScanline for every
  44.                 row.
  45.             */
  46.             unsigned short *getScanline(int y);
  47.            
  48.             //! Returns the width of the bitmap
  49.             int width();
  50.            
  51.             //! Returns the height of the bitmap
  52.             int height();
  53.            
  54.             /*! \brief Draws an RPG::Image or a part of it onto the canvas
  55.                
  56.                 This function will draw a certain part of an RPG::Image onto
  57.                 the canvas. You can omit the last four parameters to draw the
  58.                 whole image.
  59.                
  60.                 The image's brightness will automatically be adjusted to match
  61.                 the canvas \c brightness. To avoid this, you can save the
  62.                 canvas \c brightness, set it to 100, draw the image and then
  63.                 restore the original \c brightness of the canvas.
  64.                 \param x X coordinate to draw to (upper-left origin)
  65.                 \param y Y coordinate to draw to (upper-left origin)
  66.                 \param image %Image to draw
  67.                 \param srcX X coordinate of the area to use from the source
  68.                 image
  69.                 \param srcY Y coordinate of the area to use from the source
  70.                 image
  71.                 \param srcWidth Width of the area to draw (defaults to the
  72.                 whole image)
  73.                 \param srcHeight Height of the area to draw (defaults to the
  74.                 whole image)
  75.                 \warning This function is quite slow, especially if you draw
  76.                 an image which uses a mask color or an \c alpha value different
  77.                 from \c 255. Please also see the \ref optimization guidelines!
  78.                 \sa RPG::Image::draw
  79.                 \sa RPG::Canvas::drawStretched
  80.                 \sa RPG::Canvas::drawCenteredZoomed
  81.             */
  82.             void draw(int x, int y, RPG::Image *image, int srcX = 0, int srcY = 0, int srcWidth = -1, int srcHeight = -1);
  83.            
  84.             /*! \brief Draws an RPG::Image or a part of it onto the canvas,
  85.                 stretched to a certain rectangle
  86.                
  87.                 This function will draw a certain part of an RPG::Image onto
  88.                 the canvas. You can omit the last four parameters to draw the
  89.                 whole image. The image (or the part of it) will be stretched
  90.                 so it fits into the given rectangle
  91.                
  92.                 The image's brightness will automatically be adjusted to match
  93.                 the canvas \c brightness. To avoid this, you can save the
  94.                 canvas \c brightness, set it to 100, draw the image and then
  95.                 restore the original \c brightness of the canvas.
  96.                 \param x X coordinate to draw to (upper-left origin)
  97.                 \param y Y coordinate to draw to (upper-left origin)
  98.                 \param width Target width of the image after stretching
  99.                 \param height Target height of the image after stretching
  100.                 \param image %Image to draw
  101.                 \param srcX X coordinate of the area to use from the source
  102.                 image
  103.                 \param srcY Y coordinate of the area to use from the source
  104.                 image
  105.                 \param srcWidth Width of the area to draw (defaults to the
  106.                 whole image)
  107.                 \param srcHeight Height of the area to draw (defaults to the
  108.                 whole image)
  109.                 \warning This function is quite slow, especially if you draw
  110.                 an image which uses a mask color or an \c alpha value different
  111.                 from \c 255. Please also see the \ref optimization guidelines!
  112.                 \sa RPG::Canvas::drawCenteredZoomed
  113.                 \sa RPG::Canvas::draw
  114.             */
  115.             void drawStretched(int x, int y, int width, int height, RPG::Image *image, int srcX = 0, int srcY = 0, int srcWidth = -1, int srcHeight = -1);
  116.  
  117.             /*! \brief Draws an RPG::Image or a part of it onto the canvas,
  118.                 centered to a certain point, and zooms it as specified
  119.                
  120.                 This function will draw a certain part of an RPG::Image onto
  121.                 the canvas. You can omit the last four parameters to draw the
  122.                 whole image. You can specify horizontal and vertical
  123.                 magnification, and the image's center.
  124.                
  125.                 The image's brightness will automatically be adjusted to match
  126.                 the canvas \c brightness. To avoid this, you can save the
  127.                 canvas \c brightness, set it to 100, draw the image and then
  128.                 restore the original \c brightness of the canvas.
  129.                 \param x X coordinate to draw to (center origin)
  130.                 \param y Y coordinate to draw to (center origin)
  131.                 \param zoomX Horizontal magnification (\c 1.0 is normal)
  132.                 \param zoomY Vertical magnification (\c 1.0 is normal)
  133.                 \param image %Image to draw
  134.                 \param srcX X coordinate of the area to use from the source
  135.                 image
  136.                 \param srcY Y coordinate of the area to use from the source
  137.                 image
  138.                 \param srcWidth Width of the area to draw (defaults to the
  139.                 whole image)
  140.                 \param srcHeight Height of the area to draw (defaults to the
  141.                 whole image)
  142.                 \warning This function is quite slow, especially if you draw
  143.                 an image which uses a mask color or an \c alpha value different
  144.                 from \c 255 or \c zoomX and/or \c zoomY values different from
  145.                 \c 1.0. Please also see the \ref optimization guidelines!
  146.                 \sa RPG::Canvas::drawStretched
  147.                 \sa RPG::Canvas::draw
  148.             */
  149.             void drawCenteredZoomed(int x, int y, double zoomX, double zoomY, RPG::Image *image, int srcX = 0, int srcY = 0, int srcWidth = -1, int srcHeight = -1);
  150.            
  151.             /*! \brief Converts a 16-bit pixel value to 24 bit
  152.                 \param pixel16 16-bit pixel value (\c RRRRRGGGGGGBBBBB)
  153.                 \return 24-bit pixel value (\c
  154.                 00000000BBBBBBBBGGGGGGGGRRRRRRRR)
  155.             */
  156.             static inline unsigned int convert16To24Bit(unsigned short pixel16) {
  157.                 unsigned int ret = (unsigned int)(pixel16 & 0xF800) >> 8;
  158.                 ret |= (unsigned int)(pixel16 & 0x7E0) << 5;
  159.                 ret |= (unsigned int)(pixel16 & 0x3F) << 19;
  160.                 return ret;
  161.             }
  162.            
  163.             /*! \brief Converts a 16-bit pixel value to 32 bit with 100%
  164.                 opacity
  165.                 \param pixel16 16-bit pixel value (\c RRRRRGGGGGGBBBBB)
  166.                 \return 32-bit pixel value (\c
  167.                 11111111BBBBBBBBGGGGGGGGRRRRRRRR)
  168.             */
  169.             static inline unsigned int convert16To32Bit(unsigned short pixel16) {
  170.                 return convert16To24Bit(pixel16) | 0xFF000000;
  171.             }
  172.            
  173.             /*! \brief Converts a 24-bit pixel value to 16 bit
  174.                 \param pixel24 24-bit pixel value (\c
  175.                 00000000BBBBBBBBGGGGGGGGRRRRRRRR)
  176.                 \return 16-bit pixel value (\c RRRRRGGGGGGBBBBB)
  177.             */
  178.             static inline unsigned short convert24To16Bit(unsigned int pixel24) {
  179.                 unsigned short ret = (unsigned short)(pixel24 & 0xF8) << 8;
  180.                 ret |= (unsigned int)(pixel24 & 0xFC00) >> 5;
  181.                 ret |= (unsigned int)(pixel24 & 0xF80000) >> 19;
  182.                 return ret;
  183.             }
  184.            
  185.             /*! \brief Converts a 32-bit pixel value to 16 bit by combining it
  186.                 with an existing pixel
  187.                
  188.                 The result is the 16-bit pixel value which is the result of
  189.                 drawing the \c pixel32 "on top" of the \c prevPixel16.
  190.                 \param pixel32 32-bit pixel value (\c AAAAAAAABBBBBBBBGGGGGGGGRRRRRRRR)
  191.                 \param prevPixel16 16-bit pixel value to use as basis for the alpha channel
  192.                 \return 16-bit pixel value (\c RRRRRGGGGGGBBBBB)
  193.             */
  194.             static inline unsigned short convert32To16Bit(unsigned int pixel32, unsigned short prevPixel16) {
  195.                 // 16 = RRRRRGGGGGGBBBBB
  196.                 // 32 = AAAAAAAABBBBBBBBGGGGGGGGRRRRRRRR
  197.                 unsigned char alpha = (unsigned char)(pixel32 >> 24);
  198.                 if(!alpha) return convert24To16Bit(pixel32);
  199.                 #define c(v1, v2) ((v1) * (alpha + 1) + (v2) * (256 - alpha))
  200.                 unsigned short ret = (unsigned short)(c((unsigned char)(pixel32), (unsigned char)(prevPixel16 << 3))) >> 3;
  201.                 ret |= (unsigned short)(c((unsigned char)(pixel32 >> 8), (unsigned char)((prevPixel16 & 0x7E0) >> 3)) & 0xFC) << 3;
  202.                 ret |= (unsigned short)(c((unsigned char)(pixel32 >> 16), (unsigned char)((prevPixel16 & 0xF800) >> 8)) & 0xF8) << 8;
  203.                 #undef c
  204.                 return ret;
  205.             }
  206.     };
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement