Advertisement
Guest User

Untitled

a guest
Aug 5th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. ---------------------------------
  2. main.mm
  3. ---------------------------------
  4. #import <UIKit/UIKit.h>
  5.  
  6. int main(int argc, char *argv[]) {
  7. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  8. int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
  9. [pool release];
  10. return retVal;
  11. }
  12.  
  13.  
  14. ---------------------------------
  15. AppDelegate.h
  16. ---------------------------------
  17.  
  18. #import <UIKit/UIKit.h>
  19. #import "GLView.h"
  20.  
  21. @interface AppDelegate : NSObject <UIApplicationDelegate> {
  22. UIWindow* Window;
  23. GLView* View;
  24. }
  25.  
  26. @end
  27.  
  28.  
  29.  
  30. ---------------------------------
  31. AppDelegate.m
  32. ---------------------------------
  33.  
  34. #import "AppDelegate.h"
  35.  
  36. @implementation AppDelegate
  37.  
  38. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  39. {
  40. CGRect screenBounds = [[UIScreen mainScreen] bounds];
  41.  
  42. Window = [[UIWindow alloc] initWithFrame:screenBounds];
  43. View = [[GLView alloc] initWithFrame:screenBounds];
  44.  
  45. [Window addSubview:View];
  46. [Window makeKeyWindow];
  47.  
  48. [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
  49.  
  50. return YES;
  51. }
  52.  
  53. - (void)applicationWillResignActive:(UIApplication *)application {
  54. /*
  55. Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  56. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  57. */
  58. }
  59.  
  60. - (void)applicationDidEnterBackground:(UIApplication *)application {
  61. /*
  62. Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  63. If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  64. */
  65. }
  66.  
  67. - (void)applicationWillEnterForeground:(UIApplication *)application {
  68. /*
  69. Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  70. */
  71. }
  72.  
  73. - (void)applicationDidBecomeActive:(UIApplication *)application {
  74. /*
  75. Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  76. */
  77. }
  78.  
  79. - (void)applicationWillTerminate:(UIApplication *)application {
  80. /*
  81. Called when the application is about to terminate.
  82. Save data if appropriate.
  83. See also applicationDidEnterBackground:.
  84. */
  85. }
  86.  
  87. - (void)dealloc {
  88. [View release];
  89. [Window release];
  90.  
  91. [super dealloc];
  92. }
  93.  
  94. @end
  95.  
  96.  
  97.  
  98.  
  99. ---------------------------------
  100. GLView.h
  101. ---------------------------------
  102.  
  103. #import <UIKit/UIKit.h>
  104. #import <OpenGLES/EAGL.h>
  105. #import <QuartzCore/QuartzCore.h>
  106. #import <OpenGLES/ES1/gl.h>
  107. #import <OpenGLES/ES1/glext.h>
  108.  
  109.  
  110. @interface GLView : UIView {
  111. EAGLContext* Context;
  112.  
  113. id DisplayLink;
  114.  
  115. @private
  116. GLint GLWidth;
  117. GLint GLHeight;
  118.  
  119. GLuint GLFrameBuffer;
  120. GLuint GLColorBuffer;
  121. }
  122.  
  123. -(void) drawView;
  124.  
  125. @end
  126.  
  127.  
  128.  
  129.  
  130. ---------------------------------
  131. GLView.mm
  132. ---------------------------------
  133.  
  134. #import "GLView.h"
  135.  
  136. @implementation GLView
  137.  
  138. +(Class) layerClass {
  139. return [CAEAGLLayer class];
  140. }
  141.  
  142. -(id) initWithFrame:(CGRect)frame {
  143. self = [super initWithFrame:frame];
  144.  
  145. if (self) {
  146. CAEAGLLayer* eaglLayer = (CAEAGLLayer*)super.layer;
  147.  
  148. eaglLayer.opaque = YES;
  149. eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
  150.  
  151. Context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  152.  
  153. if(!Context || ![EAGLContext setCurrentContext:Context]) {
  154. [self release];
  155. return nil;
  156. }
  157.  
  158. glGenRenderbuffersOES(1, &GLColorBuffer);
  159. glBindRenderbufferOES(GL_RENDERBUFFER_OES, GLColorBuffer);
  160. [Context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer];
  161.  
  162. glGenFramebuffersOES(1, &GLFrameBuffer);
  163. glBindFramebufferOES(GL_FRAMEBUFFER_OES, GLFrameBuffer);
  164. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, GLColorBuffer);
  165.  
  166.  
  167. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &GLWidth);
  168. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &GLHeight);
  169.  
  170. if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  171. NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  172. return NO;
  173. }
  174.  
  175. glViewport(0, 0, GLWidth, GLHeight);
  176.  
  177. glMatrixMode(GL_PROJECTION);
  178. glOrthof(0, GLWidth, 0, GLHeight, -20, 20);
  179.  
  180. glMatrixMode(GL_MODELVIEW);
  181.  
  182. DisplayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(drawView)];
  183.  
  184. [DisplayLink setFrameInterval:1];
  185. [DisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
  186.  
  187. NSLog([NSString stringWithFormat:@"Screen Setup = %d, %d", GLWidth, GLHeight]);
  188. }
  189.  
  190. return self;
  191. }
  192.  
  193. -(void) drawView {
  194. [EAGLContext setCurrentContext:Context];
  195.  
  196. glBindFramebufferOES(GL_FRAMEBUFFER_OES, GLFrameBuffer);
  197.  
  198. GLfloat vertexArray[] = {
  199. 0.0, 0.0, 0.0,
  200. 100.0, 0.0, 0.0,
  201. 100.0, 100.0, 0.0,
  202. 0.0, 100.0, 0.0
  203. };
  204.  
  205. GLfloat colorArray[] = {
  206. 1.0, 0.0, 0.0, 1.0,
  207. 0.0, 1.0, 0.0, 1.0,
  208. 0.0, 0.0, 1.0, 1.0,
  209. 1.0, 0.0, 1.0, 1.0
  210. };
  211.  
  212. glLoadIdentity();
  213.  
  214. glClearColor(0.0, 104.0/255.0, 55.0/255.0, 1.0);
  215. glClear(GL_COLOR_BUFFER_BIT);
  216.  
  217. glEnableClientState(GL_VERTEX_ARRAY);
  218. glVertexPointer(3, GL_FLOAT, 0, vertexArray);
  219.  
  220. glEnableClientState(GL_COLOR_ARRAY);
  221. glColorPointer(4, GL_FLOAT, 0, colorArray);
  222.  
  223. glDrawArrays(GL_TRIANGLE_STRIP, 0, 12);
  224.  
  225. glBindRenderbufferOES(GL_RENDERBUFFER_OES, GLColorBuffer);
  226.  
  227. [Context presentRenderbuffer:GL_FRAMEBUFFER_OES];
  228. }
  229.  
  230. -(void) dealloc {
  231. if([EAGLContext currentContext] == Context) {
  232. [EAGLContext setCurrentContext:nil];
  233. }
  234.  
  235. [Context release];
  236.  
  237. [super dealloc];
  238. }
  239.  
  240. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement