Tuesday, April 5, 2011

Zoom effect

While finishing new magnetics level I wanted to add this simple but interesting effect... a zoom at the beginning of each level, displaying a huge part of the level and focusing the main character.

As usual, video recorder from the Symbian emulator provided in QtCreator IDE.




Actually, the code is pretty simple.

Just note we will have a QTimeLine, sending the current frame from 100 to a minimum. We get the max distance zoom of the current level and we do a "cross multiplication". If 100 is the higher value, and the max distance is the max distance (well... this is pretty obvious :P), we want to know the desired distance for the current frame.

.h
qreal m_rMaxZoom;

.cpp
    m_rMaxZoom = MyPointerToGameEngine->GetZoomValueForTheCurrentLevel();
    QTimeLine *timeLine = new QTimeLine(4000, this); //4 seconds
    //The timeline will start sending the frame 100, and going down to 100/m_rMaxZoom.
//This is needed because if we allow lower numbers to zoom goes too far. 
    timeLine->setFrameRange(100, 100/m_rMaxZoom);
//Connections. If the frame changes it sends the calculated frame. 
//If the timeLine finishes, "startPlayingNextLevel" from the Game Engine is called.
    connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(zoomOut(int)));
    connect(timeLine, SIGNAL(finished()), this, SIGNAL(startPlayingNextLevel()));
    timeLine->start();
//The desired first zoom (100) is not sent by the timeLine 
    zoomOut(100);
 
 
 
void CAnimationEngine::zoomOut(int iCurrentFrame)
{
    //Cross multiplication
    qreal rZoom = iCurrentFrame * m_rMaxZoom / 100;

    if(rZoom > 0.05)
        {//Basic scale transformation to the view
pointerToMyView->resetTransform(); 
                QTransform trans = pointerToMyView->transform();
                 trans.translate((width() / 2), (height() / 2));
                 trans.scale( 1 / (rZoom ), 1 / (rZoom ) );
                 trans.translate(-(width() / 2), - (height() / 2));

                 pointerToMyView->setTransform(trans);
} 
 else
        qDebug()<< "ERROR: AnimationEngine.cpp ------ ZoomOut(). Too close to zero"; 
 //When the scalation is finished, move the view in order to ensure the visibility of the player.
//It's not a complicated function; basically QGraphicsPixmapItem::ensureVisible(); 
    m_pGEng->moveTheViewToShowThePlayer();
} 

No comments:

Post a Comment