当然,您似乎是在标准 UIView 上调用 setContext,而不是在 EAGLView 上。您的视图控制器可能应该实现 -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;
}
视图控制器.h:
@interface GLViewController : UIViewController <ADBannerViewDelegate>
{
EAGLView *glView;
}
@property(nonatomic, retain) /*IBOutlet*/ EAGLView *glView;
@end
视图控制器.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;
}