问题描述
我正在学习SDL2,但是我也在使用使用OpenGL调用的imgui库.从网上各种博客上读到的内容来看,我无法轻松地混合使用SDL2渲染器和opengl调用;我要么使用另一个.
I'm am learning SDL2, but I am also using the imgui library that is using OpenGL calls. From what I read on various blogs online, I can't easily mix SDL2 renderer and opengl calls; I either use one or the other.
我阅读的大多数教程都使用渲染器,所以我不太了解如何在没有渲染器的情况下使用SDL2来绘制图元或精灵.
Most of the tutorials I've read use the renderer, so I do not quite understand how to use SDL2 without the renderer for drawing primitives, or drawing sprites.
例如: http://lazyfoo.net/tutorials/SDL/11_clip_rendering_and_sprite_sheets/index.php
他创建了sdl渲染器:
He creates the sdl renderer:
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
然后他用来渲染图像:
void LTexture::render( int x, int y, SDL_Rect* clip )
{
//Set rendering space and render to screen
SDL_Rect renderQuad = { x, y, mWidth, mHeight };
//Set clip rendering dimensions
if( clip != NULL )
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
//Render to screen
SDL_RenderCopy( gRenderer, mTexture, clip, &renderQuad );
}
然后他调用render()
从Spritesheet绘制图像:
He then calls his render()
to draw images from a spritesheet:
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Render top left sprite
gSpriteSheetTexture.render( 0, 0, &gSpriteClips[ 0 ] );
//Render top right sprite
gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 1 ].w, 0, &gSpriteClips[ 1 ] );
//Render bottom left sprite
gSpriteSheetTexture.render( 0, SCREEN_HEIGHT - gSpriteClips[ 2 ].h, &gSpriteClips[ 2 ] );
//Render bottom right sprite
gSpriteSheetTexture.render( SCREEN_WIDTH - gSpriteClips[ 3 ].w, SCREEN_HEIGHT - gSpriteClips[ 3 ].h, &gSpriteClips[ 3 ] );
//Update screen
SDL_RenderPresent( gRenderer );
我了解发生了什么,但是现在我无法使用sdl渲染器,我该如何完成相同的工作?我现在可以不使用任何SDL2绘图功能吗?我是否仅使用原始opengl调用,并且仅将SDL用于键盘绑定?
I understand what is happening, but now that I can't use the sdl renderer, how do I accomplish the same? Can I not use any SDL2 drawing functions now? Do I only use raw opengl calls, and only use SDL for keyboard bindings?
基本上,如果我不能使用SDL Renderer
,我不理解如何使用SDL2进行绘制(如在sprite示例中),因为它似乎与我正在使用的gui库不兼容,该gui库使用opengl调用进行渲染
Basically, I do not understand how to use SDL2 for drawing things (like in the sprite example) if I cannot use the SDL Renderer
since it seems incompatible with the gui library I'm using, which uses opengl calls for rendering.
推荐答案
正确.
为了直接使用OpenGL API,必须使用标志SDL_WINDOW_OPENGL
创建SDL窗口,并为其添加新的OpenGL上下文:
In order to use OpenGL API directly, the SDL window must be created with the flag SDL_WINDOW_OPENGL
, as well as a new OpenGL context for it:
auto window = SDL_CreateWindow("Window name",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN);
auto context = SDL_GL_CreateContext(window);
如果成功,将像在任何其他上下文处理程序中一样简单地执行对OpenGL的调用.问题中链接的同一网站提供了将OpenGL与SDL 2一起使用的教程,旧版和现代.请注意,两者均不涉及创建SDL渲染器.实际上,使用它是不合适的,因为这两个API旨在互斥:您应该使用一个或另一个. 此答案简要说明了为什么不应该混合使用它们.
If successful, one would simply perform calls to OpenGL like in any other context handler. That same website linked in the question provides tutorials for using OpenGL with SDL 2, both legacy and modern. Note that none of the two involve creating an SDL renderer.In fact, using it would be inappropriate since the two APIs are meant to be mutually exclusive: you should either use one or the other. This answer makes a quick explanation for why you should not mix them.
尽管如此,有些方法可以使用OpenGL从某些SDLRenderer调用中获得相同的输出(因为同一渲染器具有OpenGL实现!),但这自然是因情况而异的.要绘制一个简单的矩形吗?传统的glBegin
和glEnd
方法可能就足够了.想画一个精灵吗?您必须先将其发送到OpenGL设备的内存中.一旦了解了围绕OpenGL的绳索,制作合适的包装层通常是合适的.如果要使用依赖OpenGL的现有库,则替换其他绘图过程以使用此API(或实用程序库,例如GLU)是正确的方法.
Nevertheless, there are ways to achieve the same output from certain SDLRenderer calls using OpenGL (since the same renderer has an OpenGL implementation!), but this will naturally be case specific. Want to draw a simple rectangle? The legacy glBegin
and glEnd
approach might suffice. Want to draw a sprite? You'll have to send it to the OpenGL device's memory first. Once you know the ropes around OpenGL, making a suitable wrapper layer is often appropriate.If you want to use existing libraries that rely on OpenGL, replacing other drawing procedures to use this API (or utility libraries such as GLU) is the right way to go.
还要注意,SDL渲染器不必依赖OpenGL(它可以在Windows上使用Direct3D).
Also note that the SDL renderer does not have to rely on OpenGL (it may use Direct3D on Windows).
这篇关于如果我使用opengl进行绘图,那么SDL Renderer会没有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!