source: trunk/src/image/codec/sdl-image.cpp @ 1691

Last change on this file since 1691 was 1691, checked in by sam, 7 years ago

build: tweak everything to allow building with SDL on the Raspberry Pi.

  • Property svn:keywords set to Id
File size: 2.2 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 USE_SDL_IMAGE
16
17#if defined HAVE_SDL_SDL_H
18#   include <SDL/SDL.h>
19#else
20#   include <SDL.h>
21#endif
22#if defined HAVE_SDL_SDL_IMAGE_H
23#   include <SDL/SDL_image.h>
24#else
25#   include <SDL_image.h>
26#endif
27
28#include "core.h"
29#include "../../image/image-private.h"
30
31using namespace std;
32
33namespace lol
34{
35
36/*
37 * Image implementation class
38 */
39
40DECLARE_IMAGE_LOADER(SdlImageData, 50)
41{
42public:
43    virtual bool Open(char const *);
44    virtual bool Close();
45
46    virtual void *GetData() const;
47
48    static SDL_Surface *Create32BppSurface(ivec2 size);
49
50private:
51    SDL_Surface *img;
52};
53
54/*
55 * Public Image class
56 */
57
58bool SdlImageData::Open(char const *path)
59{
60    for (char const *name = path; *name; name++)
61        if ((img = IMG_Load(name)))
62            break;
63
64    if (!img)
65    {
66#if !LOL_RELEASE
67        Log::Error("could not load %s\n", path);
68#endif
69        return false;
70    }
71
72    size = ivec2(img->w, img->h);
73
74    if (img->format->BytesPerPixel != 4)
75    {
76        SDL_Surface *tmp = Create32BppSurface(size);
77        SDL_BlitSurface(img, NULL, tmp, NULL);
78        SDL_FreeSurface(img);
79        img = tmp;
80    }
81
82    format = img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB;
83
84    return true;
85}
86
87bool SdlImageData::Close()
88{
89    SDL_FreeSurface(img);
90
91    return true;
92}
93
94void * SdlImageData::GetData() const
95{
96    return img->pixels;
97}
98
99SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size)
100{
101    uint32_t rmask, gmask, bmask, amask;
102#if SDL_BYTEORDER == SDL_BIG_ENDIAN
103    rmask = 0xff000000;
104    gmask = 0x00ff0000;
105    bmask = 0x0000ff00;
106    amask = 0x000000ff;
107#else
108    rmask = 0x000000ff;
109    gmask = 0x0000ff00;
110    bmask = 0x00ff0000;
111    amask = 0xff000000;
112#endif
113
114    return SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, 32,
115                                rmask, gmask, bmask, amask);
116}
117
118} /* namespace lol */
119
120#endif /* defined USE_SDL_IMAGE */
121
Note: See TracBrowser for help on using the repository browser.