태그 : Cocos2d

아이폰 개발 팁 - cocos2d의 CCSpriteFrameCache 에서 plist 제거하기

cocos2d에서 텍스쳐를 효율적으로 사용하기 위해서는 

CCSpriteFrameCache를 이용해서 plist로 관리를 하게 됩니다.


그런데, 더 이상 사용하지 않을 plist는 제거를 해두면 되는데..

명시적으로 plist를 제거했음에도 깔끔하게 제거가 안되고 메모리를 계속 차지하고 있더군요.

그래서, 삽질을 좀 하다가 코드를 보니..


CCSpriteFrameCache에 추가할때 CCTextureCache에 texture를 등록하는데 제거할 때는 그냥 두더군요.

아마 다른데서 계속 사용하라는 배려인거 같은데..

plist 통째로(모든 스프라이트 포함이겠죠) 명확히 필요가 없는 시점에서는 

아래와 같이 두 cache에서 모두 제거를 해야 메모리가 정확히 해제가 됩니다.


// plist추가!

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"test.plist"];



// plist 완벽 제거!

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:@"test.plist"];

[[CCTextureCache sharedTextureCache] removeTextureForKey:@"test.png"];


removeUnused...시리즈의 함수랑은 의미가 좀 다릅니다.

Run with Performance Tool - Allocations로 꼭 메모리 사용량 확인해 주는 센스~



by 쉬귀 | 2010/11/08 23:02 | 개발 | 트랙백 | 덧글(2)

아이폰 개발 팁 - cocos2d 로딩화면 쓰레드 로딩하기

쓰레드를 이용해서 자연스러운 로딩화면을 보여주면서 

백그라운드로 로딩하는데는 여러가지 방법이 있겠지만,

cocos2d 에서 이용할 수 있는 간단한 방법 중엔 이런게 있네요.

원문 링크 중의 어느 댓글을 참고해서 테스트를 해보면..



- (void) applicationDidFinishLaunching:(UIApplication*)application

{

.....


// app뜨면 바로 로딩씬을 불러와서 실행~

loadingScene = [[LoadingScene alloc] init];

[[CCDirector sharedDirector] runWithScene: loadingScene];


// 쓰레드 생성해서 로딩하기~

NSThread* thread = [[[NSThread alloc] initWithTarget:self selector:@selector(loadingProc) object:nil] autorelease];

[thread start];

}


- (void)loadingSceneProc {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


// 이게 핵심 코드 없으면 텍스쳐 다 깨지고 난리납니다;

// 뭐하자는 얘기인지 감만 올 뿐이죠 =_=;;

EAGLContext *k_context = [[[EAGLContext alloc]

   initWithAPI :kEAGLRenderingAPIOpenGLES1

   sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]] autorelease];

[EAGLContext setCurrentContext:k_context];

// 여기서 필요한 것들을 백그라운드로 로딩!

// 실제로 사용할 MainScene에 로딩할게 잔뜩있다고 치고.. 생성해서 로딩~

mainScene = [[MainScene alloc] init];


// 이게 혹시(!) thread-safe하지 않으면 다른 방식으로 notification 처리 필요~

[[CCDirector sharedDirector] replaceScene:mainScene];

[pool release];

}



by 쉬귀 | 2010/10/31 03:03 | 개발 | 트랙백 | 덧글(1)

◀ 이전 페이지 다음 페이지 ▶