]> icculus.org git repositories - dana/openbox.git/blob - glft/test.c
improve expose handling for alpha children
[dana/openbox.git] / glft / test.c
1 #include "glft.h"
2 #include <stdio.h>
3
4
5 #include <stdio.h>
6 #include <X11/Xlib.h>
7 #include <X11/Xatom.h>
8 #include <X11/extensions/shape.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include "render.h"
12 #include <glib.h>
13 #include <GL/glx.h>
14 #include <assert.h>
15
16 static int x_error_handler(Display * disp, XErrorEvent * error)
17 {
18     char buf[1024];
19     XGetErrorText(disp, error->error_code, buf, 1024);
20     printf("%s\n", buf);
21     return 0;
22 }
23
24 #define X 10
25 #define Y 10
26 #define W 500
27 #define H 500
28
29 int main(int argc, char **argv)
30 {
31     struct GlftColor col;
32     Display *display;
33     Window win;
34     XVisualInfo *vi;
35     XEvent report, report2;
36     XClassHint chint;
37     Atom delete_win, protocols;
38     GLXContext cont;
39     int quit;
40     int config[] =
41         { GLX_DEPTH_SIZE, 1, GLX_DOUBLEBUFFER, GLX_RGBA, None };
42
43     struct GlftFont *font;
44
45     if (argc < 3) {
46         printf("Usage: %s fontname text\n", argv[0]);
47         return 1;
48     }
49
50     if (!GlftInit()) return 1;
51
52     if (!(display = XOpenDisplay(NULL))) {
53         fprintf(stderr, "couldn't connect to X server in DISPLAY\n");
54         return EXIT_FAILURE;
55     }
56
57     font = GlftFontOpen(display, DefaultScreen(display), argv[1]);
58     assert(font);
59
60     XSetErrorHandler(x_error_handler);
61     win = XCreateWindow(display, RootWindow(display, DefaultScreen(display)),
62                         X, Y, W, H, 0, 
63                         CopyFromParent,   /* depth */
64                         CopyFromParent,   /* class */
65                         CopyFromParent,   /* visual */
66                         0,                /* valuemask */
67                         0);               /* attributes */
68     chint.res_name = "glfttest";
69     chint.res_class = "Glfttest";
70     XSetClassHint(display, win, &chint);
71     XSelectInput(display, win, ExposureMask | StructureNotifyMask);
72     XMapWindow(display, win);
73
74     vi = glXChooseVisual(display, DefaultScreen(display), config);
75     if (vi == NULL)
76             printf("no conforming visual\n");
77     cont = glXCreateContext(display, vi, NULL, GL_TRUE);
78     if (cont == NULL)
79         printf("context creation failed\n");
80     glXMakeCurrent(display, win, cont);
81
82     delete_win = XInternAtom(display, "WM_DELETE_WINDOW", False);
83     protocols = XInternAtom(display, "WM_PROTOCOLS", False);
84     XSetWMProtocols(display, win, &delete_win, 1);
85     glMatrixMode(GL_PROJECTION);
86     glLoadIdentity();
87     glOrtho(-50, W-50, -100, H-100, 0, 10);
88     glMatrixMode(GL_MODELVIEW);
89     glEnable(GL_TEXTURE_2D);
90     glClearColor(0.0, 0.0, 1.0, 0.0);
91     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
92     glEnable(GL_BLEND);
93     quit = 0;
94     while (!quit) {
95         XNextEvent(display, &report);
96         switch (report.type) {
97         case ClientMessage:
98             if ((Atom)report.xclient.message_type == protocols)
99                 if ((Atom)report.xclient.data.l[0] == delete_win)
100                     quit = 1;
101         case Expose:
102             glClear(GL_COLOR_BUFFER_BIT);
103             col.r = 0.0; col.g = 0.0; col.b = 0.0; col.a = 1.0;
104             GlftRenderString(font, argv[2], strlen(argv[2]), &col, 9, -9);
105             col.r = 1.0; col.g = 1.0; col.b = 0.0; col.a = 0.25;
106             GlftRenderString(font, argv[2], strlen(argv[2]), &col, 0, 0);
107             glXSwapBuffers(display, win);
108         case ConfigureNotify:
109             break;
110         }
111
112     }
113     GlftFontClose(font);
114
115     return 1;
116 }