|
76 | 76 |
|
77 | 77 | extern NSString * cocos2dVersion(void); |
78 | 78 |
|
| 79 | + |
| 80 | + |
| 81 | +@interface CCFPSLabel : CCNode<CCTextureProtocol> |
| 82 | +@property(nonatomic, strong) NSString *string; |
| 83 | +@end |
| 84 | + |
| 85 | +static const int CCFPSLabelChars = 12; |
| 86 | +static const float CCFPSLabelItemWidth = 12; |
| 87 | +static const float CCFPSLabelItemHeight = 32; |
| 88 | + |
| 89 | +@implementation CCFPSLabel { |
| 90 | + CCSpriteVertexes _charVertexes[CCFPSLabelChars]; |
| 91 | +} |
| 92 | + |
| 93 | +-(instancetype)initWithString:(NSString *)string texture:(CCTexture *)texture |
| 94 | +{ |
| 95 | + if((self = [super init])){ |
| 96 | + _string = string; |
| 97 | + |
| 98 | + self.texture = texture; |
| 99 | + self.shader = [CCShader positionTextureColorShader]; |
| 100 | + |
| 101 | + float w = CCFPSLabelItemWidth; |
| 102 | + float h = CCFPSLabelItemHeight; |
| 103 | + |
| 104 | + float tx = CCFPSLabelItemWidth/texture.contentSize.width; |
| 105 | + float ty = CCFPSLabelItemHeight/texture.contentSize.height; |
| 106 | + |
| 107 | + for(int i=0; i<CCFPSLabelChars; i++){ |
| 108 | + float tx0 = i*tx; |
| 109 | + float tx1 = (i + 1)*tx; |
| 110 | + _charVertexes[i].bl = (CCVertex){GLKVector4Make(0.0f, 0.0f, 0.0f, 1.0f), GLKVector2Make(tx0, 0.0f), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
| 111 | + _charVertexes[i].br = (CCVertex){GLKVector4Make( w, 0.0f, 0.0f, 1.0f), GLKVector2Make(tx1, 0.0f), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
| 112 | + _charVertexes[i].tr = (CCVertex){GLKVector4Make( w, h, 0.0f, 1.0f), GLKVector2Make(tx1, ty), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
| 113 | + _charVertexes[i].tl = (CCVertex){GLKVector4Make(0.0f, h, 0.0f, 1.0f), GLKVector2Make(tx0, ty), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return self; |
| 118 | +} |
| 119 | + |
| 120 | +-(void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform |
| 121 | +{ |
| 122 | + for(int i=0; i<_string.length; i++){ |
| 123 | + int c = [_string characterAtIndex:i]; |
| 124 | + |
| 125 | + // Skip spaces. |
| 126 | + if(c == ' ') continue; |
| 127 | + |
| 128 | + // Index relative to '.'. |
| 129 | + c = MAX(0, MIN(CCFPSLabelChars - 1, c - '.')); |
| 130 | + GLKMatrix4 t = GLKMatrix4Multiply(*transform, GLKMatrix4MakeTranslation(i*CCFPSLabelItemWidth, 0.0f, 0.0f)); |
| 131 | + |
| 132 | + CCRenderBuffer buffer = [renderer enqueueTriangles:2 andVertexes:4 withState:self.renderState globalSortOrder:NSIntegerMax]; |
| 133 | + CCRenderBufferSetVertex(buffer, 0, CCVertexApplyTransform(_charVertexes[c].bl, &t)); |
| 134 | + CCRenderBufferSetVertex(buffer, 1, CCVertexApplyTransform(_charVertexes[c].br, &t)); |
| 135 | + CCRenderBufferSetVertex(buffer, 2, CCVertexApplyTransform(_charVertexes[c].tr, &t)); |
| 136 | + CCRenderBufferSetVertex(buffer, 3, CCVertexApplyTransform(_charVertexes[c].tl, &t)); |
| 137 | + CCRenderBufferSetTriangle(buffer, 0, 0, 1, 2); |
| 138 | + CCRenderBufferSetTriangle(buffer, 1, 0, 2, 3); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +@end |
| 143 | + |
79 | 144 | @interface CCScheduler (Private) |
80 | 145 | @property(nonatomic, assign) CCTime fixedUpdateInterval; |
81 | 146 | @end |
@@ -841,165 +906,96 @@ -(void)setFixedUpdateInterval:(CCTime)fixedUpdateInterval |
841 | 906 | self.scheduler.fixedUpdateInterval = fixedUpdateInterval; |
842 | 907 | } |
843 | 908 |
|
844 | | -@end |
845 | | - |
846 | | - |
847 | | -@interface CCFPSLabel : CCNode<CCTextureProtocol> |
848 | | -@property(nonatomic, strong) NSString *string; |
849 | | -@end |
850 | | - |
851 | | -static const int CCFPSLabelChars = 12; |
852 | | -static const float CCFPSLabelItemWidth = 12; |
853 | | -static const float CCFPSLabelItemHeight = 32; |
854 | | - |
855 | | -@implementation CCFPSLabel { |
856 | | - CCSpriteVertexes _charVertexes[CCFPSLabelChars]; |
857 | | -} |
858 | | - |
859 | | --(instancetype)initWithString:(NSString *)string texture:(CCTexture *)texture |
860 | | -{ |
861 | | - if((self = [super init])){ |
862 | | - _string = string; |
863 | | - |
864 | | - self.texture = texture; |
865 | | - self.shader = [CCShader positionTextureColorShader]; |
866 | | - |
867 | | - float w = CCFPSLabelItemWidth; |
868 | | - float h = CCFPSLabelItemHeight; |
869 | | - |
870 | | - float tx = CCFPSLabelItemWidth/texture.contentSize.width; |
871 | | - float ty = CCFPSLabelItemHeight/texture.contentSize.height; |
872 | | - |
873 | | - for(int i=0; i<CCFPSLabelChars; i++){ |
874 | | - float tx0 = i*tx; |
875 | | - float tx1 = (i + 1)*tx; |
876 | | - _charVertexes[i].bl = (CCVertex){GLKVector4Make(0.0f, 0.0f, 0.0f, 1.0f), GLKVector2Make(tx0, 0.0f), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
877 | | - _charVertexes[i].br = (CCVertex){GLKVector4Make( w, 0.0f, 0.0f, 1.0f), GLKVector2Make(tx1, 0.0f), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
878 | | - _charVertexes[i].tr = (CCVertex){GLKVector4Make( w, h, 0.0f, 1.0f), GLKVector2Make(tx1, ty), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
879 | | - _charVertexes[i].tl = (CCVertex){GLKVector4Make(0.0f, h, 0.0f, 1.0f), GLKVector2Make(tx0, ty), GLKVector2Make(0.0f, 0.0f), GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f)}; |
880 | | - } |
881 | | - } |
882 | | - |
883 | | - return self; |
884 | | -} |
885 | | - |
886 | | --(void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform |
887 | | -{ |
888 | | - for(int i=0; i<_string.length; i++){ |
889 | | - int c = [_string characterAtIndex:i]; |
890 | | - |
891 | | - // Skip spaces. |
892 | | - if(c == ' ') continue; |
893 | | - |
894 | | - // Index relative to '.'. |
895 | | - c = MAX(0, MIN(CCFPSLabelChars - 1, c - '.')); |
896 | | - GLKMatrix4 t = GLKMatrix4Multiply(*transform, GLKMatrix4MakeTranslation(i*CCFPSLabelItemWidth, 0.0f, 0.0f)); |
897 | | - |
898 | | - CCRenderBuffer buffer = [renderer enqueueTriangles:2 andVertexes:4 withState:self.renderState globalSortOrder:NSIntegerMax]; |
899 | | - CCRenderBufferSetVertex(buffer, 0, CCVertexApplyTransform(_charVertexes[c].bl, &t)); |
900 | | - CCRenderBufferSetVertex(buffer, 1, CCVertexApplyTransform(_charVertexes[c].br, &t)); |
901 | | - CCRenderBufferSetVertex(buffer, 2, CCVertexApplyTransform(_charVertexes[c].tr, &t)); |
902 | | - CCRenderBufferSetVertex(buffer, 3, CCVertexApplyTransform(_charVertexes[c].tl, &t)); |
903 | | - CCRenderBufferSetTriangle(buffer, 0, 0, 1, 2); |
904 | | - CCRenderBufferSetTriangle(buffer, 1, 0, 2, 3); |
905 | | - } |
906 | | -} |
907 | | - |
908 | | -@end |
909 | | - |
910 | | - |
911 | | -@implementation CCDirector(Stats) |
912 | | - |
913 | 909 | // display statistics |
914 | 910 | -(void) showStats |
915 | 911 | { |
916 | | - _frames++; |
917 | | - _accumDt += _dt; |
918 | | - |
919 | | - if( _displayStats ) { |
920 | | - // Ms per Frame |
921 | | - |
922 | | - if( _accumDt > CC_DIRECTOR_STATS_INTERVAL) |
923 | | - { |
924 | | - NSString *spfstr = [[NSString alloc] initWithFormat:@"%.3f", _secondsPerFrame]; |
925 | | - [_SPFLabel setString:spfstr]; |
926 | | - |
927 | | - _frameRate = _frames/_accumDt; |
928 | | - _frames = 0; |
929 | | - _accumDt = 0; |
930 | | - |
931 | | -// sprintf(format,"%.1f",frameRate); |
932 | | -// [FPSLabel setCString:format]; |
933 | | - |
934 | | - NSString *fpsstr = [[NSString alloc] initWithFormat:@"%.1f", _frameRate]; |
935 | | - [_FPSLabel setString:fpsstr]; |
936 | | - |
937 | | - // Subtract one for the stat label's own batch. This caused a lot of confusion on the forums... |
938 | | - NSString *draws = [[NSString alloc] initWithFormat:@"%4lu", (unsigned long)__ccNumberOfDraws - 1]; |
939 | | - [_drawsLabel setString:draws]; |
940 | | - } |
941 | | - |
942 | | - // TODO should pass as a parameter instead? Requires changing method signatures... |
943 | | - CCRenderer *renderer = [CCRenderer currentRenderer]; |
944 | | - [_drawsLabel visit:renderer parentTransform:&_projectionMatrix]; |
945 | | - [_FPSLabel visit:renderer parentTransform:&_projectionMatrix]; |
946 | | - [_SPFLabel visit:renderer parentTransform:&_projectionMatrix]; |
947 | | - } |
948 | | - |
949 | | - __ccNumberOfDraws = 0; |
| 912 | + _frames++; |
| 913 | + _accumDt += _dt; |
| 914 | + |
| 915 | + if( _displayStats ) { |
| 916 | + // Ms per Frame |
| 917 | + |
| 918 | + if( _accumDt > CC_DIRECTOR_STATS_INTERVAL) |
| 919 | + { |
| 920 | + NSString *spfstr = [[NSString alloc] initWithFormat:@"%.3f", _secondsPerFrame]; |
| 921 | + [_SPFLabel setString:spfstr]; |
| 922 | + |
| 923 | + _frameRate = _frames/_accumDt; |
| 924 | + _frames = 0; |
| 925 | + _accumDt = 0; |
| 926 | + |
| 927 | + // sprintf(format,"%.1f",frameRate); |
| 928 | + // [FPSLabel setCString:format]; |
| 929 | + |
| 930 | + NSString *fpsstr = [[NSString alloc] initWithFormat:@"%.1f", _frameRate]; |
| 931 | + [_FPSLabel setString:fpsstr]; |
| 932 | + |
| 933 | + // Subtract one for the stat label's own batch. This caused a lot of confusion on the forums... |
| 934 | + NSString *draws = [[NSString alloc] initWithFormat:@"%4lu", (unsigned long)__ccNumberOfDraws - 1]; |
| 935 | + [_drawsLabel setString:draws]; |
| 936 | + } |
| 937 | + |
| 938 | + // TODO should pass as a parameter instead? Requires changing method signatures... |
| 939 | + CCRenderer *renderer = [CCRenderer currentRenderer]; |
| 940 | + [_drawsLabel visit:renderer parentTransform:&_projectionMatrix]; |
| 941 | + [_FPSLabel visit:renderer parentTransform:&_projectionMatrix]; |
| 942 | + [_SPFLabel visit:renderer parentTransform:&_projectionMatrix]; |
| 943 | + } |
| 944 | + |
| 945 | + __ccNumberOfDraws = 0; |
950 | 946 | } |
951 | 947 |
|
952 | 948 | -(void) calculateMPF |
953 | 949 | { |
954 | | - struct timeval now; |
955 | | - gettimeofday( &now, NULL); |
956 | | - |
957 | | - _secondsPerFrame = (now.tv_sec - _lastUpdate.tv_sec) + (now.tv_usec - _lastUpdate.tv_usec) / 1000000.0f; |
| 950 | + struct timeval now; |
| 951 | + gettimeofday( &now, NULL); |
| 952 | + |
| 953 | + _secondsPerFrame = (now.tv_sec - _lastUpdate.tv_sec) + (now.tv_usec - _lastUpdate.tv_usec) / 1000000.0f; |
958 | 954 | } |
959 | 955 |
|
960 | 956 | -(void)getFPSImageData:(unsigned char**)datapointer length:(NSUInteger*)len contentScale:(CGFloat *)scale |
961 | 957 | { |
962 | | - *datapointer = cc_fps_images_png; |
963 | | - *len = cc_fps_images_len(); |
964 | | - *scale = 1.0; |
| 958 | + *datapointer = cc_fps_images_png; |
| 959 | + *len = cc_fps_images_len(); |
| 960 | + *scale = 1.0; |
965 | 961 | } |
966 | 962 |
|
967 | 963 | -(void) createStatsLabel |
968 | 964 | { |
969 | | - if( _FPSLabel && _SPFLabel ) { |
970 | | - _FPSLabel = nil; |
971 | | - _SPFLabel = nil; |
972 | | - _drawsLabel = nil; |
973 | | - |
974 | | - [[CCFileUtils sharedFileUtils] purgeCachedEntries]; |
975 | | - } |
976 | | - |
977 | | - CCTexturePixelFormat currentFormat = [CCTexture defaultAlphaPixelFormat]; |
978 | | - [CCTexture setDefaultAlphaPixelFormat:CCTexturePixelFormat_RGBA4444]; |
979 | | - |
980 | | - unsigned char *data; |
981 | | - NSUInteger data_len; |
982 | | - CGFloat contentScale = 0; |
983 | | - [self getFPSImageData:&data length:&data_len contentScale:&contentScale]; |
984 | | - |
985 | | - NSData *nsdata = [NSData dataWithBytes:data length:data_len]; |
986 | | - CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData( (__bridge CFDataRef) nsdata); |
987 | | - CGImageRef imageRef = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); |
988 | | - CCTexture *texture = [[CCTexture alloc] initWithCGImage:imageRef contentScale:contentScale]; |
989 | | - CGDataProviderRelease(imgDataProvider); |
990 | | - CGImageRelease(imageRef); |
991 | | - |
992 | | - _FPSLabel = [[CCFPSLabel alloc] initWithString:@"00.0" texture:texture]; |
993 | | - _SPFLabel = [[CCFPSLabel alloc] initWithString:@"0.000" texture:texture]; |
994 | | - _drawsLabel = [[CCFPSLabel alloc] initWithString:@"000" texture:texture]; |
995 | | - |
996 | | - [CCTexture setDefaultAlphaPixelFormat:currentFormat]; |
997 | | - |
998 | | - CGPoint offset = [self convertToGL:ccp(0, (self.flipY == 1.0) ? 0 : self.view.bounds.size.height)]; |
999 | | - CGPoint pos = ccpAdd(CC_DIRECTOR_STATS_POSITION, offset); |
1000 | | - [_drawsLabel setPosition: ccpAdd( ccp(0,34), pos ) ]; |
1001 | | - [_SPFLabel setPosition: ccpAdd( ccp(0,17), pos ) ]; |
1002 | | - [_FPSLabel setPosition: pos ]; |
| 965 | + if( _FPSLabel && _SPFLabel ) { |
| 966 | + _FPSLabel = nil; |
| 967 | + _SPFLabel = nil; |
| 968 | + _drawsLabel = nil; |
| 969 | + |
| 970 | + [[CCFileUtils sharedFileUtils] purgeCachedEntries]; |
| 971 | + } |
| 972 | + |
| 973 | + CCTexturePixelFormat currentFormat = [CCTexture defaultAlphaPixelFormat]; |
| 974 | + [CCTexture setDefaultAlphaPixelFormat:CCTexturePixelFormat_RGBA4444]; |
| 975 | + |
| 976 | + unsigned char *data; |
| 977 | + NSUInteger data_len; |
| 978 | + CGFloat contentScale = 0; |
| 979 | + [self getFPSImageData:&data length:&data_len contentScale:&contentScale]; |
| 980 | + |
| 981 | + NSData *nsdata = [NSData dataWithBytes:data length:data_len]; |
| 982 | + CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData( (__bridge CFDataRef) nsdata); |
| 983 | + CGImageRef imageRef = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault); |
| 984 | + CCTexture *texture = [[CCTexture alloc] initWithCGImage:imageRef contentScale:contentScale]; |
| 985 | + CGDataProviderRelease(imgDataProvider); |
| 986 | + CGImageRelease(imageRef); |
| 987 | + |
| 988 | + _FPSLabel = [[CCFPSLabel alloc] initWithString:@"00.0" texture:texture]; |
| 989 | + _SPFLabel = [[CCFPSLabel alloc] initWithString:@"0.000" texture:texture]; |
| 990 | + _drawsLabel = [[CCFPSLabel alloc] initWithString:@"000" texture:texture]; |
| 991 | + |
| 992 | + [CCTexture setDefaultAlphaPixelFormat:currentFormat]; |
| 993 | + |
| 994 | + CGPoint offset = [self convertToGL:ccp(0, (self.flipY == 1.0) ? 0 : self.view.bounds.size.height)]; |
| 995 | + CGPoint pos = ccpAdd(CC_DIRECTOR_STATS_POSITION, offset); |
| 996 | + [_drawsLabel setPosition: ccpAdd( ccp(0,34), pos ) ]; |
| 997 | + [_SPFLabel setPosition: ccpAdd( ccp(0,17), pos ) ]; |
| 998 | + [_FPSLabel setPosition: pos ]; |
1003 | 999 | } |
1004 | 1000 |
|
1005 | 1001 | @end |
0 commit comments