开发者

Preloading Cocos2D textures when a standard UIKit UIView is visible

开发者 https://www.devze.com 2023-02-10 04:27 出处:网络
My app uses Cocos2D for animation and 2D rendering, but the \"game lobby\" (that\'s not what it is, but for all intents and purposes) is all standard UIView/UIKit code.

My app uses Cocos2D for animation and 2D rendering, but the "game lobby" (that's not what it is, but for all intents and purposes) is all standard UIView/UIKit code.

I have a UIViewController subclass that has its main view set in the XIB file as an EAGLView. When I put all the Cocos loading code into viewDidLoad, everything works as expected, except that there is a 1-2 second freeze while Cocos loads all my textures.

I have about 20 textures that I preload into the CCSpriteFrameCache before running my scene - so my animations are smooth immediately.

I tried to move this texture loading code to my view controller's init method -- but when I try this, Cocos crashes, seemingly because there is no OpenGL context to work with.

What I want to do is, while displaying a UIViewController's main view (and not blocking the main thread), I'd like to load textures in the background, and then when finished, transition between that UIView and the OpenGL EAGLView.

I am aware that there is an addImageAsync: call in Cocos now, so it's really just a matter o开发者_如何转开发f somehow "passing around an offscreen EAGLView/EAGLContext" that I can eventually stick into Cocos when I am ready to display that view.

Does anyone have any pointers on an elegant solution to this?


The solution to this problem was to create a separate EAGLContext, using which I would load the textures. That EAGLContext needed to share a EAGLSharegroup with the actual GL view that I used to render the content. So the steps worked like this:

  1. Create/load/show my UI View
  2. Create my to-be-used EAGLView
  3. Snag the EAGLSharegroup off that EAGLView
  4. Create a EAGLContext using that sharegroup
  5. Load my textures using that EAGLContext
  6. Switch my UIView to the EAGLView
  7. Textures are loaded and available
0

精彩评论

暂无评论...
验证码 换一张
取 消