Skip to content

Refactor rendering #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ qt_add_qml_module(scratchcpp-render
listmonitorlistmodel.cpp
listmonitorlistmodel.h
irenderedtarget.h
texture.cpp
texture.h
skin.cpp
skin.h
bitmapskin.cpp
bitmapskin.h
svgskin.cpp
svgskin.h
renderedtarget.cpp
renderedtarget.h
targetpainter.cpp
Expand Down
55 changes: 55 additions & 0 deletions src/bitmapskin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <scratchcpp/costume.h>

#include "bitmapskin.h"

using namespace scratchcpprender;

BitmapSkin::BitmapSkin(libscratchcpp::Costume *costume) :
Skin(costume)
{
if (!costume)
return;

// Read image data
QBuffer buffer;
buffer.open(QBuffer::WriteOnly);
buffer.write(static_cast<const char *>(costume->data()), costume->dataSize());
buffer.close();
const char *format;

{
QImageReader reader(&buffer);
format = reader.format();
}

buffer.close();
m_image.load(&buffer, format);

// Paint the image into a texture
m_texture = createAndPaintTexture(m_image.width(), m_image.height(), false);
m_textureSize.setWidth(m_image.width());
m_textureSize.setHeight(m_image.height());
Q_ASSERT(m_texture.isValid());
}

BitmapSkin::~BitmapSkin()
{
m_texture.release();
}

Texture BitmapSkin::getTexture(double scale) const
{
return m_texture;
}

double BitmapSkin::getTextureScale(const Texture &texture) const
{
return 1;
}

void BitmapSkin::paint(QPainter *painter)
{
painter->drawImage(m_image.rect(), m_image, m_image.rect());
}
29 changes: 29 additions & 0 deletions src/bitmapskin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

#include "skin.h"
#include "texture.h"

namespace scratchcpprender
{

class BitmapSkin : public Skin
{
public:
BitmapSkin(libscratchcpp::Costume *costume);
~BitmapSkin();

Texture getTexture(double scale) const override;
double getTextureScale(const Texture &texture) const override;

protected:
void paint(QPainter *painter) override;

private:
Texture m_texture;
QSize m_textureSize;
QImage m_image;
};

} // namespace scratchcpprender
17 changes: 8 additions & 9 deletions src/irenderedtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <QtOpenGL>
#include <qnanoquickitem.h>
#include <scratchcpp/sprite.h>

Expand All @@ -15,6 +16,7 @@ namespace scratchcpprender
class StageModel;
class SpriteModel;
class SceneMouseArea;
class Texture;

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

virtual void loadCostume(libscratchcpp::Costume *costume) = 0;
virtual bool costumesLoaded() const = 0;
virtual void loadCostumes() = 0;

virtual void beforeRedraw() = 0;

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

virtual QPointF mapFromScene(const QPointF &point) const = 0;

virtual QBuffer *bitmapBuffer() = 0;
virtual const QString &bitmapUniqueKey() const = 0;
virtual libscratchcpp::Rect getBounds() const = 0;

virtual void lockCostume() = 0;
virtual void unlockCostume() = 0;
virtual QPointF mapFromScene(const QPointF &point) const = 0;

virtual bool mirrorHorizontally() const = 0;

virtual bool isSvg() const = 0;
virtual void paintSvg(QNanoPainter *painter) = 0;
virtual Texture texture() const = 0;

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