如何正确使用SDl_Threads?
我对线程、SDL 以及图形的一般工作方式很陌生。 我一直在浏览 LazyFoo 的所有 SDL 教程,对我帮助很大。 但在他关于多线程的教程中,他评论说你永远不应该在单独的线程中使用视频功能,否则可能会导致问题。 我很好奇应该如何完成,因为我对图形和线程的理解仍然很模糊。
由于我的项目之一是射击游戏,我想知道是否应该创建一个显示所有图形的线程,一个线程接收其飞船的所有玩家输入,另一个线程用于敌人 AI。
如果这不是应该如何完成的,(我认为这是错误的)是否有人对如何通过用户输入和敌人人工智能与线程来实现图形有任何建议?
对于 Lazyfoo 的教程,这是链接: http://lazyfoo.net/SDL_tutorials/
I am new to threads,SDL and how graphic work in general. I've been looking through all of LazyFoo's SDL tutorials, and had helped me greatly. But in his tutorials about multi threading, he commented that you should never use video functions in separate threads, or might cause problem. I am curious how it should be done, as I still have a vague understanding of graphics and threads.
As one of my projects is a shoot'em up, I was wondering if I should create one thread that displays all the graphics, one threads receives all the player input for his ship, and another thread for the enemy AI.
If this is NOT how it should be done, (I think it's wrong) does anyone have any advice of how graphics should be implemented with user input and enemy AI with threads?
For the Lazyfoo's tutorials, this is the link:
http://lazyfoo.net/SDL_tutorials/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您开始时,根据职责分离线程会容易得多。 解决此问题的更“正确”方法是使用线程池,这会带来更大的可扩展性。 有一些预构建的线程池解决方案,其中包括用于 SDL 的线程池解决方案。
如果您使用 OpenGL 或 DirectX 进行渲染,我怀疑对这些 API 的调用会产生大量开销,因此您至少可以在主线程中执行游戏循环的(必要的)顺序部分:获取输入、更新世界模型(包括使用人工智能计算的结果),渲染。 然后你就可以让人工智能、网络线程并行运行。 因此,您无需担心单独的渲染线程。 对此有一个例外:如果您在软件中进行计算量大(我的意思是光线投射/光线跟踪重)渲染,那么您最好以某种方式在并行线程上执行渲染“逻辑”,写入帧缓冲区,然后在你的主线程中执行上面给出的序列,并使用例如位块传输你准备好的帧缓冲区。 GDI。
关于渲染,是的,据我所知,仅从单个线程(主线程)进行渲染通常是(或曾经)是一个好主意,可以确保例如。 使用 OpenGL 就没有问题。 再次强调,这在主要平台上不再是问题。 另外,我不知道GDI或DirectX是否单独存在同样的问题。
It can be a lot easier to separate threads on a responsibilities basis when you're starting out. The more "correct" way of approaching this, which leads to greater scalability, is to use thread pools. There are pre-built thread pool solutions out there, including one for SDL.
If you're using OpenGL or DirectX to do rendering, I doubt that the calls to those APIs will generate much overhead, so you could at least do the (necessarily) sequential parts of your game loop in the main thread: get input, update world model (including using results of ai calcs), render. Then you can have AI, network threads running in parallel. Thus you don't need to worry about a separate rendering thread. There is one exception to this: if you are doing computationally heavy (and I mean as in raycasting/raytracing heavy) rendering in software, then you would be better off somehow performing rendering "logic" on a parallel thread, writing to a framebuffer, and then in your main thread doing the sequence given above, and just blitting your prepared framebuffer using eg. GDI.
In regards to rendering, yes AFAIK rendering from only a single thread (the main thread) is (or was) generally a good idea to ensure that eg. with OpenGL you have no problems. Again AFAIK this is no longer an issue on the major platforms. Also, I don't know whether GDI or DirectX separately have the same problems.
一般来说,我认为将渲染线程与 UI/AI 线程分开是个好主意。 它有助于使您的代码更易于理解。 既然有很多游戏都有源代码,为什么不看看他们是怎么做的呢?
Generally I think it is a good idea to separate the rendering thread from the UI/AI threads. It helps to keep your code more understandable. As there are many games available in source code, why don't you check out how they did it?