图像未在cocos2d中以原始大小显示

时间:2013-02-01 11:48:37

标签: iphone xcode image cocos2d-iphone textures

    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,但它没有以完整/原始尺寸显示。我的应用程序中没有任何代码可以调整大小,缩放图像。我究竟做错了什么? //抽奖中的代码

   - (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

1 个答案:

答案 0 :(得分:2)

我通过修改ccConfig文件解决了这个问题。

以前CC_TEXTURE_NPOT_SUPPORT 0 //表示支持已停用

我只是启用了此支持(将值从0更改为1)&amp;现在图像以原始尺寸显示。

定义CC_TEXTURE_NPOT_SUPPORT 1