我想使用BGI执行 DDA 画线算法。有时当我运行我的程序时它工作正常,有时它会因以下错误而中止:
[xcb] 处理队列时序列号未知 [xcb] 这很可能是一个多线程客户端,并且没有调用 XInitThreads [xcb] 正在中止,对此感到抱歉。a.out:../../src/xcb_io.c:274:poll_for_event:断言“!xcb_xlib_threads_sequence_lost”失败。中止(核心转储)
我尝试调用 XInitThreads 和之前为这个问题建议的一些其他解决方案。但它仍然在大多数时候崩溃。
这是我的代码:
#include <stdio.h>
#include <graphics.h>
#include <math.h>
#include <X11/Xlib.h>
void drawLine(int startX, int startY, int endX, int endY) {
XInitThreads();
//DDA Line Drawing Algorithm
float deltaX, deltaY, step;
int x = startX, y = endY;
deltaX = (float)(endX - startX);
deltaY = (float)(endY - startY);
if(abs(deltaX) > abs(deltaY)) {
step = deltaX;
}
else {
step = deltaY;
}
putpixel(x, y,15);
for(int i = 0; i < abs(step); i++) {
x = (int)(x + deltaX/step + 0.5);
y = (int)(y + deltaY/step + 0.5);
putpixel(x, y,15);
delay(100);
}
}
int main() {
XInitThreads();
//Graphics Initialization.
int gd = DETECT,gm;
initgraph(&gd, &gm, NULL);
//Actual Code.
int startX, startY, endX, endY, in = 0;
printf("Starting point:\nX = ");
scanf("%d", &startX);
printf("\nY = ");
scanf("%d", &startY);
printf("Ending point:\nX = ");
scanf("%d", &endX);
printf("\nY = ");
scanf("%d", &endY);
delay(1000);
drawLine(startX, startY, endX, endY);
//solution provided for same question asked previously.
while(in == 0) {
in = getchar();
}
closegraph();
return 0;
}