On Tue, Sep 20, 2005 at 08:57:01AM -0500, Matt Garman wrote: > > I've recently started to learn the Xlib API (mostly for fun). > > I'm seeing some strange behavior with my simple tutorial programs, > so I'm trying different window managers (to see if the problems are > me or the window manager). However, in FVWM, the attached program > only draws a "transparent" window. The background of the window is > the same as the root window, and none of the text shows up. > > Under enlightenment 0.16.7.2 and fluxbox 0.9.14 the text shows up. > > Any thoughts?
You're drawing into the window too early. After mapping it you
have to wait for the first Expose event before you can start
drawing. (See below).
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <X11/Xlib.h>
> #include <unistd.h>
>
> int main(int argc, char* argv[])
> {
> Display *dpy = NULL;
> Window window;
> char* display_name = NULL;
> XFontStruct* font_info;
> char* font_name = "10x20";
> GC gc;
> XGCValues values;
> unsigned long valuemask = 0;
> int screen_num = 0;
>
>
> /* create display */
> display_name = getenv("DISPLAY");
> dpy = XOpenDisplay(display_name);
> if (!dpy) {
> fprintf(stderr, "Can't open display '%s'!\n",
> display_name);
> }
> screen_num = DefaultScreen(dpy);
>
> /* create window */
> window = XCreateWindow( dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0,
> CopyFromParent, CopyFromParent, CopyFromParent, 0, 0);
> XMapWindow(dpy, window);
> XFlush(dpy);
>
> /* create graphics context (gc) */
> gc = XCreateGC(dpy, window, valuemask, &values);
> if (gc < 0) {
> fprintf(stderr, "XCreateGC() failure!\n");
> }
> XSetForeground(dpy, gc, WhitePixel(dpy, screen_num));
> XSetBackground(dpy, gc, BlackPixel(dpy, screen_num));
> XSetLineAttributes(dpy, gc, 10, LineSolid, CapButt, JoinBevel);
> XSetFillStyle(dpy, gc, FillSolid);
>
> /* set font */
> font_info = XLoadQueryFont(dpy, font_name);
> if (!font_info) {
> fprintf(stderr, "XLoadQueryFont() failed for '%s'\n", font_name);
> }
> XSetFont(dpy, gc, font_info->fid);
>
> XSync(dpy, False);
Add something like
{
XEvent e;
XWindowEvent(dpy, window, ExposureMask, &e);
}
here. In a real application you should wait for the Expose event
in the event loop instead of waiting for a specific event.
> /* draw! draw! draw! */
> XDrawString(dpy, window, gc, 0, 0, "Hello World", strlen("Hello World"));
> XFillRectangle(dpy, window, gc, 60, 150, 50, 60);
>
> XFlush(dpy);
>
> sleep(10);
>
> XCloseDisplay(dpy);
>
> return 0;
> }
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt, [EMAIL PROTECTED]
signature.asc
Description: Digital signature
