2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
19 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <SDL_image.h>
31 #include "sdl/sdlpixbuffactory.hpp"
32 #include "sdl/sdlpixbuf.hpp"
33 #include "sdl/sdlscreen.hpp"
34 #include "cave/colors.hpp"
38 Pixbuf
*SDLPixbufFactory::create_from_file(const char *filename
) const {
39 SDL_Surface
*surface
= IMG_Load(filename
);
40 if (!surface
) // if could not load
41 throw std::runtime_error(std::string("cannot load image ") + IMG_GetError());
42 return new SDLPixbuf(surface
);
46 Pixbuf
*SDLPixbufFactory::create_from_inline(int length
, unsigned char const *data
) const {
47 SDL_RWops
*rwop
= SDL_RWFromConstMem(data
, length
);
48 SDL_Surface
*surface
= IMG_Load_RW(rwop
, 1); // 1 = automatically closes rwop
49 if (!surface
) // if could not load
50 throw std::runtime_error(std::string("cannot load image ") + IMG_GetError());
51 return new SDLPixbuf(surface
);
55 Pixbuf
*SDLPixbufFactory::create(int w
, int h
) const {
56 return new SDLPixbuf(w
, h
);
60 Pixbuf
*SDLPixbufFactory::create_composite_color(Pixbuf
const &src
, const GdColor
&c
, unsigned char a
) const {
61 SDLPixbuf
const &srcsdl
= static_cast<SDLPixbuf
const &>(src
);
62 SDL_Surface
*rect
= SDL_CreateRGBSurface(0, srcsdl
.get_surface()->w
, srcsdl
.get_surface()->h
, 32, srcsdl
.rmask
, srcsdl
.gmask
, srcsdl
.bmask
, 0); /* no amask, as we set overall alpha! */
64 throw std::runtime_error(std::string("could not create surface: ") + SDL_GetError());
65 unsigned char r
, g
, b
;
67 SDL_FillRect(rect
, NULL
, SDL_MapRGB(rect
->format
, r
, g
, b
));
68 SDL_SetAlpha(rect
, SDL_SRCALPHA
, a
);
70 SDL_Surface
*ret
= SDL_CreateRGBSurface(0, srcsdl
.get_surface()->w
, srcsdl
.get_surface()->h
, 32, srcsdl
.rmask
, srcsdl
.gmask
, srcsdl
.bmask
, srcsdl
.amask
);
72 throw std::runtime_error(std::string("could not create surface: ") + SDL_GetError());
73 SDL_SetAlpha(srcsdl
.get_surface(), 0, 255);
74 SDL_BlitSurface(srcsdl
.get_surface(), NULL
, ret
, NULL
);
75 SDL_BlitSurface(rect
, NULL
, ret
, NULL
);
76 SDL_FreeSurface(rect
);
77 return new SDLPixbuf(ret
);
81 Pixbuf
*SDLPixbufFactory::create_subpixbuf(Pixbuf
&src
, int x
, int y
, int w
, int h
) const {
82 g_assert(x
>= 0 && y
>= 0 && x
+ w
<= src
.get_width() && y
+ h
<= src
.get_height());
83 SDL_Surface
*sub
= SDL_CreateRGBSurfaceFrom(&src(x
, y
), w
, h
, 32, src
.get_pitch(), src
.rmask
, src
.gmask
, src
.bmask
, src
.amask
);
84 return new SDLPixbuf(sub
);