4 * Created on: Aug 7, 2012
11 #include "../loader/Interpreter.h"
12 #include "../loader/TypeDescription.h"
14 using geometry::Vector;
15 using loader::FieldDescription;
16 using loader::Interpreter;
17 using loader::TypeDescription;
21 // TODO: maybe create a cache for frames?
22 void Frame::Draw(SDL_Surface *dest, const Vector<int> &position, int width, int height) const {
25 rect.x = position.X();
26 rect.y = position.Y();
29 SDL_FillRect(dest, &rect, SDL_MapRGB(dest->format, 0xFF, 0x00, 0x00));
34 srcRect.x = offset.X();
35 srcRect.y = offset.Y();
36 srcRect.w = BorderWidth();
37 srcRect.h = BorderHeight();
39 destRect.x = position.X();
40 destRect.y = position.Y();
41 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
44 Texture(surface, Vector<int>(RepeatWidth(), BorderHeight()), Vector<int>(offset.X() + BorderWidth(), offset.Y()))
45 .Render(dest, Vector<int>(position.X() + BorderWidth(), position.Y()), Vector<int>(position.X() + width - BorderWidth(), position.Y() + BorderHeight()));
48 srcRect.x = offset.X() + RepeatWidth() + BorderWidth();
49 srcRect.w = BorderWidth();
50 destRect.x = position.X() + width - BorderWidth();
51 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
54 Texture(surface, Vector<int>(BorderWidth(), RepeatHeight()), Vector<int>(offset.X(), offset.Y() + BorderHeight()))
55 .Render(dest, Vector<int>(position.X(), position.Y() + BorderHeight()), Vector<int>(position.X() + BorderWidth(), position.Y() + height - BorderHeight()));
58 Texture(surface, RepeatSize(), Vector<int>(offset.X() + BorderWidth(), offset.Y() + BorderHeight()))
59 .Render(dest, position + BorderSize(), position + Vector<int>(width, height) - BorderSize());
62 Texture(surface, Vector<int>(BorderWidth(), RepeatHeight()), Vector<int>(offset.X() + BorderWidth() + RepeatWidth(), offset.Y() + BorderHeight()))
63 .Render(dest, Vector<int>(position.X() + width - BorderWidth(), position.Y() + BorderHeight()), Vector<int>(position.X() + width, position.Y() + height - BorderHeight()));
66 srcRect.x = offset.X();
67 srcRect.y = offset.Y() + BorderHeight() + RepeatHeight();
68 srcRect.w = BorderWidth();
69 srcRect.h = BorderHeight();
70 destRect.x = position.X();
71 destRect.y = position.Y() + height - BorderHeight();
72 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
75 Texture(surface, Vector<int>(RepeatWidth(), BorderHeight()), Vector<int>(offset.X() + BorderWidth(), offset.Y() + BorderHeight() + RepeatHeight()))
76 .Render(dest, Vector<int>(position.X() + BorderWidth(), position.Y() + height - BorderHeight()), Vector<int>(position.X() + width - BorderWidth(), position.Y() + height));
78 // bottom-right corner
79 srcRect.x = offset.X() + BorderWidth() + RepeatWidth();
80 srcRect.w = BorderWidth();
81 destRect.x = position.X() + width - BorderWidth();
82 SDL_BlitSurface(surface, &srcRect, dest, &destRect);
86 void Frame::CreateTypeDescription() {
89 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Frame"));
91 "A frame is basically a border + a background texture.\n"
92 "It splits an image into 3*3 parts where the edges are kept as is and the sides and the middle part are repeated as needed.");
93 td.SetConstructor(&Construct);
94 td.SetSize(sizeof(Frame));
96 td.AddField("image", FieldDescription(((char *)&f.surface) - ((char *)&f), Interpreter::IMAGE_ID).SetReferenced().SetDescription("the underlying graphic from which the frame parts are cut"));
97 td.AddField("border", FieldDescription(((char *)&f.borderSize) - ((char *)&f), Interpreter::VECTOR_ID).SetDescription("size of the border part, dimensions of top-left corner"));
98 td.AddField("repeat", FieldDescription(((char *)&f.repeatSize) - ((char *)&f), Interpreter::VECTOR_ID).SetDescription("size of the repeat part, dimensions of a single tile of the background texture"));
99 td.AddField("offset", FieldDescription(((char *)&f.offset) - ((char *)&f), Interpreter::VECTOR_ID).SetDescription("offset into the image where to start cutting, coordinates of the top-left corner on the image"));
102 void Frame::Construct(void *data) {