axmol/cocos2dx/platform/iphone/ES1Renderer.m

160 lines
4.9 KiB
Objective-C

/*
* cocos2d for iPhone: http://www.cocos2d-iphone.org
*
* Copyright (c) 2010 Ricardo Quesada
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*
* File autogenerated with Xcode. Adapted for cocos2d needs.
*/
#import "ES1Renderer.h"
#import "OpenGL_Internal.h"
//#import "ccMacros.h"
@implementation ES1Renderer
@synthesize context=context_;
// Create an OpenGL ES 1.1 context
- (id)initWithDepthFormat:(GLuint)depthFormat
{
if ((self = [super init]))
{
context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
if (!context_ || ![EAGLContext setCurrentContext:context_])
{
[self release];
return nil;
}
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
glGenFramebuffersOES(1, &defaultFramebuffer_);
NSAssert( defaultFramebuffer_, @"Can't create default frame buffer");
glGenRenderbuffersOES(1, &colorRenderbuffer_);
NSAssert( colorRenderbuffer_, @"Can't create default render buffer");
glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer_);
depthFormat_ = depthFormat;
if( depthFormat_ ) {
// glGenRenderbuffersOES(1, &depthBuffer_);
// glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_);
// glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, 100, 100);
// glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_);
// default buffer
// glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
}
CHECK_GL_ERROR();
}
return self;
}
- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
{
// Allocate color buffer backing based on the current layer size
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
if (![context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer])
{
//CCLOG(@"failed to call context");
}
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth_);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight_);
//CCLOG(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_);
if (depthFormat_) {
if( ! depthBuffer_ )
glGenRenderbuffersOES(1, &depthBuffer_);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, backingWidth_, backingHeight_);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_);
// bind color buffer
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
}
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
{
//CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
return NO;
}
return YES;
}
-(CGSize) backingSize
{
return CGSizeMake( backingWidth_, backingHeight_);
}
- (NSString*) description
{
return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], self, backingWidth_, backingHeight_];
}
- (void)dealloc
{
//CCLOGINFO(@"cocos2d: deallocing %@", self);
// Tear down GL
if(defaultFramebuffer_)
{
glDeleteFramebuffersOES(1, &defaultFramebuffer_);
defaultFramebuffer_ = 0;
}
if(colorRenderbuffer_)
{
glDeleteRenderbuffersOES(1, &colorRenderbuffer_);
colorRenderbuffer_ = 0;
}
if( depthBuffer_ )
{
glDeleteRenderbuffersOES(1, &depthBuffer_);
depthBuffer_ = 0;
}
// Tear down context
if ([EAGLContext currentContext] == context_)
[EAGLContext setCurrentContext:nil];
[context_ release];
context_ = nil;
[super dealloc];
}
@end