OpenGL + 2d-render functions in one window? |
Daniel Raufison
Guest
|
Hi!
Is it possible to use functions of the accelerated 2D Render-API in the same window that is associated with an OpenGL context? Can I just create an accelerated renderer before creating the GL context and then use both simultaneously? _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
|||||||||||
|
opengl and 2d render api in SDL2 |
SOLVED SDL2 opengl + 2D rendering |
AmosT
|
This solves the issue, you have to choose opengl renderer, you have to use PRESENTVSYNC, the coordinates of the primitives are in opengl coordinates (so some more additional gl... commands are required), and you don't have to use SDL_GL_SwapWindow.
#include <SDL.h> #include <SDL_main.h> #include <SDL_render.h> #include <SDL_opengl.h> int wmain(int argc, char **argv) { SDL_Init( SDL_INIT_VIDEO ); SDL_Window * win = SDL_CreateWindow( "test", 20, 20, 640, 480, SDL_WINDOW_OPENGL); SDL_GLContext glcontext = SDL_GL_CreateContext(win); int oglIdx = -1; int nRD = SDL_GetNumRenderDrivers(); for(int i=0; i<nRD; i++) { SDL_RendererInfo info; if(!SDL_GetRenderDriverInfo(i, &info)) { if(!strcmp(info.name, "opengl")) { oglIdx = i; } } } SDL_Renderer * renderer = SDL_CreateRenderer(win, oglIdx, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); glMatrixMode(GL_PROJECTION|GL_MODELVIEW); glLoadIdentity(); glOrtho(-320, 320, 240, -240, 0, 1); SDL_Event event; bool quit=false; while(!quit) { while ( SDL_PollEvent(&event) ); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); glRotatef(0.6,0.0,0.0,1.0); float x = 30.0, y = 30.0; glBegin(GL_TRIANGLES); glColor3f(1.0,0.0,0.0); glVertex2f(x, y+90.0); glColor3f(0.0,1.0,0.0); glVertex2f(x+90.0, y-90.0); glColor3f(0.0,0.0,1.0); glVertex2f(x-90.0, y-90.0); glEnd(); glPushMatrix(); glLoadIdentity(); glOrtho(-320, 320, 240, -240, 0, 1); glColor4f(255, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_RenderDrawLine(renderer, -320+0, -240+0, -320+100, -240+100); glPopMatrix(); SDL_RenderPresent(renderer); SDL_Delay(10); } SDL_DestroyRenderer(renderer); SDL_Quit(); return 0; } |
|||||||||||
|