C++ (Qt) label2 = new QLabel(this); QMovie *movie = new QMovie(":/img/test.gif"); label2->setMovie(movie); movie->start();
C++ (Qt)///////////////////////////////////////////////////////// DQActor.h// Class for animated image// (c) DarKnight aka Sergey Aksenov (2012, Russia)// darkknight_05@mail.ru//////////////////////////////////////////////////////// #ifndef DQACTOR_H#define DQACTOR_H #include "pacman_le.h" // DQActorclass DQActor{public: QMovie *movie; QVector2D pos; std::vector<QGraphicsPixmapItem*> piVec; float rot, width, height; UINT32 currFrame, frameCount, nextFrameDelay, lastTime; // DQActor DQActor::DQActor() { _initVars(); } DQActor::DQActor(PacMan *widget, char* imgPath) { _initVars(); build(widget, imgPath); } void DQActor::_initVars() { movie = NULL; pos.setX(0.0f);pos.setY(0.0f); rot = width = height = NULL; currFrame = frameCount = nextFrameDelay = lastTime = NULL; } bool DQActor::build(PacMan *widget, char* imgPath) { bool loadOk = true; movie = new QMovie(imgPath); loadOk = movie->isValid(); int ind=0; frameCount = movie->frameCount(); nextFrameDelay = movie->nextFrameDelay(); movie->start(); do { ind = movie->currentFrameNumber(); QPixmap pix = movie->currentPixmap(); width = pix.width(); height = pix.height(); QGraphicsPixmapItem *pit = widget->scene->addPixmap(pix); pit->setTransformOriginPoint(width*0.5f,height*0.5f); // centered pit->setZValue(1.0f); // bring to front piVec.push_back(pit); if(ind>0) pit->hide(); // пря4ем остальные фреймы movie->jumpToNextFrame(); // go to next frame }while( ind < (frameCount-1) ); movie->stop(); return loadOk; } void DQActor::move(float x, float y) { pos.setX(pos.x()+x); pos.setY(pos.y()+y); piVec[currFrame]->setPos(pos.x(),pos.y()); } void DQActor::setPos(float x, float y) { pos.setX(x); pos.setY(y); piVec[currFrame]->setPos(x,y); } void DQActor::setRot(float angle) { rot = angle; piVec[currFrame]->setRotation(angle); } void DQActor::update() { // animate // for better put it in timer UINT32 go = lastTime + nextFrameDelay; if(go <= timeGetTime()) { lastTime = timeGetTime(); piVec[currFrame]->hide(); if(currFrame < (frameCount-1)) currFrame++; else currFrame=NULL; piVec[currFrame]->show(); piVec[currFrame]->setPos(pos.x(),pos.y()); piVec[currFrame]->setRotation(rot); } // update trasformation piVec[currFrame]->update(pos.x(),pos.y(),width,height); } DQActor::~DQActor() { SAFE_DELETE_VEC(piVec); SAFE_DELETE(movie); } }; #endif