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

Last change on this file since 1513 was 1513, checked in by sam, 8 years ago

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

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