Learning Cocoa game development
Yes. The animation also shows the red rectangle resizing. So there is nothing wrong with the nib.
Code:
#import "OpenGLView.h"
#define BALL_SPEED 0.001f
#define ASPECT_WIDE 5.0f
#define ASPECT_HIGH 4.0f
#define MY_ASPECT_RATIO (ASPECT_WIDE / ASPECT_HIGH)
@implementation OpenGLView
- (id) initWithFrame:(NSRect)frameRect
{
NSOpenGLPixelFormatAttribute attributes [] =
{
NSOpenGLPFAWindow,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, 32,
NSOpenGLPFADepthSize, 24,
NSOpenGLPFAStencilSize, 8,
(NSOpenGLPixelFormatAttribute)nil
};
for( unsigned int keyIdx = 0; keyIdx < 256; ++keyIdx )
{
m_keys[keyIdx] = false;
}
NSOpenGLPixelFormat * pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
self = [super initWithFrame:frameRect pixelFormat:pixelFormat];
[pixelFormat release];
return self;
}
//
//
- (void) setupAnimationTimerWithInterval:(const float)inInterval
{
SEL the_selector = @selector( timerUpdate );
NSMethodSignature * a_signature = [OpenGLView instanceMethodSignatureForSelector:the_selector];
NSInvocation * an_invocation = [NSInvocation invocationWithMethodSignature:a_signature] ;
[an_invocation setSelector:the_selector];
[an_invocation setTarget:self];
m_timer = [NSTimer scheduledTimerWithTimeInterval:inInterval
invocation:an_invocation
repeats:YES];
[m_timer retain];
[[NSRunLoop currentRunLoop] addTimer:m_timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:m_timer forMode:NSEventTrackingRunLoopMode];
[[NSRunLoop currentRunLoop] addTimer:m_timer forMode:NSModalPanelRunLoopMode];
ballX = 0.0f;
}
- (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)];
}
//
//
- (void) timerUpdate
{
[self moveBall];
[self setNeedsDisplay:YES];
}
//
//
- (void) prepareOpenGL
{
glClearColor(0, 0, 0, 0);
GLint vblSynch = 1;
[[self openGLContext] setValues:&vblSynch forParameter:NSOpenGLCPSwapInterval];
[self setupAnimationTimerWithInterval:(1.0f/60.0f)];
[super prepareOpenGL];
}
//
//
- (void) drawRect:(NSRect)rect
{
glClear(GL_COLOR_BUFFER_BIT);
[self ball];
[[self openGLContext] flushBuffer];
}
//
//
- (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;
}
//
//
- (BOOL) acceptsFirstResponder
{
return YES;
}
//
//
- (BOOL) becomeFirstResponder
{
return YES;
}
//
//
- (BOOL) resignFirstResponder
{
return YES;
}
- (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;
}
}
- (void) ball {
glColor3f(0.05f, 0.6f, 5.0f);
GLint i;
GLfloat cosine, sine, PI, radiusFraction;
PI = 3.14159265;
radiusFraction = 0.3;
glTranslatef(ballX, ballY, 0.0f);
glBegin(GL_POLYGON);
{
for(i=0;i<100;i++)
{
cosine=cos(i*2*PI/100.0);
sine=sin(i*2*PI/100.0);
glVertex2f(radiusFraction*cosine,radiusFraction*sine);
}
}
glEnd();
}
@end
Only guessing here, but try adding this method:
Code:
- (void)reshape
{
NSSize viewBounds = [self bounds].size;
glViewport(0, 0, viewBounds.width, viewBounds.height);
}
It works. A sticky that has pieces of code that are fundamental to games should be made in this forum, in which the important pieces of code that you gave to me,
are given as model code for doing a constant aspect ratio view, so that beginner programmers would not need to ask these questions again.
Thanks for helping.
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)];
}
- (void)reshape
{
NSSize viewBounds = [self bounds].size;
glViewport(0, 0, viewBounds.width, viewBounds.height);
}are given as model code for doing a constant aspect ratio view, so that beginner programmers would not need to ask these questions again.
Thanks for helping.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| How to get started in game development! | Taxxodium | 18 | 40,682 |
Jun 5, 2012 03:55 PM Last Post: AnotherJake |
|
| [Book] Cocos2d for iPhone 1 Game Development Cookbook | NathanBurba | 1 | 4,153 |
Jan 8, 2012 05:40 PM Last Post: mmztech |
|
| iphone game development on Windows? | Briksins | 22 | 18,842 |
Dec 19, 2010 01:43 PM Last Post: sefiroths |
|
| Objective C and game development | r00li | 5 | 7,799 |
Oct 5, 2009 08:39 AM Last Post: r00li |
|
| Not really game development, but i don't know where else i could ask | filfil96 | 5 | 3,928 |
Apr 25, 2009 02:05 AM Last Post: kodex |
|

