Skip to content

Commit ac77f99

Browse files
committed
Merge branch 'develop' of https://github.com/cocos2d/cocos2d-iphone into develop
2 parents 456f162 + 590c404 commit ac77f99

File tree

9 files changed

+89
-60
lines changed

9 files changed

+89
-60
lines changed

cocos2d/CCDirector.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,12 @@ and when to execute the Scenes.
134134
/* projection used */
135135
CCDirectorProjection _projection;
136136

137-
/* CCDirector delegate */
138-
__weak id<CCDirectorDelegate> _delegate;
139-
140137
/* window size in points */
141138
CGSize _winSizeInPoints;
142139

143140
/* window size in pixels */
144141
CGSize _winSizeInPixels;
145142

146-
/* the cocos2d running thread */
147-
__weak NSThread *_runningThread;
148-
149143
/* scheduler associated with this director */
150144
CCScheduler *_scheduler;
151145

cocos2d/CCDirector_Private.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
@interface CCDirector () {
2828
@protected
2929
GLKMatrix4 _projectionMatrix;
30+
__weak id<CCDirectorDelegate> _delegate;
31+
__weak NSThread *_runningThread;
32+
3033
}
3134

3235
/* Whether or not the replaced scene will receive the cleanup message.

cocos2d/CCRenderer.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,14 @@ -(void)setRenderState:(CCRenderState *)renderState
666666

667667
// Set the shader's uniform state.
668668
__unsafe_unretained NSDictionary *shaderUniforms = renderState->_shaderUniforms;
669+
__unsafe_unretained NSDictionary *globalShaderUniforms = _globalShaderUniforms;
669670
if(shaderUniforms != _shaderUniforms){
670671
glInsertEventMarkerEXT(0, "Uniforms");
671672

672673
__unsafe_unretained NSDictionary *setters = shader->_uniformSetters;
673674
for(NSString *uniformName in setters){
674675
__unsafe_unretained CCUniformSetter setter = setters[uniformName];
675-
setter(self, shaderUniforms[uniformName] ?: _globalShaderUniforms[uniformName]);
676+
setter(self, shaderUniforms, globalShaderUniforms);
676677
}
677678
_shaderUniforms = shaderUniforms;
678679
}

cocos2d/CCShader.m

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
"uniform highp vec4 cc_SinTime;\n"
7979
"uniform highp vec4 cc_CosTime;\n"
8080
"uniform highp vec4 cc_Random01;\n\n"
81-
"uniform lowp sampler2D cc_MainTexture;\n\n"
82-
"varying lowp vec4 cc_FragColor;\n"
81+
"uniform " XSTR(CC_SHADER_COLOR_PRECISION) " sampler2D cc_MainTexture;\n\n"
82+
"varying " XSTR(CC_SHADER_COLOR_PRECISION) " vec4 cc_FragColor;\n"
8383
"varying highp vec2 cc_FragTexCoord1;\n"
8484
"varying highp vec2 cc_FragTexCoord2;\n\n"
8585
"// End Cocos2D shader header.\n\n";
@@ -97,7 +97,7 @@
9797

9898
static const GLchar *CCFragmentShaderHeader =
9999
"#ifdef GL_ES\n"
100-
"precision mediump float;\n\n"
100+
"precision " XSTR(CC_SHADER_DEFAULT_FRAGMENT_PRECISION) " float;\n"
101101
"#endif\n\n"
102102
"// End Cocos2D fragment shader header.\n\n";
103103

@@ -235,8 +235,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
235235
static CCUniformSetter
236236
SetFloat(NSString *name, GLint location)
237237
{
238-
return ^(CCRenderer *renderer, NSNumber *value){
239-
value = value ?: @0;
238+
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
239+
NSNumber *value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: @(0.0);
240240
NSCAssert([value isKindOfClass:[NSNumber class]], @"Shader uniform '%@' value must be wrapped in a NSNumber.", name);
241241

242242
glUniform1f(location, value.floatValue);
@@ -246,8 +246,29 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
246246
static CCUniformSetter
247247
SetVec2(NSString *name, GLint location)
248248
{
249-
return ^(CCRenderer *renderer, NSValue *value){
250-
value = value ?: [NSValue valueWithGLKVector2:GLKVector2Make(0.0f, 0.0f)];
249+
NSString *textureName = nil;
250+
bool pixelSize = [name hasSuffix:@"PixelSize"];
251+
if(pixelSize){
252+
textureName = [name substringToIndex:name.length - @"PixelSize".length];
253+
} else if([name hasSuffix:@"Size"]){
254+
textureName = [name substringToIndex:name.length - @"Size".length];
255+
}
256+
257+
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
258+
NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name];
259+
260+
// Fall back on looking up the actual texture size if the name matches a texture.
261+
if(value == nil && textureName){
262+
CCTexture *texture = shaderUniforms[textureName] ?: globalShaderUniforms[textureName];
263+
GLKVector2 sizeInPixels = GLKVector2Make(texture.pixelWidth, texture.pixelHeight);
264+
265+
GLKVector2 size = GLKVector2MultiplyScalar(sizeInPixels, pixelSize ? 1.0 : 1.0/texture.contentScale);
266+
value = [NSValue valueWithGLKVector2:size];
267+
}
268+
269+
// Finally fall back on 0.
270+
if(value == nil) value = [NSValue valueWithGLKVector2:GLKVector2Make(0.0f, 0.0f)];
271+
251272
NSCAssert([value isKindOfClass:[NSValue class]], @"Shader uniform '%@' value must be wrapped in a NSValue.", name);
252273

253274
if(strcmp(value.objCType, @encode(GLKVector2)) == 0){
@@ -268,8 +289,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
268289
static CCUniformSetter
269290
SetVec3(NSString *name, GLint location)
270291
{
271-
return ^(CCRenderer *renderer, NSValue *value){
272-
value = value ?: [NSValue valueWithGLKVector3:GLKVector3Make(0.0f, 0.0f, 0.0f)];
292+
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
293+
NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector3:GLKVector3Make(0.0f, 0.0f, 0.0f)];
273294
NSCAssert([value isKindOfClass:[NSValue class]], @"Shader uniform '%@' value must be wrapped in a NSValue.", name);
274295
NSCAssert(strcmp(value.objCType, @encode(GLKVector3)) == 0, @"Shader uniformm 'vec3 %@' value must be passed using [NSValue valueWithGLKVector3:]", name);
275296

@@ -281,8 +302,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
281302
static CCUniformSetter
282303
SetVec4(NSString *name, GLint location)
283304
{
284-
return ^(CCRenderer *renderer, id value){
285-
value = value ?: [NSValue valueWithGLKVector4:GLKVector4Make(0.0f, 0.0f, 0.0f, 1.0f)];
305+
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
306+
NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKVector4:GLKVector4Make(0.0f, 0.0f, 0.0f, 1.0f)];
286307

287308
if([value isKindOfClass:[NSValue class]]){
288309
NSCAssert(strcmp([(NSValue *)value objCType], @encode(GLKVector4)) == 0, @"Shader uniformm 'vec4 %@' value must be passed using [NSValue valueWithGLKVector4:].", name);
@@ -301,8 +322,8 @@ +(GLuint)createVAOforCCVertexBuffer:(GLuint)vbo elementBuffer:(GLuint)ebo
301322
static CCUniformSetter
302323
SetMat4(NSString *name, GLint location)
303324
{
304-
return ^(CCRenderer *renderer, NSValue *value){
305-
value = value ?: [NSValue valueWithGLKMatrix4:GLKMatrix4Identity];
325+
return ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
326+
NSValue *value = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [NSValue valueWithGLKMatrix4:GLKMatrix4Identity];
306327
NSCAssert([value isKindOfClass:[NSValue class]], @"Shader uniform '%@' value must be wrapped in a NSValue.", name);
307328
NSCAssert(strcmp(value.objCType, @encode(GLKMatrix4)) == 0, @"Shader uniformm 'mat4 %@' value must be passed using [NSValue valueWithGLKMatrix4:]", name);
308329

@@ -344,8 +365,8 @@ -(NSDictionary *)uniformSettersForProgram:(GLuint)program
344365
case GL_FLOAT_MAT4: uniformSetters[name] = SetMat4(name, location); break;
345366
case GL_SAMPLER_2D: {
346367
// Sampler setters are handled a differently since the real work is binding the texture and not setting the uniform value.
347-
uniformSetters[name] = ^(CCRenderer *renderer, CCTexture *texture){
348-
texture = texture ?: [CCTexture none];
368+
uniformSetters[name] = ^(CCRenderer *renderer, NSDictionary *shaderUniforms, NSDictionary *globalShaderUniforms){
369+
CCTexture *texture = shaderUniforms[name] ?: globalShaderUniforms[name] ?: [CCTexture none];
349370
NSAssert([texture isKindOfClass:[CCTexture class]], @"Shader uniform '%@' value must be a CCTexture object.", name);
350371

351372
// Bind the texture to the texture unit for the uniform.

cocos2d/CCShader_Private.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#import "CCShader.h"
22

33
@class CCRenderer;
4-
typedef void (^CCUniformSetter)(__unsafe_unretained CCRenderer *renderer, __unsafe_unretained id value);
4+
typedef void (^CCUniformSetter)(
5+
__unsafe_unretained CCRenderer *renderer,
6+
__unsafe_unretained NSDictionary *shaderUniforms,
7+
__unsafe_unretained NSDictionary *globalShaderUniforms
8+
);
59

610
@interface CCShader() {
711
@public

cocos2d/CCSpriteFrame.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,7 @@
4040

4141
@class CCProxy;
4242

43-
@interface CCSpriteFrame : NSObject <NSCopying> {
44-
CGRect _rectInPixels;
45-
BOOL _rotated;
46-
CGPoint _offsetInPixels;
47-
CGSize _originalSizeInPixels;
48-
CCTexture *_texture;
49-
NSString *_textureFilename;
50-
CCProxy __weak *_proxy;
51-
}
52-
43+
@interface CCSpriteFrame : NSObject <NSCopying>
5344

5445
/// -----------------------------------------------------------------------
5546
/// @name Accessing Sprite Frame Attributes

cocos2d/CCSpriteFrame.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@
3131
#import "CCTexture.h"
3232
#import "ccMacros.h"
3333
#import "CCSpriteFrameCache.h"
34+
#import "CCTexture_Private.h"
3435

3536
@implementation CCSpriteFrame
3637
{
38+
CGRect _rectInPixels;
39+
BOOL _rotated;
40+
CGPoint _offsetInPixels;
41+
CGSize _originalSizeInPixels;
42+
CCTexture *_texture;
43+
NSString *_textureFilename;
44+
CCProxy __weak *_proxy;
3745
__weak CCTexture *_lazyTexture;
3846
}
3947

@@ -155,4 +163,27 @@ -(CCTexture*) texture
155163
{
156164
return (_texture ?: self.lazyTexture);
157165
}
166+
167+
- (BOOL)hasProxy
168+
{
169+
@synchronized(self){
170+
// NSLog(@"hasProxy: %p", self);
171+
return(_proxy != nil);
172+
}
173+
}
174+
175+
- (CCProxy *)proxy
176+
{
177+
@synchronized(self){
178+
__strong CCProxy *proxy = _proxy;
179+
180+
if (_proxy == nil){
181+
proxy = [[CCProxy alloc] initWithTarget:self];
182+
_proxy = proxy;
183+
}
184+
185+
return(proxy);
186+
}
187+
}
188+
158189
@end

cocos2d/CCSpriteFrameCache.m

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,9 @@
4444
#import "CCTexture_Private.h"
4545

4646

47-
@implementation CCSpriteFrame(Proxy)
48-
49-
- (BOOL)hasProxy
50-
{
51-
@synchronized(self)
52-
{
53-
// NSLog(@"hasProxy: %p", self);
54-
return(_proxy != nil);
55-
}
56-
}
57-
58-
- (CCProxy *)proxy
59-
{
60-
@synchronized(self)
61-
{
62-
__strong CCProxy *proxy = _proxy;
63-
64-
if (_proxy == nil)
65-
{
66-
proxy = [[CCProxy alloc] initWithTarget:self];
67-
_proxy = proxy;
68-
}
69-
70-
return(proxy);
71-
}
72-
}
73-
47+
@interface CCSpriteFrame(Proxy)
48+
- (BOOL)hasProxy;
49+
- (CCProxy *)proxy;
7450
@end
7551

7652

cocos2d/ccConfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@
130130
#define CC_SPRITE_DEBUG_DRAW 0
131131
#endif
132132

133+
#ifndef CC_SHADER_COLOR_PRECISION
134+
#define CC_SHADER_COLOR_PRECISION lowp
135+
#endif
136+
137+
#ifndef CC_SHADER_DEFAULT_FRAGMENT_PRECISION
138+
#define CC_SHADER_DEFAULT_FRAGMENT_PRECISION mediump
139+
#endif
140+
133141
#ifndef CC_ENABLE_EXPERIMENTAL_EFFECTS
134142
#define CC_ENABLE_EXPERIMENTAL_EFFECTS 0
135143
#endif

0 commit comments

Comments
 (0)