Back to index

kdeartwork  4.3.2
Public Member Functions | Protected Member Functions | Private Attributes
Frame Class Reference

Represents a single frame of a sprite's animation. More...

#include <frame.h>

List of all members.

Public Member Functions

 Frame ()
 Constructs an empty animation Frame.
 Frame (const QString &text, const QString &mask, const QRgb &defaultColor, QChar transparent= '?')
 Constructs an animation frame.
void paint (Screen *scr, int x, int y)
 Paints this Frame into the given screen.
void erase (Screen *scr, int x, int y)
 Erases this Frame from the given screen.
int width () const
 Returns the logical width of this frame.
int height () const
 Returns the logical height of this frame.

Protected Member Functions

void convertDataToPixmap (const Screen *screen)
 This function converts the Pixel data in m_data to setup m_pixmap and m_erasePixmap, which are not setup until this function is called.

Private Attributes

QVector< QVector< Screen::Pixel > > m_data
 Two-dimensional array of Pixels, which represent the appearance of this frame.
QPixmap m_pixmap
 Masked pixmap of the animation frame. Created by convertDataToPixmap().
QPixmap m_erasePixmap
 Masked pixmap used to clear frame. Created by convertDataToPixmap().
int m_height
 Height of this frame of animation in logical coordinates.
int m_width
 Width of this frame of animation in logical coordinates.
QChar m_transparentChar
 Character to be used as a special 'transparent' character. Normally is the '?' character.

Detailed Description

Represents a single frame of a sprite's animation.

See also:
Sprite

Definition at line 42 of file frame.h.


Constructor & Destructor Documentation

Frame::Frame ( ) [inline]

Constructs an empty animation Frame.

Do not insert this into a Sprite.

Definition at line 74 of file frame.h.

            : m_height(0), m_width(0)
    {
    }
Frame::Frame ( const QString &  text,
const QString &  mask,
const QRgb &  defaultColor,
QChar  transparent = '?' 
)

Constructs an animation frame.

Parameters:
textNewline-separated text used to construct the Pixel arrays. The lines do not have to be equal length, any extra needed characters will automatically be filled with transparency. Any whitespace at the beginning of a line is converted to transparency as well.
maskNewline-separated text used to mask text's colors. This can be empty or null in which case no masking is performed. However, if present, there should be the same number of lines in mask as in text, although individual lines can be shorter or empty as convienient. You can use letters to stand for colors, e.g. 'r' will make the letter in text at the same position dark red.
defaultColorThe default color to apply to characters. This color is used for all characters in text that are not altered by mask.
transparentThe character to use to represent transparent areas in text. This can be useful when the auto-transparency feature can't detect transparent areas.

Definition at line 98 of file frame.cpp.

{
    //First, process the pixels.

    QStringList rows = text.split('\n');
    m_height = rows.size();
    m_width  = 0;
    m_transparentChar = transparent;

    // curRow is deliberately not const& as we must modify it a bit in the loop.  It does
    // not alias strings in rows however otherwise we would have to manually make a copy.
    foreach(QString curRow, rows) { //krazy:exclude=foreach
        QVector<Screen::Pixel> row;
        int strLen = curRow.length(); // make sure this is cached.

        // ?? is changed to {} for trigraph purposes, change it back.
        curRow.replace("{}", "??");

        for (int pos = 0; pos < strLen; ++pos)
        {
            Screen::Pixel p;
            p.letter = curRow.at(pos).unicode();
            p.color  = defaultColor;
            row.append(p);
        }

        m_width = qMax(m_width, row.size());
        m_data.append(row);
    }

    // Now, the colors.
    QStringList cols = mask.split('\n');
    int y = 0;

    foreach(const QString &curCol, cols) {
        if (y >= m_data.size())
            break;

        for (int pos = 0; pos < curCol.length() && pos < m_data[y].size(); ++pos)
        {
            switch (curCol.at(pos).unicode())
            {
                //Colors stolen from konsole, TEWidget.cpp
                case 'R':
                    m_data[y][pos].color = 0xFF5454;
                    break;
                case 'r':
                    m_data[y][pos].color = 0xB21818;
                    break;
                case 'C':
                    m_data[y][pos].color = 0x54FFFF;
                    break;
                case 'c':
                    m_data[y][pos].color = 0x18B2B2;
                    break;
                case 'Y':
                    m_data[y][pos].color = 0xFFFF54;
                    break;
                case 'y':
                    m_data[y][pos].color = 0xB26818;
                    break;
                case 'G':
                    m_data[y][pos].color = 0x54FF54;
                    break;
                case 'g':
                    m_data[y][pos].color = 0x18B218;
                    break;
                case 'B':
                    m_data[y][pos].color = 0x5454FF;
                    break;
                case 'b':
                    m_data[y][pos].color = 0x1818B2;
                    break;
                case 'M':
                    m_data[y][pos].color = 0xFF54FF;
                    break;
                case 'm':
                    m_data[y][pos].color = 0xB218B2;
                    break;
                case 'W':
                    m_data[y][pos].color = 0xFFFFFF;
                    break;
                case 'w':
                    m_data[y][pos].color = 0xB2B2B2;
                    break;
                case ' ':
                    break;
                default:
                    kDebug() << "dunno about color code:" << curCol.at(pos);
                    m_data[y][pos].color = 0xFFFFFF;
            }
        }

        ++y;
    } // foreach
}

