diff options
Diffstat (limited to 'progs/egl/opengles1/torus.c')
-rw-r--r-- | progs/egl/opengles1/torus.c | 328 |
1 files changed, 60 insertions, 268 deletions
diff --git a/progs/egl/opengles1/torus.c b/progs/egl/opengles1/torus.c index 9438a4fe59..8f262b53d6 100644 --- a/progs/egl/opengles1/torus.c +++ b/progs/egl/opengles1/torus.c @@ -13,14 +13,9 @@ #include <math.h> #include <stdlib.h> #include <stdio.h> -#include <string.h> -#include <X11/Xlib.h> -#include <X11/Xutil.h> -#include <X11/keysym.h> #include <GLES/gl.h> -#include <GLES/glext.h> -#include <EGL/egl.h> +#include "eglut.h" static const struct { GLenum internalFormat; @@ -43,6 +38,8 @@ static const struct { static GLfloat view_rotx = 0.0, view_roty = 0.0, view_rotz = 0.0; static GLint tex_format = NUM_CPAL_FORMATS; +static GLboolean animate = GL_TRUE; +static int win; static void @@ -364,293 +361,88 @@ init(void) } -/* - * Create an RGB, double-buffered X window. - * Return the window and context handles. - */ static void -make_x_window(Display *x_dpy, EGLDisplay egl_dpy, - const char *name, - int x, int y, int width, int height, - Window *winRet, - EGLContext *ctxRet, - EGLSurface *surfRet) +idle(void) { - static const EGLint attribs[] = { - EGL_RED_SIZE, 1, - EGL_GREEN_SIZE, 1, - EGL_BLUE_SIZE, 1, - EGL_DEPTH_SIZE, 1, - EGL_NONE - }; - - int scrnum; - XSetWindowAttributes attr; - unsigned long mask; - Window root; - Window win; - XVisualInfo *visInfo, visTemplate; - int num_visuals; - EGLContext ctx; - EGLConfig config; - EGLint num_configs; - EGLint vid; - - scrnum = DefaultScreen( x_dpy ); - root = RootWindow( x_dpy, scrnum ); - - if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) { - printf("Error: couldn't get an EGL visual config\n"); - exit(1); - } - - assert(config); - assert(num_configs > 0); - - if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) { - printf("Error: eglGetConfigAttrib() failed\n"); - exit(1); - } - - /* The X window visual must match the EGL config */ - visTemplate.visualid = vid; - visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals); - if (!visInfo) { - printf("Error: couldn't get X visual\n"); - exit(1); - } - - /* window attributes */ - attr.background_pixel = 0; - attr.border_pixel = 0; - attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone); - attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask; - mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask; - - win = XCreateWindow( x_dpy, root, 0, 0, width, height, - 0, visInfo->depth, InputOutput, - visInfo->visual, mask, &attr ); - - /* set hints and properties */ - { - XSizeHints sizehints; - sizehints.x = x; - sizehints.y = y; - sizehints.width = width; - sizehints.height = height; - sizehints.flags = USSize | USPosition; - XSetNormalHints(x_dpy, win, &sizehints); - XSetStandardProperties(x_dpy, win, name, name, - None, (char **)NULL, 0, &sizehints); - } - - eglBindAPI(EGL_OPENGL_ES_API); - - ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL ); - if (!ctx) { - printf("Error: eglCreateContext failed\n"); - exit(1); - } - - *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL); - - if (!*surfRet) { - printf("Error: eglCreateWindowSurface failed\n"); - exit(1); + if (animate) { + view_rotx += 1.0; + view_roty += 2.0; + eglutPostRedisplay(); } - - XFree(visInfo); - - *winRet = win; - *ctxRet = ctx; } - static void -event_loop(Display *dpy, Window win, - EGLDisplay egl_dpy, EGLSurface egl_surf) +key(unsigned char key) { - int anim = 1; - - while (1) { - int redraw = 0; - - if (!anim || XPending(dpy)) { - XEvent event; - XNextEvent(dpy, &event); - - switch (event.type) { - case Expose: - redraw = 1; - break; - case ConfigureNotify: - reshape(event.xconfigure.width, event.xconfigure.height); - break; - case KeyPress: - { - char buffer[10]; - int r, code; - code = XLookupKeysym(&event.xkey, 0); - if (code == XK_Left) { - view_roty += 5.0; - } - else if (code == XK_Right) { - view_roty -= 5.0; - } - else if (code == XK_Up) { - view_rotx += 5.0; - } - else if (code == XK_Down) { - view_rotx -= 5.0; - } - else if (code == XK_t) { - GLint size; - tex_format = (tex_format + 1) % (NUM_CPAL_FORMATS + 1); - if (tex_format < NUM_CPAL_FORMATS) { - size = make_cpal_texture(tex_format); - printf("Using %s (%d bytes)\n", - cpal_formats[tex_format].name, size); - } - else { - size = make_texture(); - printf("Using uncompressed texture (%d bytes)\n", size); - } - } - else { - r = XLookupString(&event.xkey, buffer, sizeof(buffer), - NULL, NULL); - if (buffer[0] == ' ') { - anim = !anim; - } - else if (buffer[0] == 27) { - /* escape */ - return; - } - } - } - redraw = 1; - break; - default: - ; /*no-op*/ + switch (key) { + case ' ': + animate = !animate; + break; + case 't': + { + GLint size; + tex_format = (tex_format + 1) % (NUM_CPAL_FORMATS + 1); + if (tex_format < NUM_CPAL_FORMATS) { + size = make_cpal_texture(tex_format); + printf("Using %s (%d bytes)\n", + cpal_formats[tex_format].name, size); + } + else { + size = make_texture(); + printf("Using uncompressed texture (%d bytes)\n", size); } - } - - if (anim) { - view_rotx += 1.0; - view_roty += 2.0; - redraw = 1; - } - if (redraw) { - draw(); - eglSwapBuffers(egl_dpy, egl_surf); + eglutPostRedisplay(); } + break; + case 27: + eglutDestroyWindow(win); + exit(0); + break; + default: + break; } } - static void -usage(void) +special_key(int key) { - printf("Usage:\n"); - printf(" -display <displayname> set the display to run on\n"); - printf(" -info display OpenGL renderer info\n"); + switch (key) { + case EGLUT_KEY_LEFT: + view_roty += 5.0; + break; + case EGLUT_KEY_RIGHT: + view_roty -= 5.0; + break; + case EGLUT_KEY_UP: + view_rotx += 5.0; + break; + case EGLUT_KEY_DOWN: + view_rotx -= 5.0; + break; + default: + break; + } } - int main(int argc, char *argv[]) { - const int winWidth = 300, winHeight = 300; - Display *x_dpy; - Window win; - EGLSurface egl_surf; - EGLContext egl_ctx; - EGLDisplay egl_dpy; - char *dpyName = NULL; - GLboolean printInfo = GL_FALSE; - EGLint egl_major, egl_minor; - int i; - const char *s; - - for (i = 1; i < argc; i++) { - if (strcmp(argv[i], "-display") == 0) { - dpyName = argv[i+1]; - i++; - } - else if (strcmp(argv[i], "-info") == 0) { - printInfo = GL_TRUE; - } - else { - usage(); - return -1; - } - } - - x_dpy = XOpenDisplay(dpyName); - if (!x_dpy) { - printf("Error: couldn't open display %s\n", - dpyName ? dpyName : getenv("DISPLAY")); - return -1; - } - - egl_dpy = eglGetDisplay(x_dpy); - if (!egl_dpy) { - printf("Error: eglGetDisplay() failed\n"); - return -1; - } - - if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) { - printf("Error: eglInitialize() failed\n"); - return -1; - } - - s = eglQueryString(egl_dpy, EGL_VERSION); - printf("EGL_VERSION = %s\n", s); - - s = eglQueryString(egl_dpy, EGL_VENDOR); - printf("EGL_VENDOR = %s\n", s); - - s = eglQueryString(egl_dpy, EGL_EXTENSIONS); - printf("EGL_EXTENSIONS = %s\n", s); - - s = eglQueryString(egl_dpy, EGL_CLIENT_APIS); - printf("EGL_CLIENT_APIS = %s\n", s); + eglutInitWindowSize(300, 300); + eglutInitAPIMask(EGLUT_OPENGL_ES1_BIT); + eglutInit(argc, argv); - make_x_window(x_dpy, egl_dpy, - "torus", 0, 0, winWidth, winHeight, - &win, &egl_ctx, &egl_surf); + win = eglutCreateWindow("torus"); - XMapWindow(x_dpy, win); - if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) { - printf("Error: eglMakeCurrent() failed\n"); - return -1; - } - - if (printInfo) { - printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER)); - printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION)); - printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR)); - printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS)); - } + eglutIdleFunc(idle); + eglutReshapeFunc(reshape); + eglutDisplayFunc(draw); + eglutKeyboardFunc(key); + eglutSpecialFunc(special_key); init(); - /* Set initial projection/viewing transformation. - * We can't be sure we'll get a ConfigureNotify event when the window - * first appears. - */ - reshape(winWidth, winHeight); - - event_loop(x_dpy, win, egl_dpy, egl_surf); - - eglDestroyContext(egl_dpy, egl_ctx); - eglDestroySurface(egl_dpy, egl_surf); - eglTerminate(egl_dpy); - - - XDestroyWindow(x_dpy, win); - XCloseDisplay(x_dpy); + eglutMainLoop(); return 0; } |