diff options
Diffstat (limited to 'libs/cocos2d/CCGrabber.m')
-rwxr-xr-x | libs/cocos2d/CCGrabber.m | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/libs/cocos2d/CCGrabber.m b/libs/cocos2d/CCGrabber.m new file mode 100755 index 0000000..a259091 --- /dev/null +++ b/libs/cocos2d/CCGrabber.m | |||
@@ -0,0 +1,95 @@ | |||
1 | /* | ||
2 | * cocos2d for iPhone: http://www.cocos2d-iphone.org | ||
3 | * | ||
4 | * Copyright (c) 2009 On-Core | ||
5 | * | ||
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
7 | * of this software and associated documentation files (the "Software"), to deal | ||
8 | * in the Software without restriction, including without limitation the rights | ||
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
10 | * copies of the Software, and to permit persons to whom the Software is | ||
11 | * furnished to do so, subject to the following conditions: | ||
12 | * | ||
13 | * The above copyright notice and this permission notice shall be included in | ||
14 | * all copies or substantial portions of the Software. | ||
15 | * | ||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
22 | * THE SOFTWARE. | ||
23 | * | ||
24 | */ | ||
25 | |||
26 | |||
27 | #import "Platforms/CCGL.h" | ||
28 | #import "CCGrabber.h" | ||
29 | #import "ccMacros.h" | ||
30 | #import "CCTexture2D.h" | ||
31 | #import "Support/OpenGL_Internal.h" | ||
32 | |||
33 | @implementation CCGrabber | ||
34 | |||
35 | -(id) init | ||
36 | { | ||
37 | if(( self = [super init] )) { | ||
38 | // generate FBO | ||
39 | ccglGenFramebuffers(1, &fbo); | ||
40 | } | ||
41 | return self; | ||
42 | } | ||
43 | |||
44 | -(void)grab:(CCTexture2D*)texture | ||
45 | { | ||
46 | glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &oldFBO); | ||
47 | |||
48 | // bind | ||
49 | ccglBindFramebuffer(CC_GL_FRAMEBUFFER, fbo); | ||
50 | |||
51 | // associate texture with FBO | ||
52 | ccglFramebufferTexture2D(CC_GL_FRAMEBUFFER, CC_GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture.name, 0); | ||
53 | |||
54 | // check if it worked (probably worth doing :) ) | ||
55 | GLuint status = ccglCheckFramebufferStatus(CC_GL_FRAMEBUFFER); | ||
56 | if (status != CC_GL_FRAMEBUFFER_COMPLETE) | ||
57 | [NSException raise:@"Frame Grabber" format:@"Could not attach texture to framebuffer"]; | ||
58 | |||
59 | ccglBindFramebuffer(CC_GL_FRAMEBUFFER, oldFBO); | ||
60 | } | ||
61 | |||
62 | -(void)beforeRender:(CCTexture2D*)texture | ||
63 | { | ||
64 | glGetIntegerv(CC_GL_FRAMEBUFFER_BINDING, &oldFBO); | ||
65 | ccglBindFramebuffer(CC_GL_FRAMEBUFFER, fbo); | ||
66 | |||
67 | // BUG XXX: doesn't work with RGB565. | ||
68 | |||
69 | |||
70 | glClearColor(0,0,0,0); | ||
71 | |||
72 | // BUG #631: To fix #631, uncomment the lines with #631 | ||
73 | // Warning: But it CCGrabber won't work with 2 effects at the same time | ||
74 | // glClearColor(0.0f,0.0f,0.0f,1.0f); // #631 | ||
75 | |||
76 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
77 | |||
78 | // glColorMask(TRUE, TRUE, TRUE, FALSE); // #631 | ||
79 | |||
80 | } | ||
81 | |||
82 | -(void)afterRender:(CCTexture2D*)texture | ||
83 | { | ||
84 | ccglBindFramebuffer(CC_GL_FRAMEBUFFER, oldFBO); | ||
85 | // glColorMask(TRUE, TRUE, TRUE, TRUE); // #631 | ||
86 | } | ||
87 | |||
88 | - (void) dealloc | ||
89 | { | ||
90 | CCLOGINFO(@"cocos2d: deallocing %@", self); | ||
91 | ccglDeleteFramebuffers(1, &fbo); | ||
92 | [super dealloc]; | ||
93 | } | ||
94 | |||
95 | @end | ||