8 #include <X11/keysym.h>
12 #include "lion-render.h"
14 static VGint width
, height
;
15 struct lion
*lion
= 0;
21 vgClear(0, 0, width
, height
);
23 vgSeti(VG_MATRIX_MODE
, VG_MATRIX_PATH_USER_TO_SURFACE
);
25 vgTranslate(width
/2, height
/2);
27 vgTranslate(-width
/2, -height
/2);
35 /* new window size or exposure */
47 float clear_color
[4] = {1.0, 1.0, 1.0, 1.0};
48 vgSetfv(VG_CLEAR_COLOR
, 4, clear_color
);
55 * Create an RGB, double-buffered X window.
56 * Return the window and context handles.
59 make_x_window(Display
*x_dpy
, EGLDisplay egl_dpy
,
61 int x
, int y
, int width
, int height
,
66 static const EGLint attribs
[] = {
70 EGL_RENDERABLE_TYPE
, EGL_OPENVG_BIT
,
75 XSetWindowAttributes attr
;
79 XVisualInfo
*visInfo
, visTemplate
;
86 scrnum
= DefaultScreen( x_dpy
);
87 root
= RootWindow( x_dpy
, scrnum
);
89 if (!eglChooseConfig( egl_dpy
, attribs
, &config
, 1, &num_configs
) ||
91 printf("Error: couldn't get an EGL visual config\n");
97 if (!eglGetConfigAttrib(egl_dpy
, config
, EGL_NATIVE_VISUAL_ID
, &vid
)) {
98 printf("Error: eglGetConfigAttrib() failed\n");
102 /* The X window visual must match the EGL config */
103 visTemplate
.visualid
= vid
;
104 visInfo
= XGetVisualInfo(x_dpy
, VisualIDMask
, &visTemplate
, &num_visuals
);
106 printf("Error: couldn't get X visual\n");
110 /* window attributes */
111 attr
.background_pixel
= 0;
112 attr
.border_pixel
= 0;
113 attr
.colormap
= XCreateColormap( x_dpy
, root
, visInfo
->visual
, AllocNone
);
114 attr
.event_mask
= StructureNotifyMask
| ExposureMask
| KeyPressMask
;
115 mask
= CWBackPixel
| CWBorderPixel
| CWColormap
| CWEventMask
;
117 win
= XCreateWindow( x_dpy
, root
, 0, 0, width
, height
,
118 0, visInfo
->depth
, InputOutput
,
119 visInfo
->visual
, mask
, &attr
);
121 /* set hints and properties */
123 XSizeHints sizehints
;
126 sizehints
.width
= width
;
127 sizehints
.height
= height
;
128 sizehints
.flags
= USSize
| USPosition
;
129 XSetNormalHints(x_dpy
, win
, &sizehints
);
130 XSetStandardProperties(x_dpy
, win
, name
, name
,
131 None
, (char **)NULL
, 0, &sizehints
);
134 eglBindAPI(EGL_OPENVG_API
);
136 ctx
= eglCreateContext(egl_dpy
, config
, EGL_NO_CONTEXT
, NULL
);
138 printf("Error: eglCreateContext failed\n");
142 *surfRet
= eglCreateWindowSurface(egl_dpy
, config
, win
, NULL
);
145 printf("Error: eglCreateWindowSurface failed\n");
157 event_loop(Display
*dpy
, Window win
,
158 EGLDisplay egl_dpy
, EGLSurface egl_surf
)
163 while (XPending(dpy
) > 0) {
164 XNextEvent(dpy
, &event
);
166 switch (event
.type
) {
169 case ConfigureNotify
:
170 reshape(event
.xconfigure
.width
, event
.xconfigure
.height
);
176 code
= XLookupKeysym(&event
.xkey
, 0);
177 r
= XLookupString(&event
.xkey
, buffer
, sizeof(buffer
),
179 if (buffer
[0] == 27) {
191 eglSwapBuffers(egl_dpy
, egl_surf
);
200 printf(" -display <displayname> set the display to run on\n");
201 printf(" -info display OpenGL renderer info\n");
205 main(int argc
, char *argv
[])
207 const int winWidth
= 350, winHeight
= 450;
213 char *dpyName
= NULL
;
214 GLboolean printInfo
= GL_FALSE
;
215 EGLint egl_major
, egl_minor
;
219 for (i
= 1; i
< argc
; i
++) {
220 if (strcmp(argv
[i
], "-display") == 0) {
224 else if (strcmp(argv
[i
], "-info") == 0) {
233 x_dpy
= XOpenDisplay(dpyName
);
235 printf("Error: couldn't open display %s\n",
236 dpyName
? dpyName
: getenv("DISPLAY"));
240 egl_dpy
= eglGetDisplay(x_dpy
);
242 printf("Error: eglGetDisplay() failed\n");
246 if (!eglInitialize(egl_dpy
, &egl_major
, &egl_minor
)) {
247 printf("Error: eglInitialize() failed\n");
251 s
= eglQueryString(egl_dpy
, EGL_VERSION
);
252 printf("EGL_VERSION = %s\n", s
);
254 make_x_window(x_dpy
, egl_dpy
,
255 "Lion Example", 0, 0, winWidth
, winHeight
,
256 &win
, &egl_ctx
, &egl_surf
);
258 XMapWindow(x_dpy
, win
);
259 if (!eglMakeCurrent(egl_dpy
, egl_surf
, egl_surf
, egl_ctx
)) {
260 printf("Error: eglMakeCurrent() failed\n");
265 printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER
));
266 printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION
));
267 printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR
));
272 /* Set initial projection/viewing transformation.
273 * We can't be sure we'll get a ConfigureNotify event when the window
276 reshape(winWidth
, winHeight
);
278 event_loop(x_dpy
, win
, egl_dpy
, egl_surf
);
280 eglDestroyContext(egl_dpy
, egl_ctx
);
281 eglDestroySurface(egl_dpy
, egl_surf
);
282 eglTerminate(egl_dpy
);
285 XDestroyWindow(x_dpy
, win
);
286 XCloseDisplay(x_dpy
);