C++ 为什么 SDL 和 OpenGL 相关?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5769031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:51:02  来源:igfitidea点击:

Why are SDL and OpenGL related?

c++copenglsdl

提问by Devan Buggay

I was messing around with SDL and found out that you cannot rotate images with SDL. Everywhere the question was asked, people said to use OpenGL to do rotation. I always thought that SDL was completely separate from OpenGL, am I wrong in thinking this? I even found tutorials for using OpenGL within SDL, which confused me even further. What exactly is the relationship between SDL and OpenGL? Why not just use OpenGL if its more powerful and allows you to do more (this is from what I've read)?

我在使用 SDL 时发现无法使用 SDL 旋转图像。到处问问题,人们说使用OpenGL来做旋转。我一直认为 SDL 与 OpenGL 是完全分开的,我这样想是不是错了?我什至找到了在 SDL 中使用 OpenGL 的教程,这让我更加困惑。SDL和OpenGL到底是什么关系?如果 OpenGL 功能更强大并允许您做更多事情,为什么不直接使用它(这是我阅读的内容)?

采纳答案by karlphillip

SDLis a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, etc. It also supports 3D hardware via OpenGL.

OpenGLis a standard specification defining a cross-language, cross-platform API for writing applications that produce 2D and 3D computer graphics. The interface consists of over 250 different function calls which can be used to draw complex three-dimensional scenes from simple primitives. OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992[4] and is widely used in CAD, virtual reality, scientific visualization, information visualization, and flight simulation. It is also used in video games, where it competes with Direct3D on Microsoft Windows platforms (see OpenGL vs. Direct3D).

SDL是一个跨平台的多媒体库,旨在提供对音频、键盘、鼠标、操纵杆等的低级访问。它还通过 OpenGL 支持 3D 硬件。

OpenGL是一个标准规范,它定义了一个跨语言、跨平台的 API,用于编写生成 2D 和 3D 计算机图形的应用程序。该界面由 250 多个不同的函数调用组成,可用于从简单的图元绘制复杂的 3D 场景。OpenGL 由 Silicon Graphics Inc. (SGI) 于 1992 年开发[4],广泛应用于 CAD、虚拟现实、科学可视化、信息可视化和飞行模拟。它还用于视频游戏,在 Microsoft Windows 平台上与 Direct3D 竞争(请参阅 OpenGL 与 Direct3D)。

回答by J Trana

SDL is a layer above OpenGL; in fact it uses GDI on Windows by default and also has a DirectX backend. People are probably saying you can use OpenGL to get around limitations of SDL on platforms that by default use OpenGL(ahem, Linux) because the higher level abstraction doesn't expose that functionality. However, then your code is "less" portable to Windows (or at least Windows using the GDI backend).

SDL是OpenGL之上的一层;事实上,它默认在 Windows 上使用 GDI,并且还有一个 DirectX 后端。人们可能会说您可以使用 OpenGL 来绕过默认使用 OpenGL 的平台(嗯,Linux)的 SDL 限制,因为更高级别的抽象不公开该功能。但是,那么您的代码对于 Windows(或至少是使用 GDI 后端的 Windows)的可移植性“较低”。

Also, SDL does a lot of other things besides graphics - audio, input, etc. that OpenGL doesn't do.

此外,除了图形之外,SDL 还做了很多其他的事情 - 音频、输入等,而 OpenGL 不做。

回答by Puppy

SDL uses OpenGL as a hardware renderer for content that wants hardware rendering on some platforms. If you have such a platform, then OpenGL is the underlying API over which SDL is an abstraction.

对于需要在某些平台上进行硬件渲染的内容,SDL 使用 OpenGL 作为硬件渲染器。如果您有这样的平台,那么 OpenGL 就是底层 API,SDL 是对它的抽象。

回答by Kos

About your example about rotating graphics: it is better to do it with OpenGL (i.e. hardware accelerated) than with SDL itself (i.e. on the CPU) because it is generally computionally intensive (especially if you have a lot of bitmaps to rotate every frame and you want the effect to be smooth).

关于旋转图形的示例:使用 OpenGL(即硬件加速)比使用 SDL 本身(即在 CPU 上)更好,因为它通常是计算密集型的(特别是如果你有很多位图来旋转每一帧和您希望效果平滑)。

However, nothing (besides reason) keeps you from using the SDL_gfx package which has the module SDL_rotozoom responsible for rotating and scaling bitmaps.

但是,没有什么(除了原因)阻止您使用 SDL_gfx 包,该包具有负责旋转和缩放位图的模块 SDL_rotozoom。

回答by SurvivalMachine

SDL handles input, window creation, image loading and several other functionality that OpenGL doesn't handle.

SDL 处理输入、窗口创建、图像加载和其他一些 OpenGL 不处理的功能。

回答by Skalli

As many people have stated. SDL handles more than just the rendering. Besides handling audio, input and all the other nice stuff it makes the use of graphics simplier by giving you access to many functions which you would need to implement by yourself otherwise.
So ys, SDL is an abstraction layer above OpenGL. You are still able to access OpenGL in your application and therefore use it's full power.
A simple addition, since you seem to be new to this kind of API: If you want an API which is similar to SDL (for 2D graphics) I can recommend having a look at SFML. It's based on C++ instead of C has functions for things like rotation. It's also based on OpenGL. :)

正如许多人所说。SDL 处理的不仅仅是渲染。除了处理音频、输入和所有其他不错的东西之外,它还让您可以访问许多您需要自己实现的功能,从而使图形的使用更加简单。
所以是的,SDL 是 OpenGL 之上的抽象层。您仍然可以在您的应用程序中访问 OpenGL,因此可以使用它的全部功能。
一个简单的补充,因为您似乎对这种 API 不熟悉:如果您想要一个类似于 SDL(用于 2D 图形)的 API,我建议您查看SFML。它基于 C++ 而不是 C 具有旋转等功能。它也是基于OpenGL的。:)