]> git.localhorst.tv Git - l2e.git/blobdiff - src/graphics/Frame.cpp
removed stupid file headers that eclipse put in
[l2e.git] / src / graphics / Frame.cpp
index c682fa0f97ca7de495b04914b2c84c03d8c34fc9..dbfd814067c95668860d6e34107684a06636e748 100644 (file)
-/*
- * Frame.cpp
- *
- *  Created on: Aug 7, 2012
- *      Author: holy
- */
-
 #include "Frame.h"
 
-using geometry::Point;
+#include "Texture.h"
+#include "../loader/Interpreter.h"
+#include "../loader/TypeDescription.h"
+
+using geometry::Vector;
+using loader::FieldDescription;
+using loader::Interpreter;
+using loader::TypeDescription;
 
 namespace graphics {
 
 // TODO: maybe create a cache for frames?
-void Frame::Draw(SDL_Surface *dest, const Point<int> &position, int width, int height) const {
+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));
-       int repeatCursor(0);
-       while (repeatCursor < fullRepeatWidth) {
-               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
-               destRect.x += repeatWidth;
-               repeatCursor += repeatWidth;
-       }
+       Texture(surface, Vector<int>(RepeatWidth(), BorderHeight()), Vector<int>(offset.X() + BorderWidth(), offset.Y()))
+       .Render(dest, Vector<int>(position.X() + BorderWidth(), position.Y()), Vector<int>(position.X() + width - BorderWidth(), position.Y() + BorderHeight()));
 
        // top-right corner
-       srcRect.x += repeatWidth;
-       srcRect.w = borderWidth;
+       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 = 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;
-       }
+       // left border
+       Texture(surface, Vector<int>(BorderWidth(), RepeatHeight()), Vector<int>(offset.X(), offset.Y() + BorderHeight()))
+       .Render(dest, Vector<int>(position.X(), position.Y() + BorderHeight()), Vector<int>(position.X() + BorderWidth(), position.Y() + height - BorderHeight()));
+
+       // center fill
+       Texture(surface, RepeatSize(), Vector<int>(offset.X() + BorderWidth(), offset.Y() + BorderHeight()))
+       .Render(dest, position + BorderSize(), position + Vector<int>(width, height) - BorderSize());
+
+       // right border
+       Texture(surface, Vector<int>(BorderWidth(), RepeatHeight()), Vector<int>(offset.X() + BorderWidth() + RepeatWidth(), offset.Y() + BorderHeight()))
+       .Render(dest, Vector<int>(position.X() + width - BorderWidth(), position.Y() + BorderHeight()), Vector<int>(position.X() + width, position.Y() + height - BorderHeight()));
 
        // 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();
+       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<int>(RepeatWidth(), BorderHeight()), Vector<int>(offset.X() + BorderWidth(), offset.Y() + BorderHeight() + RepeatHeight()))
+       .Render(dest, Vector<int>(position.X() + BorderWidth(), position.Y() + height - BorderHeight()), Vector<int>(position.X() + width - BorderWidth(), position.Y() + height));
 
        // bottom-right corner
-       srcRect.x += repeatWidth;
-       srcRect.w = borderWidth;
+       srcRect.x = offset.X() + BorderWidth() + RepeatWidth();
+       srcRect.w = BorderWidth();
+       destRect.x = position.X() + width - BorderWidth();
        SDL_BlitSurface(surface, &srcRect, dest, &destRect);
 }
 
+
+void Frame::CreateTypeDescription() {
+       Frame f;
+
+       TypeDescription &td(TypeDescription::Create(TYPE_ID, "Frame"));
+       td.SetDescription(
+                       "A frame is basically a border + a background texture.\n"
+                       "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.");
+       td.SetConstructor(&Construct);
+       td.SetSize(sizeof(Frame));
+
+       td.AddField("image", FieldDescription(((char *)&f.surface) - ((char *)&f), Interpreter::IMAGE_ID).SetReferenced().SetDescription("the underlying graphic from which the frame parts are cut"));
+       td.AddField("border", FieldDescription(((char *)&f.borderSize) - ((char *)&f), Interpreter::VECTOR_ID).SetDescription("size of the border part, dimensions of top-left corner"));
+       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"));
+       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"));
+}
+
+void Frame::Construct(void *data) {
+       new (data) Frame;
+}
+
 }