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);
43 SDL_QuitSubSystem(SDL_INIT_VIDEO);
46 std::string Renderer::get_name()
51 bool Renderer::is_usable()
53 SDL_InitSubSystem(SDL_INIT_VIDEO);
55 if(SDL_GL_LoadLibrary(0))
57 SDL_QuitSubSystem(SDL_INIT_VIDEO);
61 if(!SDL_ListModes(0, SDL_OPENGL))
63 SDL_QuitSubSystem(SDL_INIT_VIDEO);
67 /*const SDL_VideoInfo *info = SDL_GetVideoInfo();
68 if(SDL_VideoModeOK(info->current_w, info->current_h, 0, SDL_OPENGL | SDL_FULLSCREEN))
70 SDL_QuitSubSystem(SDL_INIT_VIDEO);
74 SDL_QuitSubSystem(SDL_INIT_VIDEO);
79 Surface Renderer::load_surface(const std::string &filename)
81 SDL_Surface *image = IMG_Load_RW(VFS::SDL::Utils::open_physfs_in(filename), 1);
83 Surface surface = Surface(Area(image->w, image->h));
84 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
86 SDL::Blitters::blit_blend_none(image, Rect(), sdl_surface, Point());
87 SDL_FreeSurface(sdl_surface);
91 Surface Renderer::load_surface(const std::string &filename, const Color &colorkey)
93 SDL_Surface *image = IMG_Load_RW(VFS::SDL::Utils::open_physfs_in(filename), 1);
95 Surface surface = Surface(Area(image->w, image->h));
96 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
98 SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, colorkey.red, colorkey.blue, colorkey.alpha));
99 SDL::Blitters::blit_blend_none(image, Rect(), sdl_surface, Point());
100 SDL_FreeSurface(sdl_surface);
104 void Renderer::save_surface(const Surface &surface, const std::string &filename)
106 SDL_Surface *sdl_surface = SDL::Blitters::create_sdl_surface_from(surface);
108 SDL_SaveBMP(sdl_surface, filename.c_str());
109 SDL_FreeSurface(sdl_surface);
112 void Renderer::blit(const Surface &src, const Rect &src_rect, Surface &dst, const Point &dst_pos, const RenderOptions &options)
115 if(src_rect.pos == Point() && (src_rect.size == Area() || src_rect.size == src.get_size()))
121 fragment = Surface(src_rect.size);
122 Blitters::blit_blend_none(src, src_rect, fragment, Point());
126 fragment = fragment.h_flip();
130 fragment = fragment.v_flip();
132 SDL_Surface *src_surface = SDL::Blitters::create_sdl_surface_from(fragment.modulate(options.color).modulate(options.alpha));
135 SDL_Surface *dst_surface = SDL::Blitters::create_sdl_surface_from(dst);
139 SDL::Blitters::blit_blend(src_surface, Rect(Point(), fragment.get_size()), dst_surface, dst_pos, options.blend);
141 SDL_FreeSurface(dst_surface);
142 SDL_FreeSurface(src_surface);
145 void Renderer::blit(Backend::Texture *src, const Rect &src_rect, Surface &dst, const Point &dst_pos, const RenderOptions &options)
148 assert(dst.get_size() != Area());
150 blit(src->get_surface(), src_rect, dst, dst_pos, options);
153 void Renderer::fill(Surface &dst, const Color &color, const Rect &rect)
155 SDL_Surface *dst_surface = SDL::Blitters::create_sdl_surface_from(dst);
158 Uint32 mapped = SDL_MapRGBA(dst_surface->format, color.red, color.green, color.blue, color.alpha);
159 SDL_FillRect(dst_surface, &rect, mapped);
162 void Renderer::fill_blend(Surface &dst, const Color &color, const Rect &rect)
164 SDL_Surface *dst_surface = SDL::Blitters::create_sdl_surface_from(dst);
167 Uint32 mapped = SDL_MapRGBA(dst_surface->format, color.red, color.green, color.blue, color.alpha);
168 if(color.alpha == 0xff)
170 SDL_FillRect(dst_surface, &rect, mapped);
172 else if(color.alpha != 0x00)
174 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);
176 SDL_FillRect(temp, 0, mapped);
177 SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, color.alpha);
178 SDL_BlitSurface(temp, 0, dst_surface, &rect);
179 SDL_FreeSurface(temp);
183 Backend::Window *Renderer::create_window(const Area &size, const Area &logical_size, bool fullscreen)
185 return new Window(size, logical_size, fullscreen);
188 Backend::Texture *Renderer::create_texture(const Surface &surface)
190 return new Texture(surface);