简体   繁体   English

要在屏幕上绘制一些小图块,我应该使用 QQuickItem 还是 QQuickPaintedItem?

[英]To paint some small tiles on the screen, should I use QQuickItem or QQuickPaintedItem?

Essentially I need to load up a 4-color tilemap in memory that's exactly 16K in size, animate 2 of the tiles maybe 3 times a second, and render out a few tiles into a fairly small box, I'd say maybe 100 or so pixels wide and maybe 80 pixels tall (very roughly from memory) to be shown in QML/Qt Quick which I might use the scale property to scale it up larger in QML基本上我需要在内存中加载一个 4 色的瓷砖地图,它的大小正好是 16K,动画 2 个瓷砖可能每秒 3 次,然后将几个瓷砖渲染成一个相当小的盒子,我会说可能是 100 个左右要在 QML/Qt Quick 中显示的像素宽,可能是 80 像素高(非常粗略地来自内存),我可能会使用scale属性在 QML 中将其放大

Anyways I'm kind of at a loss on how to do this but I'm going to presume C++ is the best bet for this which is fine by me and after researching around I see there's 2 main options, QQuickItem & QQuickPaintedItem.无论如何,我对如何做到这一点有点不知所措,但我会假设 C++ 是最好的选择,这对我来说很好,经过研究后我看到有 2 个主要选项,QQuickItem 和 QQuickPaintedItem。

Now I understand QQuickPaintedItem is older and slower because it draws in the CPU first and then copies out to the video card so it's not the best solution.现在我明白 QQuickPaintedItem 较旧且较慢,因为它首先在 CPU 中绘制,然后再复制到视频卡,因此它不是最佳解决方案。 The most recommended option is QQuickItem which renders on the Graphics Card with OpenGL.最推荐的选项是 QQuickItem,它使用 OpenGL 在显卡上呈现。

But I don't need something that's overkill, I mean I'm dealing with ~100 x 80 pixels here, There's no line or shape operations, there's no shaders, none of that.但我不需要过大的东西,我的意思是我在这里处理 ~100 x 80 像素,没有线条或形状操作,没有着色器,没有。 It's just a bunch of pixels so I'm wondering if it'd be better to just use QQuickPaintedItem.它只是一堆像素,所以我想知道只使用 QQuickPaintedItem 是否更好。

However there may be another solution to all of this I don't know of in which case I'd love to hear it, maybe there's a much easier way to all of this.但是,我不知道所有这些可能还有另一种解决方案,在这种情况下,我很想听听,也许有更简单的方法可以解决所有这些问题。

Any help to figuring this out would be great, thanks in advance.任何帮助解决这个问题都会很棒,提前致谢。

As usual I'm looking for the simplest solution that fits my needs and I found it.像往常一样,我正在寻找适合我需求的最简单的解决方案,我找到了。

I created a TilesetEngine class that loads the tiles in QImages (because it's so simple), processes them, and then converts them over to QPixmaps where they're broken up into individual tiles and cached in memory for speed (perks of tiny simple 8x8 tiles, there's no memory footprint).我创建了一个 TilesetEngine 类,它在 QImages 中加载图块(因为它非常简单),处理它们,然后将它们转换为 QPixmaps,在那里它们被分解成单独的图块并缓存在内存中以提高速度(微小的简单 8x8 图块的好处,没有内存占用)。 The engine allows you to directly request a frame number and it'll render the static image with animated tiles matching the requested frame number.该引擎允许您直接请求帧号,它将使用与请求的帧号匹配​​的动画图块渲染静态图像。

That was the hard part because there was quite a bit of processing and post-processing but with that out of the way I created a QQuickImageProvider that serves as a sort of loose bridge and allows for a for extra options in the "id" such as specifying a scale size in the id since Qt Quick wasn't calling the const QSize& requestedSize .这是困难的部分,因为有相当多的处理和后处理,但是我创建了一个 QQuickImageProvider 作为一种松散的桥梁,并允许在“id”中提供额外的选项,例如在 id 中指定比例大小,因为 Qt Quick 没有调用const QSize& requestedSize This was pretty easy to do.这很容易做到。

With that registered to QML I just drop in a single id string, the id string specifies all options of how I want the engine to render the tileset from the animation frame, certain overlays, which tileset, etc... and the id string also tells the provider if I want it scaled or if I want the whole tileset image or just a single tile id.注册到 QML 后,我只需放入一个 id 字符串,id 字符串指定了我希望引擎如何从动画帧、某些叠加层、哪些tileset 等渲染图块集的所有选项,还有 id 字符串告诉提供者我是否想要缩放它,或者我是否想要整个图块集图像或只是一个图块 ID。 This was also super easy a lot of fun.这也超级容易很多乐趣。

Then for more fun I threw in a timer in QML and requested all the frames in the animation one-by-one and had it loop around.然后为了更有趣,我在 QML 中加入了一个计时器,并逐一请求动画中的所有帧并让它循环播放。 Here I could watch the animation and tileset or tiles come to life and animate.在这里,我可以观看动画和图块集或图块变得生动和动画。

The whole thing was very straightforward, easy, and a ton of fun.整个过程非常简单、简单,而且充满乐趣。 The only hard part was the engine whcih got real gritty in pixel manipulation but QImage, QPaint, QPixelmap, and QColor took th bulk of the work out so it could have been more difficult if it weren't for Qt's convenience classes.唯一困难的部分是引擎在像素操作方面非常坚韧,但 QImage、QPaint、QPixelmap 和 QColor 承担了大部分工作,因此如果不是 Qt 的便利类,它可能会更加困难。

I'm so glad I never mangled with any of the OpenGL stuff or my original plan, a QQuickItem or QQuickPainted Item.我很高兴我从未与任何 OpenGL 的东西或我的原始计划、QQuickItem 或 QQuickPainted Item 混淆。 This solution was perfect and easy and fits everything I need it to do,这个解决方案完美而简单,适合我需要做的一切,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 QQuickPaintedItem的绘制方法未调用 - Paint method of QQuickPaintedItem not called 从QQuickItem与QQuickPaintedItem继承时,QT中的OpenGL异常行为(渲染视频) - OpenGL strange behaviour in QT when inheriting from QQuickItem versus QQuickPaintedItem (rendering video) 如何与扩展QQuickItem中的模型交互? - How do I interact with a model in an extended QQuickItem? 我应该使用什么C ++ STL类来减少由大量小分配引起的碎片? - What C++ STL class should I use to reduce fragmentation caused by lots of small allocations? 我是否更喜欢在C ++代码中使用小类型的int(int8和int16)? - Should I prefer to use small types of int (int8 and int16) in C++ code? QtQuick QQuickItem渲染简单三角形填充全屏四边形 - QtQuick QQuickItem Rendering Simple Triangle Fills the Whole Screen With Quad 如何正确使用QQuickItem :: stackBefore()重新排序GridLayout中的项目? - How to properly use QQuickItem::stackBefore() to reorder items in GridLayout? 我的 OpenGL QQuickItem 没有我想要的大小 - My OpenGL QQuickItem won't have the size I want 如果某些实例不使用基类的属性,我应该继承一个子类吗? - Should I inherit a subclass if some of the instances don't use an attribute of base class? 在“for”语句中,我应该使用`!=`还是`<`? - In a “for” statement, should I use `!=` or `<`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM