Learning Cocoa game development

Member
Posts: 131
Joined: 2010.08
Post: #1
I know the basics of Cocoa and I have learned much OpenGL from OpenGL Programming Guide, but I still do not have skills to make Cocoa games. A tutorial about making a game would be very helpful, but I have not found any good tutorials. Either the code is not explained well, like in http://zerobyzero.ca/~ktatters/seasi...als/index.html, Core Animation is used instead of OpenGL (e.g. http://cocoawithlove.com/2009/02/ast...animation.html), or it is old (http://www.amazon.com/Game-Programmi...0486135&sr=1-1). What tutorial or book should I use? Or could someone in this forum make one, or just give the source code of a simple game, like Asteroids, that is made with Cocoa and OpenGL.
How did you learn Cocoa game development?
Quote this message in a reply
Member
Posts: 281
Joined: 2009.04
Post: #2
I'm not so sure about books, I had difficulty finding any on OpenGL game programming alone, let alone mac games! And those that I did find were out of date.

I think your best bet is to look online. I'm sorry I can't help you anymore.

~ Bring a Pen ~
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #3
As requested in your other thread, I shall answer your question here presently.

(Aug 1, 2010 08:21 AM)Miglu Wrote:  How did you learn Cocoa game development?

I learned how to program C many many years ago. Then I learned a little about OpenGL. Then OS X came around. Then later I learned how to do some Cocoa programming.

(Aug 1, 2010 08:21 AM)Miglu Wrote:  What tutorial or book should I use? Or could someone in this forum make one, or just give the source code of a simple game, like Asteroids, that is made with Cocoa and OpenGL.

That's a tall request! I can't make any promises, but if I get some spare time in the next week or two, I'll see if I can whip something together for you.

[edit] I changed the title of this thread to something a little more descriptive Wink [/edit]
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #4
Could someone explain why this way of doing a constant aspect ratio does not work.
Code:
#define GAME_WIDTH 5
#define GAME_HEIGHT 2

- (void) resizeGL
{

    NSRect rectView = [self bounds];
    NSSize boundsView = rectView.size;
    float scale;
    if ((boundsView.width / boundsView.height) > (GAME_WIDTH / GAME_HEIGHT))
    {
        scale = boundsView.height / GAME_HEIGHT;
        boundsView.width = boundsView.height * (GAME_WIDTH / GAME_HEIGHT);
    }
    else
    {
        scale = boundsView.width / GAME_WIDTH;
        boundsView.height = boundsView.width * (GAME_HEIGHT / GAME_WIDTH);
    }
    [[self window] setContentSize:NSMakeSize(boundsView.height, boundsView.width)];
}
The window is not rendered at all. Could you show the best way of doing constact aspect ratio.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #5
Try this instead, in awakeFromNib:

Code:
- (void)awakeFromNib
{
    NSSize    viewBounds;
    
    // set the window a 1 to 1 aspect ratio in size
    viewBounds = [self bounds].size;
    viewWidth = viewBounds.width;
    [[self window] setContentSize:NSMakeSize(viewWidth, viewWidth)];
    [[self window] setContentAspectRatio:NSMakeSize(1.0f, 1.0f)];
}
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #6
But could you show how to make any aspect ratio constant.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #7
It should be apparent that all you need to do is change the values in
Code:
[[self window] setContentAspectRatio:NSMakeSize(1.0f, 1.0f)];

I noticed in your other thread that you didn't set your projection matrix, which might be why you're having difficulty getting the results you want.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #8
Thanks. It works without a projection matrix.

I use this to accelerate the circle:
Code:
- (void) keyDown:(NSEvent *)theEvent
{
    NSString * characters = [theEvent characters];
    
    if( [characters length] )
    {
        [self keyAction:[characters characterAtIndex:0] isPressed:true ];
    }
}

- (void) keyUp:(NSEvent *)theEvent
{
    NSString * characters = [theEvent characters];
    
    if( [characters length] )
    {
        [self keyAction:[characters characterAtIndex:0] isPressed:false ];
    }
}

- (void) keyAction:(const char)inKey isPressed:(const bool)inValue
{
    m_keys[inKey] = inValue;
}

- (void)moveBall
{
    if (m_keys['a'])
    {
        ballX -= BALL_SPEED;
        
    }
    if (m_keys['d'])
    {
        ballX += BALL_SPEED;
        
    }
    if (m_keys['s'])
    {
        ballY -= BALL_SPEED;
    }
    if (m_keys['w'])
    {
        ballY += BALL_SPEED;
    }
}
Is this a good method, or is it better to enumerate the keys like in http://www.idevgames.com/forum/showpost....ostcount=5
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #9
You can do it however you want, as long as it works for you. The post you linked to is just the way I like to do it.

You are going to need to take delta time into account at some point though, so
Code:
ballX += BALL_SPEED;

... just isn't good enough. You'll need to do this instead, if you want any consistency:
Code:
ballX += BALL_SPEED * deltaTime;
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #10
If I use my method, what should I write in place of a, w, s and d if I want to use the arrow keys?
Thanks for answering my questions.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #11
Not sure how you would do it with your technique, but if you look in my processKeys method I am equating the character to NSLeftArrowFunctionKey, NSRightArrowFunctionKey, NSUpArrowFunctionKey, or NSDownArrowFunctionKey, which works for me.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #12
viewWidth was undeclared in your aspect ratio code, so I defined it.

Code:
- (void)awakeFromNib
{
    NSSize    viewBounds;
    CGFloat viewWidth;
    
    // set the window a 1 to 1 aspect ratio in size
    viewBounds = [self bounds].size;
    viewWidth = viewBounds.width;
    [[self window] setContentSize:NSMakeSize(viewWidth, viewWidth)];
    [[self window] setContentAspectRatio:NSMakeSize(1.0f, 1.0f)];
}

There are two problems with this code. One is that whatever the values in NSMakeSize(1.0f, 1.0f) are, the window starts as a square and when it is resized, it instantly turns into a rectangle that has the ratio of those numbers. The second is that the view is not resized. Could you correct this?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #13
Here, try this instead.

Code:
#define ASPECT_WIDE 5.0f
#define ASPECT_HIGH 4.0f
#define MY_ASPECT_RATIO (ASPECT_WIDE / ASPECT_HIGH)

- (void)awakeFromNib
{
    NSSize viewBounds = [self bounds].size;
    float height = viewBounds.height;
    float width = height * MY_ASPECT_RATIO;
    [[self window] setContentSize:NSMakeSize(width, height)];
    [[self window] setContentAspectRatio:NSMakeSize(ASPECT_WIDE, ASPECT_HIGH)];
}

To get the view to resize automatically, you need to go into the nib and select the view. Then in the inspector, click on the little ruler tab. There you will see Size & Position, and below that will be Autosizing. In the autosizing section there is a little box on the left. Click in the center of the little box until you have red arrows pointing both up and down and left and right. That will set the view to follow the size of the window.
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #14
The first problem was solved. However, although I have all of the red lines on, the view does not resize, which I can see from the fact that the circle does not resize when I resize the window.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #15
Are you sure that the red arrows are in the center of the small box in the middle? Red lines on the *outside* of that little box set which sides the view is anchored to, but do not resize the view.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Switching developers mid-game development fauxdev 4 987 Feb 17, 2014 05:46 PM
Last Post: funkboy
  How to get started in game development! Taxxodium 18 45,880 Jun 5, 2012 03:55 PM
Last Post: AnotherJake
  [Book] Cocos2d for iPhone 1 Game Development Cookbook NathanBurba 1 5,096 Jan 8, 2012 05:40 PM
Last Post: mmztech
  iphone game development on Windows? Briksins 22 21,740 Dec 19, 2010 01:43 PM
Last Post: sefiroths
  Objective C and game development r00li 5 9,115 Oct 5, 2009 08:39 AM
Last Post: r00li