Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / QuartzCore / CALayerContext.m
blobf2a57a5a16e59a2a85dca9a179db60cd248b7fd8
1 #import <QuartzCore/CALayerContext.h>
2 #import <QuartzCore/CALayer.h>
3 #import <QuartzCore/CARenderer.h>
4 #import <Foundation/NSString.h>
6 @interface CALayer(private)
7 -(void)_setContext:(CALayerContext *)context;
8 -(NSNumber *)_textureId;
9 -(void)_setTextureId:(NSNumber *)value;
10 @end
12 @implementation CALayerContext
14 -initWithFrame:(CGRect)rect {
15    CGLError error;
16    
17    CGLPixelFormatAttribute attributes[1]={
18     0,
19    };
20    GLint numberOfVirtualScreens;
21       
22    CGLChoosePixelFormat(attributes,&_pixelFormat,&numberOfVirtualScreens);
24    if((error=CGLCreateContext(_pixelFormat,NULL,&_glContext))!=kCGLNoError)
25     NSLog(@"CGLCreateContext failed with %d in %s %d",error,__FILE__,__LINE__);
27    _frame=rect;
28    
29    GLint width=rect.size.width;
30    GLint height=rect.size.height;
31    
32    GLint backingOrigin[2]={rect.origin.x,rect.origin.y};
33    GLint backingSize[2]={width,height};
35    CGLSetParameter(_glContext,kCGLCPSurfaceBackingOrigin,backingOrigin);
36    CGLSetParameter(_glContext,kCGLCPSurfaceBackingSize,backingSize);
37    
38    GLint opacity=0;
39    CGLSetParameter(_glContext,kCGLCPSurfaceOpacity,&opacity);
41    _renderer=[[CARenderer rendererWithCGLContext:_glContext options:nil] retain];
42       
43    return self;
46 -(void)dealloc {
47    [_timer invalidate];
48    [_timer release];
49    [super dealloc];
52 -(void)setFrame:(CGRect)rect {
53    _frame=rect;
54    
55    GLint width=rect.size.width;
56    GLint height=rect.size.height;
57    
58    GLint backingOrigin[2]={rect.origin.x,rect.origin.y};
59    GLint backingSize[2]={width,height};
61    CGLSetParameter(_glContext,kCGLCPSurfaceBackingOrigin,backingOrigin);
62    CGLSetParameter(_glContext,kCGLCPSurfaceBackingSize,backingSize);
65 -(void)setLayer:(CALayer *)layer {
66    layer=[layer retain];
67    [_layer release];
68    _layer=layer;
70    [_layer _setContext:self];
71    [_renderer setLayer:layer];
74 -(void)invalidate {
77 -(void)assignTextureIdsToLayerTree:(CALayer *)layer {
79    if([layer _textureId]==nil){
80     GLuint texture;
81     
82     glGenTextures(1,&texture);
83     [layer _setTextureId:[NSNumber numberWithUnsignedInt:texture]];
84    }
85    
86    for(CALayer *child in layer.sublayers)
87     [self assignTextureIdsToLayerTree:child];
90 -(void)renderLayer:(CALayer *)layer {
91    CGLSetCurrentContext(_glContext);
93    glEnable(GL_DEPTH_TEST);
94    glShadeModel(GL_SMOOTH);
96    GLint width=_frame.size.width;
97    GLint height=_frame.size.height;
98    
99    glViewport(0,0,width,height);
100    glMatrixMode(GL_PROJECTION);                      
101    glLoadIdentity();
102    glOrtho (0, width, 0, height, -1, 1);
103    
104    GLsizei i=0;
105    GLuint  deleteIds[[_deleteTextureIds count]];
106    
107    for(NSNumber *number in _deleteTextureIds)
108     deleteIds[i++]=[number unsignedIntValue];
109    
110    if(i>0)
111     glDeleteTextures(i,deleteIds);
112    
113    [_deleteTextureIds removeAllObjects];
114    
115    [self assignTextureIdsToLayerTree:layer];
116    
117    [_renderer render];
120 -(void)render {
121    [self renderLayer:_layer];
124 -(void)timer:(NSTimer *)timer {
125    [_renderer beginFrameAtTime:CACurrentMediaTime() timeStamp:NULL];
127    [self render];
129    [_renderer endFrame];
132 -(void)startTimerIfNeeded {
133    if(_timer==nil)
134     _timer=[[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES] retain];
137 -(void)deleteTextureId:(NSNumber *)textureId {
138    [_deleteTextureIds addObject:textureId];
141 @end