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

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

base: start removing occurrences of NULL on our long journey to nullptr.

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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 *m_img;
52};
53
54/*
55 * Public Image class
56 */
57
58bool SdlImageData::Open(char const *path)
59{
60    Array<String> pathlist = System::GetPathList(path);
61    for (int i = 0; i < pathlist.Count(); i++)
62    {
63        m_img = IMG_Load(pathlist[i].C());
64        if (m_img)
65            break;
66    }
67
68    if (!m_img)
69    {
70#if !LOL_RELEASE
71        Log::Error("could not load image %s\n", path);
72#endif
73        return false;
74    }
75
76    size = ivec2(m_img->w, m_img->h);
77
78    if (m_img->format->BytesPerPixel != 4)
79    {
80        SDL_Surface *tmp = Create32BppSurface(size);
81        SDL_BlitSurface(m_img, nullptr, tmp, nullptr);
82        SDL_FreeSurface(m_img);
83        m_img = tmp;
84    }
85
86    format = m_img->format->Amask ? Image::FORMAT_RGBA : Image::FORMAT_RGB;
87
88    return true;
89}
90
91bool SdlImageData::Close()
92{
93    SDL_FreeSurface(m_img);
94
95    return true;
96}
97
98void * SdlImageData::GetData() const
99{
100    return m_img->pixels;
101}
102
103SDL_Surface *SdlImageData::Create32BppSurface(ivec2 size)
104{
105    uint32_t rmask, gmask, bmask, amask;
106#if SDL_BYTEORDER == SDL_BIG_ENDIAN
107    rmask = 0xff000000;
108    gmask = 0x00ff0000;
109    bmask = 0x0000ff00;
110    amask = 0x000000ff;
111#else
112    rmask = 0x000000ff;
113    gmask = 0x0000ff00;
114    bmask = 0x00ff0000;
115    amask = 0xff000000;
116#endif
117
118    return SDL_CreateRGBSurface(SDL_SWSURFACE, size.x, size.y, 32,
119                                rmask, gmask, bmask, amask);
120}
121
122} /* namespace lol */
123
124#endif /* defined USE_SDL_IMAGE */
125
Note: See TracBrowser for help on using the repository browser.