#include "Frame.h"
+#include "../loader/TypeDescription.h"
+
using geometry::Vector;
+using loader::FieldDescription;
+using loader::TypeDescription;
namespace graphics {
// TODO: maybe create a cache for frames?
void Frame::Draw(SDL_Surface *dest, const Vector<int> &position, int width, int height) const {
+ if (!surface) {
+ SDL_Rect rect;
+ rect.x = position.X();
+ rect.y = position.Y();
+ rect.w = width;
+ rect.h = height;
+ SDL_FillRect(dest, &rect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
+ return;
+ }
// top-left corner
SDL_Rect srcRect;
- srcRect.x = xOffset;
- srcRect.y = yOffset;
- srcRect.w = borderWidth;
- srcRect.h = borderHeight;
+ srcRect.x = offset.X();
+ srcRect.y = offset.Y();
+ 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));
+ 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;
+ destRect.x += RepeatWidth();
+ repeatCursor += RepeatWidth();
}
// top-right corner
- srcRect.x += repeatWidth;
- srcRect.w = borderWidth;
+ srcRect.x += RepeatWidth();
+ srcRect.w = BorderWidth();
SDL_BlitSurface(surface, &srcRect, dest, &destRect);
// middle
- destRect.y += borderHeight;
- int fullRepeatHeight(height - (2 * borderHeight));
+ 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;
+ 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;
+ srcRect.x += BorderWidth();
+ srcRect.w = RepeatWidth();
+ destRect.x += BorderWidth();
while (repeatCursor < fullRepeatWidth) {
SDL_BlitSurface(surface, &srcRect, dest, &destRect);
- destRect.x += repeatWidth;
- repeatCursor += repeatWidth;
+ destRect.x += RepeatWidth();
+ repeatCursor += RepeatWidth();
}
// right border
- srcRect.x += repeatWidth;
- srcRect.w = borderWidth;
+ srcRect.x += RepeatWidth();
+ srcRect.w = BorderWidth();
SDL_BlitSurface(surface, &srcRect, dest, &destRect);
- destRect.y += repeatHeight;
- hRepeatCursor += repeatHeight;
+ destRect.y += RepeatHeight();
+ hRepeatCursor += RepeatHeight();
}
// bottom-left corner
- srcRect.x = xOffset;
- srcRect.y = yOffset + borderHeight + repeatHeight;
- srcRect.w = borderWidth;
- srcRect.h = borderHeight;
+ srcRect.x = offset.X();
+ srcRect.y = offset.Y() + 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;
+ 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;
+ destRect.x += RepeatWidth();
+ repeatCursor += RepeatWidth();
}
if (fullRepeatWidth < fullRepeatWidth) {
srcRect.w = fullRepeatWidth - fullRepeatWidth;
}
// bottom-right corner
- srcRect.x += repeatWidth;
- srcRect.w = borderWidth;
+ srcRect.x += RepeatWidth();
+ srcRect.w = BorderWidth();
SDL_BlitSurface(surface, &srcRect, dest, &destRect);
}
+
+void Frame::CreateTypeDescription() {
+ Frame f;
+
+ int imageId(TypeDescription::GetTypeId("Image"));
+ int vectorId(TypeDescription::GetTypeId("Vector"));
+
+ TypeDescription &td(TypeDescription::CreateOrGet("Frame"));
+ td.SetConstructor(&Construct);
+ td.SetSize(sizeof(Frame));
+
+ td.AddField("image", FieldDescription(((char *)&f.surface) - ((char *)&f), imageId, true));
+ td.AddField("border", FieldDescription(((char *)&f.borderSize) - ((char *)&f), vectorId, false));
+ td.AddField("repeat", FieldDescription(((char *)&f.repeatSize) - ((char *)&f), vectorId, false));
+ td.AddField("offset", FieldDescription(((char *)&f.offset) - ((char *)&f), vectorId, false));
+}
+
+void Frame::Construct(void *data) {
+ new (data) Frame;
+}
+
}