iDevGames Forums
Learning Cocoa game development - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Learning Cocoa game development (/thread-7930.html)

Pages: 1 2


Learning Cocoa game development - Miglu - Aug 1, 2010 08:21 AM

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?


RE: Looking for Cocoa game tutorials or books - mikey - Aug 2, 2010 12:35 PM

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.


RE: Oh yeah! - AnotherJake - Aug 8, 2010 11:25 AM

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]


RE: Learning Cocoa game development - Miglu - Aug 8, 2010 12:21 PM

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.


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 12:26 PM

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)];
}



RE: Learning Cocoa game development - Miglu - Aug 8, 2010 12:28 PM

But could you show how to make any aspect ratio constant.


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 12:30 PM

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.


RE: Learning Cocoa game development - Miglu - Aug 8, 2010 12:37 PM

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.php?p=138018&postcount=5


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 12:46 PM

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;



RE: Learning Cocoa game development - Miglu - Aug 8, 2010 12:50 PM

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.


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 12:55 PM

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.


RE: Learning Cocoa game development - Miglu - Aug 8, 2010 01:28 PM

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?


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 01:48 PM

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.


RE: Learning Cocoa game development - Miglu - Aug 8, 2010 01:54 PM

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.


RE: Learning Cocoa game development - AnotherJake - Aug 8, 2010 02:02 PM

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.