Do you want to know how your customers use your applications and devices? Try Qt Insight.
最新版Qt 6.3已正式发布。 了解更多。
最新バージョンQt 6.3がご利用いただけます。 詳細はこちら

Monster Evolution in Qt: Episode 3 (Revenge of the Cylinders)

Seems nobody is interested in free drinks these days. At last that is my impression since not many guesses have been thrown for the title of this episode. Of course, that could also mean that this monster series is really boring anyway.

Whatever the case, here I present you the third and the latest episode of the Monsterwalker family saga. For those who missed the previous two episodes: The QtScript Menace and Attack of the SquirrelFish, I suggest reading them first to get the clue of the context. It's merely about running Monster Evolution, part of the Chrome Experiments, written by Dean. As I have hinted before, now we make a radical move. After using Qt Script and JavaScriptCore in the first and second episode, respectively, now we are going to use V8, the fast JavaScript engine behind Google Chrome.


Using V8 means that we have to deal with the bindings manually. With Qt Script, we have the luxury of using its built-in Qt integration. Same goes with JavaScriptCore, we have the hook via QWebFrame. For V8, we need to go low-level and invoke the necessary V8 functions ourselves. Fortunately, V8 is already designed with embedding usage in mind.

Can't wait to see the code? Check out the git repository under cymonster subdirectory. Grab the archive (find the snapshot link) if you do not use git. Make sure to open the README file and follow the instructions there, this is absolutely mandatory as this code requires a working V8 shared library. Don't be shocked to see that the code is still as short as the previous examples. Despite our use of V8, the book-keeping and binding parts are kept to minimum.

Since V8 is claimed to have a fantastic performance, don't be surprised if now you will a very fast rotating monster, unlike in the previous episodes. Typically you should easily reach 25 fps in modern machines these days. For the fun of it, the frame rate is capped at 50 fps (I didn't bother to do it in the previous episodes, it won't be reachable anyway). On a high-end machine with fast graphics card and running the demo with OpenGL graphics system, a stunning 30 fps is not so uncommon. If you follow this series faithfully and try all these three episodes, care to share the frame rate you got from each of the episodes?

As a side note, the use of OpaquePaintEvent to avoid offscreen buffer (since we stick to SourceOver composition mode all the time) seems to work only on Windows and Linux/X11, the latter often also depends on the composition manager and good graphics driver, or a combination thereof. Thus, I decided to enable the use of offscreen buffer for Mac OS X. You can still enable it by hand on other platforms if you experience the lack of said effect. Mind you, this would cost you few fps, but that is the price you need to pay.

Last but not least, writing this monster series has been the source of fun and learning for me. I hope you enjoy reading it as much as I enjoy preparing it. May the Script be with you!

The final FAQ:

Q: Since V8 is so fast, don't you want a V8-based Qt Script?
A: No, since JavaScriptCore is still the official JavaScript engine for WebKit. In addition, there are other reasons that I won't elaborate here (to mention one: lack of a bytecode interpreter for non JIT-supported platforms).

Q: But you use it already in this example?
A: To use V8 is different that to build a full-blown Qt Script implementation using V8. The former is a fun hacking session for one afternoon, the latter is a real engineer project that spans weeks.

Q: Will there be The Monkey Traces Back?
A: Unlikely, I am exhausted.

Postscript: No monsters were harmed in the making of these demos.

Blog Topics: