2009-05-23 4 views
0

2D 게임을 그릴 때 OpenGL보기 (필요한 투영, 카메라 앵글 등)를 설정하는 데 필요한 최소한의 보일러 플레이트 코드는 무엇입니까?OpenGL 2D View의 최소 상용구 코드는 무엇입니까?

#import <Cocoa/Cocoa.h> 

@interface MyView : NSView { 
} 

@end 

= = =

#import "MyView.h" 

@implementation MyView 

- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    return self; 
} 

- (void)drawRect:(NSRect)rect { 
    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort]; 
    CGRect frame = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFURLRef url = CFBundleCopyResourceURL(mainBundle, CFSTR("background"), CFSTR("png"), NULL); 
    CGDataProviderRef provider = CGDataProviderCreateWithURL (url); 
    CGImageRef image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault); 
    CGDataProviderRelease (provider); 
    CGContextDrawImage (myContext, frame, image); 
    CGImageRelease (image); 

    //rest of drawing code here... 
} 

@end 

: 예를 들어

는 석영 2D는 (예를 들어, 배경 이미지를로드하고) 사용자 지정보기에 그리기 수행하는 데 필요한 최소는 다음과 보일러 코드의 내용이 Mac에서 Open GL을 사용하는 것과는 달리 iPhone에서 Open GS ES와 다를 수 있습니까?

+0

아, 게임 라이브러리 권장 사항이 아닙니다. cocos2D 외. 나는 OpenGL만을 기반으로하는 대답을 원한다. – Hejazzman

답변

1

iPhone에서 OpenGL 응용 프로그램을 설정하는 가장 쉬운 방법은 Xcode를 통해 "OpenGL ES 응용 프로그램"을 만드는 것입니다. 시작하기 위해 필요한 상용구 소스 코드를 생성합니다. 대안으로

@implementation EAGLView 

@synthesize context; 

// You must implement this method 
+ (Class)layerClass { 
    return [CAEAGLLayer class]; 
} 

//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder: 
- (id)initWithCoder:(NSCoder*)coder { 

    if ((self = [super initWithCoder:coder])) { 
     // Get the layer 
     CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; 

     eaglLayer.opaque = YES; 
     eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
             [NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil]; 

     context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; 

     if (!context || ![EAGLContext setCurrentContext:context]) { 
     [self release]; 
     return nil; 
     } 
    } 
    return self; 
} 

- (void)drawView 
{ 
    [EAGLContext setCurrentContext:context]; 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glViewport(0, 0, ScreenWidth, ScreenHeight); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

    // Setup the coordinate system to use 0,0 as the lower left corner 
    // and 320,480 as the upper right corner of the screen (in portrait mode). 
    glOrthof(0.0f, ScreenWidth, 0.0f, ScreenHeight, -1.0f, 1.0f); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    // Here is where you draw everything in your world. 
    DrawWorld(); 

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context presentRenderbuffer:GL_RENDERBUFFER_OES]; 
} 


- (void)layoutSubviews { 
    [EAGLContext setCurrentContext:context]; 
    [self destroyFramebuffer]; 
    [self createFramebuffer]; 
    [self drawView]; 
} 


- (BOOL)createFramebuffer { 

    glGenFramebuffersOES(1, &viewFramebuffer); 
    glGenRenderbuffersOES(1, &viewRenderbuffer); 

    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer); 
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); 
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.layer]; 
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer); 

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &ScreenWidth); 
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &ScreenHeight); 

    if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { 
     NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); 
     return NO; 
    } 

    return YES; 
} 


- (void)destroyFramebuffer { 

    glDeleteFramebuffersOES(1, &viewFramebuffer); 
    viewFramebuffer = 0; 
    glDeleteRenderbuffersOES(1, &viewRenderbuffer); 
    viewRenderbuffer = 0; 
} 

- (void)dealloc { 

    if ([EAGLContext currentContext] == context) { 
     [EAGLContext setCurrentContext:nil]; 
    } 

    [context release]; 
    [super dealloc]; 
} 

@end 

, this article 아이폰에서의 OpenGL ES 응용 프로그램을 만드는에서 좋은 연습을 제공합니다 : 여기

은 내가 OpenGL을 아이폰 게임을 위해 사용하고있는 보일러 소스입니다.

0

Mac에서 Open GL을 사용하는 것과 달리 iPhone에서 [OpenGL] ES와 다른 내용이 있습니까?

예. Mac에서는 Application Kit의 일부인 NSOpenGLView를 사용합니다. iPhone에는 AppKit이 없습니다.

저는 iPhone 개발자가 아니기 때문에 더 말할 수는 없지만 iPhone에서는 EAGL을 사용하게 될 것입니다.

관련 문제