Opengl In Sdl Window


GernotFrisch

Member
Joined
Jan 2, 2007
Messages
445
Hi,

I want to make an opengl|es draing context using an existing SDL window.
Can anyone post me a working code starting with SDL_Init, ending with glClear, please?

Here's what I call after SDL_Init so far, but it does make a new window.
Code:
#ifdef HAVE_OPENGL_ES
	#include <EGL/egl.h>
	#include <GLES/gl.h>
	#include <SDL/SDL_syswm.h>
#else
	#include <GL/gl.h>
	#include <SDL/SDL.h>
#endif
 
#include "OpenGLInit.h"
 
#ifdef HAVE_OPENGL_ES
	EGLDisplay g_eglDisplay = 0;
	EGLConfig g_eglConfig = 0;
	EGLContext g_eglContext = 0;
	EGLSurface g_eglSurface = 0;
        Display *g_x11Display = NULL;
#endif
 
// consts
#define COLOURDEPTH_RED_SIZE  		5
#define COLOURDEPTH_GREEN_SIZE 		6
#define COLOURDEPTH_BLUE_SIZE 		5
#define COLOURDEPTH_DEPTH_SIZE		16
 
#ifdef HAVE_OPENGL_ES
static const EGLint g_configAttribs[] ={
										  EGL_RED_SIZE,      	    COLOURDEPTH_RED_SIZE,
										  EGL_GREEN_SIZE,    	    COLOURDEPTH_GREEN_SIZE,
										  EGL_BLUE_SIZE,     	    COLOURDEPTH_BLUE_SIZE,
										  EGL_DEPTH_SIZE,	    COLOURDEPTH_DEPTH_SIZE,
										  EGL_SURFACE_TYPE,         EGL_WINDOW_BIT,
										  EGL_RENDERABLE_TYPE,      EGL_OPENGL_ES_BIT,
										  EGL_BIND_TO_TEXTURE_RGBA, EGL_TRUE,
										  EGL_NONE
									   };
#endif
 
/*===========================================================
Initialise opengl settings. Call straight after SDL_SetVideoMode()
===========================================================*/
 
int InitOpenGL()
{
#ifdef HAVE_OPENGL_ES
	// use EGL to initialise GLES
	g_x11Display = XOpenDisplay(NULL);
 
	if (!g_x11Display)
	{
		fprintf(stderr, "ERROR: unable to get display!n");
		return 0;
	}
 
	g_eglDisplay = eglGetDisplay((EGLNativeDisplayType)g_x11Display);
	if (g_eglDisplay == EGL_NO_DISPLAY)
	{
		printf("Unable to initialise EGL display.");
		return 0;
	}
 
	// Initialise egl
	if (!eglInitialize(g_eglDisplay, NULL, NULL))
	{
		printf("Unable to initialise EGL display.");
		return 0;
	}
 
	// Find a matching config
	EGLint numConfigsOut = 0;
	if (eglChooseConfig(g_eglDisplay, g_configAttribs, &g_eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0)
	{
		fprintf(stderr, "Unable to find appropriate EGL config.");
		return 0;
	}
 
	// Get the SDL window handle
	SDL_SysWMinfo sysInfo; //Will hold our Window information
	SDL_VERSION(&sysInfo.version); //Set SDL version
	if(SDL_GetWMInfo(&sysInfo) <= 0) 
	{
		printf("Unable to get window handle");
		return 0;
	}
 
	g_eglSurface = eglCreateWindowSurface(g_eglDisplay, g_eglConfig, (EGLNativeWindowType)sysInfo.info.x11.window, 0);
	if ( g_eglSurface == EGL_NO_SURFACE)
	{
		printf("Unable to create EGL surface!");
		return 0;
	}
 
	// Bind GLES and create the context
	eglBindAPI(EGL_OPENGL_ES_API);
	EGLint contextParams[] = {EGL_CONTEXT_CLIENT_VERSION, 1, EGL_NONE};		// Use GLES version 1.x
	g_eglContext = eglCreateContext(g_eglDisplay, g_eglConfig, NULL, NULL);
	if (g_eglContext == EGL_NO_CONTEXT)
	{
		printf("Unable to create GLES context!");
		return 0;
	}
 
	if (eglMakeCurrent(g_eglDisplay,  g_eglSurface,  g_eglSurface, g_eglContext) == EGL_FALSE)
	{
		printf("Unable to make GLES context current");
		return 0;
	}
 
#else
 
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, COLOURDEPTH_RED_SIZE);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, COLOURDEPTH_GREEN_SIZE);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, COLOURDEPTH_BLUE_SIZE);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, COLOURDEPTH_DEPTH_SIZE);
 
#endif
 
	return 1;
}
 
/*======================================================
 * Kill off any opengl specific details
  ====================================================*/
void TerminateOpenGL()
{
#ifdef HAVE_OPENGL_ES
	eglMakeCurrent(g_eglDisplay, NULL, NULL, EGL_NO_CONTEXT);
	eglDestroySurface(g_eglDisplay, g_eglSurface);
	eglDestroyContext(g_eglDisplay, g_eglContext);
	g_eglSurface = 0;
	g_eglContext = 0;
	g_eglConfig = 0;
	eglTerminate(g_eglDisplay);
	g_eglDisplay = 0;
        XCloseDisplay(g_x11Display);
        g_x11Display = NULL;
#endif
}
 
 
int SwapBuffers()
{
#ifdef HAVE_OPENGL_ES
	eglSwapBuffers(g_eglDisplay, g_eglSurface);
#else
	SDL_GL_SwapBuffers();
#endif
}
 

Pickle

Mega GP Mania
Joined
May 30, 2006
Messages
5,516
Location
Detroit, Michigan
Website
Visit site
It appears you didnt call SDL_SetVideoMode

Edit: ok i see you plan to run this after SDL_SetVideoMode

Personally I do SDL_Init and do up to eglInitialize
the rest I run after SDL_SetVideoMode.

If you want examples, I have prboom, ken' lab, d1x/d2x, but your going to have look in the forums to find the source links.
 

GernotFrisch

Member
Joined
Jan 2, 2007
Messages
445
It just doesn't work. The eglChooseConfig is OK, but it says numConfigsOut == 0.
I called InitOpenGL _after_ SDL_Init() and SDL_SetVideoMode().

Please help.
 

chris_c

Member
Joined
Jun 25, 2010
Messages
393
Age
54
KungPhoo said:
It just doesn't work. The eglChooseConfig is OK, but it says numConfigsOut == 0.
I called InitOpenGL _after_ SDL_Init() and SDL_SetVideoMode().

Please help.
Isn't it easier just to not bother with sdl and use xlib - it really aint so hard ;)
I can post some code if that would help?
 
Last edited by a moderator:
Top