diff --git a/src/sprite.cpp b/src/sprite.cpp index e69de29..8a6457e 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -0,0 +1,39 @@ +/* + * QmlArcade + * Copyright (C) 2010 Jared Adams + * + * This file is part of the QmlArcade. + * + * QmlArcade is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * QmlArcade is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with QmlArcade. If not, see . + */ + +#include "sprite.h" + +QML_DEFINE_TYPE(QmlArcade, 1,0, Sprite, Sprite); + +Sprite::Sprite(): + CoRenderer(QPointF(0, 0)) +{ +} + +Sprite::~Sprite() +{ +} + +void Sprite::draw(QPainter *painter, const QRectF &exposed) +{ + // Avoid warnings for now + painter->background(); + exposed.width(); +} diff --git a/src/sprite.h b/src/sprite.h index e69de29..8bc9cea 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -0,0 +1,48 @@ +/* + * QmlArcade + * Copyright (C) 2010 Jared Adams + * + * This file is part of the QmlArcade. + * + * QmlArcade is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * QmlArcade is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with QmlArcade. If not, see . + */ + +#ifndef SPRITE_H +#define SPRITE_H + +#include +#include + +class Sprite : public QmlComponent, public Tiled::CoRenderer +{ + Q_OBJECT + + Q_PROPERTY(qreal x READ x WRITE setX NOTIFY positionChanged) + Q_PROPERTY(qreal y READ y WRITE setY NOTIFY positionChanged) + +public: + Sprite(); + + ~Sprite(); + + virtual void draw(QPainter *painter, const QRectF &exposed); + +signals: + void positionChanged(const qreal &dummy); + +}; + +QML_DECLARE_TYPE(Sprite) + +#endif // SPRITE_H diff --git a/src/src.pro b/src/src.pro index 67660e7..8ff66e7 100644 --- a/src/src.pro +++ b/src/src.pro @@ -8,8 +8,10 @@ OBJECTS_DIR = .obj SOURCES += main.cpp \ viewer.cpp \ mapitem.cpp \ - tilelayeritem.cpp + tilelayeritem.cpp \ + sprite.cpp HEADERS += viewer.h \ mapitem.h \ - tilelayeritem.h + tilelayeritem.h \ + sprite.h OTHER_FILES += main.qml diff --git a/src/tiled/corenderer.cpp b/src/tiled/corenderer.cpp index e69de29..e38249b 100644 --- a/src/tiled/corenderer.cpp +++ b/src/tiled/corenderer.cpp @@ -0,0 +1,29 @@ +/* + * Tiled Map Editor (Qt) + * Copyright 2009 Tiled (Qt) developers (see AUTHORS file) + * + * This file is part of Tiled (Qt). + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "corenderer.h" + +using namespace Tiled; + +CoRenderer::CoRenderer(QPointF pos): + mPos(pos) +{ +} diff --git a/src/tiled/corenderer.h b/src/tiled/corenderer.h index e69de29..7cd3e06 100644 --- a/src/tiled/corenderer.h +++ b/src/tiled/corenderer.h @@ -0,0 +1,71 @@ +/* + * Tiled Map Editor (Qt) + * Copyright 2009 Tiled (Qt) developers (see AUTHORS file) + * + * This file is part of Tiled (Qt). + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef CORENDERER_H +#define CORENDERER_H + +#include +#include +#include +#include + +namespace Tiled { + +class CoRenderer +{ +public: + CoRenderer(QPointF pos); + + virtual ~CoRenderer() {} + + /** + * Returns the x position of this object. + */ + qreal x() const { return mPos.x(); } + + /** + * Sets the x position of this object. + */ + void setX(qreal x) { mPos.setX(x); } + + /** + * Returns the y position of this object. + */ + qreal y() const { return mPos.y(); } + + /** + * Sets the x position of this object. + */ + void setY(qreal y) { mPos.setY(y); } + + bool operator<(const CoRenderer &other) { return y() < other.y(); } + + virtual void draw(QPainter *painter, const QRectF &exposed) = 0; + +private: + QPointF mPos; +}; + +typedef QList CoRenderers; + +} // namespace Tiled + +#endif // CORENDERER_H diff --git a/src/tiled/layer.h b/src/tiled/layer.h index 1ff41dc..aea8ab7 100644 --- a/src/tiled/layer.h +++ b/src/tiled/layer.h @@ -28,6 +28,8 @@ #include #include +#include "corenderer.h" + namespace Tiled { class Map; @@ -153,6 +155,14 @@ public: */ virtual Layer *clone() const = 0; + QList coRenderers() const { return mCoRenderers; } + + void addCoRenderer(CoRenderer* coRenderer) + { mCoRenderers.append(coRenderer); } + + int removeCoRenderer(CoRenderer* coRenderer) + { return mCoRenderers.removeAll(coRenderer); } + protected: Layer *initializeClone(Layer *clone) const; @@ -165,6 +175,7 @@ protected: bool mVisible; Map *mMap; QMap mProperties; + CoRenderers mCoRenderers; }; } // namespace Tiled diff --git a/src/tiled/orthogonalrenderer.cpp b/src/tiled/orthogonalrenderer.cpp index 315b779..dbf35a8 100644 --- a/src/tiled/orthogonalrenderer.cpp +++ b/src/tiled/orthogonalrenderer.cpp @@ -95,9 +95,11 @@ void OrthogonalRenderer::drawTileLayer(QPainter *painter, int endX = layer->width(); int endY = layer->height(); + QRectF rect(layer->x(), layer->y(), layer->width(), layer->height()); + if (!exposed.isNull()) { const int extraHeight = layer->maxTileHeight() - tileHeight; - QRectF rect = exposed.adjusted(0, 0, 0, extraHeight); + rect = exposed.adjusted(0, 0, 0, extraHeight); rect.translate(-layerPos); startX = qMax((int) rect.x() / tileWidth, 0); @@ -106,7 +108,26 @@ void OrthogonalRenderer::drawTileLayer(QPainter *painter, endY = qMin((int) std::ceil(rect.bottom()) / tileHeight + 1, endY); } + CoRenderers coreRenderers = layer->coRenderers(); + qSort(coreRenderers); + QListIterator ci(coreRenderers); + + // Skip CoRenderers above the exposed view + while (ci.hasNext() && ci.peekNext()->y() <= startY) { + ci.next(); + } + for (int y = startY; y < endY; ++y) { + // Draw all CoRenderers before the tiles in this layer + while (ci.hasNext() && ci.peekNext()->y() <= y) { + CoRenderer* cr = ci.next(); + + // Only draw CoRenderers in the exposed view + if (cr->x() >= startX && cr->x() <= endX) { + cr->draw(painter, rect); + } + } + for (int x = startX; x < endX; ++x) { const Tile *tile = layer->tileAt(x, y); if (!tile) diff --git a/src/tiled/tiled.pri b/src/tiled/tiled.pri index 7ee56e1..b002619 100644 --- a/src/tiled/tiled.pri +++ b/src/tiled/tiled.pri @@ -10,7 +10,8 @@ SOURCES += $$PWD/layer.cpp \ $$PWD/isometricrenderer.cpp \ $$PWD/orthogonalrenderer.cpp \ $$PWD/compression.cpp \ - $$PWD/tilesetmanager.cpp + $$PWD/tilesetmanager.cpp \ + tiled/corenderer.cpp HEADERS += $$PWD/layer.h \ $$PWD/map.h \ $$PWD/mapobject.h \ @@ -25,4 +26,5 @@ HEADERS += $$PWD/layer.h \ $$PWD/orthogonalrenderer.h \ $$PWD/maprenderer.h \ $$PWD/compression.h \ - $$PWD/tilesetmanager.h + $$PWD/tilesetmanager.h \ + tiled/corenderer.h