游戏开发论坛

 找回密码
 立即注册
搜索
查看: 6447|回复: 0

用Storyboard创建iOS 的 OpenglES 应用

[复制链接]

1万

主题

1万

帖子

2万

积分

管理员

中级会员

Rank: 9Rank: 9Rank: 9

积分
20468
发表于 2012-7-6 16:00:00 | 显示全部楼层 |阅读模式
博客:金属星球

1、创建一个继承于GLKViewController类的新类,比如HelloGLKitViewController

2、编辑文件HelloGLKitViewController.m,增加以下内容


@interface HelloGLKitViewController (){float _curRed;
    BOOL _increasing;

}@property(strong, nonatomic) EAGLContext *context;

@end

@implementation HelloGLKitViewController
@synthesize context = _context;


并增加


  1. -(void)viewDidLoad
  2. {[super viewDidLoad];

  3.     self.context =[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

  4.     if(!self.context){
  5.         NSLog(@"Failed to create ES context");
  6.     }

  7.     GLKView *view =(GLKView *)self.view;
  8.     view.context = self.context;
  9. }

  10. -(void)viewDidUnload
  11. {[super viewDidUnload];

  12.     if([EAGLContext currentContext]== self.context){[EAGLContext setCurrentContext:nil];
  13.     }
  14.     self.context =nil;
  15. }#pragma mark - GLKViewDelegate

  16. -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

  17.     glClearColor(_curRed, 0.0, 0.0, 1.0);
  18.     glClear(GL_COLOR_BUFFER_BIT);

  19. }

  20. #pragma mark - GLKViewControllerDelegate

  21. -(void)update {if(_increasing){
  22.         _curRed +=1.0* self.timeSinceLastUpdate;
  23.     }else{
  24.         _curRed -=1.0* self.timeSinceLastUpdate;
  25.     }if(_curRed >=1.0){
  26.         _curRed =1.0;
  27.         _increasing =NO;
  28.     }if(_curRed <=0.0){
  29.         _curRed =0.0;
  30.         _increasing =YES;
  31.     }}
  32.    
  33.     // 暂停功能
  34.     -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {
  35.     self.paused =!self.paused;
  36. }
复制代码


3、在AppDelegate.m文件中删除 didFinishLaunchingWithOptions 函数中的内容,只留下 return YES;

4、然后创建一个新的Storyboard文件,命名为:MainStoryboard.storyboard打开这个文件,拖拽一个 View Controller 到空白处,选择这个控件在 Identity Inspector 中修改 class 为我们之前创建好的HelloGLKitViewController再点击控件内部的空白处,同样的方法设置 class 为 GLKView 。

5、下一步需要将这个故事板设定为启动项打开 HelloGLKit-Info.plist 文件,右键添加一行,下拉列表中选择 Main storyboard file base name 在后面的格子中填上故事板的文件名 MainStoryboard.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-28 02:45

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表