diff options
Diffstat (limited to 'src/gles-test.c')
-rw-r--r-- | src/gles-test.c | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/src/gles-test.c b/src/gles-test.c new file mode 100644 index 0000000..d9c5282 --- /dev/null +++ b/src/gles-test.c @@ -0,0 +1,190 @@ +#include <EGL/egl.h> +#include <GLES/gl.h> + +#include <sdl-freetype.h> +#include <sdl-freetype-opengles.h> +#include <sdl-ft.h> + +#include <stdio.h> + +static EGLDisplay eglDisplay = 0; +static EGLConfig eglConfig = 0; +static EGLContext eglContext = 0; +static EGLSurface eglSurface = 0; +static Display *x11Display = NULL; +static Window x11Window = None; + +// consts +#define COLOURDEPTH_RED_SIZE 8 +#define COLOURDEPTH_GREEN_SIZE 8 +#define COLOURDEPTH_BLUE_SIZE 8 +#define COLOURDEPTH_DEPTH_SIZE 16 + +static const EGLint 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_NONE +}; + +static int InitOpenGL(int width, int height) +{ + // use EGL to initialise GLES + x11Display = XOpenDisplay(NULL); + + if (!x11Display) + { + fprintf(stderr, "ERROR: unable to get display!n"); + return 0; + } + + x11Window = XCreateSimpleWindow(x11Display, + RootWindow(x11Display, DefaultScreen(x11Display)), + 1, 1, width, height, 0, + BlackPixel(x11Display, DefaultScreen(x11Display)), + BlackPixel(x11Display, DefaultScreen(x11Display))); + if (x11Window == None) + { + fprintf(stderr, "ERROR: unable to create a window!n"); + return 0; + } + + XMapWindow(x11Display, x11Window); + XFlush(x11Display); + + eglDisplay = eglGetDisplay((EGLNativeDisplayType)x11Display); + if (eglDisplay == EGL_NO_DISPLAY) + { + printf("Unable to initialise EGL display."); + return 0; + } + + // Initialise egl + if (!eglInitialize(eglDisplay, NULL, NULL)) + { + printf("Unable to initialise EGL display."); + return 0; + } + + // Find a matching config + EGLint numConfigsOut = 0; + if (eglChooseConfig(eglDisplay, configAttribs, &eglConfig, 1, &numConfigsOut) != EGL_TRUE || numConfigsOut == 0) + { + fprintf(stderr, "Unable to find appropriate EGL config."); + return 0; + } + + eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, (EGLNativeWindowType)x11Window, 0); + if ( 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 + eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL); + if (eglContext == EGL_NO_CONTEXT) + { + printf("Unable to create GLES context!"); + return 0; + } + + if (eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) == EGL_FALSE) + { + printf("Unable to make GLES context current"); + return 0; + } + + return 1; +} + +/*====================================================== + * Kill off any opengl specific details + ====================================================*/ +static void TerminateOpenGL() +{ + eglMakeCurrent(eglDisplay, NULL, NULL, EGL_NO_CONTEXT); + eglDestroySurface(eglDisplay, eglSurface); + eglDestroyContext(eglDisplay, eglContext); + eglSurface = 0; + eglContext = 0; + eglConfig = 0; + + eglTerminate(eglDisplay); + eglDisplay = 0; + + XDestroyWindow(x11Display, x11Window); + XCloseDisplay(x11Display); + x11Display = NULL; +} + +static int SwapBuffers() +{ + eglSwapBuffers(eglDisplay, eglSurface); +} + +#define TEXT "sdl freetype OpenGLES render" + +int +main(int argc, char *argv[]) +{ + int done; + sdl_freetype_font_t * font; + sdl_freetype_glyph_render_t * render; + sdl_freetype_text_extents_t extents; + + if (!InitOpenGL(640, 480)) + return -1; + + font = sdl_ft_create_font ("sans", 20, + SDL_FT_WEIGHT_NORMAL, SDL_FT_SLANT_NORMAL, 96); + if (!font) + goto errquit0; + + render = sdl_freetype_opengles_render_create (); + if (!render) + goto errquit1; + sdl_freetype_font_set_render (font, render); + + glMatrixMode (GL_PROJECTION) ; + glLoadIdentity (); + glOrthof (0, 640.0f, 480.0f, 0, -1.0, 1.0); + + glMatrixMode (GL_MODELVIEW); + glLoadIdentity (); + + done = 0; + while (!done) { + XEvent xevent; + while (XPending(x11Display)) { + XNextEvent(x11Display, &xevent); + if (xevent.type == KeyPress) + done = 1; + } + + glClearColor (0.8f, 0.8f, 0.8f, 0.0f); + glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + sdl_freetype_font_utf8_extents (font, &extents, TEXT, -1); + sdl_freetype_font_show_utf8 (font, NULL, 255, 0, 0, 255, + (640 - extents.width) / 2 + extents.x_bearing, + (480 - extents.height) / 2 + extents.y_bearing, + TEXT, -1); + + SwapBuffers(); + usleep(10000); + } + + errquit1: + sdl_freetype_font_destroy (font); + sdl_freetype_fini (); + + errquit0: + TerminateOpenGL(); + return 0; +} |