2011年3月21日星期一

HappyFrog........just for fun

In the wake of angry bird become more and more popular,so I just wonder how to implement it with Qt.Below are some hint.
1.GraphicsView Framework(or Declarative UI) interagted with Box2D physics engine.
2.Explosion effect can be done with function drawPixmapFragment(which is introduced in Qt 4.7,make an optimize on state changes for QPainter,so it's faster than multiple calls to drawPixmap())
3.In order to boost my development,I wanna use some Element from Qml,such as flickAble or ListView,etc..


The first two are not difficult to implement it.
basically,lots of people want to know how to integrated qml with c++ in this demo,in particularly how to add item in the flickAble element (you konw,I am a lazy and humble guy and on the flip side,I guest not everyone konws that how to make kinetic animation,In fact it's not difficult).
(more detaileds watch the video Below).
In order to dynamically add item in the flickable,we can write some code in qml
Flickable {
     id: myFlickable
     ......
     function addItem(file) {
         var component = Qt.createComponent(file)
         component.createObject(myFlickable.contentItem);
     }
 }
Maybe someone just figure out an "easier" way

Flickable{
  id: myFlickable
  ......
  Loader { id: itemLoader }
  ......
  MouseArea {
          anchors.fill: parent
          onClicked: itemLoader.source = "items.qml"
     }
  }    
}
In case code you wrote like above,unfortunately,item inserted dynamiclly can not be "flicked",so it didn't work out if you forget passing Flickable.contentItem for the item created,even thought it seems like itemLoader is a "child" of myFlickable.
In C++
consider about foregoing problem we met.There are two little things we need to care about.
1.How to get the pointer of "Flickable"
2.How to get the pointer of "myFlickable.contentItem"(a property of Flickable)
;) some related apis have been prepared by Qt,easy and convinient.
   1.create QtObject Element for Flickable element,as the qt document mentioned "It can also be useful for C++ integration, as it is just a plain QObject.This allows a C++ application to locate an item within a QML component using the QObject::findChild() method". 
   2.with QObject'property to fetch the pointer of Flickable.contentItem.
   main.qml
Flikable {
      id:flickable;
      objectName: "flickArea"
      .......
      .......
  }
   
  main.cpp
 QDeclarativeComponent component(&engine,QUrl("main.qml"));
 if(component.status() != QDeclarativeComponent::Ready){
     foreach(const QDeclarativeError &error,component.errors()){
         qFatal(error.toString().toStdString().c_str());
     }
 } 
 QDeclarativeItem *mainView = qobject_cast< QDeclarativeItem *>(component.create());
 QDeclarativeItem *flickArea = mainView->findChild< QDeclarativeItem * >("flickArea");
 QDeclarativeItem *contentItem = qvariant_cast< QDeclarativeItem * >(flickArea->property("contentItem"));

//pass contenItem as item's parent.
ItemInserted   *item = new (contentItem);

That's all.Btw.
If you are a qml enthusiast,qml-Box2d(http://gitorious.org/qml-box2d) plugins is on the
way,here,Maybe it's useful for you to write fantastic game or something related stuff.
of course,HappyFrog is an opensource project,you can find here
http://code.google.com/p/happyfrog/
you can also checkout the latest source which is hosted on the my gitorious page.
git clone git://gitorious.org/happyfrog/happyfrog.git

没有评论:

发表评论