2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <SDL_image.h>
22 #include "cave/helper/colors.hpp"
23 #include "sdl/sdlpixbuf.hpp"
26 int SDLPixbuf::get_width() const {
31 int SDLPixbuf::get_height() const {
36 void SDLPixbuf::check_and_convert_to_rgba() {
37 /* if the r/g/b/masks do not match our preset values, the image is not stored in R,G,B,[A] byte order
38 * in memory. so convert it. */
39 if (surface
->format
->BytesPerPixel
!=4 || surface
->format
->Rmask
!= rmask
|| surface
->format
->Gmask
!= gmask
|| surface
->format
->Bmask
!= bmask
) {
40 SDL_Surface
*newsurface
= SDL_CreateRGBSurface(SDL_SRCALPHA
, surface
->w
, surface
->h
, 32, rmask
, gmask
, bmask
, surface
->format
->Amask
!= 0 ? amask
: 0);
41 SDL_SetAlpha(surface
, 0, SDL_ALPHA_OPAQUE
);
42 SDL_BlitSurface(surface
, NULL
, newsurface
, NULL
);
43 SDL_FreeSurface(surface
);
49 SDLPixbuf::SDLPixbuf(int length
, unsigned char const *data
) {
50 SDL_RWops
*rwop
=SDL_RWFromConstMem(data
, length
);
51 surface
=IMG_Load_RW(rwop
, 1); // 1 = automatically closes rwop
52 if (!surface
) // if could not load
53 throw std::runtime_error(std::string("cannot load image ")+IMG_GetError());
54 check_and_convert_to_rgba();
58 SDLPixbuf::SDLPixbuf(const char *filename
) {
59 surface
=IMG_Load(filename
);
60 if (!surface
) // if could not load
61 throw std::runtime_error(std::string("cannot load image ")+IMG_GetError());
62 check_and_convert_to_rgba();
66 SDLPixbuf::SDLPixbuf(int w
, int h
) {
67 surface
=SDL_CreateRGBSurface(SDL_SRCALPHA
, w
, h
, 32, rmask
, gmask
, bmask
, amask
);
69 throw std::runtime_error(std::string("could not create surface: ")+SDL_GetError());
73 SDLPixbuf::SDLPixbuf(SDL_Surface
*surface_
)
78 SDLPixbuf::~SDLPixbuf() {
79 SDL_FreeSurface(surface
);
83 void SDLPixbuf::fill_rect(int x
, int y
, int w
, int h
, const GdColor
&c
) {
89 SDL_FillRect(surface
, &dst
, SDL_MapRGB(surface
->format
, c
.get_r(), c
.get_g(), c
.get_b()));
93 bool SDLPixbuf::has_alpha() const {
94 return surface
->format
->Amask
!=0;
98 void SDLPixbuf::blit_full(int x
, int y
, int w
, int h
, Pixbuf
&dest
, int dx
, int dy
) const {
106 SDL_SetAlpha(surface
, SDL_SRCALPHA
, SDL_ALPHA_OPAQUE
);
107 SDL_BlitSurface(surface
, &src
, static_cast<SDLPixbuf
&>(dest
).surface
, &dst
);
111 void SDLPixbuf::copy_full(int x
, int y
, int w
, int h
, Pixbuf
&dest
, int dx
, int dy
) const {
119 SDL_SetAlpha(surface
, 0, SDL_ALPHA_OPAQUE
);
120 SDL_BlitSurface(surface
, &src
, static_cast<SDLPixbuf
&>(dest
).surface
, &dst
);
124 unsigned char *SDLPixbuf::get_pixels() const {
125 return static_cast<unsigned char *>(surface
->pixels
);
129 int SDLPixbuf::get_pitch() const {
130 return surface
->pitch
;
134 void SDLPixbuf::lock() const {
135 if (SDL_MUSTLOCK(surface
))
136 SDL_LockSurface(surface
);
140 void SDLPixbuf::unlock() const {
141 if (SDL_MUSTLOCK(surface
))
142 SDL_UnlockSurface(surface
);