source: trunk/sys/TinyGL/examples/x11.c @ 266

Last change on this file since 266 was 1, checked in by alain, 7 years ago

First import

File size: 2.9 KB
Line 
1#include <math.h>
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5
6#include <X11/Xlib.h>
7#include <X11/Xutil.h>
8#include <X11/keysym.h>
9
10#include <GL/glx.h> 
11#include <GL/gl.h> 
12#include "ui.h"
13
14
15#ifndef M_PI
16#  define M_PI 3.14159265
17#endif
18
19static int attributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
20
21static Bool WaitForNotify(Display *d, XEvent *e, char *arg) 
22{
23  return (e->type == MapNotify) && (e->xmap.window == (Window)arg); 
24}
25
26Display *dpy;
27Window win;
28
29void tkSwapBuffers(void)
30{
31    glXSwapBuffers(dpy,win);
32}
33
34int ui_loop(int argc, char **argv, const char *name)
35{
36  XVisualInfo *vi;
37  Colormap cmap;
38  XSetWindowAttributes swa;
39  XSizeHints hint;
40  GLXContext cx;
41  XEvent event;
42  int k, width, height;
43  char buf[80];
44  XEvent xev;
45  KeySym keysym;
46  XComposeStatus status;
47 
48  /* get a connection */
49  dpy = XOpenDisplay(NULL);
50  if (dpy == NULL) {
51      fprintf(stderr,"Could not open X display\n");
52      exit(1);
53  }
54 
55  /* get an appropriate visual */
56  vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
57  if (vi == NULL) {
58      fprintf(stderr, "No suitable visual for glx\n");
59      exit(1);
60  }
61     
62  /* create a GLX context */
63  cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
64
65  /* create a color map */
66  cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
67                         vi->visual, AllocNone);
68
69  /* create a window */
70  width = 400;
71  height = 300;
72  hint.x = 0;
73  hint.y = 0;
74  hint.width = width;
75  hint.height = height;
76  hint.flags = PPosition | PSize;
77  swa.colormap = cmap;
78  swa.border_pixel = 0;
79  swa.event_mask = StructureNotifyMask;
80  win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height,
81                      0, vi->depth, InputOutput, vi->visual,
82                      CWBorderPixel|CWColormap|CWEventMask, &swa);
83  XSetStandardProperties (dpy, win, name, name, None, NULL, 0, &hint);
84
85  XMapWindow(dpy, win);
86  XIfEvent(dpy, &event, WaitForNotify, (char*)win);
87  XSelectInput(dpy, win, KeyPressMask | StructureNotifyMask | ExposureMask);
88
89  /* connect the context to the window */
90  glXMakeCurrent(dpy, win, cx);
91
92  init();
93  reshape(width, height);
94
95  while (1) {
96    if (XPending(dpy) > 0) {
97      XNextEvent(dpy,&xev);
98      switch(xev.type) {
99      case KeyPress:
100        XLookupString((XKeyEvent *)&xev,buf,80,&keysym,&status);
101        switch(keysym) {
102        case XK_Up:
103            k = KEY_UP;
104            break;
105        case XK_Down:
106            k = KEY_DOWN;
107            break;
108        case XK_Left:
109            k = KEY_LEFT;
110            break;
111        case XK_Right:
112            k = KEY_RIGHT;
113            break;
114        case XK_Escape:
115            k = KEY_ESCAPE;
116            break;
117        default:
118            k = keysym;
119        }
120        key(k, 0);
121        break;
122      case ConfigureNotify:
123        {
124          int width,height;
125          width = xev.xconfigure.width;
126          height = xev.xconfigure.height;
127          glXWaitX();
128          reshape(width, height);
129        }
130        break;
131      }
132    } else {
133      idle();
134    }
135  }
136  return 0;
137}
138
139
Note: See TracBrowser for help on using the repository browser.