From 7651db1476453d8d4162e8708265a36c1a3c0441 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Sun, 21 Oct 2012 20:00:47 +0200 Subject: [PATCH] changed Frame implementation --- src/graphics/Frame.cpp | 80 +++++++++++----------------------------- src/graphics/Texture.cpp | 9 ++++- src/graphics/Texture.h | 9 +++-- 3 files changed, 34 insertions(+), 64 deletions(-) diff --git a/src/graphics/Frame.cpp b/src/graphics/Frame.cpp index 46fa1b5..86b9cd2 100644 --- a/src/graphics/Frame.cpp +++ b/src/graphics/Frame.cpp @@ -7,6 +7,7 @@ #include "Frame.h" +#include "Texture.h" #include "../loader/Interpreter.h" #include "../loader/TypeDescription.h" @@ -40,55 +41,26 @@ void Frame::Draw(SDL_Surface *dest, const Vector &position, int width, int SDL_BlitSurface(surface, &srcRect, dest, &destRect); // top border - srcRect.x += BorderWidth(); - srcRect.w = RepeatWidth(); - destRect.x += BorderWidth(); - int fullRepeatWidth(width - (2 * BorderWidth())); - int repeatCursor(0); - while (repeatCursor < fullRepeatWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += RepeatWidth(); - repeatCursor += RepeatWidth(); - } + Texture(surface, Vector(RepeatWidth(), BorderHeight()), Vector(offset.X() + BorderWidth(), offset.Y())) + .Render(dest, Vector(position.X() + BorderWidth(), position.Y()), Vector(position.X() + width - BorderWidth(), position.Y() + BorderHeight())); // top-right corner - srcRect.x += RepeatWidth(); + srcRect.x = offset.X() + RepeatWidth() + BorderWidth(); srcRect.w = BorderWidth(); + destRect.x = position.X() + width - BorderWidth(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); - // middle - destRect.y += BorderHeight(); - int fullRepeatHeight(height - (2 * BorderHeight())); - int hRepeatCursor(0); - while (hRepeatCursor < fullRepeatHeight) { - - // left border - srcRect.x = offset.X(); - srcRect.y = offset.Y() + BorderHeight(); - srcRect.w = BorderWidth(); - srcRect.h = RepeatHeight(); - destRect.x = position.X(); - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - - // fill - repeatCursor = 0; - srcRect.x += BorderWidth(); - srcRect.w = RepeatWidth(); - destRect.x += BorderWidth(); - while (repeatCursor < fullRepeatWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += RepeatWidth(); - repeatCursor += RepeatWidth(); - } - - // right border - srcRect.x += RepeatWidth(); - srcRect.w = BorderWidth(); - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - - destRect.y += RepeatHeight(); - hRepeatCursor += RepeatHeight(); - } + // left border + Texture(surface, Vector(BorderWidth(), RepeatHeight()), Vector(offset.X(), offset.Y() + BorderHeight())) + .Render(dest, Vector(position.X(), position.Y() + BorderHeight()), Vector(position.X() + BorderWidth(), position.Y() + height - BorderHeight())); + + // center fill + Texture(surface, RepeatSize(), Vector(offset.X() + BorderWidth(), offset.Y() + BorderHeight())) + .Render(dest, position + BorderSize(), position + Vector(width, height) - BorderSize()); + + // right border + Texture(surface, Vector(BorderWidth(), RepeatHeight()), Vector(offset.X() + BorderWidth() + RepeatWidth(), offset.Y() + BorderHeight())) + .Render(dest, Vector(position.X() + width - BorderWidth(), position.Y() + BorderHeight()), Vector(position.X() + width, position.Y() + height - BorderHeight())); // bottom-left corner srcRect.x = offset.X(); @@ -96,27 +68,17 @@ void Frame::Draw(SDL_Surface *dest, const Vector &position, int width, int srcRect.w = BorderWidth(); srcRect.h = BorderHeight(); destRect.x = position.X(); + destRect.y = position.Y() + height - BorderHeight(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); // bottom border - srcRect.x += BorderWidth(); - srcRect.w = RepeatWidth(); - destRect.x += BorderWidth(); - repeatCursor = 0; - while (repeatCursor < fullRepeatWidth) { - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += RepeatWidth(); - repeatCursor += RepeatWidth(); - } - if (fullRepeatWidth < fullRepeatWidth) { - srcRect.w = fullRepeatWidth - fullRepeatWidth; - SDL_BlitSurface(surface, &srcRect, dest, &destRect); - destRect.x += fullRepeatWidth - fullRepeatWidth; - } + Texture(surface, Vector(RepeatWidth(), BorderHeight()), Vector(offset.X() + BorderWidth(), offset.Y() + BorderHeight() + RepeatHeight())) + .Render(dest, Vector(position.X() + BorderWidth(), position.Y() + height - BorderHeight()), Vector(position.X() + width - BorderWidth(), position.Y() + height)); // bottom-right corner - srcRect.x += RepeatWidth(); + srcRect.x = offset.X() + BorderWidth() + RepeatWidth(); srcRect.w = BorderWidth(); + destRect.x = position.X() + width - BorderWidth(); SDL_BlitSurface(surface, &srcRect, dest, &destRect); } diff --git a/src/graphics/Texture.cpp b/src/graphics/Texture.cpp index 6fc8f0e..2bb0e97 100644 --- a/src/graphics/Texture.cpp +++ b/src/graphics/Texture.cpp @@ -13,8 +13,13 @@ using geometry::Vector; namespace graphics { -Texture::Texture() -: surface(0) { +Texture::Texture( + SDL_Surface *surface, + const Vector &size, + const Vector &offset) +: surface(surface) +, size(size) +, offset(offset) { } diff --git a/src/graphics/Texture.h b/src/graphics/Texture.h index 3b6357e..ecb516a 100644 --- a/src/graphics/Texture.h +++ b/src/graphics/Texture.h @@ -17,7 +17,10 @@ namespace graphics { class Texture { public: - Texture(); + explicit Texture( + SDL_Surface *surface = 0, + const geometry::Vector &size = geometry::Vector(), + const geometry::Vector &offset = geometry::Vector()); ~Texture(); public: @@ -25,13 +28,13 @@ public: public: void SetSurface(SDL_Surface *s) { surface = s; } - void SetOffset(const geometry::Vector &o) { offset = o; } void SetSize(const geometry::Vector &s) { size = s; } + void SetOffset(const geometry::Vector &o) { offset = o; } private: SDL_Surface *surface; - geometry::Vector offset; geometry::Vector size; + geometry::Vector offset; }; -- 2.39.2