+/*
+ * Frame.cpp
+ *
+ * Created on: Aug 7, 2012
+ * Author: holy
+ */
+
+#include "Frame.h"
+
+using geometry::Point;
+
+namespace graphics {
+
+// TODO: maybe create a cache for frames?
+void Frame::Draw(SDL_Surface *dest, Point<int> position, int width, int height) const {
+ // top-left corner
+ SDL_Rect srcRect;
+ srcRect.x = xOffset;
+ srcRect.y = yOffset;
+ srcRect.w = borderWidth;
+ srcRect.h = borderHeight;
+ SDL_Rect destRect;
+ destRect.x = position.X();
+ destRect.y = position.Y();
+ 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;
+ }
+
+ // top-right corner
+ srcRect.x += repeatWidth;
+ srcRect.w = 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 = xOffset;
+ srcRect.y = yOffset + 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;
+ }
+
+ // bottom-left corner
+ srcRect.x = xOffset;
+ srcRect.y = yOffset + borderHeight + repeatHeight;
+ srcRect.w = borderWidth;
+ srcRect.h = borderHeight;
+ destRect.x = position.X();
+ 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;
+ }
+
+ // bottom-right corner
+ srcRect.x += repeatWidth;
+ srcRect.w = borderWidth;
+ SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+}
+
+}