2013-02-01 3 views
2
originalImage = [UIImage imageNamed:@"IMG_1968.PNG"]; 
     NSLog(@"originalImage Height :: %f", originalImage.size.height); 
     NSLog(@"originalImage Width :: %f", originalImage.size.width); 
    texture2D = [[CCTexture2D alloc] initWithImage:originalImage]; 

이미지로 텍스처를 만들었지 만 이미지 크기가 320 * 480이지만 전체/원래 크기로 표시되지 않습니다. 이미지 크기를 조정하고 크기를 조정할 수있는 코드가 앱에 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 무승부이미지가 cocos2d의 원래 크기로 표시되지 않습니다.

- (id)init { 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) {  
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate: 
    self priority:0 swallowsTouches:YES]; 
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888]; 
    originalImage = [UIImage imageNamed:@"IMG_1968.PNG"]; 

    texture2D = [[CCTexture2D alloc] initWithImage:originalImage]; 

    //  CCSprite *sp = [CCSprite spriteWithTexture:texture2D]; 
    //   [sp setAnchorPoint:ccp(0.0,0.0)]; 
    //  [self addChild:sp z:2]; 

    [self body_init]; 
    [self scheduleUpdateWithPriority:-1]; 
    self.isTouchEnabled = YES; 
} 
return self; 
    } 

    - (void)draw { 
    glDisableClientState(GL_COLOR_ARRAY); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
    glColor4ub(224,224,244,200); 

    [self body_redraw]; 

    glBlendFunc(CC_BLEND_SRC, CC_BLEND_DST); 
    glEnableClientState(GL_COLOR_ARRAY); 

} 

- (void)update:(ccTime)deltaTime { 
[self body_dynamics:mousex:mousey]; 

} 
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

isFirstTouch=YES; 
CGPoint location = [self convertTouchToNodeSpace: touch]; 
mousex = location.x; 
mousey = location.y; 
firstPoint=location; 

NSLog(@"TouchBegan -> mousex:%f mousey:%f", mousex, mousey); 

grab = [self body_grab:location.x:location.y]; 

NSLog(@"GRAB %d",grab); 

return YES; 
} 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { 
CGPoint location = [self convertTouchToNodeSpace: touch]; 
isFirstTouch=NO; 
int gridSizeX2 = 10; 

if ((location.x < firstPoint.x + gridSizeX2 && location.y < firstPoint.y + gridSizeX2) 
    && (location.x > firstPoint.x - gridSizeX2 && location.y > firstPoint.y - gridSizeX2)){ 

      mousex = location.x; 
      mousey = location.y; 

    NSLog(@"TouchMoved -> mousex:%f mousey:%f", mousex, mousey); 
} 
} 

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 
grab = -1; 


[self grabTextureToImage]; 
} 
-(void) grabTextureToImage 
{ 
NSString *filepath = [[NSHomeDirectory() stringByAppendingPathComponent: 
@"Documents"] stringByAppendingPathComponent:@"IMG_1968.PNG"]; 
//Make sure your contentSize is not zero before doing this... 
CCRenderTexture *rTexture =[CCRenderTexture renderTextureWithWidth: 
texture2D.contentSize.width height:texture2D.contentSize.height]; 
[rTexture begin]; 
//[rTexture visit]; 
[self visit]; 
[rTexture end]; 

[rTexture saveBuffer:filepath format:kCCImageFormatPNG]; 

NSLog(@"Image Saved to Path %@",filepath); 
} 
- (void)body_dynamics:(int)x:(int)y { 

if (grab != -1 && !mass[grab].nail &&!isFirstTouch) 
{ 
    mass[grab].x[0] = x; 
    mass[grab].x[1] = y; 
    mass[grab].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0; 
} 
} 

- (int)body_grab:(int)x:(int)y { 

float dx[2]; 
float d; 
float min_d; 
float min_i; 
int i; 

for (i = 0; i < GRID_SIZE_X*GRID_SIZE_Y; i++) 
{ 
    dx[0] = mass[i].x[0] - x; 
    dx[1] = mass[i].x[1] - y; 
    d = sqrt(dx[0]*dx[0] + dx[1]*dx[1]); 
    if (i == 0 || d < min_d) 
    { 
     min_i = i; 
     min_d = d; 
    } 
} 

return min_i; 
} 

