source: trunk/monsterz/ios/classes/EAGLView.m @ 739

Last change on this file since 739 was 739, checked in by sam, 10 years ago

ios: clean up code.

File size: 3.8 KB
Line 
1//
2// Monsterz
3//
4// Copyright: (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#import <QuartzCore/QuartzCore.h>
12
13#import "EAGLView.h"
14
15@interface EAGLView (PrivateMethods)
16- (void)createFramebuffer;
17- (void)deleteFramebuffer;
18@end
19
20@implementation EAGLView
21
22@dynamic context;
23
24// You must implement this method
25+ (Class)layerClass
26{
27    return [CAEAGLLayer class];
28}
29
30// The EAGL view is stored in the nib file. When it's unarchived it's sent
31// -initWithCoder:.
32- (id)initWithCoder:(NSCoder*)coder
33{
34    self = [super initWithCoder:coder];
35    if (self)
36    {
37        CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
38
39        eaglLayer.opaque = TRUE;
40        eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
41                                        [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
42                                        kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
43                                        nil];
44    }
45
46    return self;
47}
48
49- (void)dealloc
50{
51    [self deleteFramebuffer];
52    [context release];
53    [super dealloc];
54}
55
56- (EAGLContext *)context
57{
58    return context;
59}
60
61- (void)setContext:(EAGLContext *)newContext
62{
63    if (context != newContext)
64    {
65        [self deleteFramebuffer];
66        [context release];
67        context = [newContext retain];
68        [EAGLContext setCurrentContext:nil];
69    }
70}
71
72- (void)createFramebuffer
73{
74    if (context && !defaultFramebuffer)
75    {
76        [EAGLContext setCurrentContext:context];
77
78        // Create default framebuffer object.
79        glGenFramebuffers(1, &defaultFramebuffer);
80        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
81
82        // Create color render buffer and allocate backing store.
83        glGenRenderbuffers(1, &colorRenderbuffer);
84        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
85        [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer *)self.layer];
86        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
87        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
88
89        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);
90
91        if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
92            NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
93    }
94}
95
96- (void)deleteFramebuffer
97{
98    if (context)
99    {
100        [EAGLContext setCurrentContext:context];
101
102        if (defaultFramebuffer)
103        {
104            glDeleteFramebuffers(1, &defaultFramebuffer);
105            defaultFramebuffer = 0;
106        }
107
108        if (colorRenderbuffer)
109        {
110            glDeleteRenderbuffers(1, &colorRenderbuffer);
111            colorRenderbuffer = 0;
112        }
113    }
114}
115
116- (void)setFramebuffer
117{
118    if (context)
119    {
120        [EAGLContext setCurrentContext:context];
121
122        if (!defaultFramebuffer)
123            [self createFramebuffer];
124
125        glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
126        glViewport(0, 0, framebufferWidth, framebufferHeight);
127    }
128}
129
130- (BOOL)presentFramebuffer
131{
132    BOOL success = FALSE;
133
134    if (context)
135    {
136        [EAGLContext setCurrentContext:context];
137        glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
138        success = [context presentRenderbuffer:GL_RENDERBUFFER];
139    }
140
141    return success;
142}
143
144- (void)layoutSubviews
145{
146    // The framebuffer will be re-created at the beginning of the next
147    // setFramebuffer method call.
148    [self deleteFramebuffer];
149}
150
151@end
152
Note: See TracBrowser for help on using the repository browser.