20130313
[gdash.git] / src / sdl / sdlpixbuffactory.cpp
blobde57b8fc8b7aac4214d732d6a979acba1acc59d4
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 <stdexcept>
20 #include <SDL.h>
21 #include <SDL_image.h>
22 #include <glib.h>
24 #include "sdl/sdlpixbuffactory.hpp"
25 #include "sdl/sdlpixmap.hpp"
26 #include "sdl/sdlscreen.hpp"
27 #include "cave/helper/colors.hpp"
29 SDLPixbufFactory::SDLPixbufFactory(GdScalingType scaling_type_, bool pal_emulation_)
31 PixbufFactory(scaling_type_, pal_emulation_)
35 SDLPixbuf *SDLPixbufFactory::create_from_file(const char *filename) const
37 return new SDLPixbuf(filename);
40 SDLPixbuf *SDLPixbufFactory::create_from_inline(int length, unsigned char const *data) const
42 return new SDLPixbuf(length, data);
45 SDLPixbuf *SDLPixbufFactory::create(int w, int h) const
47 return new SDLPixbuf(w, h);
50 Pixmap *SDLPixbufFactory::create_pixmap_from_pixbuf(Pixbuf const& pb, bool format_alpha) const
52 SDLPixbuf *scaled = static_cast<SDLPixbuf*> (create_scaled(pb));
53 SDL_Surface *surface;
54 if (format_alpha)
55 surface=SDL_DisplayFormatAlpha(scaled->surface);
56 else
57 surface=SDL_DisplayFormat(scaled->surface);
58 return new SDLPixmap(surface);
61 SDLPixbuf *SDLPixbufFactory::create_composite_color(Pixbuf const &src, const GdColor& c, unsigned char a) const
63 SDLPixbuf const &srcsdl = static_cast<SDLPixbuf const &>(src);
64 SDL_Surface *rect=SDL_CreateRGBSurface(0, srcsdl.surface->w, srcsdl.surface->h, 32, srcsdl.rmask, srcsdl.gmask, srcsdl.bmask, 0); /* no amask, as we set overall alpha! */
65 if (!rect)
66 throw std::runtime_error(std::string("could not create surface: ")+SDL_GetError());
67 SDL_FillRect(rect, NULL, SDL_MapRGB(rect->format, c.get_r(), c.get_g(), c.get_b()));
68 SDL_SetAlpha(rect, SDL_SRCALPHA, a); /* 50% alpha; nice choice. also sdl is rendering faster for the special value alpha=128 */
70 SDL_Surface *ret=SDL_CreateRGBSurface(0, srcsdl.surface->w, srcsdl.surface->h, 32, srcsdl.rmask, srcsdl.gmask, srcsdl.bmask, srcsdl.amask);
71 if (!ret)
72 throw std::runtime_error(std::string("could not create surface: ")+SDL_GetError());
73 SDL_SetAlpha(srcsdl.surface, 0, 255);
74 SDL_BlitSurface(srcsdl.surface, NULL, ret, NULL);
75 SDL_BlitSurface(rect, NULL, ret, NULL);
76 SDL_FreeSurface(rect);
77 return new SDLPixbuf(ret);
80 SDLPixbuf *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);