用 C++ 重写一个简单的 Pygame 2D 绘图函数
我有一个 2D 向量列表(比如 20x20 / 400 点),我在屏幕上绘制这些点,如下所示:
for row in grid:
for point in row:
pygame.draw.circle(window, white, (particle.x, particle.y), 2, 0)
pygame.display.flip() #redraw the screen
这非常有效,但是比我预期的要慢得多。
我想用 C++ 重写它,并希望能学到一些东西(我正在 C++ atm 上做一个单元,所以它会有所帮助)。解决这个问题最简单的方法是什么?我研究过 Direct X,到目前为止已经遵循了一堆教程并绘制了一些基本的三角形。但是我找不到一个简单的(绘图点)。
I have a 2D list of vectors (say 20x20 / 400 points) and I am drawing these points on a screen like so:
for row in grid:
for point in row:
pygame.draw.circle(window, white, (particle.x, particle.y), 2, 0)
pygame.display.flip() #redraw the screen
This works perfectly, however it's much slower then I expected.
I want to rewrite this in C++ and hopefully learn some stuff (I am doing a unit on C++ atm, so it'll help) on the way. What's the easiest way to approach this? I have looked at Direct X, and have so far followed a bunch of tutorials and have drawn some rudimentary triangles. However I can't find a simple (draw point).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DirectX 没有只绘制一个点的函数。它仅在顶点和索引缓冲区上运行。如果您想要更简单的方法来表达一点,您需要编写一个包装器。
要绘制点列表,您需要使用 DrawPrimitive( D3DPT_POINTLIST,...)。然而,仅仅绘制一个点并不容易。您必须准备缓冲区,锁定它,填充数据,然后绘制缓冲区。或者您可以使用动态顶点缓冲区 - 来优化性能。有一个 DrawPrimitiveUP 调用应该能够渲染存储在系统内存中的图元(而不是使用缓冲区),但据我所知,它与 纯设备,因此您必须使用软件顶点处理。
在 OpenGL 中,有 glVertex2f 和 glVertex3f。您的调用将如下所示(可能存在拼写错误或语法错误 - 我没有编译/运行它):
OpenGL 比 DirectX 更容易玩弄和实验。我建议查看 SDL,并将其与 OpenGL 结合使用。或者您可以使用 GLUT 而不是 SDL。
或者你可以尝试使用 Qt 4。它有一个非常好的 2D 渲染例程。
DirectX doesn't have functions for drawing just one point. It operates on vertex and index buffers only. If you want simpler way to make just one point, you'll need to write a wrapper.
For drawing lists of points you'll need to use DrawPrimitive(D3DPT_POINTLIST, ...). however, there will be no easy way to just plot a point. You'll have to prepare buffer, lock it, fill with data, then draw the buffer. Or you could use dynamic vertex buffers - to optimize performance. There is a DrawPrimitiveUP call that is supposed to be able to render primitives stored in system memory (instead of using buffers), but as far as I know, it doesn't work (may silently discard primitives) with pure devices, so you'll have to use software vertex processing.
In OpenGL you have glVertex2f and glVertex3f. Your call would look like this (there might be a typo or syntax error - I didn't compiler/run it) :
OpenGL is MUCH easier for playing around and experimenting than DirectX. I'd recommend to take a look at SDL, and use it in conjuction with OpenGL. Or you could use GLUT instead of SDL.
Or you could try using Qt 4. It has a very good 2D rendering routines.
当我第一次涉足游戏/图形编程时,我开始喜欢 Allegro。它具有广泛的功能和相当简单的学习曲线。
When I first dabbled with game/graphics programming I became fond of Allegro. It's got a huge range of features and a pretty easy learning curve.