Cocos2d - CCSprite image texture gets blurred - iphone

i am using the latest cocos2d to create a game project,i have added the images having respective retina and normal images.But when ever i add a ccsprite with its initialize method ,initWithFileName:(NSString*)fileName ,the image texture gets blurred while running ,in retina and non retina devices

For all texture OR only for some texture? If it is for all texture then change pixelFormat.
In AppController, set backbuffer pixelFormat to kEAGLColorFormatRGBA8.
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGBA8 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];

Please try this on your sprite:
[myccsprite setAliasTexParameters];
It will tell the sprite not to anti-alias.

In AppController,i set backbuffer pixelFormat to kEAGLColorFormatRGBA8.This really worked
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGBA8 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];

Related

How to scale Cocos2d app to iPhone 5 screen?

I have a Cocos3d v1.01 app and I am currently making it try to fit the iPhone 5 full screen.
By simply stretching the background is enough for most screens as it is just a pattern that fills the screen and no need to change any UI parts, however on just one of the screens I am struggling to change.
This is the code part I believe to set the background for that;
-(void)animations
{
AppDelegate *app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
CGSize size = [[CCDirector sharedDirector] winSize];
NSMutableArray *bodyanimframeBuddyBack =[[NSMutableArray alloc]init];
for(int j = 1;j<=4;++j)
{
[bodyanimframeBuddyBack addObject:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:[NSString stringWithFormat:#"shark_sea_waves5%d.png",j]]];
}
CCAnimation *BuddyAnimBack = [CCAnimation animationWithFrames:bodyanimframeBuddyBack delay:0.3f];
self.backgroundAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:BuddyAnimBack restoreOriginalFrame:NO]];
CCSprite *bg=[CCSprite spriteWithSpriteFrameName:#"shark_sea_waves51.png"];
bg.position=ccp(size.width/2, size.height/2);
[self addChild:bg];
[bg runAction:backgroundAction];
Is there a way of adjusting that ? I am guessing it is the bg.position part that would need changing to tell it whether it is iPhone5 or not?
Thanks in advance,
Chris
bg.scaleX = size.width / bg.contentSize.width;
This will stretch the bg width to match the screen width.
Why can't you place seperate background for iPhone5..just like you did for iPad.
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define TEX_MM_BG (IS_IPHONE5) ? ( #"shark_sea_waves51-whd.png") : ( #"shark_sea_waves51.png")
-(void)setupBackground
{
CCSprite *bg = [CCSprite spriteWithFile:TEX_MM_BG];
bg.position = ccp(mS.width*0.5f, mS.height*0.5f);
[self addChild:bg z:-3 tag:kTagBackground];
}
//Also put these image in hard disk.
shark_sea_waves51.png //480x320
shark_sea_waves51-hd.png //960x640
shark_sea_waves51-whd.png //1136x640
shark_sea_waves51-ipad.png //1024x768
shark_sea_waves51-ipadhd.png //2048x1536

Cocos2d low image quality

I am now developing a game for iOS .I create a sprite sheet with Texturepacker which produces 2 file .plist and .png file . When I use these files in my code I found that the images of the all sprites have low quality and the color are pale very pale and I think this is because texturepacker program .
Please I want your advise what shall I do to overcome this problem .
I guess u didn't change back buffer pixel formate. U can try this.
Used pixel formate kEAGLColorFormatRGBA8 instead of kEAGLColorFormatRGB565.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create the main window
window_ = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
CCGLView *glView = [CCGLView viewWithFrame:[window_ bounds]
pixelFormat:kEAGLColorFormatRGBA8 //Guru - replaced kEAGLColorFormatRGBA8 / kEAGLColorFormatRGB565
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];

CCShaky3D turns the background black

I'm trying to make my sprite have a shake effect. However, while the sprite does shake, the entire background turns black. Can anybody help me with this?
Here's the code that I've written to add the sprite to my layer along with the action that I run right after.
CCSprite * picture = [CCSprite spriteWithFile:#"picture.png"];
picture.position = ccp(winsize.width/4,
picture.contentSize.height * 0.8);
[self addChild:picture];
CCShaky3D * shake = [CCShaky3D actionWithRange:4
shakeZ:NO
grid:ccg(12, 12)
duration:0.5];
[picture runAction:shake];
Can anybody help me?
Have you enabled depth buffering of the EAGLView? Most 3D actions require depth buffering (GL_DEPTH_COMPONENT16_OES or GL_DEPTH_COMPONENT24_OES) to avoid visual artifacts. You may also have to use a 32-Bit frame buffer with alpha channels by using the kEAGLColorFormatRGBA8 instead of kEAGLColorFormatRGB565.
EAGLView is initialized in the app delegate class:
EAGLView* glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:0
numberOfSamples:0];

Changing z-order of draw method in Cocos2d?

Right now I am trying to draw a polygon in Cocos2d, but I need it over a background, how do I change draw's z-index and make it top priority so that way I can see the line instead of it being covered by the background?
Here is the draw method:
I decided to just make it a line instead of polygon, and adjust the width of it but otherwise it is the same...
-(void) draw {
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glLineWidth(5.0f);
ccDrawLine(healthBar[0], healthBar[1]);
}
Use reorderChild function of CCNode. And put your rectangle over background
-(void) reorderChild:(CCNode *) child z:(int) zOrder
In your app delegate, you need to modify your GLView so that it has depth enabled. Then, you should be able to modify the depth of your draw call. In your app delegate, look for something like this:
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
];
Change the 0 to either GL_DEPTH_COMPONENT16_OES or GL_DEPTH_COMPONENT24_OES.
Edit:
I'm not familiar with OpenGL, but perhaps this page will help you.

Is it possible to get a 16-bit depth buffer on the iPhone?

We're making a game using cocos2d but are having problems with the depth buffer.
I'm trying to setup a 16-bit depth buffer on the iPhone but so far I only get 24-bit depth.
The reason I want 16-bit depth buffer is:
A: I don't need the precision of 24-bit.
B: I'm hoping it will be faster.
This is how I set the depth format in cocos2d:
[[CCDirector sharedDirector] setDepthBufferFormat:kDepthBuffer16];
Which at some point ends up in EAGLView (v1.3)
glGenRenderbuffersOES(1, &_depthBuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _depthBuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, _depthFormat, newSize.width, newSize.height);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, _depthBuffer);
Where _depthFormat is GL_DEPTH_COMPONENT16_OES (verified while debugging)
And here's how I check the number of bits:
GLint depthBufferBits;
glGetIntegerv( GL_DEPTH_BITS, &depthBufferBits );
NSLog( #"Depth buffer bits: %d", depthBufferBits );
And the output is:
Depth buffer bits: 24
What am I missing? I've tried the same code on iPod Touch (2nd gen) and iPhone 3GS, always comes back as 24 bit.
Update:
I've now updated cocos2d to the latest version from the GIT repo.
Here's how I initialize the director and window:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// Init the window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
// cocos2d will inherit these values
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
[CCDirector setDirectorType:CCDirectorTypeDisplayLink];
CCDirector* director = [CCDirector sharedDirector];
[director setAnimationInterval:1.0/60];
[director setDisplayFPS:YES];
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8 // RGBA8 color buffer
depthFormat:GL_DEPTH_COMPONENT16_OES // 16-bit depth buffer
preserveBackbuffer:NO];
[director setOpenGLView:glView];
[director setProjection:kCCDirectorProjection2D];
[window addSubview:glView];
[window makeKeyAndVisible];
[CCTexture2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888];
[director runWithScene:[LevelScene scene]];
}
Still getting a 24-bit depth buffer according to glGetIntegerv( GL_DEPTH_BITS, &depthBufferBits );
I'm not sure about the internals but setDepthBufferFormat is deprecated. I can also imagine that it's simply (no longer) possible to change the depth buffer after the GL view has been created. With Cocos2D v0.99.5 beta 3 the initialization has changed quite a bit. I suggest you update to the latest version, even though it's beta.
Then take a look here: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:setup_buffers
The important part is the initialization of the EAGLView, where you can set the depth buffer:
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:GL_DEPTH_COMPONENT16_OES
preserveBackbuffer:NO];