Hi woonsung,
you need to make additions to the cocos2d library, since the relevant
part is in Texture2d.
like the following:
scaleWithStretch is to stretch the item, just withScale will crop it
off at the end.
set the curScale.x and curScale.y for the extension as needed.
hope this is useful for you.
Chris.
=============
Additions to Texture2d (cocos2d 0.71 I believe I started with)
=============
//@note: here to enable drawing things like progress bars.
- (void) drawAtPoint:(CGPoint)point withScale: (CGPoint) scale
{
GLfloat coordinates[] = { 0.0f, _maxT * scale.y,
_maxS * scale.x, _maxT * scale.y,
0.0f, 0.0f,
_maxS *scale.x, 0.0f };
GLfloat width = (GLfloat)_width * _maxS * scale.x,
height = (GLfloat)_height * _maxT * scale.y;
GLfloat vertices[] = {point.x, point.y, 0.0f,
width + point.x, point.y, 0.0f,
point.x, height + point.y, 0.0f,
width + point.x, height + point.y, 0.0f };
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
//@note: here to enable drawing things like stretchy bars.
- (void) drawAtPoint:(CGPoint)point withScaleStretch: (CGPoint) scale
{
GLfloat coordinates[] = { 0.0f, _maxT,
_maxS, _maxT,
0.0f, 0.0f,
_maxS, 0.0f };
GLfloat width = (GLfloat)_width * _maxS * scale.x,
height = (GLfloat)_height * _maxT * scale.y;
GLfloat vertices[] = { point.x, point.y, 0.0f,
width + point.x, point.y, 0.0f,
point.x, height + point.y, 0.0f,
width + point.x, height + point.y, 0.0f };
glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
=============
Then some sort of extension subclassing sprite: (this too isn't full code)
in the interface:
=============
typedef enum _scaleMode
{
scalemode_crop,
scalemode_stretch,
} ScaleMode;
@interface SpriteExt: Sprite
{
cpVect curScale;
ScaleMode scaleMode;
}
@property(readwrite, assign) cpVect curScale;
@property(readwrite, assign) ScaleMode scaleMode;
=============
and in the implementation:
=============
-(id) init
{
if (![super init])
return nil;
curScale.x = curScale.y = 1;
scaleMode = scalemode_crop;
return self;
}
-(void) draw
{
if (curScale.x == 1 && curScale.y == 1)
{
[super draw];
return;
}
glEnableClientState( GL_VERTEX_ARRAY);
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glEnable( GL_TEXTURE_2D);
glColor4ub( r, g, b, opacity);
if (scaleMode == scalemode_crop)
[texture drawAtPoint: CGPointZero withScale: curScale];
else
[texture drawAtPoint: CGPointZero withScaleStretch: curScale];
glColor4ub( 255, 255, 255, 255);
glDisable( GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}
On Sat, Jun 20, 2009 at 12:33 AM,
<cocos2d-iphone-discuss@gamesforfood.cl> wrote:
> woonsung says:
>
> Cut to the desired size of a sprite can draw?
>
> If you have an image of size 100 * 100
> However, and I want to size 80 * 100.
>
> Using images such as time bar.
> Do you understand?
>
> ;)
>
> Sprite *Box = [Sprite spriteWithFile:@"box.png"];
>
> [Box setPosition:ccp(160, 240)];
> [self addChild:Box z:1];
>
> Read the whole thread.