1 // Copyright Timothy Goya 2007.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include "Renderer.hpp"
9 #include <unison/video/Surface.hpp>
10 #include <unison/video/Window.hpp>
11 #include <unison/video/sdl/Blitters.hpp>
12 #include <unison/video/backend/Texture.hpp>
13 #include <unison/vfs/sdl/Utils.hpp>
18 #include "SDL_image.h"
36 SDL_InitSubSystem(SDL_INIT_VIDEO);
41 SDL_QuitSubSystem(SDL_INIT_VIDEO);
44 std::string Renderer::get_name()
49 bool Renderer::is_usable()
51 SDL_InitSubSystem(SDL_INIT_VIDEO);
53 if(!SDL_ListModes(0, SDL_SWSURFACE))
55 SDL_QuitSubSystem(SDL_INIT_VIDEO);
59 /*const SDL_VideoInfo *info = SDL_GetVideoInfo();
60 if(SDL_VideoModeOK(info->current_w, info->current_h, 0, SDL_SWSURFACE | SDL_FULLSCREEN))
62 SDL_QuitSubSystem(SDL_INIT_VIDEO);
66 SDL_QuitSubSystem(SDL_INIT_VIDEO);
71 Surface Renderer::load_surface(const std::string &filename)
73 SDL_Surface *image = IMG_Load_RW(VFS::SDL::Utils::open_physfs_in(filename), 1);
75 Surface surface = Surface(Area(image->w, image->h));
76 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
78 SDL::Blitters::blit_blend_none(image, Rect(), sdl_surface, Point());
79 SDL_FreeSurface(sdl_surface);
83 Surface Renderer::load_surface(const std::string &filename, const Color &colorkey)
85 SDL_Surface *image = IMG_Load_RW(VFS::SDL::Utils::open_physfs_in(filename), 1);
87 Surface surface = Surface(Area(image->w, image->h));
88 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
90 SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, colorkey.red, colorkey.blue, colorkey.alpha));
91 SDL::Blitters::blit_blend_none(image, Rect(), sdl_surface, Point());
92 SDL_FreeSurface(sdl_surface);
96 void Renderer::save_surface(const Surface &surface, const std::string &filename)
98 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
100 SDL_SaveBMP(sdl_surface, filename.c_str());
101 SDL_FreeSurface(sdl_surface);
104 void Renderer::blit(const Surface &src, const Rect &src_rect, Surface &dst, const Point &dst_pos, const RenderOptions &options)
107 if(src_rect.pos == Point() && (src_rect.size == Area() || src_rect.size == src.get_size()))
113 fragment = Surface(src_rect.size);
114 Video::Blitters::blit_blend_none(src, src_rect, fragment, Point());
118 fragment = fragment.h_flip();
122 fragment = fragment.v_flip();
124 SDL_Surface *src_surface = Blitters::create_sdl_surface_from(fragment.modulate(options.color).modulate(options.alpha));
127 SDL_Surface *dst_surface = Blitters::create_sdl_surface_from(dst);
131 Blitters::blit_blend(src_surface, Rect(Point(), fragment.get_size()), dst_surface, dst_pos, options.blend);
133 SDL_FreeSurface(dst_surface);
134 SDL_FreeSurface(src_surface);
137 void Renderer::blit(Backend::Texture *src, const Rect &src_rect, Surface &dst, const Point &dst_pos, const RenderOptions &options)
139 blit(src->get_surface(), src_rect, dst, dst_pos, options);
142 void Renderer::fill(Surface &dst, const Color &color, const Rect &rect)
144 SDL_Surface *dst_surface = Blitters::create_sdl_surface_from(dst);
147 Uint32 mapped = SDL_MapRGBA(dst_surface->format, color.red, color.green, color.blue, color.alpha);
148 SDL_FillRect(dst_surface, &rect, mapped);
151 void Renderer::fill_blend(Surface &dst, const Color &color, const Rect &rect)
153 SDL_Surface *dst_surface = Blitters::create_sdl_surface_from(dst);
156 Uint32 mapped = SDL_MapRGBA(dst_surface->format, color.red, color.green, color.blue, color.alpha);
157 if(color.alpha == 0xff)
159 SDL_FillRect(dst_surface, &rect, mapped);
161 else if(color.alpha != 0x00)
163 SDL_Surface *temp = SDL_CreateRGBSurface(dst_surface->flags, rect.size.x, rect.size.y, dst_surface->format->BitsPerPixel, dst_surface->format->Rmask, dst_surface->format->Gmask, dst_surface->format->Bmask, dst_surface->format->Amask);
165 SDL_FillRect(temp, 0, mapped);
166 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, color.alpha);
167 SDL_BlitSurface(temp, 0, dst_surface, &rect);
168 SDL_FreeSurface(temp);
172 Backend::Window *Renderer::create_window(const Area &size, const Area &logical_size, bool fullscreen)
174 return new Window(size, logical_size, fullscreen);
177 Backend::Texture *Renderer::create_texture(const Surface &surface)
179 return new Texture(surface);