This is supposed to

Member
Posts: 131
Joined: 2010.08
Post: #1
I have a problem with an application but can not find a way to attach the project to this message. Could attachments be added to this forum? It would be nicer than putting the code in the message body.
Code:
#import <Cocoa/Cocoa.h>


@interface OpenGLView : NSOpenGLView
{
    NSTimer * m_timer;
    NSRect m_rectView;
    bool m_keys[256];
    GLfloat ballX;
}

//
//
- (void) keyDown:(NSEvent *)theEvent;

//
//
- (void) keyUp:(NSEvent *)theEvent;

//
//
- (void) keyAction:(const char)inKey isPressed:(const bool)inValue;

//
//
- (BOOL) acceptsFirstResponder;

//
//
- (BOOL) becomeFirstResponder;

//
//
- (BOOL) resignFirstResponder;

//
//
- (id) initWithFrame:(NSRect)frameRect;

//
//
- (void) setupAnimationTimerWithInterval:(const float)inInterval;

//
//
- (void) timerUpdate;

//
//
- (void) resizeGL;

//
//
- (void) prepareOpenGL;

//
//
- (void) drawRect:(NSRect)rect;

//
//
-(void) ball;

//
//
- (void) moveBall;

@end
Code:
#import "OpenGLView.h"
#define BALL_SPEED            1.0f
@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) timerUpdate
{
    [self moveBall];
    [self setNeedsDisplay:YES];
}

//
//
- (void) resizeGL
{
    NSRect rectView = [self bounds];
    
    if( m_rectView.size.width != rectView.size.width || m_rectView.size.height != rectView.size.height )
    {
        glViewport( 0, 0, rectView.size.width, rectView.size.height );
        m_rectView = rectView;
    }
}

//
//
- (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
{
    //
    //
    [self resizeGL];
    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;
        
    }
}


- (void) ball {
    
    glColor3f(0.01f, 0.6f, 5.0f);
    
    GLint i;
    
    GLfloat cosine, sine, PI, sizeFraction;
    PI = 3.14159265;
    sizeFraction = 0.3;
    
    glTranslatef(ballX, 0.0f, 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(sizeFraction*cosine,sizeFraction*sine);
    }
}


@end
This is supposed to render a circle that moves left or right when a or d is pressed. However, nothing is rendered. The nib is correct, as there is an OpenGLView which has this view, "OpenGLView", as its identity. What should I do to correct this problem? You could also mention other ways to improve this application.
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #2
I dunno, but you're missing a glEnd() in your ball method. Should be:

Code:
- (void) ball {
    
    glColor3f(0.01f, 0.6f, 5.0f);
    
    GLint i;
    
    GLfloat cosine, sine, PI, sizeFraction;
    PI = 3.14159265;
    sizeFraction = 0.3;
    
    glTranslatef(ballX, 0.0f, 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(sizeFraction*cosine,sizeFraction*sine);
    }
    glEnd();
}
Quote this message in a reply
Member
Posts: 131
Joined: 2010.08
Post: #3
Thanks. Adding glEnd() caused the background to be rendered and adding {} inside the glBeginglEnd caused the circle to be rendered.
Could you guys also answer my question in http://www.idevgames.com/forums/thread-7930.html
Quote this message in a reply
Post Reply