secret
Last active

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#define GravityDirection -1 // -1 for down, 1 for up
#define ParticleWidth 2
#define ParticleHeight 2
 
 
// Import the interfaces
#import "HelloWorldLayer.h"
 
// Needed to obtain the Navigation Controller
#import "AppDelegate.h"
 
struct SandParticle
{
CGPoint Position;
 
BOOL CMTop;
BOOL CMBottom;
BOOL CMLeft;
BOOL CMRight;
};
 
 
// HelloWorldLayer implementation
@implementation HelloWorldLayer
{
struct SandParticle SandMatrix[5000];
NSMutableArray *DrawLines;
int CurrentLineIndex;
int SandCount;
bool StartDrop;
CGSize WindowSize;
CCLabelTTF *label;
Byte ScreenBuffer[960*640*4];
UIColor *ClearColor;
}
 
// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
 
-(id) init
{
self = [super init];
if (self)
{
WindowSize = [[CCDirector sharedDirector] winSize];
ClearColor = [[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:1];
[self setIsTouchEnabled:YES];
DrawLines = [[NSMutableArray alloc] init];
CCLayerColor *Demo = [CCLayerColor layerWithColor:ccc4(255, 0, 0, 255)];
[self setContentSize:CGSizeMake(WindowSize.width, WindowSize.height)];
[self addChild:Demo z:-10];
[self schedule:@selector(AddNewParticle:) interval:0.03];
[self schedule:@selector(UpdateParticles:) interval:0.03];
label = [CCLabelTTF labelWithString:@"Pixeldrop" fontName:@"Game Over" fontSize:150];
[label setAnchorPoint:CGPointZero];
[label setPosition:CGPointMake(WindowSize.width/2 - label.boundingBox.size.width/2, WindowSize.height - label.boundingBox.size.height)];
[label setColor:ccc3(0, 255, 0)];
[self addChild:label];
}
return self;
}
 
#define MaxParticles 2000
 
- (void) AddNewParticle: (ccTime) dt
{
if (SandCount < MaxParticles && StartDrop)
{
struct SandParticle NewParticle;
NewParticle.Position = CGPointMake(WindowSize.width/2, WindowSize.height- label.boundingBox.size.height + 10);
NewParticle.CMBottom = YES;
SandMatrix[SandCount] = NewParticle;
SandCount++;
}
glReadPixels(0,0,WindowSize.width,WindowSize.height,GL_RGBA,GL_UNSIGNED_BYTE,&ScreenBuffer);
}
 
 
 
- (BOOL) GetColorAtPoint: (int) GetX : (int)GetY
{
int YSize = (int)WindowSize.width*4;
Byte R = ScreenBuffer[(YSize*GetY) + 0 + (4 * GetX)];
Byte G = ScreenBuffer[(YSize*GetY) + 1 + (4 * GetX)];
Byte B = ScreenBuffer[(YSize*GetY) + 2 + (4 * GetX)];
Byte A = ScreenBuffer[(YSize*GetY) + 3 + (4 * GetX)];
return (R==255 && G == 0 && B == 0 && A == 255);
}
 
- (void) UpdateParticles: (ccTime) dt
{
for (int CPLoop = 0; CPLoop <= SandCount; CPLoop++)
{
struct SandParticle CurrentParticle = SandMatrix[CPLoop];
if (CurrentParticle.Position.y < ParticleHeight + 1) continue;
CurrentParticle.CMBottom = [self GetColorAtPoint:CurrentParticle.Position.x:CurrentParticle.Position.y-1];
CurrentParticle.CMLeft = [self GetColorAtPoint:CurrentParticle.Position.x-ParticleWidth*1.5:CurrentParticle.Position.y-1];
CurrentParticle.CMRight = [self GetColorAtPoint:CurrentParticle.Position.x+ParticleWidth*1.5:CurrentParticle.Position.y-1];
int Randomized = arc4random_uniform(2);
if (CurrentParticle.CMBottom)
{
CurrentParticle.Position.y -= ParticleHeight;
if (Randomized == 1)
CurrentParticle.Position.x += 1;
else
CurrentParticle.Position.x -= 1;
}
else if (CurrentParticle.CMLeft && CurrentParticle.CMRight)
{
CurrentParticle.Position.y -=ParticleHeight;
if (Randomized == 1)
CurrentParticle.Position.x += 1;
else
CurrentParticle.Position.x -= 1;
}
else if (CurrentParticle.CMLeft)
{
CurrentParticle.Position.x -=ParticleWidth;
CurrentParticle.Position.y -=ParticleHeight;
}
else if (CurrentParticle.CMRight)
{
CurrentParticle.Position.x +=ParticleWidth;
CurrentParticle.Position.y -=ParticleHeight;
}
SandMatrix[CPLoop] = CurrentParticle;
}
}
 
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *TouchArray = [[NSMutableArray alloc] init];
[DrawLines addObject:TouchArray];
StartDrop = YES;
}
 
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *Touch = [touches anyObject];
CGPoint CurrentTouchLocation = [Touch locationInView:[Touch view]];
CGPoint PreviousTouchLocation = [Touch previousLocationInView:[Touch view]];
NSMutableArray *NewTouch = [[NSMutableArray alloc] init];
CurrentTouchLocation = [[CCDirector sharedDirector] convertToGL:CurrentTouchLocation];
PreviousTouchLocation= [[CCDirector sharedDirector] convertToGL:PreviousTouchLocation];
[NewTouch addObject:NSStringFromCGPoint(CurrentTouchLocation)];
[NewTouch addObject:NSStringFromCGPoint(PreviousTouchLocation)];
NSMutableArray *CurrentLine = [DrawLines objectAtIndex:CurrentLineIndex];
[CurrentLine addObject:NewTouch];
[DrawLines replaceObjectAtIndex:CurrentLineIndex withObject:CurrentLine];
}
 
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
CurrentLineIndex++;
}
 
