Skip to content

Commit fdf102d

Browse files
committed
Move CCRenderer NSValue additions into it’s own file.
1 parent 43bd397 commit fdf102d

File tree

4 files changed

+169
-129
lines changed

4 files changed

+169
-129
lines changed

cocos2d/CCRenderer.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#import "ccTypes.h"
2626
#import "CCShader.h"
2727

28+
#import "NSValue+CCRenderer.h"
29+
2830

2931
@class CCTexture;
3032

@@ -116,16 +118,6 @@ CCRenderCheckVisbility(const GLKMatrix4 *transform, GLKVector2 center, GLKVector
116118
}
117119

118120

119-
@interface NSValue(CCRenderer)
120-
121-
+(NSValue *)valueWithGLKVector2:(GLKVector2)vector;
122-
+(NSValue *)valueWithGLKVector3:(GLKVector3)vector;
123-
+(NSValue *)valueWithGLKVector4:(GLKVector4)vector;
124-
125-
+(NSValue *)valueWithGLKMatrix4:(GLKMatrix4)matrix;
126-
127-
@end
128-
129121
/// Key used to set the source color factor for [CCBlendMode blendModeWithOptions:].
130122
extern const NSString *CCBlendFuncSrcColor;
131123
/// Key used to set the destination color factor for [CCBlendMode blendModeWithOptions:].

cocos2d/CCRenderer.m

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -36,125 +36,10 @@
3636
#import "CCMetalSupport_Private.h"
3737
#endif
3838

39-
//MARK: NSValue Additions.
40-
@implementation NSValue(CCRenderer)
41-
42-
+(NSValue *)valueWithGLKVector2:(GLKVector2)vector
43-
{
44-
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector2)];
45-
}
46-
47-
+(NSValue *)valueWithGLKVector3:(GLKVector3)vector
48-
{
49-
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector3)];
50-
}
51-
52-
+(NSValue *)valueWithGLKVector4:(GLKVector4)vector
53-
{
54-
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector4)];
55-
}
56-
57-
+(NSValue *)valueWithGLKMatrix4:(GLKMatrix4)matrix
58-
{
59-
return [NSValue valueWithBytes:&matrix objCType:@encode(GLKMatrix4)];
60-
}
61-
62-
static void
63-
AdvanceUntilAfter(const char **cursor, const char c)
64-
{
65-
while(**cursor != c) (*cursor)++;
66-
(*cursor)++;
67-
}
68-
69-
static size_t
70-
ReadValue(const char **cursor)
71-
{
72-
char c = (**cursor);
73-
(*cursor)++;
74-
75-
switch(c){
76-
case 'c': return sizeof(char); break;
77-
case 'i': return sizeof(int); break;
78-
case 's': return sizeof(short); break;
79-
case 'l': return sizeof(long); break;
80-
case 'q': return sizeof(long long); break;
81-
case 'C': return sizeof(unsigned char); break;
82-
case 'I': return sizeof(unsigned int); break;
83-
case 'S': return sizeof(unsigned short); break;
84-
case 'L': return sizeof(unsigned long); break;
85-
case 'Q': return sizeof(unsigned long long); break;
86-
case 'f': return sizeof(float); break;
87-
case 'd': return sizeof(double); break;
88-
case 'B': return sizeof(_Bool); break;
89-
90-
case '{': {
91-
// struct
92-
AdvanceUntilAfter(cursor, '=');
93-
94-
size_t bytes = 0;
95-
while(**cursor != '}'){
96-
bytes += ReadValue(cursor);
97-
}
98-
99-
(*cursor)++;
100-
return bytes;
101-
}
102-
103-
case '(': {
104-
// Union
105-
AdvanceUntilAfter(cursor, '=');
106-
107-
size_t bytes = 0;
108-
while(**cursor != ')'){
109-
bytes = MAX(bytes, ReadValue(cursor));
110-
}
111-
112-
(*cursor)++;
113-
return bytes;
114-
}
115-
116-
case '[': {
117-
// array
118-
size_t count = 0;
119-
for(; '0' <= **cursor && **cursor <= '9'; (*cursor)++){
120-
count = count*10 + (**cursor - '0');
121-
}
122-
123-
size_t elementSize = ReadValue(cursor);
124-
125-
(*cursor)++;
126-
return count*elementSize;
127-
}
128-
129-
case 'v': // void
130-
case '*': // char *
131-
case 'b': // bitfield
132-
case '@': // object
133-
case '#': // class
134-
case ':': // selector
135-
case '^': // pointer
136-
default:
137-
NSCAssert(NO, @"@encode() type %c is forbidden to use with shader uniforms.", c);
138-
return 0;
139-
}
140-
}
141-
142-
// Partial implementation to calculate the size of an @encode() string.
143-
// Doesn't handle all types, alignment, etc.
144-
-(size_t)CCRendererSizeOf
145-
{
146-
size_t bytes = 0;
147-
148-
const char *objCType = self.objCType;
149-
const char **cursor = &objCType;
150-
151-
while(**cursor != '\0'){
152-
bytes += ReadValue(cursor);
153-
}
154-
155-
return bytes;
156-
}
39+
@interface NSValue()
15740

