segunda-feira, 11 de julho de 2011

Events and Animations

Hi Again !

This past week i've experimented with events and animations using the Qt Framework. Processing events with Qt is really simple and there isn't much to say as we will see. On the other hand there is a lot to say about the Qt's Animation Framework. It was confusing at first (the official example isn't exactly newbie friendly) but it is easy when you get used to it and it doesn't take long.

Events

I was confused by the Qt nomenclature for events and event handlers. So here it is: SIGNAL = EVENT; SLOT = EVENT HANDLER.

This slot/signal is more (flexible) than plain callbacks and looks more like C# Delegates.

To associate the event to the event handler you use the connect method/function. There is a global function connect and a static method in the QObject class (inherited by almost all other classes).

This method/function receives the object that originated the event, the event you are tracking, the object that will handle it and which method is the event handler.

Here an example:

// Event + Event Handler
QObject::connect(lui, SIGNAL(accepted()), this, SLOT(init_game()));

lui is a dialog. the QDialog class emit and accepted event when you click the yes button. This snippet binds the init_game method of the current object (this) with the accepted signal (event) from lui.

Animations

There are 2 ways to work with animations. The very simple one and the complex yet powerful one. The major difference is that first one animates a single "entities" and is just a simple animation while the other can have multiple states (think of a state as a key frame), entities and animations going on.




The first snippet (1.1) creates an animation by using the QPropertyAnimation class which makes a property change smoothly. In this example it will change lui->pos() (A Qt Property is exactly like a C# property -- which c++ doesn't natively supports. Although it seems like (is) a function it is just an attribute with a getter/setter created for you.) from (0,0) to (200,200) in 750ms. The path that the component will follow was given by animPos->setEasingCurve(QEasingCurve::InOutQuart) (a 4th grade polynomial).

The following snippet (1.2) just add a second animation occurring simultaneously as the first. To do that we create another animation and add it to a QParallelAnimationGroup. This class will launch all Animations in paralalel and there is a QSequentialAnimationGroup if you want an animation in a sequential order.

Although last part(2) is a bit long it isn't complicated. Instead of setting the initial and final state like we did we will create a state machine and let it set the final state (and initial if you like. Animations that doesn't have an initial state we will use current value. State machines need an initial state).

I started it creating the state machine and 3 states and added 2 of those states (init and inPos) to the third (root). It isn't needed but shows that more complex state machines can be created. The following step describes how the component (lui) should be when each is reached.

After the animation group is completed we are ready to go. Instead of starting the animation directly i adopted the Qt tutorial suggestion to trigger it using an event. So QTimer was added to make serve as an animation countdown (100ms).

Next Topics


That was what i had in mind for this post but there is a few topics that i want to play with before actually coding a game. A few topics that might deserve to look at:
  1. Translations
  2. Scripting
  3. Fast UI Construction

terça-feira, 5 de julho de 2011

3 months later

After exactly 3 months since my last post "I'm back" quoting Randy Quaid from The Independence Day final battle.

In those 3 months i've worked on my thesis, i discovered my results were dopped (this means a big hole in my experiments) and i had to go back to the drawing board and i lost a few months of work. But things are now a bit better and i have promising new ideas. :)

But back on topic I've also tried to give a first step in making a game. A Frenchman once said that nothing costs more than the first step .... And he is right !

Unreal Dev Kit was a wall ... I couldn't understand anything. It is fantastic and if you follow tutorials you can do all that easily but if you want to make anything other than an amazing shooter .... good luck.

I saw a few other engines but i had no time to play with them. Or they were even less gentle with beginners. So i had to take a different approach.

Now i am using the ever flexible Qt c++ framework to make a 2D-top view game. That is free software at its best: cross-platform (across several platforms), easy to learn, easy to use, tons-and-more-tons of utilities and more. It doesn't like c++ standards much but it is still great. Here is an example of what it can do.





Very, Very simple code ...

First i create an application. That is the entrance for the awesome Qt world. Inside it the framework will handle everything that makes your life a lot easier.

After that a scene. That is the virtual world you want to create. My virtual world has 2 rectangles, a button and a PNG image. That is pretty much what i expect to use from the game i have in mind.

The view part is like the hole in the wall. It is the portion of the virtual world you can actually see. It also gives me a "free" window to place it so i don't have to make it myself.

And the last line you give the green light to the application (actually that is the entrance point to Qt).

For the next post (soon ... i am already using soon like a game company <.<) I ll play a bit with other fundamentals like events, animation (Qt has an interesting framework for Animations).

Till next post !