當然,你似乎是在標準的UIView而不是EAGLView上調用setContext。您的視圖控制器可能應該實施-loadView,做這樣的事情:
這似乎很奇怪
- (void)loadView {
self.view = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
的一件事是你與initWithNibName而不是initWithFrame,這正是我所期望的,你需要初始化你的視圖控制器在取消nib/xib文件時要做的事情。
這是我正在做的事情,也許它會有所幫助。我注意到:我有一個自定義視圖控制器,它有一個UIView和一個子視圖,它是EAGLView。(我也在研究添加一個ADBanner子視圖)。我沒有在我的調用'init'控制器,它的視圖當我嘗試將它添加到窗口作爲子視圖時被加載。)
main.m - 類似。
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Create the window programatically:
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
controller = [GLViewController alloc];
[window addSubview:controller.view];
glView = [controller.glView retain];
[window makeKeyAndVisible];
return YES;
}
ViewController.h:
@interface GLViewController : UIViewController <ADBannerViewDelegate>
{
EAGLView *glView;
}
@property(nonatomic, retain) /*IBOutlet*/ EAGLView *glView;
@end
ViewController.m:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
}
-(void)viewDidLoad
{
[super viewDidLoad];
glView = [[EAGLView alloc] initWithFrame:[UIScreen mainScreen].bounds];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
glView.userInteractionEnabled=YES;
[self.view addSubview:glView];
}
EAGLView.m:
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;// JPS - WARNING: this does not work on iOS 3.2!!!
self.contentScaleFactor = [UIScreen mainScreen].scale;
CGSize displaySize = [[UIScreen mainScreen]currentMode].size;
CGRect displayRect = [UIScreen mainScreen].bounds;
eaglLayer.frame = displayRect;
eaglLayer.opaque = YES;
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys : [NSNumber numberWithBool : NO],
kEAGLDrawablePropertyRetainedBacking,
kEAGLColorFormatRGBA8,
kEAGLDrawablePropertyColorFormat, nil];
context = [[EAGLContext alloc] initWithAPI : kEAGLRenderingAPIOpenGLES1];
if (!context || ![EAGLContext setCurrentContext : context]) {
[self release];
return;
}
glGenFramebuffersOES(1, &viewFramebuffer);
glGenRenderbuffersOES(1, &viewRenderbuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
[context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer *)self.layer];
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
glGenRenderbuffersOES(1, &depthRenderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
// Set up thd display link to sync rendering with vblank
animating = FALSE;
displayLinkSupported = FALSE;
animationFrameInterval = 1;
displayLink = nil;
animationTimer = nil;
// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer
// class is used as fallback when it isn't available.
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
displayLinkSupported = TRUE;
}
return self;
}