Friday, May 14, 2010

View switching on iPhone SDK


While creating sample game for iPhone I required to create menu page for game. Here I am putting down code that I used to create menu for my sample game.

First create a view based application.


Then created menu view as following, with two button that will load new view.

Then add new view controller, I named new view as first view and add button on first view that will switch view to menu screen.


Now connect buttons to message that get activated when button is pressed and when button connection is ready then we can write code that will enable view switching. 

In menu view controller, I used following code to load first view. 

- (IBAction) siwthToView1:(id) sender {        

    FirstView *firstView = 
[[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];

    [self.view addSubview: firstView.view];

}

addSubView message will add new view on view stack and display new view on screen.

Now, in first view I used removeFromSuperview message of UIView to go back to menu view.

removeFromSuperview will remove current view from view stack and displays the superview.


- (IBAction) back:(id) sender {

    [self.view removeFromSuperview];
}

So this was all, by using this simple code I implemented my game's menu page.



Saturday, May 8, 2010

Color match game for Maemo using Qt

I have added new game called color match to my reaction game. Idea behind game is to understand problem before you react to it.

In color match game, you will see two box on screen, on upper box there will be text which is color name and lower box will display color. You have to tell if color name displayed in upper box is matching color  displayed in lower box. This is it, if your answer is correct than you will get points. Now in this game I am also considering time which you took to answer, more you take time lower the points.

Here are fews snaps from game. And if you are interested in code, I have uploaded code on my glorious repository. I will also upload game to maemo extra-devel soon.

Wednesday, May 5, 2010

Simple download manager in Qt with puase/ resume support

Update: Here is link to my full working code sample.

Recently I needed to create simple download manager that support pause/ resume.

I was using Qt's network module (QNetworkAccessManager to be specific) to download media content, but QNetworkAccessManager class by itself does not support pause/resume feature.

But with little effort we can make it to support pause/resume. Trick it to set "Range" header while making HTTP request.

Please visit RFC 2616 for more information related to Range header.

To implement download manager, I have used example code from Qt 4.6 (http://doc.qt.nokia.com/4.6/network-downloadmanager.html) and then added code to support pause and resume.

In my code when user pause download I am saving current request and on resume I am adding Range header to saved request then starting download using that saved request.

downloadSizeAtPause = _currentRequest->_device->size();
QByteArray rangeHeaderValue = "bytes=" + QByteArray::number(downloadSizeAtPause) + "-"; 

_currentRequest->_request.setRawHeader("Range",rangeHeaderValue);

QTimer::singleShot(0,this,SLOT(startNextDownload()));

So here is simple download manager.



 

Tuesday, April 27, 2010

Creating custom view for iPhone

Currently I am trying to create simple game for iPhone. To create game first thing I needed to learn is to create custom view and custom drawing. So here I am putting down my learning in hope that it will help some one who started learning iPhone development.

I started with creating new window based project and named it CustomView.


Then I created new class named CustomView and derived it from UIView.


After creating class derived from UIVIew, we need to make sure that application's window add newly created class as its subview like shown in code below. 

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    CustomView *view = [[CustomView alloc] initWithFrame:window.frame];
   
    // Override point for customization after application launch
    [window addSubview:view];
    [window makeKeyAndVisible];
}

Then finally implement drawRect with custom drawing code. Here is my code with comment, in following code I am drawing a text, a rectangle , a circle and an Image.

- (void)drawRect:(CGRect)rect {
    // Drawing code
    //acquire graphics context
    CGContextRef ctxt = UIGraphicsGetCurrentContext();
   
    //set brush and set brush color to yellow
    CGContextSetFillColorWithColor(ctxt, [[UIColor yellowColor] CGColor]);
   
    //create string to draw
    NSString *hello = @"Hello World!";
    //create font to be used
    UIFont* font = [UIFont systemFontOfSize:20];
   
    //find horizontal center position for text
    int xLocation = ([self frame].size.width / 2) - ( [hello sizeWithFont:font].width /2 );
    [hello drawAtPoint:CGPointMake( xLocation, 100 ) withFont:[UIFont systemFontOfSize:16.0]];
   
    //draw rect
    CGContextFillRect(ctxt, CGRectMake(50, 50, 50, 50));
    //change brush color to green and draw circle
    CGContextSetFillColorWithColor(ctxt, [[UIColor greenColor] CGColor]);
    CGContextFillEllipseInRect(ctxt, CGRectMake(150, 150, 50, 50));
   
    //draw image
    UIImage *image = [UIImage imageNamed:@"red-ind.png"];
    [image drawAtPoint:CGPointMake( 100,200)];
}

So end result looks like following.

Saturday, April 24, 2010

Simple sprite animation using Qt

I was always wondering how character are animated in games so I thought to create simple animated character using Qt.

Technique is quite simple, you need to draw sequence of image with different character position.
To hold image of different character position I am using QList.

mLeftSprite.append( QPixmap(":left_1.png"));
mLeftSprite.append( QPixmap(":left_2.png"));
mLeftSprite.append( QPixmap(":left_3.png"));
mLeftSprite.append( QPixmap(":left_4.png"));
mLeftSprite.append( QPixmap(":left_5.png"));
mLeftSprite.append( QPixmap(":left_6.png"));  

To animate character I have used timer which timeout after every 100ms and I am displaying next caracter image when timer expire and also updating character position on screen at same time

mCurrentFrame = ++mCurrentFrame % 6;
mPos.setX( mPos.x() + mXDir * 10 ); 
  
And finally draw image on screen
 
painter->drawPixmap(mPos,(*mCurrentSprite)[mCurrentFrame]);

So end result is like following.



If you are instrested in looking at code than here is link to gitorious.