Xlib 不会画任何东西
Xlib won't draw anything
我很纠结于我做错了什么,我尝试过复制示例代码,我尝试过更改颜色。这是我的代码:
//Headers
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
int main(int argc, char *argv[])
{
//Vars to create a window
Display *dis;
int screen;
Window win;
XEvent event;
//Graphics content
XGCValues values;
unsigned long valuemask = 0;
GC gc;
//To store mouse location
int mouseX[2], mouseY[2];
//Stores screen dimensions
XWindowAttributes xwa;
int screenHeight, screenWidth;
//Colors
Colormap colormap;
XColor backgroundColor, white;
//Checks for open display
dis = XOpenDisplay(NULL);
//Displays error
if(dis == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
//Sets screen
screen = DefaultScreen(dis);
colormap = DefaultColormap(dis, screen);
//Background color
XParseColor(dis, colormap, "#75677e", &backgroundColor);
XAllocColor(dis, colormap, &backgroundColor);
//White
XParseColor(dis, colormap, "#ffffff", &white);
XAllocColor(dis, colormap, &white);
//Creates window
win = XCreateSimpleWindow(dis, RootWindow(dis, screen), 100, 100, 500, 300, 1, BlackPixel(dis, screen), backgroundColor.pixel);
//Changes window to be full screen
//Atom atoms[2] = { XInternAtom(dis, "_NET_WM_STATE_FULLSCREEN", False), None };
//XChangeProperty(dis, win, XInternAtom(dis, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *)atoms, 1);
//Allocates graphics content
gc = XCreateGC(dis, win, valuemask, &values);
XSetLineAttributes(dis, gc, 2, LineSolid, CapButt, JoinBevel);
XSetFillStyle(dis, gc, FillSolid);
XSync(dis, False);
//Stores screen dimensions
//TODO: test
XGetWindowAttributes(dis, win, &xwa);
screenWidth = xwa.width;
screenHeight = xwa.height;
//Inner circle
//XFillArc(dis, win)
XSetForeground(dis, gc, BlackPixel(dis, screen));
XFillRectangle(dis, win, gc, 0, 100, 50, 50);
//Listens for input
XSelectInput(dis, win, ExposureMask | KeyPressMask);
//Maps window
XMapWindow(dis, win);
while(1)
{
XNextEvent(dis, &event);
}
XCloseDisplay(dis);
return 0;
}
当我 运行 它时,我在控制台中没有收到任何错误,与 BadDrawable 或任何东西无关。它打开 window 就好了,但屏幕上没有出现矩形。我也试过让它画线、点和弧。
这不是一个权威的答案,因为我对这个主题的了解充其量是粗略的,但你的事件循环看起来很空洞。当收到 Expose
事件时,需要重新绘制 window。
例如:
while(1)
{
XNextEvent(dis, &event);
switch(event.type) {
case Expose:
if (event.xexpose.count) break;
XFillRectangle(dis, win, gc, 0, 100, 50, 50);
break;
default:
break;
}
}
我很纠结于我做错了什么,我尝试过复制示例代码,我尝试过更改颜色。这是我的代码:
//Headers
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
int main(int argc, char *argv[])
{
//Vars to create a window
Display *dis;
int screen;
Window win;
XEvent event;
//Graphics content
XGCValues values;
unsigned long valuemask = 0;
GC gc;
//To store mouse location
int mouseX[2], mouseY[2];
//Stores screen dimensions
XWindowAttributes xwa;
int screenHeight, screenWidth;
//Colors
Colormap colormap;
XColor backgroundColor, white;
//Checks for open display
dis = XOpenDisplay(NULL);
//Displays error
if(dis == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}
//Sets screen
screen = DefaultScreen(dis);
colormap = DefaultColormap(dis, screen);
//Background color
XParseColor(dis, colormap, "#75677e", &backgroundColor);
XAllocColor(dis, colormap, &backgroundColor);
//White
XParseColor(dis, colormap, "#ffffff", &white);
XAllocColor(dis, colormap, &white);
//Creates window
win = XCreateSimpleWindow(dis, RootWindow(dis, screen), 100, 100, 500, 300, 1, BlackPixel(dis, screen), backgroundColor.pixel);
//Changes window to be full screen
//Atom atoms[2] = { XInternAtom(dis, "_NET_WM_STATE_FULLSCREEN", False), None };
//XChangeProperty(dis, win, XInternAtom(dis, "_NET_WM_STATE", False), XA_ATOM, 32, PropModeReplace, (unsigned char *)atoms, 1);
//Allocates graphics content
gc = XCreateGC(dis, win, valuemask, &values);
XSetLineAttributes(dis, gc, 2, LineSolid, CapButt, JoinBevel);
XSetFillStyle(dis, gc, FillSolid);
XSync(dis, False);
//Stores screen dimensions
//TODO: test
XGetWindowAttributes(dis, win, &xwa);
screenWidth = xwa.width;
screenHeight = xwa.height;
//Inner circle
//XFillArc(dis, win)
XSetForeground(dis, gc, BlackPixel(dis, screen));
XFillRectangle(dis, win, gc, 0, 100, 50, 50);
//Listens for input
XSelectInput(dis, win, ExposureMask | KeyPressMask);
//Maps window
XMapWindow(dis, win);
while(1)
{
XNextEvent(dis, &event);
}
XCloseDisplay(dis);
return 0;
}
当我 运行 它时,我在控制台中没有收到任何错误,与 BadDrawable 或任何东西无关。它打开 window 就好了,但屏幕上没有出现矩形。我也试过让它画线、点和弧。
这不是一个权威的答案,因为我对这个主题的了解充其量是粗略的,但你的事件循环看起来很空洞。当收到 Expose
事件时,需要重新绘制 window。
例如:
while(1)
{
XNextEvent(dis, &event);
switch(event.type) {
case Expose:
if (event.xexpose.count) break;
XFillRectangle(dis, win, gc, 0, 100, 50, 50);
break;
default:
break;
}
}