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