Learning Cocoa game development

Member
Posts: 131
Joined: 2010.08
Post: #16
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
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #17
Only guessing here, but try adding this method:

Code:
- (void)reshape
{
    NSSize viewBounds = [self bounds].size;
    glViewport(0, 0, viewBounds.width, viewBounds.height);
}
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #18
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,
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  How to get started in game development! Taxxodium 19 48,033 Nov 1, 2014 11:06 PM
Last Post: sazal786
  Switching developers mid-game development fauxdev 4 1,562 Feb 17, 2014 05:46 PM
Last Post: funkboy
  [Book] Cocos2d for iPhone 1 Game Development Cookbook NathanBurba 1 5,592 Jan 8, 2012 05:40 PM
Last Post: mmztech
  iphone game development on Windows? Briksins 22 22,858 Dec 19, 2010 01:43 PM
Last Post: sefiroths
  Objective C and game development r00li 5 9,433 Oct 5, 2009 08:39 AM
Last Post: r00li