在X11中等效"Invalidate Rect"/"WM_PAINT"

def*_*ult 10 c++ linux x11 xlib

我正在将一些代码从Windows移植到XLib.在windows代码中,我可以通过调用InvalidateRect然后处理相应的WM_PAINT消息来强制重绘.但是,我无法在X11/XLib中找到如何执行此操作.我看到有Expose消息但不确定是否是同一件事.

如果重要,我需要这样做以强制窗口以特定帧速率渲染基于OpenGL的程序.

小智 5

为了稍微扩展 B????? 给出的有用答案,

使用原始 Xlib,您可以随时在单个线程中进行绘制,因为每个 Xlib 函数都指定了完整的显示、窗口和上下文。AFAIK,多线程所有赌注都关闭了。

如果您在桌面环境中,您还必须有一个 Expose 事件处理程序,并为这些事件选择。即使您正在编写全屏程序,拥有一个也无妨。

大多数工具包都不那么灵活,只在指定的事件处理程序中绘制(但在许多其他方式中使用更好),并且有一些等效于 Windows InvalidateRect。在原始 Xlib 中,您可以通过向自己发送一个 Expose 事件来获得相同的效果。这样做不会导致任何真正的性能问题,并且会使代码更容易被其他程序员理解,并且更容易移植,所以你也可以。

还有 XClearArea 和 XClearWindow 函数可以为您生成 Expose 事件,但它们首先使用背景颜色擦除部分/全部,这可能会导致闪烁。

使用 OpenGL 会变得有点复杂,因为您还必须使用 GLX。我在http://cs.anu.edu.au/~hugh.fisher/3dteach/ 上有一个非常简单的 OpenGL/Xlib 程序, 作为示例可能很有用。

  • 使用 `XClearArea(disp, win, 0, 0, 1, 1, true)` 来清除一个像素区域就足以在不添加闪烁的情况下引发 `Expose 事件`。如果您没有立即看到“Expose”事件,您可能希望立即使用“XFlush()”跟随“XClearArea()”。 (3认同)

BЈо*_*вић 4

您需要处理 Expose 事件。本教程通过示例解释了如何处理 Expose 事件:

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
/*Linux users will need to add -ldl to the Makefile to compile 
 *this example.
 */
Display *dis;
Window win;
XEvent report;
GC green_gc;
XColor green_col;
Colormap colormap;
/*
Try changing the green[] = below to a different color.
The color can also be from /usr/X11R6/lib/X11/rgb.txt, such as RoyalBlue4.
A # (number sign) is only needed when using hexadecimal colors.
*/
char green[] = "#00FF00";

int main() {
    dis = XOpenDisplay(NULL);
    win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 500, 500, 0, BlackPixel (dis, 0), BlackPixel(dis, 0));
    XMapWindow(dis, win);
    colormap = DefaultColormap(dis, 0);
    green_gc = XCreateGC(dis, win, 0, 0);
    XParseColor(dis, colormap, green, &green_col);
    XAllocColor(dis, colormap, &green_col);
    XSetForeground(dis, green_gc, green_col.pixel);

    XSelectInput(dis, win, ExposureMask | KeyPressMask | ButtonPressMask);

    XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497);
    XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398);
    XFlush(dis);

    while (1)  {
    XNextEvent(dis, &report);
        switch  (report.type) {
        case Expose:   
            fprintf(stdout, "I have been exposed.\n");
                XDrawRectangle(dis, win, green_gc, 1, 1, 497, 497);
                XDrawRectangle(dis, win, green_gc, 50, 50, 398, 398);
                XFlush(dis);
            break;
            case KeyPress:
        /*Close the program if q is pressed.*/
                if (XLookupKeysym(&report.xkey, 0) == XK_q) {
                exit(0);
                }
            break;
        }
    }

return 0;
}
Run Code Online (Sandbox Code Playgroud)

我可能误解了这个问题。如果您想在应用程序中创建 Expose 事件,您可以创建并设置Exposure event,并使用XSendEvent发送它。

  • 好吧,我想我明白了。在 Windows 中,我只能在“WM_PAINT”消息期间绘制到窗口。在 X11 中,我似乎可以随时绘制到窗口,而不仅仅是响应“Expose”事件。因此,在这种情况下,我不需要生成暴露事件,我可以在准备好时进行绘制。 (2认同)