2012-10-29 84 views
0

當我啓動我的遊戲時,我希望屏幕在後臺加載資源時顯示載入圖像。我通過加載一個簡單的UIImageView組件並添加一個「微調器」來做到這一點,以便讓用戶反饋,設備正在後臺加載一些東西。將屏幕載入遊戲轉換

雖然此圖像顯示,我加載了所有的圖像和紋理,並設置了我的OpenGL視圖並讓它呈現。 當第二幀得到渲染時,我想隱藏ImageView並僅顯示OpenGL視圖。我不希望OpenGL視圖在第一幀顯示,因爲它需要非常長的渲染時間。

但是,我加載了所有資源並設置了OpenGL視圖的DisplayLink,以便在新線程中輸入渲染循環,然後在加載完成時顯示OpenGL視圖。渲染循環似乎並未開始。

這裏是我的視圖控制器的方法的loadView

- (void)loadView 
{ 
CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame]; 

    // Set up the image view 
    UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"Startscreen" ofType:@"png"]]; 
    _imageView = [[UIImageView alloc] initWithFrame:mainScreenFrame]; 
    _imageView.image = img; 

    // Set up the spinner 
    _spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    [_spinner setCenter:CGPointMake(mainScreenFrame.size.height/3.0*1.85, mainScreenFrame.size.width/10.0*7.55)]; 
    [_imageView addSubview:_spinner];  
    [_spinner startAnimating]; 

    // Show the loading image 
    self.view = _imageView; 

    /* Load resources in a new thread -- this shows the spinner during loading */ 
    [NSThread detachNewThreadSelector:@selector(loadGLView) toTarget:self withObject:nil]; 
} 

loadGLView只做以下並初始化OpenGL視圖,並開始加載過程。

_glView = [[JungleOpenGLView alloc] initWithFrame:mainScreenFrame]; 

這是我在OpenGL視圖中設置DisplayLink的方式。

CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(setupRender:)]; 
[displayLink setFrameInterval:2]; 
[displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; 

當第二個框獲取呈現我發送通知和視圖控制器然後將

self.view = _glView; 

回答

0

我發現我錯了。 而是像這樣的加載方法創建一個新的線程:

[NSThread detachNewThreadSelector:@selector(loadGLView) toTarget:self withObject:nil]; 

我用:

[self performSelectorInBackground:@selector(loadGLView) withObject:nil]; 

這做的伎倆。關於這個主題的良好閱讀(創建線程)可以在這裏找到:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html