From: Ingo Ruhnke Date: Sat, 16 Aug 2014 19:54:16 +0000 (+0200) Subject: Changed DrawingContext::draw_surface_part() to handle srcrect/dstrect, instead of... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=634111449bef8b030090510475857adde9f3f87e;p=supertux.git Changed DrawingContext::draw_surface_part() to handle srcrect/dstrect, instead of just srcrect/dstpos, this allows surface scaling --- diff --git a/src/sprite/sprite.cpp b/src/sprite/sprite.cpp index 11c819fca..41c732fcd 100644 --- a/src/sprite/sprite.cpp +++ b/src/sprite/sprite.cpp @@ -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); } diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index 57057b843..7ed007934 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -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); diff --git a/src/video/drawing_context.hpp b/src/video/drawing_context.hpp index 99982e301..cc2bc0104 100644 --- a/src/video/drawing_context.hpp +++ b/src/video/drawing_context.hpp @@ -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)); diff --git a/src/video/drawing_request.hpp b/src/video/drawing_request.hpp index 59b977b26..60043c9a8 100644 --- a/src/video/drawing_request.hpp +++ b/src/video/drawing_request.hpp @@ -23,6 +23,8 @@ #include +#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; diff --git a/src/video/font.cpp b/src/video/font.cpp index ed431e2d0..bc51c7eee 100644 --- a/src/video/font.cpp +++ b/src/video/font.cpp @@ -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; diff --git a/src/video/gl/gl_lightmap.cpp b/src/video/gl/gl_lightmap.cpp index 48291a67e..41041817f 100644 --- a/src/video/gl/gl_lightmap.cpp +++ b/src/video/gl/gl_lightmap.cpp @@ -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, diff --git a/src/video/gl/gl_renderer.cpp b/src/video/gl/gl_renderer.cpp index 82e9c1a80..15fcafe58 100644 --- a/src/video/gl/gl_renderer.cpp +++ b/src/video/gl/gl_renderer.cpp @@ -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, diff --git a/src/video/sdl/sdl_painter.cpp b/src/video/sdl/sdl_painter.cpp index 25b884393..7f250bb1e 100644 --- a/src/video/sdl/sdl_painter.cpp +++ b/src/video/sdl/sdl_painter.cpp @@ -99,16 +99,16 @@ SDLPainter::draw_surface_part(SDL_Renderer* renderer, const DrawingRequest& requ boost::shared_ptr sdltexture = boost::dynamic_pointer_cast(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(request.color.red * 255); Uint8 g = static_cast(request.color.green * 255);