]> git.localhorst.tv Git - l2e.git/blobdiff - src/graphics/Sprite.cpp
reordered type description creation to avoid reallocation
[l2e.git] / src / graphics / Sprite.cpp
index afd6209546c3a6bdcb8f359f43e89dd8c75ec8c5..28d3b5c215c33a0459ad0963fe3f6393a0d3f9cb 100644 (file)
@@ -7,21 +7,52 @@
 
 #include "Sprite.h"
 
-using geometry::Point;
+#include "../loader/TypeDescription.h"
+
+using geometry::Vector;
+using loader::FieldDescription;
+using loader::TypeDescription;
 
 namespace graphics {
 
-void Sprite::Draw(SDL_Surface *dest, Point<int> position, int col, int row) const {
+void Sprite::Draw(SDL_Surface *dest, const Vector<int> &position, int col, int row) const {
        SDL_Rect srcRect, destRect;
-       srcRect.x = col * Width();
-       srcRect.y = row * Height();
+       srcRect.x = offset.X() + col * Width();
+       srcRect.y = offset.Y() + row * Height();
        srcRect.w = Width();
        srcRect.h = Height();
        destRect.x = position.X();
        destRect.y = position.Y();
-       destRect.w = Width();
-       destRect.h = Height();
-       SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+       if (surface) {
+               SDL_BlitSurface(surface, &srcRect, dest, &destRect);
+       } else {
+               destRect.w = Width();
+               destRect.h = Height();
+               bool red(true);
+               while (destRect.w > 1 && destRect.h > 1) {
+                       SDL_FillRect(dest, &destRect, SDL_MapRGB(dest->format, red ? 0xFF : 0, 0, 0));
+                       ++destRect.x;
+                       ++destRect.y;
+                       destRect.w -= 2;
+                       destRect.h -= 2;
+                       red = !red;
+               }
+       }
+}
+
+
+void Sprite::CreateTypeDescription() {
+       Sprite s;
+
+       int imageId(TypeDescription::GetTypeId("Image"));
+       int vectorId(TypeDescription::GetTypeId("Vector"));
+
+       TypeDescription &td(TypeDescription::CreateOrGet("Sprite"));
+       td.SetSize(sizeof(Sprite));
+
+       td.AddField("image", FieldDescription(((char *)&s.surface) - ((char *)&s), imageId, true));
+       td.AddField("size", FieldDescription(((char *)&s.size) - ((char *)&s), vectorId, false));
+       td.AddField("offset", FieldDescription(((char *)&s.offset) - ((char *)&s), vectorId, false));
 }
 
 }