Learning Cocoa game development
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?
How did you learn Cocoa game development?
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.
I think your best bet is to look online. I'm sorry I can't help you anymore.
~ Bring a Pen ~
As requested in your other thread, I shall answer your question here presently.
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.
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
[/edit]
(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

Could someone explain why this way of doing a constant aspect ratio does not work.
The window is not rendered at all. Could you show the best way of doing constact aspect ratio.
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)];
}
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)];
}
But could you show how to make any aspect ratio constant.
It should be apparent that all you need to do is change the values in
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.
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.
Thanks. It works without a projection matrix.
I use this to accelerate the circle:
Is this a good method, or is it better to enumerate the keys like in http://www.idevgames.com/forum/showpost....ostcount=5
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;
}
}
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
... just isn't good enough. You'll need to do this instead, if you want any consistency:
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;
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.
Thanks for answering my questions.
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.
viewWidth was undeclared in your aspect ratio code, so I defined it.
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?
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?
Here, try this instead.
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.
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.
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.
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.
Possibly Related Threads...
Thread: | Author | Replies: | Views: | Last Post | |
Game Development - language? | bubbles | 0 | 1,699 |
Mar 1, 2016 07:00 PM Last Post: bubbles |
|
How to get started in game development! | Taxxodium | 22 | 73,949 |
Jan 14, 2016 07:25 AM Last Post: Program-Ace |
|
Switching developers mid-game development | fauxdev | 4 | 6,231 |
Feb 17, 2014 05:46 PM Last Post: funkboy |
|
iphone game development on Windows? | Briksins | 22 | 34,155 |
Dec 19, 2010 01:43 PM Last Post: sefiroths |
|
Objective C and game development | r00li | 5 | 12,396 |
Oct 5, 2009 08:39 AM Last Post: r00li |