Changed DrawingContext::draw_surface_part() to handle srcrect/dstrect, instead of...
authorIngo Ruhnke <grumbel@gmail.com>
Sat, 16 Aug 2014 19:54:16 +0000 (21:54 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Sat, 16 Aug 2014 19:54:16 +0000 (21:54 +0200)
src/sprite/sprite.cpp
src/video/drawing_context.cpp
src/video/drawing_context.hpp
src/video/drawing_request.hpp
src/video/font.cpp
src/video/gl/gl_lightmap.cpp
src/video/gl/gl_renderer.cpp
src/video/sdl/sdl_painter.cpp

index 11c819f..41c732f 100644 (file)
@@ -152,8 +152,10 @@ Sprite::draw_part(DrawingContext& context, const Vector& source,
   assert(action != 0);
   update();
 
-  context.draw_surface_part(action->surfaces[frameidx], source, size,
-                            pos - Vector(action->x_offset, action->y_offset),
+  context.draw_surface_part(action->surfaces[frameidx],
+                            Rectf(source, Sizef(size)),
+                            Rectf(pos - Vector(action->x_offset, action->y_offset),
+                                  action->surfaces[frameidx]->get_size()),
                             layer + action->z_order);
 }
 
index 57057b8..7ed0079 100644 (file)
@@ -113,8 +113,9 @@ DrawingContext::draw_surface(SurfacePtr surface, const Vector& position,
 }
 
 void
-DrawingContext::draw_surface_part(SurfacePtr surface, const Vector& source,
-                                  const Vector& size, const Vector& dest, int layer)
+DrawingContext::draw_surface_part(SurfacePtr surface,
+                                  const Rectf& srcrect, const Rectf& dstrect,
+                                  int layer)
 {
   assert(surface != 0);
 
@@ -122,31 +123,16 @@ DrawingContext::draw_surface_part(SurfacePtr surface, const Vector& source,
 
   request->target = target;
   request->type = SURFACE_PART;
-  request->pos = transform.apply(dest);
+  request->pos = transform.apply(dstrect.p1);
   request->layer = layer;
   request->drawing_effect = transform.drawing_effect;
   request->alpha = transform.alpha;
 
   SurfacePartRequest* surfacepartrequest = new(obst) SurfacePartRequest();
-  surfacepartrequest->size = size;
-  surfacepartrequest->source = source;
+  surfacepartrequest->srcrect = srcrect;
+  surfacepartrequest->dstsize = dstrect.get_size();
   surfacepartrequest->surface = surface.get();
 
-  // clip on screen borders
-  if(request->pos.x < 0) {
-    surfacepartrequest->size.x += request->pos.x;
-    if(surfacepartrequest->size.x <= 0)
-      return;
-    surfacepartrequest->source.x -= request->pos.x;
-    request->pos.x = 0;
-  }
-  if(request->pos.y < 0) {
-    surfacepartrequest->size.y += request->pos.y;
-    if(surfacepartrequest->size.y <= 0)
-      return;
-    surfacepartrequest->source.y -= request->pos.y;
-    request->pos.y = 0;
-  }
   request->request_data = surfacepartrequest;
 
   requests->push_back(request);
index 99982e3..cc2bc01 100644 (file)
@@ -99,8 +99,9 @@ public:
                     float angle, const Color& color, const Blend& blend,
                     int layer);
   /// Adds a drawing request for part of a surface.
-  void draw_surface_part(SurfacePtr surface, const Vector& source,
-                         const Vector& size, const Vector& dest, int layer);
+  void draw_surface_part(SurfacePtr surface,
+                         const Rectf& srcrect, const Rectf& dstrect,
+                         int layer);
   /// Draws a text.
   void draw_text(FontPtr font, const std::string& text,
                  const Vector& position, FontAlignment alignment, int layer, Color color = Color(1.0,1.0,1.0));
index 59b977b..60043c9 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <stdint.h>
 
+#include "math/rectf.hpp"
+#include "math/sizef.hpp"
 #include "math/vector.hpp"
 #include "video/color.hpp"
 #include "video/drawing_context.hpp"
@@ -59,13 +61,13 @@ struct SurfacePartRequest : public DrawingRequestData
 {
   SurfacePartRequest() :
     surface(),
-    source(),
-    size()
+    srcrect(),
+    dstsize()
   {}
 
   const Surface* surface;
-  Vector source;
-  Vector size;
+  Rectf srcrect;
+  Sizef dstsize;
 
 private:
   SurfacePartRequest(const SurfacePartRequest&) = delete;
index ed431e2..bc51c7e 100644 (file)
@@ -434,8 +434,8 @@ Font::draw_chars(Renderer *renderer, bool notshadow, const std::string& text,
       request.alpha = alpha;
 
       SurfacePartRequest surfacepartrequest;
-      surfacepartrequest.size = glyph.rect.p2 - glyph.rect.p1;
-      surfacepartrequest.source = glyph.rect.p1;
+      surfacepartrequest.srcrect = glyph.rect;
+      surfacepartrequest.dstsize = glyph.rect.get_size();
       surfacepartrequest.surface = notshadow ? glyph_surfaces[glyph.surface_idx].get() : shadow_surfaces[glyph.surface_idx].get();
 
       request.request_data = &surfacepartrequest;
index 48291a6..4104181 100644 (file)
@@ -179,15 +179,15 @@ GLLightmap::draw_surface_part(const DrawingRequest& request)
   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
 
-  float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
-  float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
-  float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
-  float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
+  float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
+  float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
+  float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
+  float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();
 
   glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
   intern_draw(request.pos.x, request.pos.y,
-              request.pos.x + surfacepartrequest->size.x,
-              request.pos.y + surfacepartrequest->size.y,
+              request.pos.x + surfacepartrequest->dstsize.width,
+              request.pos.y + surfacepartrequest->dstsize.height,
               uv_left,
               uv_top,
               uv_right,
index 82e9c1a..15fcafe 100644 (file)
@@ -158,10 +158,10 @@ GLRenderer::draw_surface_part(const DrawingRequest& request)
   float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
   float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
 
-  float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
-  float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
-  float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
-  float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
+  float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p1.x) / surface->get_width();
+  float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p1.y) / surface->get_height();
+  float uv_right = surface_data->get_uv_left() + (uv_width * surfacepartrequest->srcrect.p2.x) / surface->get_width();
+  float uv_bottom = surface_data->get_uv_top() + (uv_height * surfacepartrequest->srcrect.p2.y) / surface->get_height();
 
   GLuint th = gltexture->get_handle();
   if (th != last_texture) {
@@ -169,8 +169,8 @@ GLRenderer::draw_surface_part(const DrawingRequest& request)
     glBindTexture(GL_TEXTURE_2D, th);
   }
   intern_draw(request.pos.x, request.pos.y,
-              request.pos.x + surfacepartrequest->size.x,
-              request.pos.y + surfacepartrequest->size.y,
+              request.pos.x + surfacepartrequest->dstsize.width,
+              request.pos.y + surfacepartrequest->dstsize.height,
               uv_left,
               uv_top,
               uv_right,
index 25b8843..7f250bb 100644 (file)
@@ -99,16 +99,16 @@ SDLPainter::draw_surface_part(SDL_Renderer* renderer, const DrawingRequest& requ
   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;
+  src_rect.x = surfacepartrequest->srcrect.p1.x;
+  src_rect.y = surfacepartrequest->srcrect.p1.y;
+  src_rect.w = surfacepartrequest->srcrect.get_width();
+  src_rect.h = surfacepartrequest->srcrect.get_height();
 
   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;
+  dst_rect.w = surfacepartrequest->dstsize.width;
+  dst_rect.h = surfacepartrequest->dstsize.height;
 
   Uint8 r = static_cast<Uint8>(request.color.red * 255);
   Uint8 g = static_cast<Uint8>(request.color.green * 255);