SDL::Renderer Class Reference

#include <sdl_renderer.hpp>

Inherits Renderer.

List of all members.

Public Member Functions

 Renderer ()
 ~Renderer ()
void draw_surface (const DrawingRequest &request)
void draw_surface_part (const DrawingRequest &request)
void draw_text (const DrawingRequest &request)
void draw_gradient (const DrawingRequest &request)
void draw_filled_rect (const DrawingRequest &request)
void draw_inverse_ellipse (const DrawingRequest &request)
void do_take_screenshot ()
void flip ()
void resize (int w, int h)
void apply_config ()

Private Attributes

SDL_Surface * screen
int numerator
int denominator


Detailed Description

Definition at line 28 of file sdl_renderer.hpp.


Constructor & Destructor Documentation

Renderer::Renderer (  ) 

Reimplemented from Renderer.

Definition at line 131 of file sdl_renderer.cpp.

References config, Renderer::instance_, log_info, screen, texture_manager, and Config::use_fullscreen.

00132   {
00133     ::Renderer::instance_ = this;
00134 
00135     const SDL_VideoInfo *info = SDL_GetVideoInfo();
00136     log_info << "Hardware surfaces are " << (info->hw_available ? "" : "not ") << "available." << std::endl;
00137     log_info << "Hardware to hardware blits are " << (info->blit_hw ? "" : "not ") << "accelerated." << std::endl;
00138     log_info << "Hardware to hardware blits with colorkey are " << (info->blit_hw_CC ? "" : "not ") << "accelerated." << std::endl;
00139     log_info << "Hardware to hardware blits with alpha are " << (info->blit_hw_A ? "" : "not ") << "accelerated." << std::endl;
00140     log_info << "Software to hardware blits are " << (info->blit_sw ? "" : "not ") << "accelerated." << std::endl;
00141     log_info << "Software to hardware blits with colorkey are " << (info->blit_sw_CC ? "" : "not ") << "accelerated." << std::endl;
00142     log_info << "Software to hardware blits with alpha are " << (info->blit_sw_A ? "" : "not ") << "accelerated." << std::endl;
00143     log_info << "Color fills are " << (info->blit_fill ? "" : "not ") << "accelerated." << std::endl;
00144 
00145     int flags = SDL_SWSURFACE | SDL_ANYFORMAT;
00146     if(config->use_fullscreen)
00147       flags |= SDL_FULLSCREEN;
00148     
00149     int width  = 800; //FIXME: config->screenwidth;
00150     int height = 600; //FIXME: config->screenheight;
00151 
00152     screen = SDL_SetVideoMode(width, height, 0, flags);
00153     if(screen == 0) {
00154       std::stringstream msg;
00155       msg << "Couldn't set video mode (" << width << "x" << height
00156           << "): " << SDL_GetError();
00157       throw std::runtime_error(msg.str());
00158     }
00159 
00160     numerator   = 1;
00161     denominator = 1;
00162     /* FIXME: 
00163     float xfactor = (float) config->screenwidth / SCREEN_WIDTH;
00164     float yfactor = (float) config->screenheight / SCREEN_HEIGHT;
00165     if(xfactor < yfactor)
00166     {
00167       numerator = config->screenwidth;
00168       denominator = SCREEN_WIDTH;
00169     }
00170     else
00171     {
00172       numerator = config->screenheight;
00173       denominator = SCREEN_HEIGHT;
00174     }
00175     */
00176     if(texture_manager == 0)
00177       texture_manager = new TextureManager();
00178   }

Renderer::~Renderer (  )  [virtual]

Reimplemented from Renderer.

Definition at line 180 of file sdl_renderer.cpp.

00181   {
00182   }


Member Function Documentation

void Renderer::draw_surface ( const DrawingRequest request  )  [virtual]

Implements Renderer.

Definition at line 185 of file sdl_renderer.cpp.

References DrawingRequest::alpha, DrawingRequest::color, DrawingRequest::drawing_effect, Surface::get_flipx(), SDL::SurfaceData::get_src_rect(), Surface::get_surface_data(), Surface::get_texture(), HORIZONTAL_FLIP, DrawingRequest::pos, DrawingRequest::request_data, screen, Vector::x, and Vector::y.

00186   {
00187     //FIXME: support parameters request.alpha, request.angle, request.blend
00188     const Surface* surface = (const Surface*) request.request_data;
00189     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
00190     SDL::SurfaceData *surface_data = reinterpret_cast<SDL::SurfaceData *>(surface->get_surface_data());
00191 
00192     DrawingEffect effect = request.drawing_effect;
00193     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
00194 
00195     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
00196 
00197     // get and check SDL_Surface
00198     if (transform == 0) {
00199       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
00200       return;
00201     }
00202 
00203     SDL_Rect *src_rect = surface_data->get_src_rect(effect);
00204     SDL_Rect dst_rect;
00205     dst_rect.x = (int) request.pos.x * numerator / denominator;
00206     dst_rect.y = (int) request.pos.y * numerator / denominator;
00207 
00208     Uint8 alpha = 0;
00209     if(request.alpha != 1.0)
00210     {
00211       if(!transform->format->Amask)
00212       {
00213         if(transform->flags & SDL_SRCALPHA)
00214         {
00215           alpha = transform->format->alpha;
00216         }
00217         else
00218         {
00219           alpha = 255;
00220         }
00221         SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
00222       }
00223       /*else
00224       {
00225         transform = apply_alpha(transform, request.alpha);
00226       }*/
00227     }
00228 
00229     SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
00230 
00231     if(request.alpha != 1.0)
00232     {
00233       if(!transform->format->Amask)
00234       {
00235         if(alpha == 255)
00236         {
00237           SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
00238         }
00239         else
00240         {
00241           SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
00242         }
00243       }
00244       /*else
00245       {
00246         SDL_FreeSurface(transform);
00247       }*/
00248     }
00249   }

void Renderer::draw_surface_part ( const DrawingRequest request  )  [virtual]

Implements Renderer.

Definition at line 252 of file sdl_renderer.cpp.

References DrawingRequest::alpha, DrawingRequest::color, DrawingRequest::drawing_effect, HORIZONTAL_FLIP, DrawingRequest::pos, DrawingRequest::request_data, screen, SurfacePartRequest::size, SurfacePartRequest::source, SurfacePartRequest::surface, VERTICAL_FLIP, Vector::x, and Vector::y.

00253   {
00254     const SurfacePartRequest* surfacepartrequest
00255       = (SurfacePartRequest*) request.request_data;
00256 
00257     const Surface* surface = surfacepartrequest->surface;
00258     SDL::Texture *sdltexture = dynamic_cast<SDL::Texture *>(surface->get_texture());
00259 
00260     DrawingEffect effect = request.drawing_effect;
00261     if (surface->get_flipx()) effect = HORIZONTAL_FLIP;
00262 
00263     SDL_Surface *transform = sdltexture->get_transform(request.color, effect);
00264 
00265     // get and check SDL_Surface
00266     if (transform == 0) {
00267       std::cerr << "Warning: Tried to draw NULL surface, skipped draw" << std::endl;
00268       return;
00269     }
00270 
00271     int ox, oy;
00272     if (effect == HORIZONTAL_FLIP)
00273     {
00274       ox = sdltexture->get_texture_width() - surface->get_x() - (int) surfacepartrequest->size.x;
00275     }
00276     else
00277     {
00278       ox = surface->get_x();
00279     }
00280     if (effect == VERTICAL_FLIP)
00281     {
00282       oy = sdltexture->get_texture_height() - surface->get_y() - (int) surfacepartrequest->size.y;
00283     }
00284     else
00285     {
00286       oy = surface->get_y();
00287     }
00288 
00289     SDL_Rect src_rect;
00290     src_rect.x = (ox + (int) surfacepartrequest->source.x) * numerator / denominator;
00291     src_rect.y = (oy + (int) surfacepartrequest->source.y) * numerator / denominator;
00292     src_rect.w = (int) surfacepartrequest->size.x * numerator / denominator;
00293     src_rect.h = (int) surfacepartrequest->size.y * numerator / denominator;
00294 
00295     SDL_Rect dst_rect;
00296     dst_rect.x = (int) request.pos.x * numerator / denominator;
00297     dst_rect.y = (int) request.pos.y * numerator / denominator;
00298 
00299     Uint8 alpha = 0;
00300     if(request.alpha != 1.0)
00301     {
00302       if(!transform->format->Amask)
00303       {
00304         if(transform->flags & SDL_SRCALPHA)
00305         {
00306           alpha = transform->format->alpha;
00307         }
00308         else
00309         {
00310           alpha = 255;
00311         }
00312         SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
00313       }
00314       /*else
00315       {
00316         transform = apply_alpha(transform, request.alpha);
00317       }*/
00318     }
00319 
00320     SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
00321 
00322     if(request.alpha != 1.0)
00323     {
00324       if(!transform->format->Amask)
00325       {
00326         if(alpha == 255)
00327         {
00328           SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
00329         }
00330         else
00331         {
00332           SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
00333         }
00334       }
00335       /*else
00336       {
00337         SDL_FreeSurface(transform);
00338       }*/
00339     }
00340   }

void SDL::Renderer::draw_text ( const DrawingRequest request  ) 

void Renderer::draw_gradient ( const DrawingRequest request  )  [virtual]

Implements Renderer.

Definition at line 343 of file sdl_renderer.cpp.

References GradientRequest::bottom, DrawingRequest::request_data, screen, and GradientRequest::top.

00344   {
00345     const GradientRequest* gradientrequest 
00346       = (GradientRequest*) request.request_data;
00347     const Color& top = gradientrequest->top;
00348     const Color& bottom = gradientrequest->bottom;
00349 
00350     for(int y = 0;y < screen->h;++y)
00351     {
00352       Uint8 r = (Uint8)((((float)(top.red-bottom.red)/(0-screen->h)) * y + top.red) * 255);
00353       Uint8 g = (Uint8)((((float)(top.green-bottom.green)/(0-screen->h)) * y + top.green) * 255);
00354       Uint8 b = (Uint8)((((float)(top.blue-bottom.blue)/(0-screen->h)) * y + top.blue) * 255);
00355       Uint8 a = (Uint8)((((float)(top.alpha-bottom.alpha)/(0-screen->h)) * y + top.alpha) * 255);
00356       Uint32 color = SDL_MapRGB(screen->format, r, g, b);
00357 
00358       SDL_Rect rect;
00359       rect.x = 0;
00360       rect.y = y;
00361       rect.w = screen->w;
00362       rect.h = 1;
00363 
00364       if(a == SDL_ALPHA_OPAQUE) {
00365         SDL_FillRect(screen, &rect, color);
00366       } else if(a != SDL_ALPHA_TRANSPARENT) {
00367         SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
00368 
00369         SDL_FillRect(temp, 0, color);
00370         SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
00371         SDL_BlitSurface(temp, 0, screen, &rect);
00372         SDL_FreeSurface(temp);
00373       }
00374     }
00375   }

void Renderer::draw_filled_rect ( const DrawingRequest request  )  [virtual]

Implements Renderer.

Definition at line 378 of file sdl_renderer.cpp.

References Color::alpha, Color::blue, FillRectRequest::color, Color::green, DrawingRequest::pos, Color::red, DrawingRequest::request_data, screen, SCREEN_HEIGHT, SCREEN_WIDTH, FillRectRequest::size, Vector::x, and Vector::y.

00379   {
00380     const FillRectRequest* fillrectrequest
00381       = (FillRectRequest*) request.request_data;
00382 
00383     SDL_Rect rect;
00384     rect.x = (Sint16)request.pos.x * screen->w / SCREEN_WIDTH;
00385     rect.y = (Sint16)request.pos.y * screen->h / SCREEN_HEIGHT;
00386     rect.w = (Uint16)fillrectrequest->size.x * screen->w / SCREEN_WIDTH;
00387     rect.h = (Uint16)fillrectrequest->size.y * screen->h / SCREEN_HEIGHT;
00388     Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
00389     Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
00390     Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
00391     Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
00392     Uint32 color = SDL_MapRGB(screen->format, r, g, b);
00393     if(a == SDL_ALPHA_OPAQUE) {
00394       SDL_FillRect(screen, &rect, color);
00395     } else if(a != SDL_ALPHA_TRANSPARENT) {
00396       SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
00397 
00398       SDL_FillRect(temp, 0, color);
00399       SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
00400       SDL_BlitSurface(temp, 0, screen, &rect);
00401       SDL_FreeSurface(temp);
00402     }
00403   }

void Renderer::draw_inverse_ellipse ( const DrawingRequest request  )  [virtual]

Implements Renderer.

Definition at line 406 of file sdl_renderer.cpp.

00407   {
00408   }

void Renderer::do_take_screenshot (  )  [virtual]

Implements Renderer.

Definition at line 411 of file sdl_renderer.cpp.

References log_debug, log_warning, and screen.

00412   {
00413     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
00414 
00415     SDL_Surface *screen = SDL_GetVideoSurface();
00416 
00417     // save screenshot
00418     static const std::string writeDir = PHYSFS_getWriteDir();
00419     static const std::string dirSep = PHYSFS_getDirSeparator();
00420     static const std::string baseName = "screenshot";
00421     static const std::string fileExt = ".bmp";
00422     std::string fullFilename;
00423     for (int num = 0; num < 1000; num++) {
00424       std::ostringstream oss;
00425       oss << baseName;
00426       oss << std::setw(3) << std::setfill('0') << num;
00427       oss << fileExt;
00428       std::string fileName = oss.str();
00429       fullFilename = writeDir + dirSep + fileName;
00430       if (!PHYSFS_exists(fileName.c_str())) {
00431         SDL_SaveBMP(screen, fullFilename.c_str());
00432         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
00433         return;
00434       }
00435     }
00436     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
00437   }

void Renderer::flip (  )  [virtual]

Implements Renderer.

Definition at line 440 of file sdl_renderer.cpp.

References screen.

00441   {
00442     SDL_Flip(screen);
00443   }

void Renderer::resize ( int  w,
int  h 
) [virtual]

Implements Renderer.

Definition at line 446 of file sdl_renderer.cpp.

00447   {
00448     
00449   }

void SDL::Renderer::apply_config (  )  [inline, virtual]

Implements Renderer.

Definition at line 43 of file sdl_renderer.hpp.

00043 {}


Member Data Documentation

SDL_Surface* SDL::Renderer::screen [private]

Definition at line 45 of file sdl_renderer.hpp.

int SDL::Renderer::numerator [private]

Definition at line 46 of file sdl_renderer.hpp.

int SDL::Renderer::denominator [private]

Definition at line 46 of file sdl_renderer.hpp.


The documentation for this class was generated from the following files:
Generated on Mon Nov 16 03:38:18 2009 for SuperTux by  doxygen 1.5.1