Saturday, October 23, 2010

Creating Qt game for mobile device with differnt screen size

Till now I have created few Qt games for Maemo 5 (N900) device, and now I wanted to run same games on Symbian device which support Qt.

First problem I encounter doing so is different screen size from N900, in my current game implementation I have hard coded screen size and coordination of game object.

After playing with graphics framework from some time, I created a small prototype application that shows two rectangle one on top left other on bottom right corner and can run on different Symbian device and N900 as well. So first thing I did is to remove all hard coded coordinate and implemented resize event, then scaled all graphics item which are added to graphics scene.

Following is my prototype code.

In below code, I have added QGraphicsView as QMainWindow's central widget and also reimplemented resizeEvent of QMainWindow.
GraphicsWidget::GraphicsWidget(QWidget *parent)
    : QMainWindow(parent)
    _scene = new MyScene();
    _view = new QGraphicsView(_scene,this);


    // Disable scrollbars

void GraphicsWidget::resizeEvent(QResizeEvent* event)

And then in MyScene class, derived from QGraphicsScene, on sceneRectChanged signal i am scaling all item to scale factor according to my reference screen size, which is of n900 device's screen size.
    item1 = new MyGraphicsItem();
    addItem( item1 );

    item2 = new MyGraphicsItem();
    addItem( item2 );

void MyScene::resize(const QRectF& newRect)
    qreal w = (qreal)newRect.width()/800 ;
    qreal h =  (qreal)newRect.height()/480;


            newRect.height() - (item2->boundingRect().height()*h));
Finally in main function, I am showing QMainWindow in full screen mode.
int main(int argc, char* argv[] )
    QApplication app(argc,argv);
    GraphicsWidget widget;
    return app.exec();
Following are snaps from prototype running on emulator for different device.

No comments:

Post a Comment