summary refs log tree commit diff stats
path: root/libs/cocos2d/Platforms/iOS/ES1Renderer.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
committerStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
commit9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch)
tree5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/Platforms/iOS/ES1Renderer.m
downloadcartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/Platforms/iOS/ES1Renderer.m')
-rwxr-xr-xlibs/cocos2d/Platforms/iOS/ES1Renderer.m259
1 files changed, 259 insertions, 0 deletions
diff --git a/libs/cocos2d/Platforms/iOS/ES1Renderer.m b/libs/cocos2d/Platforms/iOS/ES1Renderer.m new file mode 100755 index 0000000..398d946 --- /dev/null +++ b/libs/cocos2d/Platforms/iOS/ES1Renderer.m
@@ -0,0 +1,259 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2010 Ricardo Quesada
5 * Copyright (c) 2011 Zynga Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 *
25 *
26 * File autogenerated with Xcode. Adapted for cocos2d needs.
27 */
28
29// Only compile this code on iOS. These files should NOT be included on your Mac project.
30// But in case they are included, it won't be compiled.
31#import <Availability.h>
32#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
33
34#import "ES1Renderer.h"
35#import "../../Support/OpenGL_Internal.h"
36#import "../../ccMacros.h"
37
38
39@interface ES1Renderer (private)
40
41- (GLenum) convertPixelFormat:(int) pixelFormat;
42
43@end
44
45
46@implementation ES1Renderer
47
48@synthesize context=context_;
49
50- (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples
51{
52 if ((self = [super init]))
53 {
54 if ( sharegroup == nil )
55 {
56 context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
57 }
58 else
59 {
60 context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:sharegroup];
61 }
62
63 if (!context_ || ![EAGLContext setCurrentContext:context_])
64 {
65 [self release];
66 return nil;
67 }
68
69 // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
70 glGenFramebuffersOES(1, &defaultFramebuffer_);
71 NSAssert( defaultFramebuffer_, @"Can't create default frame buffer");
72 glGenRenderbuffersOES(1, &colorRenderbuffer_);
73 NSAssert( colorRenderbuffer_, @"Can't create default render buffer");
74
75 glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_);
76 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
77 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer_);
78
79 depthFormat_ = depthFormat;
80
81 if( depthFormat_ ) {
82// glGenRenderbuffersOES(1, &depthBuffer_);
83// glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_);
84// glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, 100, 100);
85// glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_);
86
87 // default buffer
88// glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
89 }
90
91 pixelFormat_ = pixelFormat;
92 multiSampling_ = multiSampling;
93 if (multiSampling_)
94 {
95 GLint maxSamplesAllowed;
96 glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed);
97 samplesToUse_ = MIN(maxSamplesAllowed,requestedSamples);
98
99 /* Create the MSAA framebuffer (offscreen) */
100 glGenFramebuffersOES(1, &msaaFramebuffer_);
101 glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_);
102
103 }
104
105 CHECK_GL_ERROR();
106 }
107
108 return self;
109}
110
111- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
112{
113 // Allocate color buffer backing based on the current layer size
114
115 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
116
117 if (![context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer])
118 {
119 CCLOG(@"failed to call context");
120 }
121
122 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth_);
123 glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight_);
124
125 CCLOG(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_);
126
127 if (multiSampling_)
128 {
129
130 if ( msaaColorbuffer_) {
131 glDeleteRenderbuffersOES(1, &msaaColorbuffer_);
132 msaaColorbuffer_ = 0;
133 }
134
135 /* Create the offscreen MSAA color buffer.
136 After rendering, the contents of this will be blitted into ColorRenderbuffer */
137
138 //msaaFrameBuffer needs to be binded
139 glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_);
140 glGenRenderbuffersOES(1, &msaaColorbuffer_);
141 glBindRenderbufferOES(GL_RENDERBUFFER_OES, msaaColorbuffer_);
142 glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_,pixelFormat_ , backingWidth_, backingHeight_);
143 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, msaaColorbuffer_);
144
145 if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
146 {
147 CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
148 return NO;
149 }
150 }
151
152 if (depthFormat_)
153 {
154 if( ! depthBuffer_ )
155 glGenRenderbuffersOES(1, &depthBuffer_);
156
157 glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_);
158 if( multiSampling_ )
159 glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_, depthFormat_,backingWidth_, backingHeight_);
160 else
161 glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, backingWidth_, backingHeight_);
162 glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_);
163
164 // bind color buffer
165 glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_);
166 }
167
168 glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_);
169
170 if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
171 {
172 CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
173 return NO;
174 }
175
176 CHECK_GL_ERROR();
177
178 return YES;
179}
180
181-(CGSize) backingSize
182{
183 return CGSizeMake( backingWidth_, backingHeight_);
184}
185
186- (NSString*) description
187{
188 return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], self, backingWidth_, backingHeight_];
189}
190
191
192- (void)dealloc
193{
194 CCLOGINFO(@"cocos2d: deallocing %@", self);
195
196 // Tear down GL
197 if(defaultFramebuffer_)
198 {
199 glDeleteFramebuffersOES(1, &defaultFramebuffer_);
200 defaultFramebuffer_ = 0;
201 }
202
203 if(colorRenderbuffer_)
204 {
205 glDeleteRenderbuffersOES(1, &colorRenderbuffer_);
206 colorRenderbuffer_ = 0;
207 }
208
209 if( depthBuffer_ )
210 {
211 glDeleteRenderbuffersOES(1, &depthBuffer_);
212 depthBuffer_ = 0;
213 }
214
215 if ( msaaColorbuffer_)
216 {
217 glDeleteRenderbuffersOES(1, &msaaColorbuffer_);
218 msaaColorbuffer_ = 0;
219 }
220
221 if ( msaaFramebuffer_)
222 {
223 glDeleteRenderbuffersOES(1, &msaaFramebuffer_);
224 msaaFramebuffer_ = 0;
225 }
226
227 // Tear down context
228 if ([EAGLContext currentContext] == context_)
229 [EAGLContext setCurrentContext:nil];
230
231 [context_ release];
232 context_ = nil;
233
234 [super dealloc];
235}
236
237- (unsigned int) colorRenderBuffer
238{
239 return colorRenderbuffer_;
240}
241
242- (unsigned int) defaultFrameBuffer
243{
244 return defaultFramebuffer_;
245}
246
247- (unsigned int) msaaFrameBuffer
248{
249 return msaaFramebuffer_;
250}
251
252- (unsigned int) msaaColorBuffer
253{
254 return msaaColorbuffer_;
255}
256
257@end
258
259#endif // __IPHONE_OS_VERSION_MAX_ALLOWED