The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
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
AmosT


Joined: 26 Aug 2013
Posts: 2
Yes, it is possible; here is an example, but unluckily the colors of the 2d render primitive are wrong and the primitive is rotating while it should not. What could be wrong?

#include <SDL2/SDL.h>
#include <SDL2/SDL_main.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_opengl.h>

int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Window * win = SDL_CreateWindow( "test", 0, 0, 640, 480, ::SDL_WINDOW_RESIZABLE | ::SDL_WINDOW_OPENGL);
SDL_GLContext glcontext = SDL_GL_CreateContext(win);
SDL_Renderer * renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
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) )
{
switch (event.type)
{
case SDL_QUIT :
quit = true;
break;
case SDL_KEYDOWN:
{
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE :
quit = true;
break;
default :
break;
}
}
default :
break;
}
}
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();
SDL_GL_SwapWindow(win);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawLine(renderer, 0, 0, 100, 100);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
SDL_DestroyRenderer(renderer);
SDL_Quit();
return EXIT_SUCCESS;
}
SOLVED SDL2 opengl + 2D rendering
AmosT


Joined: 26 Aug 2013
Posts: 2
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;
}