- (void)body_redraw { 
int k; 
int i, j; 
if(mass == NULL) { 
    NSLog(@"mass is null"); 
    return; 
} 
glBindTexture(GL_TEXTURE_2D, [texture2D name]); 

    k = 0; 
    for (i = 0; i < GRID_SIZE_X - 1; i++) 
    { 
    for (j = 0; j < GRID_SIZE_Y - 1; j++) 
    { 
     GLfloat vertices[]= { 
      mass[k].x[0],mass[k].x[1],mass[k].x[2], 
      mass[k + 1].x[0],mass[k + 1].x[1],mass[k + 1].x[2], 
      mass[k + GRID_SIZE_Y + 1].x[0],mass[k + GRID_SIZE_Y + 1].x[1],mass[k + GRID_SIZE_Y 
+ 1].x[2], 
      mass[k + GRID_SIZE_Y].x[0],mass[k + GRID_SIZE_Y].x[1],mass[k + GRID_SIZE_Y].x[2] 
     }; 
     GLfloat tex[]={ 
      mass[k].t[0], mass[k].t[1], 
      mass[k + 1].t[0], mass[k + 1].t[1], 
      mass[k + GRID_SIZE_Y + 1].t[0], mass[k + GRID_SIZE_Y + 1].t[1], 
      mass[k + GRID_SIZE_Y].t[0], mass[k + GRID_SIZE_Y].t[1] 
     }; 

     glVertexPointer(3, GL_FLOAT, 0, vertices); 
     glTexCoordPointer(2, GL_FLOAT, 0, tex); 

     glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 

     k++; 
    } 
    k++; 
} 
} 


- (void)body_init { 

    GLint width = texture2D.contentSizeInPixels.width; 
    GLint height = texture2D.contentSizeInPixels.height; 
    int i, j; 
    int k; 

    if (mass == NULL) 
    { 
    mass = (MASS *) malloc(sizeof(MASS)*GRID_SIZE_X*GRID_SIZE_Y); 
    if (mass == NULL) 
    { 
     fprintf(stderr, "body: Can't allocate memory.\n"); 
     exit(-1); 
    } 
    } 

    k = 0; 
    for (i = 0; i < GRID_SIZE_X; i++) 
    for (j = 0; j < GRID_SIZE_Y; j++) 
    { 
     //this code implements grid on texture2D, gets vertex & side vertex in array 
     mass[k].nail = (i == 0 || j == 0 || i == GRID_SIZE_X - 1 
         || j == GRID_SIZE_Y - 1);//value is 0/1 

     mass[k].x[0] = i/(GRID_SIZE_X - 1.0)*width; 
     mass[k].x[1] = j/(GRID_SIZE_Y - 1.0)*height; 
     mass[k].x[2] = -(CLIP_FAR - CLIP_NEAR)/4.0; 

     mass[k].v[0] = 0.0; 
     mass[k].v[1] = 0.0; 
     mass[k].v[2] = 0.0; 

     mass[k].t[0] = i/(GRID_SIZE_X - 1.0); 
     mass[k].t[1] = j/(GRID_SIZE_Y - 1.0); 

     k++; 
    } 
} 

- (void)dealloc { 
// in case you have something to dealloc, do it in this method 
// in this particular example nothing needs to be released. 
// cocos2d will automatically release all the children (Label) 

    [originalImage release]; 

[self unscheduleUpdate]; 
[texture2D release]; 
// don't forget to call "super dealloc" 
    [super dealloc]; 
} 

@end 
+0

스프라이트 생성 코드 표시. 망막 디스플레이가 활성화 되었습니까? – Guru

+0

어떤 스프라이트도 생성하지 않습니다 –

+0

망막 디스플레이 지원이 활성화되었습니다 –

답변

2

에서 // 코드는 내가 ccConfig 파일을 수정하여이 문제를 해결했다.

이전 CC_TEXTURE_NPOT_SUPPORT // 0을 지원 단순히 & 이제 이미지`의 원래 크기로 표시하고,이 지지체 (0에서 1로 변화 값)을 활성화

비활성화 된 것을 의미한다.

CC_TEXTURE_NPOT_SUPPORT 1

관련 문제