Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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