source: trunk/src/image/image-private.h @ 963

Last change on this file since 963 was 963, checked in by sam, 9 years ago

build: fix PS3 and Android build; it was broken since the ImageData refactor.

  • Property svn:keywords set to Id
File size: 2.6 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//
12// The ImageData class
13// -------------------
14//
15
16#if !defined __LOL_IMAGE_PRIVATE_H__
17#define __LOL_IMAGE_PRIVATE_H__
18
19#include "image.h"
20
21namespace lol
22{
23
24class ImageLoader
25{
26    friend class Image;
27    friend class ImageData;
28
29public:
30    ImageData *(*fun)(char const *path);
31    ImageLoader *next;
32    int priority;
33
34    static bool RegisterAllLoaders();
35
36    static void RegisterLoader(ImageLoader *loader)
37    {
38        Helper(loader);
39    }
40
41private:
42    static ImageData *Load(char const *path)
43    {
44        ImageLoader *parser = Helper(NULL);
45        ImageData *ret = NULL;
46
47        while (parser && !ret)
48        {
49            ret = parser->fun(path);
50            parser = parser->next;
51        }
52
53        return ret;
54    }
55
56    static ImageLoader *Helper(ImageLoader *set)
57    {
58        static ImageLoader *loaders = NULL;
59
60        if (!set)
61            return loaders;
62
63        ImageLoader **parser = &loaders;
64        while (*parser && (*parser)->priority > set->priority)
65            parser = &(*parser)->next;
66        set->next = *parser;
67        *parser = set;
68
69        return NULL;
70    }
71};
72
73class ImageData
74{
75    friend class Image;
76
77public:
78    virtual ~ImageData() {}
79
80    virtual bool Open(char const *) = 0;
81    virtual bool Close() = 0;
82
83    virtual void *GetData() const = 0;
84
85protected:
86    ivec2 size;
87    Image::format_t format;
88};
89
90#define REGISTER_IMAGE_LOADER(name) \
91    extern void (Register##name)(); \
92    Register##name();
93
94#define DECLARE_IMAGE_LOADER(name, prio) \
95    template<typename T> class name##ImageLoader : public ImageLoader \
96    { \
97    public: \
98        name##ImageLoader() \
99        { \
100            static ImageLoader loader; \
101            loader.fun = Load; \
102            loader.priority = prio; \
103            RegisterLoader(&loader); \
104        } \
105        static ImageData *Load(char const *path) \
106        { \
107            T *ret = new T(); \
108            if (!ret->Open(path)) \
109            { \
110                delete ret; \
111                return NULL; \
112            } \
113            return ret; \
114        } \
115    }; \
116    class name; \
117    name##ImageLoader<name> name##ImageLoaderInstance; \
118    void Register##name() \
119    { \
120        (void)&name##ImageLoaderInstance; \
121    } \
122    class name : public ImageData
123
124} /* namespace lol */
125
126#endif // __LOL_IMAGE_PRIVATE_H__
127
Note: See TracBrowser for help on using the repository browser.