Created
October 19, 2009 23:34
-
-
Save nilium/213841 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2009 Noel R. Cower | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#ifndef C2DRENDERER_MM_RIA4GF2R | |
#define C2DRENDERER_MM_RIA4GF2R | |
#import <Cocoa/Cocoa.h> | |
typedef union { | |
struct { | |
union {CGFloat red,r;}; | |
union {CGFloat green,g;}; | |
union {CGFloat blue,b;}; | |
union {CGFloat alpha,a;}; | |
}; | |
CGFloat buffer[4]; | |
} C2DColor; | |
#define RVERTEX_SIZE (sizeof(CGFloat)*2) | |
#define RTEXCOORD_SIZE (sizeof(CGFloat)*2) | |
#define RCOLOR_SIZE (sizeof(CGFloat)*4) | |
const int RENDER2D_BUFFER_SIZE_BYTES = 131072; // 128kb | |
const int RENDER2D_ARRAY_LENGTH = 512; | |
@interface C2DRenderer : NSObject | |
{ | |
// client arrays | |
NSMutableData *vertex_buffer; | |
NSMutableData *color_buffer; | |
NSMutableData *texcoord_buffer; | |
int index; // current vertex index | |
int sets; // number of arrays to render | |
C2DColor autocolor; // automatic color set to rectangles and other prims | |
NSMutableData *array_indices; // array of vertex indices | |
NSMutableData *array_counts; // array of numbers of vertices to render | |
int lock; | |
} | |
@property(readwrite) C2DColor autoColor; | |
-(void)addPolygonWithVertices:(GLsizei)num positions:(const CGFloat*)posbuf colors:(const CGFloat*)colbuf texcoords:(const CGFloat*)texbuf; | |
-(void)addRectangle:(CGRect)rect texcoords:(CGRect)texcoords; | |
-(void)lockBuffers; | |
-(void)unlockBuffers; | |
-(void)render; | |
-(void)resetBuffers; | |
@end | |
#endif /* end of include guard: C2DRENDERER_MM_RIA4GF2R */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2009 Noel R. Cower | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
/* Note: None of this is tested. At all. */ | |
#import "C2DRenderer.h" | |
@implementation C2DRenderer | |
@synthesize autoColor=autocolor; | |
- (id)init | |
{ | |
if((self = [super init])) | |
{ | |
index = sets = lock = 0; | |
// set up buffers | |
vertex_buffer = [[NSMutableData alloc] initWithLength:RENDER2D_BUFFER_SIZE_BYTES]; | |
texcoord_buffer = [[NSMutableData alloc] initWithLength:RENDER2D_BUFFER_SIZE_BYTES]; | |
color_buffer = [[NSMutableData alloc] initWithLength:RENDER2D_BUFFER_SIZE_BYTES]; | |
// set up arrays | |
array_indices = [[NSMutableData alloc] initWithLength:RENDER2D_ARRAY_LENGTH*sizeof(GLint)]; | |
array_counts = [[NSMutableData alloc] initWithLength:RENDER2D_ARRAY_LENGTH*sizeof(GLsizei)]; | |
autocolor.r = autocolor.g = autocolor.b = autocolor.a = 1.0; | |
} | |
return self; | |
} | |
-(void)dealloc { | |
if ( lock == 0 ) { | |
@throw [NSException exceptionWithName:@"BuffersLockedException" | |
reason:@"Attempting to deallocate renderer while buffers are locked and bound" | |
userInfo:nil]; | |
} | |
[vertex_buffer release]; | |
[texcoord_buffer release]; | |
[color_buffer release]; | |
[array_indices release]; | |
[array_counts release]; | |
[super dealloc]; | |
} | |
-(void)finalize { | |
if ( lock == 0 ) { | |
@throw [NSException exceptionWithName:@"BuffersLockedException" | |
reason:@"Attempting to finalize renderer while buffers are locked and bound" | |
userInfo:nil]; | |
} | |
[super finalize]; | |
} | |
-(void)render { | |
[self lockBuffers]; | |
glMultiDrawArrays(GL_POLYGON, (const GLint*)[array_indices bytes], (const GLsizei*)[array_counts bytes], sets); | |
[self unlockBuffers]; | |
} | |
-(void)lockBuffers { | |
if (++lock == 1) { | |
glVertexPointer(2, GL_DOUBLE, 0, [vertex_buffer bytes]); | |
glTexCoordPointer(2, GL_DOUBLE, 0, [texcoord_buffer bytes]); | |
glColorPointer(2, GL_DOUBLE, 0, [color_buffer bytes]); | |
} | |
} | |
-(void)unlockBuffers { | |
if (lock == 0) { | |
@throw [NSException exceptionWithName:@"UnmatchedUnlockException" | |
reason:@"Attempt to unlock buffers when they are not locked - you shouldn't do this" | |
@userInfo:nil]; | |
} | |
if (--lock == 0) { | |
glVertexPointer(4, GL_FLOAT, 0, 0); | |
glTexCoordPointer(4, GL_FLOAT, 0, 0); | |
glColorPointer(4, GL_FLOAT, 0, 0); | |
} | |
} | |
-(void)addPolygonWithVertices:(GLsizei)num positions:(const CGFloat*)posbuf colors:(const CGFloat*)colbuf texcoords:(const CGFloat*)texbuf { | |
if ( lock != 0 ) { | |
@throw [NSException exceptionWithName:@"BuffersLockedException" | |
reason:@"Cannot add new data to buffers while they are locked." | |
userInfo:nil]; | |
} | |
// make sure the data fits in the buffers | |
unsigned array_length = [array_indices length]; | |
if ( sets >= array_length/4 ) { | |
[array_indices increaseLengthBy:array_length]; | |
[array_counts increaseLengthBy:[array_counts length]]; | |
} | |
if ( [vertex_buffer length] <= num*RVERTEX_SIZE ) { | |
[vertex_buffer increaseLengthBy:[vertex_buffer length]]; | |
// texcoord buffer has data inserted at the same rate, so it's going to run out of space at the same time | |
[texcoord_buffer increaseLengthBy:[texcoord_buffer length]]; | |
} | |
if ( [color_buffer length] <= num*RCOLOR_SIZE ) | |
[color_buffer increaseLengthBy:[color_buffer length]]; | |
// cram the info into the stupid buffers | |
[vertex_buffer replaceBytesInRange:NSMakeRange(index*RVERTEX_SIZE, num*RVERTEX_SIZE) withBytes:posbuf]; | |
[texcoord_buffer replaceBytesInRange:NSMakeRange(index*RTEXCOORD_SIZE, num*RTEXCOORD_SIZE) withBytes:texbuf]; | |
[color_buffer replaceBytesInRange:NSMakeRange(index*RCOLOR_SIZE, num*RCOLOR_SIZE) withBytes:colbuf]; | |
((GLint*)[array_indices mutableBytes])[sets] = index; | |
((GLsizei*)[array_counts mutableBytes])[sets] = num; | |
++sets; | |
index += num; | |
} | |
-(void)addRectangle:(CGRect)rect texcoords:(CGRect)texcoords { | |
CGFloat positions[8] = { | |
rect.origin.x, rect.origin.y, | |
rect.origin.x+rect.size.width, rect.origin.y, | |
rect.origin.x+rect.size.width, rect.origin.y+rect.size.height, | |
rect.origin.x, rect.origin.y+rect.size.height | |
}; | |
CGFloat texbuf[8] = { | |
texcoords.origin.x, texcoords.origin.y, | |
texcoords.origin.x+texcoords.size.width, texcoords.origin.y, | |
texcoords.origin.x+texcoords.size.width, texcoords.origin.y+texcoords.size.height, | |
texcoords.origin.x, texcoords.origin.y+texcoords.size.height | |
}; | |
C2DColor colors[4] = {autocolor, autocolor, autocolor, autocolor}; | |
[self addPolygonWithVertices:4 | |
positions:positions | |
colors:(const CGFloat*)(&colors[0]) | |
texcoords:texbuf]; | |
} | |
-(void)resetBuffers { | |
if ( lock != 0 ) { | |
@throw [NSException exceptionWithName:@"BuffersLockedException" | |
reason:@"Cannot reset buffers when they are locked." | |
userInfo:nil]; | |
} | |
sets = 0; | |
index = 0; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment