Clear the screen to avoid garbage in unreachable areas after we reset the coordinate...
[supertux.git] / src / video / sdl / sdl_renderer.cpp
index ec15d30..61504e7 100644 (file)
@@ -21,6 +21,7 @@
 #include "video/drawing_request.hpp"
 #include "video/sdl/sdl_surface_data.hpp"
 #include "video/sdl/sdl_texture.hpp"
+#include "video/sdl/sdl_painter.hpp"
 
 #include <iomanip>
 #include <iostream>
 
 SDLRenderer::SDLRenderer() :
   window(),
-  renderer()
+  renderer(),
+  desktop_size()
 {
   Renderer::instance_ = this;
 
+  SDL_DisplayMode mode;
+  SDL_GetCurrentDisplayMode(0, &mode);
+  desktop_size = Size(mode.w, mode.h);
+
   log_info << "creating SDLRenderer" << std::endl;
   int width  = g_config->window_size.width;
   int height = g_config->window_size.height;
 
-  int flags = 0;
+  int flags = SDL_WINDOW_RESIZABLE;
   if(g_config->use_fullscreen)
   {
     flags |= SDL_WINDOW_FULLSCREEN;
@@ -47,6 +53,12 @@ SDLRenderer::SDLRenderer() :
     height = g_config->fullscreen_size.height;
   }
 
+  SCREEN_WIDTH = width;
+  SCREEN_HEIGHT = height;
+
+  PHYSICAL_SCREEN_WIDTH = width;
+  PHYSICAL_SCREEN_HEIGHT = height;
+
   int ret = SDL_CreateWindowAndRenderer(width, height, flags,
                                         &window, &renderer);
 
@@ -81,6 +93,8 @@ SDLRenderer::SDLRenderer() :
 
   if(texture_manager == 0)
     texture_manager = new TextureManager();
+
+  apply_config();
 }
 
 SDLRenderer::~SDLRenderer()
@@ -92,271 +106,31 @@ SDLRenderer::~SDLRenderer()
 void
 SDLRenderer::draw_surface(const DrawingRequest& request)
 {
-  //FIXME: support parameters request.angle, request.blend
-  const Surface* surface = (const Surface*) request.request_data;
-  boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->get_texture());
-
-  SDL_Rect dst_rect;
-  dst_rect.x = request.pos.x;
-  dst_rect.y = request.pos.y;
-  dst_rect.w = sdltexture->get_image_width();
-  dst_rect.h = sdltexture->get_image_height();
-
-  Uint8 r = static_cast<Uint8>(request.color.red * 255);
-  Uint8 g = static_cast<Uint8>(request.color.green * 255);
-  Uint8 b = static_cast<Uint8>(request.color.blue * 255);
-  Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
-  SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
-  SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
-
-  if (surface->get_flipx())
-  {
-    SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
-  }
-  else
-  {
-    switch(request.drawing_effect)
-    {
-      case VERTICAL_FLIP:
-        SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
-        break;
-
-      case HORIZONTAL_FLIP:
-        SDL_RenderCopyEx(renderer, sdltexture->get_texture(), NULL, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
-        break;
-
-      default:
-      case NO_EFFECT:
-        SDL_RenderCopy(renderer, sdltexture->get_texture(), NULL, &dst_rect);
-        break;
-    }
-  }
+  SDLPainter::draw_surface(renderer, request);
 }
 
 void
 SDLRenderer::draw_surface_part(const DrawingRequest& request)
 {
-  //FIXME: support parameters request.angle, request.blend
-  const SurfacePartRequest* surface = (const SurfacePartRequest*) request.request_data;
-  const SurfacePartRequest* surfacepartrequest = (SurfacePartRequest*) request.request_data;
-
-  boost::shared_ptr<SDLTexture> sdltexture = boost::dynamic_pointer_cast<SDLTexture>(surface->surface->get_texture());
-
-  SDL_Rect src_rect;
-  src_rect.x = surfacepartrequest->source.x;
-  src_rect.y = surfacepartrequest->source.y;
-  src_rect.w = surfacepartrequest->size.x;
-  src_rect.h = surfacepartrequest->size.y;
-
-  SDL_Rect dst_rect;
-  dst_rect.x = request.pos.x;
-  dst_rect.y = request.pos.y;
-  dst_rect.w = surfacepartrequest->size.x;
-  dst_rect.h = surfacepartrequest->size.y;
-
-  Uint8 r = static_cast<Uint8>(request.color.red * 255);
-  Uint8 g = static_cast<Uint8>(request.color.green * 255);
-  Uint8 b = static_cast<Uint8>(request.color.blue * 255);
-  Uint8 a = static_cast<Uint8>(request.color.alpha * request.alpha * 255);
-  SDL_SetTextureColorMod(sdltexture->get_texture(), r, g, b);
-  SDL_SetTextureAlphaMod(sdltexture->get_texture(), a);
-
-  if (surface->surface->get_flipx())
-  {
-    SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
-  }
-  else
-  {
-    switch(request.drawing_effect)
-    {
-      case VERTICAL_FLIP:
-        SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_VERTICAL);
-        break;
-
-      case HORIZONTAL_FLIP:
-        SDL_RenderCopyEx(renderer, sdltexture->get_texture(), &src_rect, &dst_rect, 0, NULL, SDL_FLIP_HORIZONTAL);
-        break;
-
-      default:
-      case NO_EFFECT:
-        SDL_RenderCopy(renderer, sdltexture->get_texture(), &src_rect, &dst_rect);
-        break;
-    }
-  }
+  SDLPainter::draw_surface_part(renderer, request);
 }
 
 void
 SDLRenderer::draw_gradient(const DrawingRequest& request)
 {
-  const GradientRequest* gradientrequest 
-    = (GradientRequest*) request.request_data;
-  const Color& top = gradientrequest->top;
-  const Color& bottom = gradientrequest->bottom;
-
-  int w;
-  int h;
-  SDL_GetWindowSize(window, &w, &h);
-
-  // calculate the maximum number of steps needed for the gradient
-  int n = static_cast<int>(std::max(std::max(fabsf(top.red - bottom.red),
-                                             fabsf(top.green - bottom.green)),
-                                    std::max(fabsf(top.blue - bottom.blue),
-                                             fabsf(top.alpha - bottom.alpha))) * 255);
-  for(int i = 0; i < n; ++i)
-  {
-    SDL_Rect rect;
-    rect.x = 0;
-    rect.y = h * i / n;
-    rect.w = w;
-    rect.h = (h * (i+1) / n) - rect.y;
-
-    float p = static_cast<float>(i+1) / static_cast<float>(n);
-    Uint8 r = static_cast<Uint8>(((1.0f - p) * top.red + p * bottom.red)  * 255);
-    Uint8 g = static_cast<Uint8>(((1.0f - p) * top.green + p * bottom.green) * 255);
-    Uint8 b = static_cast<Uint8>(((1.0f - p) * top.blue + p * bottom.blue) * 255);
-    Uint8 a = static_cast<Uint8>(((1.0f - p) * top.alpha + p * bottom.alpha) * 255);
-
-    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
-    SDL_SetRenderDrawColor(renderer, r, g, b, a);
-    SDL_RenderFillRect(renderer, &rect);
-  }
+  SDLPainter::draw_gradient(renderer, request);
 }
 
 void
 SDLRenderer::draw_filled_rect(const DrawingRequest& request)
 {
-  const FillRectRequest* fillrectrequest
-    = (FillRectRequest*) request.request_data;
-
-  SDL_Rect rect;
-  rect.x = request.pos.x;
-  rect.y = request.pos.y;
-  rect.w = fillrectrequest->size.x;
-  rect.h = fillrectrequest->size.y;
-
-  Uint8 r = static_cast<Uint8>(fillrectrequest->color.red * 255);
-  Uint8 g = static_cast<Uint8>(fillrectrequest->color.green * 255);
-  Uint8 b = static_cast<Uint8>(fillrectrequest->color.blue * 255);
-  Uint8 a = static_cast<Uint8>(fillrectrequest->color.alpha * 255);
-
-  int radius = std::min(std::min(rect.h / 2, rect.w / 2),
-                        static_cast<int>(fillrectrequest->radius));
-
-  if (radius)
-  {
-    int slices = radius;
-
-    // rounded top and bottom parts
-    std::vector<SDL_Rect> rects;
-    rects.reserve(2*slices + 1);
-    for(int i = 0; i < slices; ++i)
-    {
-      float p = (static_cast<float>(i) + 0.5f) / static_cast<float>(slices);
-      int xoff = radius - static_cast<int>(sqrtf(1.0f - p*p) * radius);
-
-      SDL_Rect tmp;
-      tmp.x = rect.x + xoff;
-      tmp.y = rect.y + (radius - i);
-      tmp.w = rect.w - 2*(xoff);
-      tmp.h = 1;
-      rects.push_back(tmp);
-
-      SDL_Rect tmp2;
-      tmp2.x = rect.x + xoff;
-      tmp2.y = rect.y + rect.h - radius + i;
-      tmp2.w = rect.w - 2*xoff;
-      tmp2.h = 1;
-
-      if (tmp2.y != tmp.y)
-      {
-        rects.push_back(tmp2);
-      }
-    }
-
-    if (2*radius < rect.h)
-    {
-      // center rectangle
-      SDL_Rect tmp;
-      tmp.x = rect.x;
-      tmp.y = rect.y + radius + 1;
-      tmp.w = rect.w;
-      tmp.h = rect.h - 2*radius - 1;
-      rects.push_back(tmp);
-    }
-
-    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
-    SDL_SetRenderDrawColor(renderer, r, g, b, a);
-    SDL_RenderFillRects(renderer, &*rects.begin(), rects.size());
-  }
-  else
-  {
-    if((rect.w != 0) && (rect.h != 0))
-    {
-      SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
-      SDL_SetRenderDrawColor(renderer, r, g, b, a);
-      SDL_RenderFillRect(renderer, &rect);
-    }
-  }
+  SDLPainter::draw_filled_rect(renderer, request);
 }
 
 void
 SDLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
 {
-  const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
-
-  int window_w;
-  int window_h;
-  SDL_GetWindowSize(window, &window_w, &window_h);
-
-  float x = request.pos.x;
-  float w = ellipse->size.x;
-  float h = ellipse->size.y;
-
-  int top = request.pos.y - (h / 2);
-
-  const int max_slices = 256;
-  SDL_Rect rects[2*max_slices+2];
-  int slices = std::min(static_cast<int>(ellipse->size.y), max_slices);
-  for(int i = 0; i < slices; ++i)
-  {
-    float p = ((static_cast<float>(i) + 0.5f) / static_cast<float>(slices)) * 2.0f - 1.0f; 
-    int xoff = static_cast<int>(sqrtf(1.0f - p*p) * w / 2);
-
-    SDL_Rect& left  = rects[2*i+0];
-    SDL_Rect& right = rects[2*i+1];
-
-    left.x = 0;
-    left.y = top + (i * h / slices);
-    left.w = x - xoff;
-    left.h = (top + ((i+1) * h / slices)) - left.y;
-
-    right.x = x + xoff;
-    right.y = left.y;
-    right.w = window_w - right.x;
-    right.h = left.h;
-  }
-
-  SDL_Rect& top_rect = rects[2*slices+0];
-  SDL_Rect& bottom_rect = rects[2*slices+1];
-
-  top_rect.x = 0;
-  top_rect.y = 0;
-  top_rect.w = window_w;
-  top_rect.h = top;
-
-  bottom_rect.x = 0;
-  bottom_rect.y = top + h;
-  bottom_rect.w = window_w;
-  bottom_rect.h = window_h - bottom_rect.y;
-
-  Uint8 r = static_cast<Uint8>(ellipse->color.red * 255);
-  Uint8 g = static_cast<Uint8>(ellipse->color.green * 255);
-  Uint8 b = static_cast<Uint8>(ellipse->color.blue * 255);
-  Uint8 a = static_cast<Uint8>(ellipse->color.alpha * 255);
-
-  SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
-  SDL_SetRenderDrawColor(renderer, r, g, b, a);
-  SDL_RenderFillRects(renderer, rects, 2*slices+2);
+  SDLPainter::draw_inverse_ellipse(renderer, request);
 }
 
 void 
@@ -432,9 +206,143 @@ SDLRenderer::flip()
 }
 
 void
-SDLRenderer::resize(int, int)
+SDLRenderer::resize(int w , int h)
+{
+  g_config->window_size = Size(w, h);
+
+  PHYSICAL_SCREEN_WIDTH = w;
+  PHYSICAL_SCREEN_HEIGHT = h;
+
+  apply_config();
+}
+
+void
+SDLRenderer::apply_config()
 {
-    
+  if (false)
+  {
+    log_info << "Applying Config:" 
+             << "\n  Desktop: " << desktop_size.width << "x" << desktop_size.height
+             << "\n  Window:  " << g_config->window_size
+             << "\n  FullRes: " << g_config->fullscreen_size
+             << "\n  Aspect:  " << g_config->aspect_size
+             << "\n  Magnif:  " << g_config->magnification
+             << std::endl;
+  }
+
+  float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
+  if (g_config->aspect_size != Size(0, 0))
+  {
+    target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
+  }
+
+  float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
+  if (desktop_size.width != -1 && desktop_size.height != -1)
+  {
+    desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
+  }
+
+  Size screen_size;
+
+  // Get the screen width
+  if (g_config->use_fullscreen)
+  {
+    screen_size = g_config->fullscreen_size;
+    desktop_aspect = float(screen_size.width) / float(screen_size.height);
+  }
+  else
+  {
+    screen_size = g_config->window_size;
+  }
+
+  //apply_video_mode(screen_size, g_config->use_fullscreen);
+
+  if (target_aspect > 1.0f)
+  {
+    SCREEN_WIDTH  = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
+    SCREEN_HEIGHT = static_cast<int>(screen_size.height);
+  }
+  else
+  {
+    SCREEN_WIDTH  = static_cast<int>(screen_size.width);
+    SCREEN_HEIGHT = static_cast<int>(screen_size.height  * (target_aspect / desktop_aspect));
+  }
+
+  Size max_size(1280, 800);
+  Size min_size(640, 480);
+
+  if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
+  {
+    // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
+    // max_size.width/max_size.height resp. min_size.width/min_size.height
+    if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
+    {
+      float scale1  = float(max_size.width)/SCREEN_WIDTH;
+      float scale2  = float(max_size.height)/SCREEN_HEIGHT;
+      float scale   = (scale1 < scale2) ? scale1 : scale2;
+      SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  * scale);
+      SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
+    } 
+    else if (SCREEN_WIDTH < min_size.width || SCREEN_HEIGHT < min_size.height)
+    {
+      float scale1  = float(min_size.width)/SCREEN_WIDTH;
+      float scale2  = float(min_size.height)/SCREEN_HEIGHT;
+      float scale   = (scale1 < scale2) ? scale1 : scale2;
+      SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  * scale);
+      SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
+    }
+  }
+  else
+  {
+    SCREEN_WIDTH  = static_cast<int>(SCREEN_WIDTH  / g_config->magnification);
+    SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
+
+    // This works by adding black borders around the screen to limit
+    // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
+    Size new_size = screen_size;
+
+    if (SCREEN_WIDTH > max_size.width)
+    {
+      new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
+      SCREEN_WIDTH = static_cast<int>(max_size.width);
+    }
+
+    if (SCREEN_HEIGHT > max_size.height)
+    {
+      new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
+      SCREEN_HEIGHT = static_cast<int>(max_size.height);
+    }
+  }
+
+  // Clear the screen to avoid garbage in unreachable areas after we
+  // reset the coordinate system
+  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+  SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
+  SDL_RenderClear(renderer);
+  SDL_RenderPresent(renderer);
+  SDL_RenderClear(renderer);
+
+  // This doesn't really do what we want, as it sales the area to fill
+  // the screen, but seems to be the only way to reset the coordinate
+  // system and it's "close enough" to what we want, see:
+  // https://bugzilla.libsdl.org/show_bug.cgi?id=2179
+  SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
+}
+
+Vector
+SDLRenderer::to_logical(int physical_x, int physical_y, bool foobar)
+{
+  if (foobar)
+  {
+    // SDL translates coordinates automatically, except for SDL_GetMouseState(), thus foobar
+    return Vector(physical_x * float(SCREEN_WIDTH) / (PHYSICAL_SCREEN_WIDTH),
+                  physical_y * float(SCREEN_HEIGHT) / (PHYSICAL_SCREEN_HEIGHT));
+  }
+  else
+  {
+    // SDL is doing the translation internally, so we have nothing to do
+    return Vector(physical_x, physical_y);
+  }
 }
 
 void