blob: 7224fed399d269d6231968041278712bd6ae5035 [file] [log] [blame]
Zack Rusin544dd4b2009-05-01 12:41:38 -04001#include <assert.h>
2#include <math.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6#include <X11/Xlib.h>
7#include <X11/Xutil.h>
8#include <X11/keysym.h>
9#include <VG/openvg.h>
10#include <GLES/egl.h>
11
12#include "lion-render.h"
13
14static VGint width, height;
15struct lion *lion = 0;
16VGfloat angle = 0;
17
18static void
19draw(void)
20{
21 vgClear(0, 0, width, height);
22
23 vgSeti(VG_MATRIX_MODE, VG_MATRIX_PATH_USER_TO_SURFACE);
24 vgLoadIdentity();
25 vgTranslate(width/2, height/2);
26 vgRotate(angle);
27 vgTranslate(-width/2, -height/2);
28
29 lion_render(lion);
30
31 ++angle;
32}
33
34
35/* new window size or exposure */
36static void
37reshape(int w, int h)
38{
39 width = w;
40 height = h;
41}
42
43
44static void
45init(void)
46{
47 float clear_color[4] = {1.0, 1.0, 1.0, 1.0};
48 vgSetfv(VG_CLEAR_COLOR, 4, clear_color);
49
50 lion = lion_create();
51}
52
53
54/*
55 * Create an RGB, double-buffered X window.
56 * Return the window and context handles.
57 */
58static void
59make_x_window(Display *x_dpy, EGLDisplay egl_dpy,
60 const char *name,
61 int x, int y, int width, int height,
62 Window *winRet,
63 EGLContext *ctxRet,
64 EGLSurface *surfRet)
65{
66 static const EGLint attribs[] = {
67 EGL_RED_SIZE, 1,
68 EGL_GREEN_SIZE, 1,
69 EGL_BLUE_SIZE, 1,
70 EGL_NONE
71 };
72
73 int scrnum;
74 XSetWindowAttributes attr;
75 unsigned long mask;
76 Window root;
77 Window win;
78 XVisualInfo *visInfo, visTemplate;
79 int num_visuals;
80 EGLContext ctx;
81 EGLConfig config;
82 EGLint num_configs;
83 EGLint vid;
84
85 scrnum = DefaultScreen( x_dpy );
86 root = RootWindow( x_dpy, scrnum );
87
88 if (!eglChooseConfig( egl_dpy, attribs, &config, 1, &num_configs)) {
89 printf("Error: couldn't get an EGL visual config\n");
90 exit(1);
91 }
92
93 assert(config);
94 assert(num_configs > 0);
95
96 if (!eglGetConfigAttrib(egl_dpy, config, EGL_NATIVE_VISUAL_ID, &vid)) {
97 printf("Error: eglGetConfigAttrib() failed\n");
98 exit(1);
99 }
100
101 /* The X window visual must match the EGL config */
102 visTemplate.visualid = vid;
103 visInfo = XGetVisualInfo(x_dpy, VisualIDMask, &visTemplate, &num_visuals);
104 if (!visInfo) {
105 printf("Error: couldn't get X visual\n");
106 exit(1);
107 }
108
109 /* window attributes */
110 attr.background_pixel = 0;
111 attr.border_pixel = 0;
112 attr.colormap = XCreateColormap( x_dpy, root, visInfo->visual, AllocNone);
113 attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
114 mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
115
116 win = XCreateWindow( x_dpy, root, 0, 0, width, height,
117 0, visInfo->depth, InputOutput,
118 visInfo->visual, mask, &attr );
119
120 /* set hints and properties */
121 {
122 XSizeHints sizehints;
123 sizehints.x = x;
124 sizehints.y = y;
125 sizehints.width = width;
126 sizehints.height = height;
127 sizehints.flags = USSize | USPosition;
128 XSetNormalHints(x_dpy, win, &sizehints);
129 XSetStandardProperties(x_dpy, win, name, name,
130 None, (char **)NULL, 0, &sizehints);
131 }
132
133 eglBindAPI(EGL_OPENVG_API);
134
135 ctx = eglCreateContext(egl_dpy, config, EGL_NO_CONTEXT, NULL );
136 if (!ctx) {
137 printf("Error: eglCreateContext failed\n");
138 exit(1);
139 }
140
141 *surfRet = eglCreateWindowSurface(egl_dpy, config, win, NULL);
142
143 if (!*surfRet) {
144 printf("Error: eglCreateWindowSurface failed\n");
145 exit(1);
146 }
147
148 XFree(visInfo);
149
150 *winRet = win;
151 *ctxRet = ctx;
152}
153
154
155static void
156event_loop(Display *dpy, Window win,
157 EGLDisplay egl_dpy, EGLSurface egl_surf)
158{
159 while (1) {
160 XEvent event;
161
162 while (XPending(dpy) > 0) {
163 XNextEvent(dpy, &event);
164
165 switch (event.type) {
166 case Expose:
167 break;
168 case ConfigureNotify:
169 reshape(event.xconfigure.width, event.xconfigure.height);
170 break;
171 case KeyPress:
172 {
173 char buffer[10];
174 int r, code;
175 code = XLookupKeysym(&event.xkey, 0);
176 r = XLookupString(&event.xkey, buffer, sizeof(buffer),
177 NULL, NULL);
178 if (buffer[0] == 27) {
179 /* escape */
180 return;
181 }
182 }
183 break;
184 default:
185 ; /*no-op*/
186 }
187 }
188
189 draw();
190 eglSwapBuffers(egl_dpy, egl_surf);
191 }
192}
193
194
195static void
196usage(void)
197{
198 printf("Usage:\n");
199 printf(" -display <displayname> set the display to run on\n");
200 printf(" -info display OpenGL renderer info\n");
201}
202
203int
204main(int argc, char *argv[])
205{
206 const int winWidth = 350, winHeight = 450;
207 Display *x_dpy;
208 Window win;
209 EGLSurface egl_surf;
210 EGLContext egl_ctx;
211 EGLDisplay egl_dpy;
212 char *dpyName = NULL;
213 GLboolean printInfo = GL_FALSE;
214 EGLint egl_major, egl_minor;
215 int i;
216 const char *s;
217
218 for (i = 1; i < argc; i++) {
219 if (strcmp(argv[i], "-display") == 0) {
220 dpyName = argv[i+1];
221 i++;
222 }
223 else if (strcmp(argv[i], "-info") == 0) {
224 printInfo = GL_TRUE;
225 }
226 else {
227 usage();
228 return -1;
229 }
230 }
231
232 x_dpy = XOpenDisplay(dpyName);
233 if (!x_dpy) {
234 printf("Error: couldn't open display %s\n",
235 dpyName ? dpyName : getenv("DISPLAY"));
236 return -1;
237 }
238
239 egl_dpy = eglGetDisplay(x_dpy);
240 if (!egl_dpy) {
241 printf("Error: eglGetDisplay() failed\n");
242 return -1;
243 }
244
245 if (!eglInitialize(egl_dpy, &egl_major, &egl_minor)) {
246 printf("Error: eglInitialize() failed\n");
247 return -1;
248 }
249
250 s = eglQueryString(egl_dpy, EGL_VERSION);
251 printf("EGL_VERSION = %s\n", s);
252
253 make_x_window(x_dpy, egl_dpy,
254 "Lion Example", 0, 0, winWidth, winHeight,
255 &win, &egl_ctx, &egl_surf);
256
257 XMapWindow(x_dpy, win);
258 if (!eglMakeCurrent(egl_dpy, egl_surf, egl_surf, egl_ctx)) {
259 printf("Error: eglMakeCurrent() failed\n");
260 return -1;
261 }
262
263 if (printInfo) {
264 printf("VG_RENDERER = %s\n", (char *) vgGetString(VG_RENDERER));
265 printf("VG_VERSION = %s\n", (char *) vgGetString(VG_VERSION));
266 printf("VG_VENDOR = %s\n", (char *) vgGetString(VG_VENDOR));
267 }
268
269 init();
270
271 /* Set initial projection/viewing transformation.
272 * We can't be sure we'll get a ConfigureNotify event when the window
273 * first appears.
274 */
275 reshape(winWidth, winHeight);
276
277 event_loop(x_dpy, win, egl_dpy, egl_surf);
278
279 eglDestroyContext(egl_dpy, egl_ctx);
280 eglDestroySurface(egl_dpy, egl_surf);
281 eglTerminate(egl_dpy);
282
283
284 XDestroyWindow(x_dpy, win);
285 XCloseDisplay(x_dpy);
286
287 return 0;
288}