Create CCTexture2D from a CCSprite with primitives being drawn in draw method - cocos2d-iphone

I have a CCSprite subclass. In the draw method, I am drawing some cocos2d primitives like lines and such. How can I create a CCTexture2D of the sprite? I can't use sprite.texture because that doesn't include the primitives I am drawing.

You can add sprite to object of CCRenderTexture2D and after this can draw sprite to texture.
look at the example
CCSprite *spr = nil;//your sprite
CCRenderTexture* renderTexture = [CCRenderTexture renderTextureWithWidth:spr.contentSize.width height:spr.contentSize.height];
spr.anchorPoint = ccp(0, 0);
spr.position = ccp(0, 0);
[renderTexture addChild:spr];
[renderTexture begin];
[spr draw]; // or [spr visit];
[renderTexture end];
CCTexture2D *result = renderTexture.sprite.texture;
Now you will have texture, that contains sprite and primitives, that it draws in draw method.
Hope, it will help you:)

Related

SDL 2.0 sprite draws skewed (transformed) after OpenGL draws

I'm using SDL to create a window and draw OpenGL in it and after drawing OpenGL I use SDL to show sprites (UI). It worked for me on Windows, OSX and NDK but it doesn't work for me on iOS. This is how I draw the sprite:
I create the window:
int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
gWindow = SDL_CreateWindow("example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 400, 800, flags);
I create the renderer:
gRenderer = SDL_CreateRenderer(gWindow, id, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
I load the texture:
SDL_Texture* newTexture = NULL;
SDL_Surface* loadedSurface = SDL_LoadBMP(path.c_str());
newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
SDL_FreeSurface(loadedSurface);
That's where I do OpenGL drawing. I load .3ds models, load textures, use blending etc.
And then
I draw the sprite:
dstRect.x = 0.0f;
dstRect.y = 0.0f;
dstRect.w = 128.0f;
dstRect.h = 64.0f;
SDL_RenderCopy(gRenderer, newTexture, NULL , &dstRect);
SDL_RenderPresent(gRenderer);
the result is strange. The sprite shows skewed instead of being drawn in a rectangle.
result http://vvcap.net/db/zHhZwoZa1ng7caeP1BG3.png
What could be the reason of the sprite being transformed like that ? How can I fix this ?
Has anybody had a similar problem ?
It almost looks to me as if the camera transform is rotated slightly around the y-axis and the perspective projection is making it looked skewed.
If the sprite is meant to be draw into the screen-space make sure that you have an orthographic projection enabled before the draw, with the width and height being the size of the physical screen (ie. 480x800).
I didn't find a solution but I just used SDL_RenderCopyEx instead of SDL_RenderCopy and it worked.

Animate CCTexture2D using spritesheet

I have made spritesheet in Zwoptex , I know TexturePacker is better than this but i have just started with cocos2d iphone, so haven't purchase that.
I have made CCTexture2D using following code.

texture = [[CCTextureCache sharedTextureCache] addImage:#"1.png"];
self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture];
CC_NODE_DRAW_SETUP();
And i use this CCtexture2D object to draw texture around soft body.Using Following code.
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords);
ccGLBindTexture2D([texture name]);
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, textCoords);
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_TRUE, 0, triangleFanPos);
glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_SEGMENT+2);
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Color);
Now I want to animate texture of soft body. I know how to animate sprite using spritesheet. But now i am confuse how to make CCTexture2D using spritesheet and how can i animate texture using different images like we do in sprite animation?
Can anyone give me any direction in solving this issue?
First of all i want to say sorry for this question. I want to share my code here. I want to animate the texture which i am giving to the soft body.
I was confused , how can i give new image to the cctexture2d object and rotate that at the angle of previous textures angle. But I came to know that cocos2d iphone automatically handles that.
I animate my texture2d object by just giving it the image sequences with delay. Code for that is shown below.
Called Function
-(void)changeTexture1:(NSNumber*)i{
texture = [[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:#"blink%#.png",i]];
}
Calling function:
-(void)makeAnimation{
for (int i = 2; i<7; i++) {
[self performSelector:#selector(changeTexture1:) withObject:[NSNumber numberWithInt:i] afterDelay:0.1*i];
}
int j = 1;
for(int i=6;i>0;i--){
[self performSelector:#selector(changeTexture1:) withObject:[NSNumber numberWithInt:i] afterDelay:0.1*6 + 0.1*j++];
}
}

Making a hole on a CCRenderTexture

I am trying to make a hole on a CCRenderTexture with Cocos2D 2.0.
More specifically I have:
a CCSprite "stars" that shows some stars repeating a png image;
on top of that I have a CCRenderTexture "dark" that completely covers the "stars" sprite.
I want to be able to cut a hole on "dark" in order to show the stars below.
I am using CCRenderTexture (as suggested in some tutorials) but the hole I manage to make is never fully transparent, so the stars visible through the hole are partially obscured.
I really hope some one can show me the light, I spent over a week on this...
This is the code:
#interface MyBackgroundLayer : CCLayerGradient {
}
#end
#implementation MyBackgroundLayer
CCRenderTexture * dark;
CCSprite * stars;
-(id) init
{
if (self=[super init]) {
CGSize size = [[CCDirector sharedDirector] winSize];
// background
stars = [CCSprite spriteWithFile:#"stars.png" rect:CGRectMake(0, 0, size.width, size.height)];
stars.anchorPoint = ccp(0,0);
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[stars.texture setTexParameters:&params];
[self addChild:stars];
// dark layer to cover the background
dark = [CCRenderTexture renderTextureWithWidth:size.width height:size.height];
[dark clear:0 g:0 b:0 a:1.0f];
[self addChild: dark];
dark.position = ccp(size.width/2,size.height/2);
[[dark sprite] setBlendFunc: (ccBlendFunc) { GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA }];
}
return self;
}
-(void)draw
{
[super draw];
[dark beginWithClear:0 g:0 b:0 a:1];
glColorMask(0, 0, 0, 1);
// Here I am using 0.5 as alpha value, this could seems the reason why the hole is not fully transparent.
// However if I change the alpha value to 1.0f or 0.0f the hole becomes completely opaque
ccColor4F color = ccc4f(1.f, 1.f, 1.f, 0.5f);
ccDrawSolidRect(ccp(0,0), ccp(600, 600), color);
glColorMask(1,1,1,1);
[dark end];
}
#end
I think what you're looking for is something like this ( may need some editing).
As for your code ..i thing the problem is in the blend function. Check this out to see how they work.

Cocos2D - remove layer opacity on region

I got a Scene with a layer with the top z-index wich one adds a dark effect to the whole scene setting its opacity. What i am trying to do now is to remove the shadow/dark effect in a concrete region (inside a cone/triangle) as the image shows (inside the red polygon). In other words, i want to have the same "luminosity" (zero opacity) inside the triangle as on the left side of the screen.
code:
-(id) init
{
if( (self=[super initWithColor:ccc4(0,0,0,128)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLayerColor* layer1 = [CCLayerColor layerWithColor: ccc4(0, 0, 0, 180) width: winSize.width height: winSize.height];
layer1.position = ccp(50,0);
[self addChild: layer1 z:2];
CCSprite *background = [CCSprite spriteWithFile:#"background.png"];
background.position = ccp(background.contentSize.width/2, background.contentSize.height/2);
[self addChild:background];
CCSprite *player = [CCSprite spriteWithFile:#"Player.png"rect:CGRectMake(0, 0, 27, 40)];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
}
return self;
}
any idea on how to do this ?? maybe i should try to do it on another whay and not use a CCLayer to add the dark effect ??
Thanks in advance
The simplest way would be to use a CCSprite with a black image the size of the background. Then cut out the whole you need in an image program and draw that sprite over the background with lowered opacity.

How do I change a CCSprite's Dimensions after changing texture?

I have a piece of code in my iOS project that swaps the texture of a CCSprite via setTexture, like so:
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"Circle.png"]];
However, the dimensions of the CCSprite's texture before and after the swap are different, and as a result, the Circle.png texture is getting cropped; stuck at the size of original texture (as the circle is larger).
How can I adjust the size after swapping the Texture?
(Related, but not helpful in solving this)
Try this:
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:#"Circle.png"];
if (texture)
{
// Get the size of the new texture:
CGSize size = [texture contentSize];
[sprite setTexture:texture];
// use the size of the new texture:
[sprite setTextureRect:CGRectMake(0.0f, 0.0f, size.width,size.height)];
}
You can just recreate sprite with spriteWithFile constructor. the dimensions will be set automatically