- (void) draw
{
[super draw];
for (int CPLoop = 0; CPLoop <= SandCount; CPLoop++)
{
struct SandParticle CurrentParticle = SandMatrix[CPLoop];
if (CurrentParticle.Position.x > 0 && CurrentParticle.Position.y > 0)
ccDrawSolidRect(CGPointMake(CurrentParticle.Position.x, CurrentParticle.Position.y), CGPointMake(CurrentParticle.Position.x+ParticleWidth, CurrentParticle.Position.y+ParticleHeight), ccc4f(255, 255, 255, 255));
else break;
}
for (NSArray *Line in DrawLines)
{
for (NSArray *Point in Line)
{
CGPoint CurrentTouchLocation = CGPointFromString([Point objectAtIndex:0]);
CGPoint PreviousTouchLocation = CGPointFromString([Point objectAtIndex:1]);
glLineWidth(5.0f);
ccDrawLine(PreviousTouchLocation, CurrentTouchLocation);
 
}
}
}
 
 
@end
  • (BOOL) GetColorAtPoint: (int) GetX : (int)GetY
    {
    //int YSize = (int)WindowSize.width*4;
    //int i = (YSize*GetY) + (4 * GetX);
    int i = (WindowSize.width*GetY + GetX)*4;
    if( i < 0 || i > bufferSize ) return NO;

    Byte R = ScreenBuffer[i];
    Byte G = ScreenBuffer[++i];
    Byte B = ScreenBuffer[++i];
    Byte A = ScreenBuffer[++i];

    return (R==255 && G == 0 && B == 0 && A == 255);
    }

Если затык действительно в GetColorAtPoint, то можно попробовать или перевести GetColorAtPoint на C-функцию, или хотя бы объединить 3 вызова в один: адрес пикселя CMLeft вычисляется умножением, а две CMBottom и CMRight сложением.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.