Member Function Documentation

void Frame::convertDataToPixmap ( const Screen screen) [protected]

This function converts the Pixel data in m_data to setup m_pixmap and m_erasePixmap, which are not setup until this function is called.

m_data is not valid after this call is performed to save memory.

Parameters:
screenThe Screen we will be drawing into later.

Definition at line 39 of file frame.cpp.

{
    if(!height() || !width()) {
        // Assume we're not ready to go.
        return;
    }

    int w = screen->cellWidth(), h = screen->cellHeight();
    QImage pix(width() * w, height() * h, QImage::Format_RGB32);
    pix.fill(Qt::white);

    QPainter p;

    p.begin(&pix);

    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.setRenderHint(QPainter::TextAntialiasing);
    p.setFont(KGlobalSettings::fixedFont());

    QFontMetrics fm(p.fontMetrics());

    for(int j = 0; j < m_data.count(); ++j) {
        QVector<Screen::Pixel> row = m_data[j];
        if(row.isEmpty())
            continue;

        int first, last;
        for (first = 0; first < row.count() && row[first].letter == ' '; ++first)
            ;

        last = row.count() - 1; // Assume the end is already stripped.

        for(int i = first; i <= last; ++i) {
            if(row[i].letter == m_transparentChar)
                continue;

            QRect r(i * w, j * h, w, h);

            p.setPen(row[i].color);
            p.fillRect(r, Qt::black); // Completely fill since drawText won't even in Opaque.
            p.drawText(r, Qt::AlignCenter, QString(row[i].letter));
        }
    }

    p.end();

    QImage eraseImage(pix.size(), QImage::Format_RGB32);
    eraseImage.fill(qRgb(0, 0, 0));

    // Since QPixmap::fill() seems broken on some nVidia drivers????
    m_erasePixmap = QPixmap::fromImage(eraseImage);
    QBitmap mask = QBitmap::fromImage(pix.createHeuristicMask());
    m_pixmap = QPixmap::fromImage(pix);
    m_pixmap.setMask(mask);

    // Clear m_data to save a wee bit of memory.
    m_data.clear();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void Frame::erase ( Screen scr,
int  x,
int  y 
)

Erases this Frame from the given screen.

Parameters:
scrThe Screen to draw into.
xThe logical x coordinate of the left edge of the update region.
yThe logical y coordinate of the top edge of the update region.

Definition at line 203 of file frame.cpp.

{
    if(m_erasePixmap.isNull())
        convertDataToPixmap(scr);

    scr->clearSpan(x, y, m_erasePixmap);
}

Here is the call graph for this function:

int Frame::height ( ) const [inline]

Returns the logical height of this frame.

Definition at line 131 of file frame.h.

    {
        return m_height;
    }

Here is the caller graph for this function:

void Frame::paint ( Screen scr,
int  x,
int  y 
)

Paints this Frame into the given screen.

Parameters:
scrThe Screen to draw into.
xThe logical x coordinate of the left edge of the update region.
yThe logical y coordinate of the top edge of the update region.

Definition at line 195 of file frame.cpp.

{
    if(m_pixmap.isNull())
        convertDataToPixmap(scr);

    scr->updateSpan(x, y, m_pixmap);
}

Here is the call graph for this function:

int Frame::width ( ) const [inline]

Returns the logical width of this frame.

Definition at line 125 of file frame.h.

    {
        return m_width;
    }

Here is the caller graph for this function:


Member Data Documentation

QVector<QVector<Screen::Pixel> > Frame::m_data [private]

Two-dimensional array of Pixels, which represent the appearance of this frame.

This is used to create m_pixmap and m_erasePixmap when they are needed.

See also:
Pixel

Definition at line 51 of file frame.h.

QPixmap Frame::m_erasePixmap [private]

Masked pixmap used to clear frame. Created by convertDataToPixmap().

Definition at line 57 of file frame.h.

int Frame::m_height [private]

Height of this frame of animation in logical coordinates.

Definition at line 60 of file frame.h.

QPixmap Frame::m_pixmap [private]

Masked pixmap of the animation frame. Created by convertDataToPixmap().

Definition at line 54 of file frame.h.

QChar Frame::m_transparentChar [private]

Character to be used as a special 'transparent' character. Normally is the '?' character.

Definition at line 67 of file frame.h.

int Frame::m_width [private]

Width of this frame of animation in logical coordinates.

Definition at line 63 of file frame.h.


The documentation for this class was generated from the following files: