My program is going to create two windows and each window has a rectangle. One window is parent window with title bar and the other is its child without title bar. When I run it for several times, The parent’s title bar is missing. after another running, it comes back. Who knows the reason?
My code is as follow:
/*
- Example of an X Window System OpenGL program.
- OpenGL code is taken from auxdemo.c in the Platform SDK
*/
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
/* X globals, defines, and prototypes */
Display *dpy;
//Window glwin, glwinc;
//static int attributes[] = {GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
static int attributes[] = {GLX_RGBA, GLX_RED_SIZE, 4, GLX_GREEN_SIZE, 4, GLX_BLUE_SIZE, 4, GLX_DOUBLEBUFFER, None};
//#define SWAPBUFFERS glXSwapBuffers(dpy, glwin)
#define WIDTH 500
#define HEIGHT 500
/* OpenGL globals, defines, and prototypes */
GLfloat latitude, longitude, latinc, longinc;
GLdouble radius;
GLvoid resize(GLsizei, GLsizei);
GLvoid initializeGL(int n, Window win, GLXContext cx);
GLvoid drawScene(int n, Window glwin, GLXContext cx, GLint flag);
static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
{
if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
return GL_TRUE;
}
return GL_FALSE;
}
int direct = 0;
int ndraw = 3;
int pix1 = 0;
int pix2 = 0;
void
main(int argc, char **argv)
{
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
XSetWindowAttributes swa2;
GLXContext cx, cx2;
XEvent event;
GLboolean needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
int dummy, status;
Window glwin = 0, glwinc;
Screen *root_screen;
char window_name[32];
if (argc > 1)
direct = argv[1][0] - '0';
if (argc > 2)
ndraw = argv[2][0] - '0';
printf("
init direct=%d, ndraw=%d", direct, ndraw);
dpy = XOpenDisplay(NULL);
if (dpy == NULL){
fprintf(stderr, "could not open display
“);
exit(1);
}
printf(”, dpy = %p
", dpy);
if(!glXQueryExtension(dpy, &dummy, &dummy)){
fprintf(stderr, "could not open display");
exit(1);
}
/* find an OpenGL-capable Color Index visual with depth buffer */
vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributes);
if (vi == NULL) {
fprintf(stderr, "could not get visual
");
exit(1);
}
/* create an OpenGL rendering context */
if ((ndraw & 1) != 0)
{
cx = glXCreateContext(dpy, vi, None, direct);
if (cx == NULL) {
fprintf(stderr, "could not create rendering context
");
exit(1);
}
}
/* create an OpenGL rendering context */
if ((ndraw & 2) != 0)
{
cx2 = glXCreateContext(dpy, vi, None, direct);
if (cx2 == NULL) {
fprintf(stderr, "could not create rendering context2
");
exit(1);
}
}
/* create an X colormap since probably not using default visual */
root_screen = RootWindow(dpy, vi->screen);
printf("
root_screen=%x (screen=%d)
", root_screen, vi->screen);
cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
vi->visual, AllocNone);
swa.colormap = cmap;
// swa.border_pixel = 0x604020; /* XWhitePixel(,); */
// swa.background_pixel = 0x204060;
swa.event_mask = ExposureMask | KeyPressMask | StructureNotifyMask;
// CREATE PARENT
if ((ndraw & 1) != 0)
{
glwin = XCreateWindow(dpy, RootWindow(dpy, vi->screen),
0, 0, // 80, 40,
WIDTH, HEIGHT,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
XClearWindow(dpy, glwin);
sprintf(window_name, "test_window%d", 0);
XSetStandardProperties(dpy, glwin, window_name, window_name, None, argv, argc, NULL);
//status = glXMakeCurrent(dpy, glwin, cx);
//printf("
status1=%d
", status);
XMapWindow(dpy, glwin);
XIfEvent(dpy, &event, WaitForMapNotify, (char *)glwin);
}
// CREATE CHILD
if ((ndraw & 2) != 0)
{
glwinc = XCreateWindow(dpy, (glwin == 0) ? root_screen : glwin,
50, 10,
(4 * WIDTH) / 8,
(4 * HEIGHT) / 8,
0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);
XClearWindow(dpy, glwinc);
// sprintf(window_name, “test_window%d”, 1);
// XSetStandardProperties(dpy, glwinc, window_name, window_name, None, argv, argc, NULL);
XMapWindow(dpy, glwinc);
XIfEvent(dpy, &event, WaitForMapNotify, (char *)glwinc);
}
if ((ndraw & 1) != 0)
{
initializeGL(0, glwin, cx);
}
if ((ndraw & 2) != 0)
{
initializeGL(2, glwinc, cx2);
}
//resize(WIDTH, HEIGHT);
/* Animation loop */
if ((ndraw & 3) != 0)
while (1) {
#if 0
KeySym key;
while (XPending(dpy)) {
XNextEvent(dpy, &event);
switch (event.type) {
case KeyPress:
XLookupString((XKeyEvent *)&event, NULL, 0, &key, NULL);
switch (key) {
case XK_Left:
longinc += 0.5;
break;
case XK_Right:
longinc -= 0.5;
break;
case XK_Up:
latinc += 0.5;
break;
case XK_Down:
latinc -= 0.5;
break;
}
break;
case ConfigureNotify:
resize(event.xconfigure.width, event.xconfigure.height);
break;
}
}
#endif
if ((ndraw & 1) != 0)
{
//resize(WIDTH, HEIGHT);
drawScene(0, glwin, cx, 1);
}
if ((ndraw & 2) != 0)
{
//resize(WIDTH/2, HEIGHT/2);
drawScene(1, glwinc, cx2, 1);
}
}
XCloseDisplay(dpy);
}
/* OpenGL code */
GLvoid resize( GLsizei width, GLsizei height )
{
// Set viewport to window dimmension
glViewport(0, 0, width, height); // parameters: lower-left and w and h
// Reset coord system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far);
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLvoid initializeGL(int n, Window win, GLXContext cx)
{
glXMakeCurrent(dpy, win, cx);
if (n == 0)
{ // parent window
glClearColor(1.0, 0.0, 0.0, 0.5); // red window
}
else
{ // child window
glClearColor(0.0, 1.0, 0.0, 0.5); // green window
}
glClearDepth( 1.0 );
glEnable(GL_DEPTH_TEST);
}
GLvoid drawScene(int n, Window win, GLXContext cx, GLint flag)
{
glXMakeCurrent(dpy, win, cx);
// Clear the buffer
if (n == 0)
{ // parent window
resize((8 * WIDTH) / 8, (8 * HEIGHT) / 8);
}
else
{ // child window
resize((4 * WIDTH) / 8, (4 * HEIGHT) / 8);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (flag)
{
if (n == 0)
glColor3f(1.0f, 1.0f, 0.0); // yellow
else
glColor3f(0.0f, 0.0f, 1.0); // blue
glBegin(GL_POLYGON);
glVertex3f(0.10f, 0.10f, 0.0f);
glVertex3f(0.10f, 0.45f, 0.0f);
glVertex3f(0.45f, 0.45f, 0.0f);
glVertex3f(0.45f, 0.10f, 0.0f);
glEnd();
}
glXSwapBuffers(dpy, win);
}