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

标签 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

最佳答案

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

以前的 CC_TEXTURE_NPOT_SUPPORT 0//表示支持已禁用

我只是启用了此支持(将值从 0 更改为 1)并且现在图像以其原始大小显示。

定义 CC_TEXTURE_NPOT_SUPPORT 1

关于iphone - 图像在 cocos2d 中未以其原始大小显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14645649/

相关文章:

iphone - UIPickerView:发送到实例的无法识别的选择器 - 仅在 ios 3.1.x 上

c# - 我怎样才能得到这个 iTextSharp 图像到 "float right"?

java - 图像分辨率 vs 屏幕分辨率 vs 相机分辨率

php - ffmpeg 将任何用户视频文件转换为在手机上运行 h264 mp4

iphone - 如何在 iPhone iOS 5.0 中禁用表情符号键盘

iphone - 绘制一个多边形并用背景图像填充它

objective-c - Interface Builder 将控制导出设置为 nil——为什么?

ios - 将类从 Storyboard附加到 TableView 时出现问题

c - SQLite 与 Ansi C 和 xCode

c++ - c++ 中的硬编码位图会产生倾斜的线条