103 lines
2.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined __APPLE__ && defined __MACH__ && defined __arm__
  14. #include <cmath>
  15. #import <UIKit/UIKit.h>
  16. #include "core.h"
  17. #include "../../image/image-private.h"
  18. using namespace std;
  19. namespace lol
  20. {
  21. /*
  22. * Image implementation class
  23. */
  24. DECLARE_IMAGE_LOADER(IosImageData, 100)
  25. {
  26. public:
  27. virtual bool Open(char const *);
  28. virtual bool Close();
  29. virtual void *GetData() const;
  30. private:
  31. uint8_t *pixels;
  32. };
  33. /*
  34. * Public Image class
  35. */
  36. bool IosImageData::Open(char const *path)
  37. {
  38. NSString *fullpath = [NSString stringWithUTF8String:path];
  39. NSArray *chunks = [fullpath componentsSeparatedByString: @"/"];
  40. NSString *filename = [chunks objectAtIndex: [chunks count] - 1];
  41. chunks = [filename componentsSeparatedByString: @"."];
  42. NSString *prefix = [chunks objectAtIndex: 0];
  43. NSString *mypath = [[NSBundle mainBundle] pathForResource:prefix ofType:@"png"];
  44. NSData *pngdata = [[NSData alloc] initWithContentsOfFile:mypath];
  45. UIImage *image = [[UIImage alloc] initWithData:pngdata];
  46. if (!image)
  47. {
  48. #if !LOL_RELEASE
  49. Log::Error("could not load %s\n", path);
  50. #endif
  51. return false;
  52. }
  53. int w = CGImageGetWidth(image.CGImage);
  54. int h = CGImageGetHeight(image.CGImage);
  55. size = ivec2(w, h);
  56. format = FORMAT_RGBA;
  57. CGColorSpaceRef cspace = CGColorSpaceCreateDeviceRGB();
  58. pixels = (uint8_t *)malloc(w * h * 4);
  59. CGContextRef ctx =
  60. CGBitmapContextCreate(pixels, w, h, 8, 4 * w, cspace,
  61. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  62. CGColorSpaceRelease(cspace);
  63. CGContextClearRect(ctx, CGRectMake(0, 0, w, h));
  64. CGContextTranslateCTM(ctx, 0, h - h);
  65. CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), image.CGImage);
  66. CGContextRelease(ctx);
  67. [image release];
  68. [pngdata release];
  69. return true;
  70. }
  71. bool IosImageData::Close()
  72. {
  73. free(pixels);
  74. return true;
  75. }
  76. void * IosImageData::GetData() const
  77. {
  78. return pixels;
  79. }
  80. } /* namespace lol */
  81. #endif /* defined __APPLE__ && defined __MACH__ && defined __arm__ */