Learning the QT animation framework

Source: Internet
Author: User
Learning the QT animation framework

In the last year, I encountered a bottleneck in learning about game development, mainly because there are many complicated states to maintain in the game. the previously used switch-If method now seems to have reached the end of the road. Therefore, I started to learn more advanced game development content. I think it is the most urgent part to learn about AI in games. In fact, the AI of games is not only artificial intelligence, but also contains the knowledge of the game architecture, especially how to deal with a very complex system.

As an excellent cross-platform open-source framework, QT has made relevant research achievements. The animation framework and state machine framework were introduced at the beginning of qt4.6. During this time, I felt I had to learn the QT animation framework. Therefore, I wrote this log, hoping to help those who are eager to learn but confused about game animation. (Original blog, original address: http://blog.csdn.net/jiangcaiyang123/article/details/8680816)

Source code: here

Getting started, I want to create a movable button. Below is the program:

The button "Please help me" will be moved from the upper left corner of the window to the lower right corner, which takes 5 seconds. The source code of this program is as follows:

#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QPushButton>class Widget: public QWidget{    Q_OBJECTpublic:    explicit Widget( void );private:    QPushButton*         m_pButton;};#endif // WIDGET_H

The widget class defines a pointer to a qpushbutton object to display a button. The following is the implementation file:

# Include <qtgui> # include "widget. H "/* --------------------------------------------------------------------------- */Widget: widget (void): qwidget (0) {// The initialization class member resize (640,360 ); setwindowtitle (TR ("photo"); m_pbutton = new qpushbutton (TR ("Please help"), this); // set the animation qpropertyanimation * panimation = new qpropertyanimation (m_pbutton, & quot; geometry & quot;); panimation-> setduration (5000); int textwidth = fontmetrics (). width (m_pbutton-> text (); int textheight = fontmetrics (). height (); panimation-> setstartvalue (qrect (0, 0, textwidth, textheight); panimation-> setendvalue (qrect (width ()-textwidth, height ()-textheight, textwidth, textheight); panimation-> Start ();}

The implementation file uses the qpropertyanimation class to implement an attribute animation, that is, the attribute of the button is set through linear interpolation (lerp. Note that the above "Geometry" name is one of the attributes of the button. The attributes of the QT built-in class can be seen in the properties of each class in the help of QT creator. Then, the start and end statuses are set through qpropertyanimation: setstartvalue and qpropertyanimation: setendvalue. Finally, use qpropertyanimation: Start () to start the animation.
Finally, the main. cpp is simply completed.

#include <QApplication>#include <QTranslator>#include "Widget.h"int main( int argc, char** argv ){    QApplication app( argc, argv );    QTranslator trans;    trans.load( ":/zh_CN.qm" );    app.installTranslator( &trans );    Widget w;    w.show( );    return app.exec( );}

Next I will show how QT combines the state machine framework with the animation framework to create a simple finite state machine animation.

"Please help me." This button does not move automatically. It will be moved from the top left corner to the bottom right corner, and then press it to move the new place to the top left corner, it takes 2 seconds. It only adds a few features based on the previous program. The following is the header file of the class widget.

#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QPushButton>#include <QPropertyAnimation>#include <QStateMachine>class Widget: public QWidget{    Q_OBJECTpublic:    explicit Widget( void );private:    QStateMachine*       m_pMachine;    QPushButton*         m_pButton;};#endif // WIDGET_H

Based on the first example, the qstatemachine class is added to represent a finite state machine. The following is the code for widget. cpp:

# Include <qstate> # include <qsignaltransition> # include "widget. H "/* ------------------------------------------------------------------------- */Widget: widget (void): qwidget (0) {// set the window member resize (640,360 ); setwindowtitle (TR ("qt_easyanimation2"); m_pbutton = new qpushbutton (TR ("Please help"), this); int textwidth = fontmetrics (). width (m_pbutton-> text (); int textheight = fontmetrics (). height (); // set the animation and Finite State Machine m_pmachine = new qstatemachine (this); qstate * pstate1 = new qstate (m_pmachine); qstate * pstate2 = new qstate (m_pmachine ); pstate1-> assignproperty (m_pbutton, "Geometry", qrect (0, 0, textwidth, textheight); pstate2-> assignproperty (m_pbutton, "Geometry", qrect (width () -textwidth, height ()-textheight, textwidth, textheight); qsignaltransition * transition1 = pstate1-> addtransition (m_pbutton, signal (clicked (), pstate2 ); optional * transition2 = pstate2-> addtransition (m_pbutton, signal (clicked (), pstate1); qpropertyanimation * panimation = new qpropertyanimation (m_pbutton, "Geometry "); panimation-> setduration (2000); transition1-> addanimation (panimation); transition2-> addanimation (panimation); m_pmachine-> setinitialstate (pstate1 ); // set the initial status m_pmachine-> Start ();}

This code first creates the state machine and then creates the state one by one. This ensures that the input state machine pointer is a valid pointer. The qstate: assignproperty () function is used to specify the attributes of the QT class to be animated. Then, a qsignaltransition is created to set the animation conversion from state1 to state2. Make sure that the attributes specified by qpropertyanimation are consistent with those in qstate: assignproperty. Finally, set the initial state and start the finite state machine. Main. cpp is the same as the original one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.