/libs/cocos2d/CCTexture2D.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 562 lines · 384 code · 92 blank · 86 comment · 64 complexity · 998ccdb0c813dfdec7f0358282416d56 MD5 · raw file

  1. /*
  2. ===== IMPORTANT =====
  3. This is sample code demonstrating API, technology or techniques in development.
  4. Although this sample code has been reviewed for technical accuracy, it is not
  5. final. Apple is supplying this information to help you plan for the adoption of
  6. the technologies and programming interfaces described herein. This information
  7. is subject to change, and software implemented based on this sample code should
  8. be tested with final operating system software and final documentation. Newer
  9. versions of this sample code may be provided with future seeds of the API or
  10. technology. For information about updates to this and other developer
  11. documentation, view the New & Updated sidebars in subsequent documentation
  12. seeds.
  13. =====================
  14. File: Texture2D.m
  15. Abstract: Creates OpenGL 2D textures from images or text.
  16. Version: 1.6
  17. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  18. ("Apple") in consideration of your agreement to the following terms, and your
  19. use, installation, modification or redistribution of this Apple software
  20. constitutes acceptance of these terms. If you do not agree with these terms,
  21. please do not use, install, modify or redistribute this Apple software.
  22. In consideration of your agreement to abide by the following terms, and subject
  23. to these terms, Apple grants you a personal, non-exclusive license, under
  24. Apple's copyrights in this original Apple software (the "Apple Software"), to
  25. use, reproduce, modify and redistribute the Apple Software, with or without
  26. modifications, in source and/or binary forms; provided that if you redistribute
  27. the Apple Software in its entirety and without modifications, you must retain
  28. this notice and the following text and disclaimers in all such redistributions
  29. of the Apple Software.
  30. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  31. to endorse or promote products derived from the Apple Software without specific
  32. prior written permission from Apple. Except as expressly stated in this notice,
  33. no other rights or licenses, express or implied, are granted by Apple herein,
  34. including but not limited to any patent rights that may be infringed by your
  35. derivative works or by other works in which the Apple Software may be
  36. incorporated.
  37. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  38. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  39. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  41. COMBINATION WITH YOUR PRODUCTS.
  42. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  43. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  44. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  46. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  47. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  48. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. Copyright (C) 2008 Apple Inc. All Rights Reserved.
  50. */
  51. /*
  52. * Support for RGBA_4_4_4_4 and RGBA_5_5_5_1 was copied from:
  53. * https://devforums.apple.com/message/37855#37855 by a1studmuffin
  54. */
  55. #import <OpenGLES/ES1/glext.h>
  56. #import "ccConfig.h"
  57. #import "ccMacros.h"
  58. #import "CCTexture2D.h"
  59. #import "CCPVRTexture.h"
  60. #import "CCConfiguration.h"
  61. #import "Support/ccUtils.h"
  62. #if CC_FONT_LABEL_SUPPORT
  63. // FontLabel support
  64. #import "FontManager.h"
  65. #import "FontLabelStringDrawing.h"
  66. #endif// CC_FONT_LABEL_SUPPORT
  67. //CLASS IMPLEMENTATIONS:
  68. // If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
  69. // Default is: RGBA8888 (32-bit textures)
  70. static CCTexture2DPixelFormat defaultAlphaPixelFormat_ = kCCTexture2DPixelFormat_Default;
  71. // By default PVR images are treated as if they don't have the alpha channel premultiplied
  72. static BOOL PVRHaveAlphaPremultiplied_ = NO;
  73. @implementation CCTexture2D
  74. @synthesize contentSize=size_, pixelFormat=format_, pixelsWide=width_, pixelsHigh=height_, name=name_, maxS=maxS_, maxT=maxT_;
  75. @synthesize hasPremultipliedAlpha=hasPremultipliedAlpha_;
  76. - (id) initWithData:(const void*)data pixelFormat:(CCTexture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size
  77. {
  78. if((self = [super init])) {
  79. glGenTextures(1, &name_);
  80. glBindTexture(GL_TEXTURE_2D, name_);
  81. [self setAntiAliasTexParameters];
  82. // Specify OpenGL texture image
  83. switch(pixelFormat)
  84. {
  85. case kCCTexture2DPixelFormat_RGBA8888:
  86. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
  87. break;
  88. case kCCTexture2DPixelFormat_RGBA4444:
  89. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, data);
  90. break;
  91. case kCCTexture2DPixelFormat_RGB5A1:
  92. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, data);
  93. break;
  94. case kCCTexture2DPixelFormat_RGB565:
  95. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
  96. break;
  97. case kCCTexture2DPixelFormat_A8:
  98. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
  99. break;
  100. default:
  101. [NSException raise:NSInternalInconsistencyException format:@""];
  102. }
  103. size_ = size;
  104. width_ = width;
  105. height_ = height;
  106. format_ = pixelFormat;
  107. maxS_ = size.width / (float)width;
  108. maxT_ = size.height / (float)height;
  109. hasPremultipliedAlpha_ = NO;
  110. }
  111. return self;
  112. }
  113. - (void) dealloc
  114. {
  115. CCLOGINFO(@"cocos2d: deallocing %@", self);
  116. if(name_)
  117. glDeleteTextures(1, &name_);
  118. [super dealloc];
  119. }
  120. - (NSString*) description
  121. {
  122. return [NSString stringWithFormat:@"<%@ = %08X | Name = %i | Dimensions = %ix%i | Coordinates = (%.2f, %.2f)>", [self class], self, name_, width_, height_, maxS_, maxT_];
  123. }
  124. @end
  125. @implementation CCTexture2D (Image)
  126. - (id) initWithImage:(UIImage *)uiImage
  127. {
  128. NSUInteger POTWide, POTHigh;
  129. CGImageRef CGImage;
  130. CGContextRef context = nil;
  131. void* data = nil;;
  132. CGColorSpaceRef colorSpace;
  133. void* tempData;
  134. unsigned int* inPixel32;
  135. unsigned short* outPixel16;
  136. BOOL hasAlpha;
  137. CGImageAlphaInfo info;
  138. CGSize imageSize;
  139. CCTexture2DPixelFormat pixelFormat;
  140. CGImage = uiImage.CGImage;
  141. if(CGImage == NULL) {
  142. CCLOG(@"cocos2d: CCTexture2D. Can't create Texture. UIImage is nil");
  143. [self release];
  144. return nil;
  145. }
  146. CCConfiguration *conf = [CCConfiguration sharedConfiguration];
  147. #if CC_TEXTURE_NPOT_SUPPORT
  148. if( [conf supportsNPOT] ) {
  149. POTWide = CGImageGetWidth(CGImage);
  150. POTHigh = CGImageGetHeight(CGImage);
  151. } else
  152. #endif
  153. {
  154. POTWide = ccNextPOT(CGImageGetWidth(CGImage));
  155. POTHigh = ccNextPOT(CGImageGetHeight(CGImage));
  156. }
  157. unsigned maxTextureSize = [conf maxTextureSize];
  158. if( POTHigh > maxTextureSize || POTWide > maxTextureSize ) {
  159. CCLOG(@"cocos2d: WARNING: Image (%d x %d) is bigger than the supported %d x %d", POTWide, POTHigh, maxTextureSize, maxTextureSize);
  160. [self release];
  161. return nil;
  162. }
  163. info = CGImageGetAlphaInfo(CGImage);
  164. hasAlpha = ((info == kCGImageAlphaPremultipliedLast) || (info == kCGImageAlphaPremultipliedFirst) || (info == kCGImageAlphaLast) || (info == kCGImageAlphaFirst) ? YES : NO);
  165. size_t bpp = CGImageGetBitsPerComponent(CGImage);
  166. colorSpace = CGImageGetColorSpace(CGImage);
  167. if(colorSpace) {
  168. if(hasAlpha || bpp >= 8)
  169. pixelFormat = defaultAlphaPixelFormat_;
  170. else {
  171. CCLOG(@"cocos2d: CCTexture2D: Using RGB565 texture since image has no alpha");
  172. pixelFormat = kCCTexture2DPixelFormat_RGB565;
  173. }
  174. } else {
  175. // NOTE: No colorspace means a mask image
  176. CCLOG(@"cocos2d: CCTexture2D: Using A8 texture since image is a mask");
  177. pixelFormat = kCCTexture2DPixelFormat_A8;
  178. }
  179. imageSize = CGSizeMake(CGImageGetWidth(CGImage), CGImageGetHeight(CGImage));
  180. // Create the bitmap graphics context
  181. switch(pixelFormat) {
  182. case kCCTexture2DPixelFormat_RGBA8888:
  183. case kCCTexture2DPixelFormat_RGBA4444:
  184. case kCCTexture2DPixelFormat_RGB5A1:
  185. colorSpace = CGColorSpaceCreateDeviceRGB();
  186. data = malloc(POTHigh * POTWide * 4);
  187. info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast;
  188. // info = kCGImageAlphaPremultipliedLast; // issue #886. This patch breaks BMP images.
  189. context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, 4 * POTWide, colorSpace, info | kCGBitmapByteOrder32Big);
  190. CGColorSpaceRelease(colorSpace);
  191. break;
  192. case kCCTexture2DPixelFormat_RGB565:
  193. colorSpace = CGColorSpaceCreateDeviceRGB();
  194. data = malloc(POTHigh * POTWide * 4);
  195. info = kCGImageAlphaNoneSkipLast;
  196. context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, 4 * POTWide, colorSpace, info | kCGBitmapByteOrder32Big);
  197. CGColorSpaceRelease(colorSpace);
  198. break;
  199. case kCCTexture2DPixelFormat_A8:
  200. data = malloc(POTHigh * POTWide);
  201. info = kCGImageAlphaOnly;
  202. context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, POTWide, NULL, info);
  203. break;
  204. default:
  205. [NSException raise:NSInternalInconsistencyException format:@"Invalid pixel format"];
  206. }
  207. CGContextClearRect(context, CGRectMake(0, 0, POTWide, POTHigh));
  208. CGContextTranslateCTM(context, 0, POTHigh - imageSize.height);
  209. CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(CGImage), CGImageGetHeight(CGImage)), CGImage);
  210. // Repack the pixel data into the right format
  211. if(pixelFormat == kCCTexture2DPixelFormat_RGB565) {
  212. //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGGBBBBB"
  213. tempData = malloc(POTHigh * POTWide * 2);
  214. inPixel32 = (unsigned int*)data;
  215. outPixel16 = (unsigned short*)tempData;
  216. for(unsigned int i = 0; i < POTWide * POTHigh; ++i, ++inPixel32)
  217. *outPixel16++ = ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | ((((*inPixel32 >> 8) & 0xFF) >> 2) << 5) | ((((*inPixel32 >> 16) & 0xFF) >> 3) << 0);
  218. free(data);
  219. data = tempData;
  220. }
  221. else if (pixelFormat == kCCTexture2DPixelFormat_RGBA4444) {
  222. //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRGGGGBBBBAAAA"
  223. tempData = malloc(POTHigh * POTWide * 2);
  224. inPixel32 = (unsigned int*)data;
  225. outPixel16 = (unsigned short*)tempData;
  226. for(unsigned int i = 0; i < POTWide * POTHigh; ++i, ++inPixel32)
  227. *outPixel16++ =
  228. ((((*inPixel32 >> 0) & 0xFF) >> 4) << 12) | // R
  229. ((((*inPixel32 >> 8) & 0xFF) >> 4) << 8) | // G
  230. ((((*inPixel32 >> 16) & 0xFF) >> 4) << 4) | // B
  231. ((((*inPixel32 >> 24) & 0xFF) >> 4) << 0); // A
  232. free(data);
  233. data = tempData;
  234. }
  235. else if (pixelFormat == kCCTexture2DPixelFormat_RGB5A1) {
  236. //Convert "RRRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA" to "RRRRRGGGGGBBBBBA"
  237. tempData = malloc(POTHigh * POTWide * 2);
  238. inPixel32 = (unsigned int*)data;
  239. outPixel16 = (unsigned short*)tempData;
  240. for(unsigned int i = 0; i < POTWide * POTHigh; ++i, ++inPixel32)
  241. *outPixel16++ =
  242. ((((*inPixel32 >> 0) & 0xFF) >> 3) << 11) | // R
  243. ((((*inPixel32 >> 8) & 0xFF) >> 3) << 6) | // G
  244. ((((*inPixel32 >> 16) & 0xFF) >> 3) << 1) | // B
  245. ((((*inPixel32 >> 24) & 0xFF) >> 7) << 0); // A
  246. free(data);
  247. data = tempData;
  248. }
  249. self = [self initWithData:data pixelFormat:pixelFormat pixelsWide:POTWide pixelsHigh:POTHigh contentSize:imageSize];
  250. // should be after calling super init
  251. hasPremultipliedAlpha_ = (info == kCGImageAlphaPremultipliedLast || info == kCGImageAlphaPremultipliedFirst);
  252. CGContextRelease(context);
  253. free(data);
  254. return self;
  255. }
  256. @end
  257. @implementation CCTexture2D (Text)
  258. - (id) initWithString:(NSString*)string fontName:(NSString*)name fontSize:(CGFloat)size
  259. {
  260. CGSize dim;
  261. #if CC_FONT_LABEL_SUPPORT
  262. ZFont *zFont = [[FontManager sharedManager] zFontWithName:name pointSize:size];
  263. if (zFont != nil)
  264. dim = [string sizeWithZFont:zFont];
  265. else
  266. #endif
  267. dim = [string sizeWithFont:[UIFont fontWithName:name size:size]];
  268. return [self initWithString:string dimensions:dim alignment:UITextAlignmentCenter fontName:name fontSize:size];
  269. }
  270. - (id) initWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(UITextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
  271. {
  272. NSUInteger width,
  273. height,
  274. i;
  275. CGContextRef context;
  276. void* data;
  277. CGColorSpaceRef colorSpace;
  278. id uiFont;
  279. width = dimensions.width;
  280. if((width != 1) && (width & (width - 1))) {
  281. i = 1;
  282. while(i < width)
  283. i *= 2;
  284. width = i;
  285. }
  286. height = dimensions.height;
  287. if((height != 1) && (height & (height - 1))) {
  288. i = 1;
  289. while(i < height)
  290. i *= 2;
  291. height = i;
  292. }
  293. colorSpace = CGColorSpaceCreateDeviceGray();
  294. data = calloc(height, width);
  295. context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
  296. CGColorSpaceRelease(colorSpace);
  297. CGContextSetGrayFillColor(context, 1.0f, 1.0f);
  298. CGContextTranslateCTM(context, 0.0f, height);
  299. CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
  300. UIGraphicsPushContext(context);
  301. #if CC_FONT_LABEL_SUPPORT
  302. uiFont = [[FontManager sharedManager] zFontWithName:name pointSize:size];
  303. if (uiFont != nil)
  304. [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withZFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
  305. else
  306. #endif // CC_FONT_LABEL_SUPPORT
  307. {
  308. uiFont = [UIFont fontWithName:name size:size];
  309. [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
  310. }
  311. if( ! uiFont )
  312. CCLOG(@"cocos2d: Texture2D: Font '%@' not found", name);
  313. UIGraphicsPopContext();
  314. self = [self initWithData:data pixelFormat:kCCTexture2DPixelFormat_A8 pixelsWide:width pixelsHigh:height contentSize:dimensions];
  315. CGContextRelease(context);
  316. free(data);
  317. return self;
  318. }
  319. @end
  320. @implementation CCTexture2D (Drawing)
  321. - (void) drawAtPoint:(CGPoint)point
  322. {
  323. GLfloat coordinates[] = { 0.0f, maxT_,
  324. maxS_, maxT_,
  325. 0.0f, 0.0f,
  326. maxS_, 0.0f };
  327. GLfloat width = (GLfloat)width_ * maxS_,
  328. height = (GLfloat)height_ * maxT_;
  329. GLfloat vertices[] = { point.x, point.y, 0.0f,
  330. width + point.x, point.y, 0.0f,
  331. point.x, height + point.y, 0.0f,
  332. width + point.x, height + point.y, 0.0f };
  333. glBindTexture(GL_TEXTURE_2D, name_);
  334. glVertexPointer(3, GL_FLOAT, 0, vertices);
  335. glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
  336. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  337. }
  338. - (void) drawInRect:(CGRect)rect
  339. {
  340. GLfloat coordinates[] = { 0.0f, maxT_,
  341. maxS_, maxT_,
  342. 0.0f, 0.0f,
  343. maxS_, 0.0f };
  344. GLfloat vertices[] = { rect.origin.x, rect.origin.y, /*0.0f,*/
  345. rect.origin.x + rect.size.width, rect.origin.y, /*0.0f,*/
  346. rect.origin.x, rect.origin.y + rect.size.height, /*0.0f,*/
  347. rect.origin.x + rect.size.width, rect.origin.y + rect.size.height, /*0.0f*/ };
  348. glBindTexture(GL_TEXTURE_2D, name_);
  349. glVertexPointer(2, GL_FLOAT, 0, vertices);
  350. glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
  351. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  352. }
  353. @end
  354. @implementation CCTexture2D (PVR)
  355. -(id) initWithPVRTCData: (const void*)data level:(int)level bpp:(int)bpp hasAlpha:(BOOL)hasAlpha length:(int)length
  356. {
  357. // GLint saveName;
  358. if( ! [[CCConfiguration sharedConfiguration] supportsPVRTC] ) {
  359. CCLOG(@"cocos2d: WARNING: PVRTC images is not supported");
  360. [self release];
  361. return nil;
  362. }
  363. if((self = [super init])) {
  364. glGenTextures(1, &name_);
  365. glBindTexture(GL_TEXTURE_2D, name_);
  366. [self setAntiAliasTexParameters];
  367. GLenum format;
  368. GLsizei size = length * length * bpp / 8;
  369. if(hasAlpha) {
  370. format = (bpp == 4) ? GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  371. } else {
  372. format = (bpp == 4) ? GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG : GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  373. }
  374. if(size < 32) {
  375. size = 32;
  376. }
  377. glCompressedTexImage2D(GL_TEXTURE_2D, level, format, length, length, 0, size, data);
  378. size_ = CGSizeMake(length, length);
  379. width_ = length;
  380. height_ = length;
  381. maxS_ = 1.0f;
  382. maxT_ = 1.0f;
  383. hasPremultipliedAlpha_ = PVRHaveAlphaPremultiplied_;
  384. }
  385. return self;
  386. }
  387. -(id) initWithPVRFile: (NSString*) file
  388. {
  389. if( (self = [super init]) ) {
  390. CCPVRTexture *pvr = [[CCPVRTexture alloc] initWithContentsOfFile:file];
  391. if( pvr ) {
  392. pvr.retainName = YES; // don't dealloc texture on release
  393. name_ = pvr.name; // texture id
  394. maxS_ = 1; // only POT texture are supported
  395. maxT_ = 1;
  396. width_ = pvr.width;
  397. height_ = pvr.height;
  398. size_ = CGSizeMake(width_, height_);
  399. hasPremultipliedAlpha_ = PVRHaveAlphaPremultiplied_;
  400. [pvr release];
  401. [self setAntiAliasTexParameters];
  402. } else {
  403. CCLOG(@"cocos2d: Couldn't load PVR image");
  404. [self release];
  405. return nil;
  406. }
  407. }
  408. return self;
  409. }
  410. +(void) PVRImagesHavePremultipliedAlpha:(BOOL)haveAlphaPremultiplied
  411. {
  412. PVRHaveAlphaPremultiplied_ = haveAlphaPremultiplied;
  413. }
  414. @end
  415. //
  416. // Use to apply MIN/MAG filter
  417. //
  418. @implementation CCTexture2D (GLFilter)
  419. -(void) generateMipmap
  420. {
  421. NSAssert( width_ == ccNextPOT(width_) && height_ == ccNextPOT(height_), @"Mimpap texture only works in POT textures");
  422. glBindTexture( GL_TEXTURE_2D, name_ );
  423. glGenerateMipmapOES(GL_TEXTURE_2D);
  424. }
  425. -(void) setTexParameters: (ccTexParams*) texParams
  426. {
  427. NSAssert( (width_ == ccNextPOT(width_) && height_ == ccNextPOT(height_)) ||
  428. (texParams->wrapS == GL_CLAMP_TO_EDGE && texParams->wrapT == GL_CLAMP_TO_EDGE),
  429. @"GL_CLAMP_TO_EDGE should be used in NPOT textures");
  430. glBindTexture( GL_TEXTURE_2D, self.name );
  431. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texParams->minFilter );
  432. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texParams->magFilter );
  433. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, texParams->wrapS );
  434. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, texParams->wrapT );
  435. }
  436. -(void) setAliasTexParameters
  437. {
  438. ccTexParams texParams = { GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
  439. [self setTexParameters: &texParams];
  440. }
  441. -(void) setAntiAliasTexParameters
  442. {
  443. ccTexParams texParams = { GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE };
  444. [self setTexParameters: &texParams];
  445. }
  446. @end
  447. //
  448. // Texture options for images that contains alpha
  449. //
  450. @implementation CCTexture2D (PixelFormat)
  451. +(void) setDefaultAlphaPixelFormat:(CCTexture2DPixelFormat)format
  452. {
  453. defaultAlphaPixelFormat_ = format;
  454. }
  455. +(CCTexture2DPixelFormat) defaultAlphaPixelFormat
  456. {
  457. return defaultAlphaPixelFormat_;
  458. }
  459. @end