天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Texture2D :

用图象或文字创建OpenGL 2D 的Texture

 

Label的定义:

Label *left = [Label labelWithString:@"alignment left" dimensions:CGSizeMake(480,50) alignment:UITextAlignmentLeft fontName:@"Marker Felt" fontSize:32];

Label labelWithString:label显示的文字即title部分

dimensions:CGSizeMake:定义label在的尺寸即宽和高

alignment:UITextAlignmentLeft:显示的位置

fontName:字体  fontSize:字号

left.position = ccp(240,200);  (ccp:即对象在屏幕中的位置点(x,y))

 

[sprite.texture setAntiAliasTexParameters];

使用该语句可以使图象在放大后仍然很清晰,不用该语句图像放大放会变得很模糊

 

标签的设定与获取

 [self addChild:label z:0 tag:kTagLabel]; //设定标签的tag,方便后面取出

 Label *label = (Label*) [self getChildByTag:kTagLabel];   //获取标签的值
 [label setColor:ccc3(16,16,255)]; //设定字体的颜色

 

例子:

#pragma mark TexturePixelFormat
@implementation TexturePixelFormat
-(void) onEnter
{
    //
    // This example displays 1 png images 4 times.
    // Each time the image is generated using:
    // 1- 32-bit RGBA8
    // 2- 16-bit RGBA4
    // 3- 16-bit RGB5A1
    // 4- 16-bit RGB565
    [super onEnter];
   
    Label *label = (Label*) [self getChildByTag:kTagLabel];  //获取已定义的标签
    [label setColor:ccc3(16,16,255)];  //设定标签字体的颜色
   
    CGSize s = [[Director sharedDirector] winSize];
   
    Sprite *background = [Sprite spriteWithFile:@"background1.jpg"];
    background.position = ccp(240,160);  //根据480*320 所以该为置是从中心开始的
    [self addChild:background z:-1];
   
    // RGBA 8888 image (32-bit)   //设定为32为图像
    [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];

    Sprite *sprite1 = [Sprite spriteWithFile:@"test-rgba1.png"];
    sprite1.position = ccp(64, s.height/2);
    [self addChild:sprite1 z:0];
   
    // remove texture from texture manager     //从材质管理器中移除某灵的材质
    [[TextureMgr sharedTextureMgr] removeTexture:sprite1.texture];

    // RGBA 4444 image (16-bit) 
 //设定为16为图像
    [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA4444];
    Sprite *sprite2 = [Sprite spriteWithFile:@"test-rgba1.png"];
    sprite2.position = ccp(64+128, s.height/2);  //由于图像的宽度为128,所以正好在第一张图像的结束处开始的
    [self addChild:sprite2 z:0];

    // remove texture from texture manager   
    [[TextureMgr sharedTextureMgr] removeTexture:sprite2.texture];

    // RGB5A1 image (16-bit)   //可以对比对图像颜色变深了
    [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGB5A1];
    Sprite *sprite3 = [Sprite spriteWithFile:@"test-rgba1.png"];
    sprite3.position = ccp(64+128*2, s.height/2);
    [self addChild:sprite3 z:0];

    // remove texture from texture manager   
    [[TextureMgr sharedTextureMgr] removeTexture:sprite3.texture];

    // RGB565 image (16-bit)
    [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGB565];
    Sprite *sprite4 = [Sprite spriteWithFile:@"test-rgba1.png"];
    sprite4.position = ccp(64+128*3, s.height/2);
    [self addChild:sprite4 z:0];

    // remove texture from texture manager   
    [[TextureMgr sharedTextureMgr] removeTexture:sprite4.texture];

   
    id fadeout = [FadeOut actionWithDuration:2];  //2秒钟消失
    id fadein = [FadeIn actionWithDuration:2];
    id seq = [Sequence actions: [DelayTime actionWithDuration:2], fadeout, fadein, nil];
    id seq_4ever = [RepeatForever actionWithAction:seq];
   
    [sprite1 runAction:seq_4ever];
    [sprite2 runAction: [[seq_4ever copy] autorelease]];
    [sprite3 runAction: [[seq_4ever copy] autorelease]];
    [sprite4 runAction: [[seq_4ever copy] autorelease]];

    // restore default
    [Texture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_Default];
}

 

 例子:TextureBlend

参考:http://blog.csdn.net/alicehyxx/archive/2009/06/12/4263058.aspx

Blend 混合是将源色和目标色以某种方式混合生成特效的技术。混合常用来绘制透明或半透明的物体。在混合中起关键作用的α值实际上是将源色和目标色按给定比率进行 混合,以达到不同程度的透明。α值为0则完全透明,α值为1则完全不透明。混合操作只能在RGBA模式下进行,颜色索引模式下无法指定α值。物体的绘制顺 序会影响到OpenGL的混合处理。

 

 cloud = [Sprite spriteWithFile:@"test_blend.bmp"];
 [self addChild:cloud z:i+1 tag:200+i];
 cloud.position = ccp(50+25*i, 320-80);
 cloud.blendFunc = (ccBlendFunc) { GL_SRC_ALPHA, GL_ONE };  // additive blending

 

例子:TextureAsync

#pragma mark TextureAsync
@implementation TextureAsync
-(id) init
{
    if( (self=[super init]) ) {
       
        imageOffset = 0;
   
        CGSize size =[[Director sharedDirector] winSize];

        Label *label = [Label labelWithString:@"Loading..." fontName:@"Marker Felt" fontSize:32];
        label.position = ccp( size.width/2, size.height/2);
        [self addChild:label z:10];
       
        id scale = [ScaleBy actionWithDuration:0.3f scale:2];
        id scale_back = [scale reverse];
        id seq = [Sequence actions: scale, scale_back, nil];
        [label runAction: [RepeatForever actionWithAction:seq]];
       
        [self schedule:@selector(loadImages:) interval:1.0f]; 

//schedule:在一定的时间里去回调某个函数。interval:间隔的时间,要创建一个'慢动作'的效果,使用的值低于1.0。要创//建一个'快fordward'的效果,使用高于1.0价值。

 

//如果使用schedule,将会拥有以下功能

//           Ø  自动暂停/恢复。
//           Ø  当层(Scene, Sprite, CocosNode)进入计时器便会自动启动阶段,在离开时,它会自动停用阶段。
//           Ø  你的target/selecto将在一个延迟时间内自动调用

       
    }
    return self;
}

- (void) dealloc
{
    [[TextureMgr sharedTextureMgr] removeAllTextures];
    [super dealloc];
}

//回调函数
-(void) loadImages:(ccTime) dt
{
    [self unschedule:_cmd];

    for( int i=0;i < 8;i++) {
        for( int j=0;j < 8; j++) {
            NSString *sprite = [NSString stringWithFormat:@"sprite-%d-%d.png", i, j];
            [[TextureMgr sharedTextureMgr] addImageAsync:sprite target:self selector:@selector(imageLoaded:)];
        }
    }   
    [[TextureMgr sharedTextureMgr] addImageAsync:@"background1.jpg" target:self selector:@selector(imageLoaded:)];
    [[TextureMgr sharedTextureMgr] addImageAsync:@"background2.jpg" target:self selector:@selector(imageLoaded:)];
    [[TextureMgr sharedTextureMgr] addImageAsync:@"background.png" target:self selector:@selector(imageLoaded:)];
    [[TextureMgr sharedTextureMgr] addImageAsync:@"atlastest.png" target:self selector:@selector(imageLoaded:)];
    [[TextureMgr sharedTextureMgr] addImageAsync:@"grossini_dance_atlas.png" target:self selector:@selector(imageLoaded:)];
}

-(void) imageLoaded: (Texture2D*) tex
{
    // IMPORTANT: The order on the callback is not guaranteed. Don't depend on the callback

   //重要:关于回调顺序是不能保证。不依赖于回调

   // 通过传输过来的当前 (Texture2D*) tex,通spriteWithTexture定义精灵,并改变精灵的属性最后显示出来
    // This test just creates a sprite based on the Texture
  
    Sprite *sprite = [Sprite spriteWithTexture:tex];
    sprite.anchorPoint = ccp(0,0);   //使精灵在屏幕的左下角显示

//anchorPoint:较正座标的定位点:(0,0)指左下角,(1,1)指右上角,(0.5,0.5)指中心。精灵和其他textured Nodes 有一个//anchorPoint缺省值(0.5f,0.5f默认anchorPoint)
    [self addChild:sprite z:-1];
    CGSize size =[[Director sharedDirector] winSize];
    int i = imageOffset * 32;
    sprite.position = ccp( i % (int)size.width, (i / (int)size.width) * 32 ); 
    imageOffset++;
}

-(NSString *) title
{
    return @"Texture Async Load";
}
@end

posted on 2010-08-20 15:25  老舟  阅读(2021)  评论(0编辑  收藏  举报