source: trunk/src/image/codec/ios-image.cpp @ 961

Last change on this file since 961 was 961, checked in by sam, 11 years ago

build: fix a shitload of build errors introduced by the Image refactoring
and the OS X port.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
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
23using namespace std;
24
25namespace lol
26{
27
28/*
29 * Image implementation class
30 */
31
32DECLARE_IMAGE_LOADER(IosImageData, 100)
33{
34public:
35    virtual bool Open(char const *);
36    virtual bool Close();
37
38    virtual void *GetData() const;
39
40private:
41    uint8_t *pixels;
42};
43
44/*
45 * Public Image class
46 */
47
48bool 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
87bool IosImageData::Close()
88{
89    free(pixels);
90
91    return true;
92}
93
94void * IosImageData::GetData() const
95{
96    return pixels;
97}
98
99} /* namespace lol */
100
101#endif /* defined __APPLE__ && defined __MACH__ && defined __arm__ */
102
Note: See TracBrowser for help on using the repository browser.