41+
// Defined in NSValue+CCRenderer.m.
42+
-(size_t)CCRendererSizeOf;
15843

15944
@end
16045

@@ -727,7 +612,6 @@ -(void)commit
727612
[_indexBuffer commit];
728613
}
729614

730-
731615
@end
732616

733617

cocos2d/NSValue+CCRenderer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* cocos2d for iPhone: http://www.cocos2d-iphone.org
3+
*
4+
* Copyright (c) 2014 Cocos2D Authors
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+
#import "ccTypes.h"
26+
27+
@interface NSValue(CCRenderer)
28+
29+
/// Create an NSValue wrapping a GLKVector2.
30+
+(NSValue *)valueWithGLKVector2:(GLKVector2)vector;
31+
32+
/// Create an NSValue wrapping a GLKVector3.
33+
+(NSValue *)valueWithGLKVector3:(GLKVector3)vector;
34+
35+
/// Create an NSValue wrapping a GLKVector4.
36+
+(NSValue *)valueWithGLKVector4:(GLKVector4)vector;
37+
38+
/// Create an NSValue wrapping a GLKMatrix4.
39+
+(NSValue *)valueWithGLKMatrix4:(GLKMatrix4)matrix;
40+
41+
@end

cocos2d/NSValue+CCRenderer.m

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#import "NSValue+CCRenderer.h"
2+
3+
//MARK: NSValue Additions.
4+
@implementation NSValue(CCRenderer)
5+
6+
+(NSValue *)valueWithGLKVector2:(GLKVector2)vector
7+
{
8+
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector2)];
9+
}
10+
11+
+(NSValue *)valueWithGLKVector3:(GLKVector3)vector
12+
{
13+
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector3)];
14+
}
15+
16+
+(NSValue *)valueWithGLKVector4:(GLKVector4)vector
17+
{
18+
return [NSValue valueWithBytes:&vector objCType:@encode(GLKVector4)];
19+
}
20+
21+
+(NSValue *)valueWithGLKMatrix4:(GLKMatrix4)matrix
22+
{
23+
return [NSValue valueWithBytes:&matrix objCType:@encode(GLKMatrix4)];
24+
}
25+
26+
static void
27+
AdvanceUntilAfter(const char **cursor, const char c)
28+
{
29+
while(**cursor != c) (*cursor)++;
30+
(*cursor)++;
31+
}
32+
33+
static size_t
34+
ReadValue(const char **cursor)
35+
{
36+
char c = (**cursor);
37+
(*cursor)++;
38+
39+
switch(c){
40+
case 'c': return sizeof(char); break;
41+
case 'i': return sizeof(int); break;
42+
case 's': return sizeof(short); break;
43+
case 'l': return sizeof(long); break;
44+
case 'q': return sizeof(long long); break;
45+
case 'C': return sizeof(unsigned char); break;
46+
case 'I': return sizeof(unsigned int); break;
47+
case 'S': return sizeof(unsigned short); break;
48+
case 'L': return sizeof(unsigned long); break;
49+
case 'Q': return sizeof(unsigned long long); break;
50+
case 'f': return sizeof(float); break;
51+
case 'd': return sizeof(double); break;
52+
case 'B': return sizeof(_Bool); break;
53+
54+
case '{': {
55+
// struct
56+
AdvanceUntilAfter(cursor, '=');
57+
58+
size_t bytes = 0;
59+
while(**cursor != '}'){
60+
bytes += ReadValue(cursor);
61+
}
62+
63+
(*cursor)++;
64+
return bytes;
65+
}
66+
67+
case '(': {
68+
// Union
69+
AdvanceUntilAfter(cursor, '=');
70+
71+
size_t bytes = 0;
72+
while(**cursor != ')'){
73+
bytes = MAX(bytes, ReadValue(cursor));
74+
}
75+
76+
(*cursor)++;
77+
return bytes;
78+
}
79+
80+
case '[': {
81+
// array
82+
size_t count = 0;
83+
for(; '0' <= **cursor && **cursor <= '9'; (*cursor)++){
84+
count = count*10 + (**cursor - '0');
85+
}
86+
87+
size_t elementSize = ReadValue(cursor);
88+
89+
(*cursor)++;
90+
return count*elementSize;
91+
}
92+
93+
case 'v': // void
94+
case '*': // char *
95+
case 'b': // bitfield
96+
case '@': // object
97+
case '#': // class
98+
case ':': // selector
99+
case '^': // pointer
100+
default:
101+
NSCAssert(NO, @"@encode() type %c is forbidden to use with shader uniforms.", c);
102+
return 0;
103+
}
104+
}
105+
106+
// Partial implementation to calculate the size of an @encode() string.
107+
// Doesn't handle all types, alignment, etc.
108+
-(size_t)CCRendererSizeOf
109+
{
110+
size_t bytes = 0;
111+
112+
const char *objCType = self.objCType;
113+
const char **cursor = &objCType;
114+
115+
while(**cursor != '\0'){
116+
bytes += ReadValue(cursor);
117+
}
118+
119+
return bytes;
120+
}
121+
122+
123+
@end

0 commit comments

Comments
 (0)