Created
September 26, 2010 22:31
-
-
Save DylanLukes/598373 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// NSBundle+GLUtil.m | |
// Concorde | |
// | |
// Created by Dylan Lukes on 9/25/10. | |
// Copyright 2010 Dylan Lukes. All rights reserved. | |
// | |
#import "NSBundle+CGLUtil.h" | |
@implementation NSBundle (CGLUtil) | |
- (BOOL)loadShaderNamed:(NSString*)shaderName buffer:(const GLchar**)buffer length:(GLint*)length error:(NSError**)error{ | |
NSDictionary *errorInfo; | |
NSURL* url = [self URLForResource:shaderName withExtension:nil subdirectory:@"Shaders"]; | |
NSString* shader = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:error]; | |
if (!shader) { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Shader \"%@\" could not be loaded.", shaderName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Does \"%@\" exist?", shaderName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:0 userInfo:errorInfo]; | |
return NO; | |
} | |
if (buffer){ | |
*buffer = [shader UTF8String]; | |
} else { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Reference for \"buffer\" passed for \"%@\" is invalid.", shaderName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Make sure you are passing a ** type", shaderName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:1 userInfo:errorInfo]; | |
return NO; | |
} | |
if (length){ | |
*length = [shader lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | |
} else { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Reference for \"length\" passed for \"%@\" is invalid.", shaderName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Make sure you are passing a * type", shaderName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:1 userInfo:errorInfo]; | |
return NO; | |
} | |
return YES; | |
} | |
- (BOOL)loadTextureNamed:(NSString*)textureName pixels:(GLvoid**)pixels width:(GLsizei*)width height:(GLsizei*)height flip:(BOOL)flip error:(NSError**)error{ | |
NSDictionary *errorInfo; | |
NSURL* url = [self URLForResource:textureName withExtension:nil subdirectory:@"Textures"]; | |
NSLog(@"%@", url); | |
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL); | |
CGImageRef tex = CGImageSourceCreateImageAtIndex(source, 0, NULL); | |
if (!tex) { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Texture \"%@\" could not be loaded.", textureName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Does \"%@\" exist?", textureName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:0 userInfo:errorInfo]; | |
return NO; | |
} | |
size_t w = CGImageGetWidth(tex); | |
size_t h = CGImageGetWidth(tex); | |
GLvoid* imgdata = malloc(w * h * 4); | |
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = CGBitmapContextCreate(imgdata, w, h, 8, w * 4, colorspace, kCGImageAlphaPremultipliedLast); | |
if (flip) { | |
CGContextTranslateCTM(context, 0.0f, h); | |
CGContextScaleCTM(context, 1.0f, -1.0f); | |
} | |
CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)w, (CGFloat)h), tex); | |
CGContextRelease(context); | |
CGColorSpaceRelease(colorspace); | |
CGImageRelease(tex); | |
CFRelease(source); | |
if (width){ | |
*width = w; | |
} else { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Reference for \"width\" passed for \"%@\" is invalid.", textureName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Make sure you are passing a * type", textureName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:1 userInfo:errorInfo]; | |
return NO; | |
} | |
if (height){ | |
*height = h; | |
} else { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Reference for \"height\" passed for \"%@\" is invalid.", textureName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Make sure you are passing a * type", textureName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:1 userInfo:errorInfo]; | |
return NO; | |
} | |
if (pixels){ | |
*pixels = imgdata; | |
} else { | |
errorInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSString stringWithFormat:@"Reference for \"length\" passed for \"%@\" is invalid.", textureName], | |
NSLocalizedDescriptionKey, | |
[NSString stringWithFormat:@"Make sure you are passing a ** type", textureName], | |
NSLocalizedRecoverySuggestionErrorKey, | |
nil]; | |
*error = [NSError errorWithDomain:@"com.dlukes.cglutil" code:1 userInfo:errorInfo]; | |
return NO; | |
} | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment