Added ugly workaround for Console crash at startup
[supertux.git] / src / video / sdl / sdl_renderer.cpp
index 654e729..2ed479e 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>
 #include <stdexcept>
 #include "SDL2/SDL_video.h"
 
+#include "video/util.hpp"
+
 SDLRenderer::SDLRenderer() :
-  window(),
-  renderer()
+  m_window(),
+  m_renderer(),
+  m_viewport(),
+  m_desktop_size(0, 0),
+  m_scale(1.0f, 1.0f)
 {
-  Renderer::instance_ = this;
+  SDL_DisplayMode mode;
+  if (SDL_GetDesktopDisplayMode(0, &mode) != 0)
+  {
+    log_warning << "Couldn't get desktop display mode: " << SDL_GetError() << std::endl;
+  }
+  else
+  {
+    m_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;
-    width  = g_config->fullscreen_size.width;
-    height = g_config->fullscreen_size.height;
+    if (g_config->fullscreen_size == Size(0, 0))
+    {
+      flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
+      width = g_config->window_size.width;
+      height = g_config->window_size.height;
+    }
+    else
+    {
+      flags |= SDL_WINDOW_FULLSCREEN;
+      width  = g_config->fullscreen_size.width;
+      height = g_config->fullscreen_size.height;
+    }
   }
 
+  SCREEN_WIDTH = width;
+  SCREEN_HEIGHT = height;
+
+  m_viewport.x = 0;
+  m_viewport.y = 0;
+  m_viewport.w = width;
+  m_viewport.h = height;
+
+  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2");
+
   int ret = SDL_CreateWindowAndRenderer(width, height, flags,
-                                        &window, &renderer);
+                                        &m_window, &m_renderer);
 
   if(ret != 0) {
     std::stringstream msg;
@@ -58,7 +91,7 @@ SDLRenderer::SDLRenderer() :
   }
 
   SDL_RendererInfo info;
-  if (SDL_GetRendererInfo(renderer, &info) != 0)
+  if (SDL_GetRendererInfo(m_renderer, &info) != 0)
   {
     log_warning << "Couldn't get RendererInfo: " << SDL_GetError() << std::endl;
   }
@@ -66,10 +99,10 @@ SDLRenderer::SDLRenderer() :
   {
     log_info << "SDL_Renderer: " << info.name << std::endl;
     log_info << "SDL_RendererFlags: " << std::endl;
-    if (info.flags & SDL_RENDERER_SOFTWARE) log_info << "  SDL_RENDERER_SOFTWARE" << std::endl;
-    if (info.flags & SDL_RENDERER_ACCELERATED) log_info << "  SDL_RENDERER_ACCELERATED" << std::endl;
-    if (info.flags & SDL_RENDERER_PRESENTVSYNC) log_info << "  SDL_RENDERER_PRESENTVSYNC" << std::endl;
-    if (info.flags & SDL_RENDERER_TARGETTEXTURE) log_info << "  SDL_RENDERER_TARGETTEXTURE" << std::endl;
+    if (info.flags & SDL_RENDERER_SOFTWARE) { log_info << "  SDL_RENDERER_SOFTWARE" << std::endl; }
+    if (info.flags & SDL_RENDERER_ACCELERATED) { log_info << "  SDL_RENDERER_ACCELERATED" << std::endl; }
+    if (info.flags & SDL_RENDERER_PRESENTVSYNC) { log_info << "  SDL_RENDERER_PRESENTVSYNC" << std::endl; }
+    if (info.flags & SDL_RENDERER_TARGETTEXTURE) { log_info << "  SDL_RENDERER_TARGETTEXTURE" << std::endl; }
     log_info << "Texture Formats: " << std::endl;
     for(size_t i = 0; i < info.num_texture_formats; ++i)
     {
@@ -79,280 +112,64 @@ SDLRenderer::SDLRenderer() :
     log_info << "Max Texture Height: " << info.max_texture_height << std::endl;
   }
 
-  SDL_SetWindowTitle(window, "SuperTux");
-  if(texture_manager == 0)
-    texture_manager = new TextureManager();
+  g_config->window_size = Size(width, height);
+  apply_config();
 }
 
 SDLRenderer::~SDLRenderer()
 {
-  SDL_DestroyRenderer(renderer);
-  SDL_DestroyWindow(window);
+  SDL_DestroyRenderer(m_renderer);
+  SDL_DestroyWindow(m_window);
 }
 
 void
-SDLRenderer::draw_surface(const DrawingRequest& request)
+SDLRenderer::start_draw()
 {
-  //FIXME: support parameters request.alpha, 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_RenderSetScale(m_renderer, m_scale.x, m_scale.y);
+}
 
-  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();
+void
+SDLRenderer::end_draw()
+{
+}
 
-  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;
-    }
-  }
+void
+SDLRenderer::draw_surface(const DrawingRequest& request)
+{
+  SDLPainter::draw_surface(m_renderer, request);
 }
 
 void
 SDLRenderer::draw_surface_part(const DrawingRequest& request)
 {
-  //FIXME: support parameters request.alpha, 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;
-
-  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(m_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(m_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(m_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(m_renderer, request);
 }
 
-void 
+void
 SDLRenderer::do_take_screenshot()
 {
   // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
   int width;
   int height;
-  if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0)
+  if (SDL_GetRendererOutputSize(m_renderer, &width, &height) != 0)
   {
     log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl;
   }
@@ -377,7 +194,7 @@ SDLRenderer::do_take_screenshot()
     }
     else
     {
-      int ret = SDL_RenderReadPixels(renderer, NULL,
+      int ret = SDL_RenderReadPixels(m_renderer, NULL,
                                      SDL_PIXELFORMAT_ABGR8888,
                                      surface->pixels,
                                      surface->pitch);
@@ -415,13 +232,135 @@ SDLRenderer::do_take_screenshot()
 void
 SDLRenderer::flip()
 {
-  SDL_RenderPresent(renderer);
+  SDL_RenderPresent(m_renderer);
+}
+
+void
+SDLRenderer::resize(int w , int h)
+{
+  g_config->window_size = Size(w, h);
+
+  apply_config();
+}
+
+void
+SDLRenderer::apply_video_mode()
+{
+  if (!g_config->use_fullscreen)
+  {
+    SDL_SetWindowFullscreen(m_window, 0);
+  }
+  else
+  {
+    if (g_config->fullscreen_size.width == 0 &&
+        g_config->fullscreen_size.height == 0)
+    {
+        if (SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
+        {
+          log_warning << "failed to switch to desktop fullscreen mode: "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          log_info << "switched to desktop fullscreen mode" << std::endl;
+        }
+    }
+    else
+    {
+      SDL_DisplayMode mode;
+      mode.format = SDL_PIXELFORMAT_RGB888;
+      mode.w = g_config->fullscreen_size.width;
+      mode.h = g_config->fullscreen_size.height;
+      mode.refresh_rate = g_config->fullscreen_refresh_rate;
+      mode.driverdata = 0;
+
+      if (SDL_SetWindowDisplayMode(m_window, &mode) != 0)
+      {
+        log_warning << "failed to set display mode: "
+                    << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                    << SDL_GetError() << std::endl;
+      }
+      else
+      {
+        if (SDL_SetWindowFullscreen(m_window, SDL_WINDOW_FULLSCREEN) != 0)
+        {
+          log_warning << "failed to switch to fullscreen mode: "
+                      << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          log_info << "switched to fullscreen mode: "
+                   << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
+        }
+      }
+    }
+  }
+}
+
+void
+SDLRenderer::apply_viewport()
+{
+  Size target_size = (g_config->use_fullscreen && g_config->fullscreen_size != Size(0, 0)) ?
+    g_config->fullscreen_size :
+    g_config->window_size;
+
+  float pixel_aspect_ratio = 1.0f;
+  if (g_config->aspect_size != Size(0, 0))
+  {
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(m_desktop_size,
+                                                      g_config->aspect_size);
+  }
+  else if (g_config->use_fullscreen)
+  {
+    pixel_aspect_ratio = calculate_pixel_aspect_ratio(m_desktop_size,
+                                                      target_size);
+  }
+
+  // calculate the viewport
+  Size max_size(1280, 800);
+  Size min_size(640, 480);
+
+  Size logical_size;
+  calculate_viewport(min_size, max_size,
+                     target_size,
+                     pixel_aspect_ratio,
+                     g_config->magnification,
+                     m_scale, logical_size, m_viewport);
+
+  SCREEN_WIDTH = logical_size.width;
+  SCREEN_HEIGHT = logical_size.height;
+
+  if (m_viewport.x != 0 || m_viewport.y != 0)
+  {
+    // Clear the screen to avoid garbage in unreachable areas after we
+    // reset the coordinate system
+    SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255);
+    SDL_SetRenderDrawBlendMode(m_renderer, SDL_BLENDMODE_NONE);
+    SDL_RenderClear(m_renderer);
+    SDL_RenderPresent(m_renderer);
+    SDL_RenderClear(m_renderer);
+  }
+
+  // SetViewport() works in scaled screen coordinates, so we have to
+  // reset it to 1.0, 1.0 to get meaningful results
+  SDL_RenderSetScale(m_renderer, 1.0f, 1.0f);
+  SDL_RenderSetViewport(m_renderer, &m_viewport);
+  SDL_RenderSetScale(m_renderer, m_scale.x, m_scale.y);
 }
 
 void
-SDLRenderer::resize(int, int)
+SDLRenderer::apply_config()
+{
+  apply_video_mode();
+  apply_viewport();
+}
+
+Vector
+SDLRenderer::to_logical(int physical_x, int physical_y)
 {
-    
+  return Vector(static_cast<float>(physical_x - m_viewport.x) * SCREEN_WIDTH / m_viewport.w,
+                static_cast<float>(physical_y - m_viewport.y) * SCREEN_HEIGHT / m_viewport.h);
 }
 
 void
@@ -429,7 +368,7 @@ SDLRenderer::set_gamma(float gamma)
 {
   Uint16 ramp[256];
   SDL_CalculateGammaRamp(gamma, ramp);
-  SDL_SetWindowGammaRamp(window, ramp, ramp, ramp);
+  SDL_SetWindowGammaRamp(m_window, ramp, ramp, ramp);
 }
 
 /* EOF */