Skip to content

Commit 3daad87

Browse files
authored
Merge pull request #70 from scratchcpp/refactor_rendering
Refactor rendering
2 parents fa5135c + ee71b83 commit 3daad87

40 files changed

+1343
-493
lines changed

src/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ qt_add_qml_module(scratchcpp-render
3131
listmonitorlistmodel.cpp
3232
listmonitorlistmodel.h
3333
irenderedtarget.h
34+
texture.cpp
35+
texture.h
36+
skin.cpp
37+
skin.h
38+
bitmapskin.cpp
39+
bitmapskin.h
40+
svgskin.cpp
41+
svgskin.h
3442
renderedtarget.cpp
3543
renderedtarget.h
3644
targetpainter.cpp

src/bitmapskin.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <scratchcpp/costume.h>
4+
5+
#include "bitmapskin.h"
6+
7+
using namespace scratchcpprender;
8+
9+
BitmapSkin::BitmapSkin(libscratchcpp::Costume *costume) :
10+
Skin(costume)
11+
{
12+
if (!costume)
13+
return;
14+
15+
// Read image data
16+
QBuffer buffer;
17+
buffer.open(QBuffer::WriteOnly);
18+
buffer.write(static_cast<const char *>(costume->data()), costume->dataSize());
19+
buffer.close();
20+
const char *format;
21+
22+
{
23+
QImageReader reader(&buffer);
24+
format = reader.format();
25+
}
26+
27+
buffer.close();
28+
m_image.load(&buffer, format);
29+
30+
// Paint the image into a texture
31+
m_texture = createAndPaintTexture(m_image.width(), m_image.height(), false);
32+
m_textureSize.setWidth(m_image.width());
33+
m_textureSize.setHeight(m_image.height());
34+
Q_ASSERT(m_texture.isValid());
35+
}
36+
37+
BitmapSkin::~BitmapSkin()
38+
{
39+
m_texture.release();
40+
}
41+
42+
Texture BitmapSkin::getTexture(double scale) const
43+
{
44+
return m_texture;
45+
}
46+
47+
double BitmapSkin::getTextureScale(const Texture &texture) const
48+
{
49+
return 1;
50+
}
51+
52+
void BitmapSkin::paint(QPainter *painter)
53+
{
54+
painter->drawImage(m_image.rect(), m_image, m_image.rect());
55+
}

src/bitmapskin.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include "skin.h"
6+
#include "texture.h"
7+
8+
namespace scratchcpprender
9+
{
10+
11+
class BitmapSkin : public Skin
12+
{
13+
public:
14+
BitmapSkin(libscratchcpp::Costume *costume);
15+
~BitmapSkin();
16+
17+
Texture getTexture(double scale) const override;
18+
double getTextureScale(const Texture &texture) const override;
19+
20+
protected:
21+
void paint(QPainter *painter) override;
22+
23+
private:
24+
Texture m_texture;
25+
QSize m_textureSize;
26+
QImage m_image;
27+
};
28+
29+
} // namespace scratchcpprender

src/irenderedtarget.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include <QtOpenGL>
56
#include <qnanoquickitem.h>
67
#include <scratchcpp/sprite.h>
78

@@ -15,6 +16,7 @@ namespace scratchcpprender
1516
class StageModel;
1617
class SpriteModel;
1718
class SceneMouseArea;
19+
class Texture;
1820

1921
class IRenderedTarget : public QNanoQuickItem
2022
{
@@ -33,8 +35,10 @@ class IRenderedTarget : public QNanoQuickItem
3335
virtual void updateDirection(double direction) = 0;
3436
virtual void updateRotationStyle(libscratchcpp::Sprite::RotationStyle style) = 0;
3537
virtual void updateLayerOrder(int layerOrder) = 0;
38+
virtual void updateCostume(libscratchcpp::Costume *costume) = 0;
3639

37-
virtual void loadCostume(libscratchcpp::Costume *costume) = 0;
40+
virtual bool costumesLoaded() const = 0;
41+
virtual void loadCostumes() = 0;
3842

3943
virtual void beforeRedraw() = 0;
4044

@@ -63,18 +67,13 @@ class IRenderedTarget : public QNanoQuickItem
6367
virtual qreal height() const = 0;
6468
virtual void setHeight(qreal width) = 0;
6569

66-
virtual QPointF mapFromScene(const QPointF &point) const = 0;
67-
68-
virtual QBuffer *bitmapBuffer() = 0;
69-
virtual const QString &bitmapUniqueKey() const = 0;
70+
virtual libscratchcpp::Rect getBounds() const = 0;
7071

71-
virtual void lockCostume() = 0;
72-
virtual void unlockCostume() = 0;
72+
virtual QPointF mapFromScene(const QPointF &point) const = 0;
7373

7474
virtual bool mirrorHorizontally() const = 0;
7575

76-
virtual bool isSvg() const = 0;
77-
virtual void paintSvg(QNanoPainter *painter) = 0;
76+
virtual Texture texture() const = 0;
7877

7978
virtual void updateHullPoints(QOpenGLFramebufferObject *fbo) = 0;
8079
virtual const std::vector<QPointF> &hullPoints() const = 0;

0 commit comments

Comments
 (0)