aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/tinygl-0.4-ugfx/examples/x11.c
blob: 13913f51833e035b0f67776570a01cbd98b4687e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#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 <GL/glx.h> 
#include <GL/gl.h> 
#include "ui.h"


#ifndef M_PI
#  define M_PI 3.14159265
#endif

static int attributeList[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };

static Bool WaitForNotify(Display *d, XEvent *e, char *arg) 
{
  return (e->type == MapNotify) && (e->xmap.window == (Window)arg); 
}

Display *dpy;
Window win;

void tkSwapBuffers(void)
{
    glXSwapBuffers(dpy,win);
}

int ui_loop(int argc, char **argv, const char *name)
{
  XVisualInfo *vi;
  Colormap cmap;
  XSetWindowAttributes swa;
  XSizeHints hint;
  GLXContext cx;
  XEvent event;
  int k, width, height;
  char buf[80];
  XEvent xev;
  KeySym keysym;
  XComposeStatus status;
  
  /* get a connection */
  dpy = XOpenDisplay(NULL);
  if (dpy == NULL) {
      fprintf(stderr,"Could not open X display\n");
      exit(1);
  }
  
  /* get an appropriate visual */
  vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  if (vi == NULL) {
      fprintf(stderr, "No suitable visual for glx\n");
      exit(1);
  }
      
  /* create a GLX context */
  cx = glXCreateContext(dpy, vi, 0, GL_TRUE);

  /* create a color map */
  cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
			 vi->visual, AllocNone);

  /* create a window */
  width = 400;
  height = 300;
  hint.x = 0;
  hint.y = 0;
  hint.width = width;
  hint.height = height;
  hint.flags = PPosition | PSize;
  swa.colormap = cmap;
  swa.border_pixel = 0;
  swa.event_mask = StructureNotifyMask;
  win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height,
		      0, vi->depth, InputOutput, vi->visual,
		      CWBorderPixel|CWColormap|CWEventMask, &swa);
  XSetStandardProperties (dpy, win, name, name, None, NULL, 0, &hint);

  XMapWindow(dpy, win);
  XIfEvent(dpy, &event, WaitForNotify, (char*)win);
  XSelectInput(dpy, win, KeyPressMask | StructureNotifyMask | ExposureMask);

  /* connect the context to the window */
  glXMakeCurrent(dpy, win, cx);

  init();
  reshape(width, height);

  while (1) {
    if (XPending(dpy) > 0) {
      XNextEvent(dpy,&xev);
      switch(xev.type) {
      case KeyPress:
	XLookupString((XKeyEvent *)&xev,buf,80,&keysym,&status);
        switch(keysym) {
        case XK_Up:
            k = KEY_UP;
            break;
        case XK_Down:
            k = KEY_DOWN;
            break;
        case XK_Left:
            k = KEY_LEFT;
            break;
        case XK_Right:
            k = KEY_RIGHT;
            break;
        case XK_Escape:
            k = KEY_ESCAPE;
            break;
        default:
            k = keysym;
        }
        key(k, 0);
	break;
      case ConfigureNotify:
	{
	  int width,height;
	  width = xev.xconfigure.width;
	  height = xev.xconfigure.height;
	  glXWaitX();
          reshape(width, height);
	}
	break;
      }
    } else {
      idle();
    }
  }
  return 0;
}