gpt4 book ai didi

C-X11 API : Graphics not Working in a While Loop

转载 作者:太空宇宙 更新时间:2023-11-04 02:43:50 24 4
gpt4 key购买 nike

我正在尝试动态绘制背景。图形正常工作很好......但是:

int width, height;
GC gc_backcolor;
XGCValues gcv_backcolor;

width = c_values.width;
height = c_values.height;

gcv_backcolor.background = c_values.backcolor;
gcv_backcolor.foreground = c_values.backcolor;
gc_backcolor = XCreateGC(display, canvas, GCBackground | GCForeground, &gcv_backcolor);

int x = 0;
int y = 0;

while (y < height) {
x = 0;

while (x < width) {
XDrawPoint(display, canvas, gc_backcolor, x, y);
x++;
}
y++;
}

x = 0;
y = 0;

...出于某种原因,当我循环运行它时,它不起作用。如果有人能向我解释为什么它会这样,我将不胜感激。

难道是我在canvas expose事件上调用了这个函数?

  while(1) {
XNextEvent(display, &event);

if (event.xany.window == canvas) {
if (event.type == Expose) {
Canvas_Draw(display, canvas, c_values);
}

}
}

这是我的主要代码:

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include "Canvas.h"
#include "Extensions.h"

int main(int argc, char* argv[])
{
Display* display;
int screen;
Window window;
Window canvas;
XEvent event;
GC gc_canvas;

//Canvas* canvas;

display = XOpenDisplay(NULL);

if (display == NULL) {
fprintf(stderr, "Error trying to open display\n");
exit(1);
}

screen = DefaultScreen(display);
window = XCreateSimpleWindow(display, RootWindow(display, screen), 0, 0, 640, 480, 1, BlackPixel(display, screen), WhitePixel(display, screen));

XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);

/* Create Canvas */

XGCValues gcv_canvas;
CanvasValues c_values;

gcv_canvas.foreground = 0xff00ff;
gcv_canvas.background = 0xff00ff;

canvas = XCreateSimpleWindow(display, window, 0, 0, 256, 256, 1, BlackPixel(display, screen), WhitePixel(display, screen));

gc_canvas = XCreateGC(display, canvas, GCForeground | GCBackground, &gcv_canvas);

XSelectInput(display, canvas, ExposureMask | KeyPressMask);
XMapWindow(display, canvas);

Canvas_Create_Content_Field(display, canvas, c_values);

c_values.backcolor = 0x00ff00;

//Canvas_Draw(display, canvas, c_values); this code only appears to work in the expose event

/* Create Canvas */

while(1) {
XNextEvent(display, &event);

if (event.xany.window == canvas) {
if (event.type == Expose) {
Canvas_Draw(display, canvas, c_values);
}

}
}

return 0;
}

最佳答案

您没有在外部 y 循环中重置 x。试试这个

while (y < height) {
x = 0; // <<--- insert here
while (x < width) {
XDrawPoint(display, canvas, gc_backcolor, x, y);
x++;
}
y++;
}

更新

您还将前景和背景设置为相同的颜色

gcv_backcolor.background = c_values.backcolor;
gcv_backcolor.foreground = c_values.backcolor;

返回的背景颜色用于绘制

gc_backcolor = XCreateGC(display, canvas, GCBackground | GCForeground, &gcv_backcolor);
...
XDrawPoint(display, canvas, gc_backcolor, x, y);

关于C-X11 API : Graphics not Working in a While Loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29316148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com