20130319
[gdash.git] / src / sdl / sdlpixbuf.cpp
blob1a10c9754a70e731443fc1f2bb6e1525d74d384d
1 /*
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.
17 #include "config.h"
19 #include <SDL_image.h>
20 #include <stdexcept>
22 #include "cave/helper/colors.hpp"
23 #include "sdl/sdlpixbuf.hpp"
26 int SDLPixbuf::get_width() const {
27 return surface->w;
31 int SDLPixbuf::get_height() const {
32 return surface->h;
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);
44 surface = newsurface;
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);
68 if (!surface)
69 throw std::runtime_error(std::string("could not create surface: ")+SDL_GetError());
73 SDLPixbuf::SDLPixbuf(SDL_Surface *surface_)
74 : 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) {
84 SDL_Rect dst;
85 dst.x=x;
86 dst.y=y;
87 dst.w=w;
88 dst.h=h;
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 {
99 SDL_Rect src, dst;
100 src.x=x;
101 src.y=y;
102 src.w=w;
103 src.h=h;
104 dst.x=dx;
105 dst.y=dy;
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 {
112 SDL_Rect src, dst;
113 src.x=x;
114 src.y=y;
115 src.w=w;
116 src.h=h;
117 dst.x=dx;
118 dst.y=dy;
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);