C 如何在X11中存储事件生成的数据?
我正在做一个事件驱动的项目,在x窗口上绘制形状。每当我在屏幕上单击鼠标时,都会生成新的x和y值。我的问题是:如何在下面的代码中存储不同的x和y值,假设每次单击鼠标时都会生成新的x和y值C 如何在X11中存储事件生成的数据?,c,x11,C,X11,我正在做一个事件驱动的项目,在x窗口上绘制形状。每当我在屏幕上单击鼠标时,都会生成新的x和y值。我的问题是:如何在下面的代码中存储不同的x和y值,假设每次单击鼠标时都会生成新的x和y值 int x, y; x = report.xbutton.x; y = report.xbutton.y; if (report.xbutton.button == Button1) { XFillArc(display_ptr, win, gc_red, x - win_h
int x, y;
x = report.xbutton.x;
y = report.xbutton.y;
if (report.xbutton.button == Button1) {
XFillArc(display_ptr, win, gc_red,
x - win_height/80, y - win_height/80,
win_height/60, win_height/60, 0, 360*64);
}
代码的一个版本可能是:
typedef struct Position
{
int x;
int y;
} Position;
typedef struct PosnList
{
size_t num_pts;
size_t max_pts;
Position *points;
} PosnList;
void add_point(int x, int y, PosnList *p)
{
if (p->num_pts >= p->max_pts)
{
size_t new_num = (p->max_pts + 2) * 2;
Position *new_pts = realloc(p->points, new_num * sizeof(Position));
if (new_pts == 0)
...handle out of memory error...
p->max_pts = new_num;
p->points = new_pts;
}
p->points[p->num_pts++] = (Position){ x, y };
}
void zap_posnlist(PosnList *p)
{
free(p->points);
p->num_pts = 0;
p->max_pts = 0;
p->points = 0;
}
那么您的代码将执行以下操作:
int x, y;
x = report.xbutton.x;
y = report.xbutton.y;
if (report.xbutton.button == Button1) {
XFillArc(display_ptr, win, gc_red,
x - win_height/80, y - win_height/80,
win_height/60, win_height/60, 0, 360*64);
add_point(x, y, &positions);
}
在某个地方有一个变量:
PosnList positions = { 0, 0, 0 };
请注意,add_point()
函数使用realloc()
完成初始内存分配和增量内存分配。代码使用C99复合文字将值x
和y
分配给数组中的下一个位置。如果你没有C99,你需要做两个独立的作业
zap_posnlist()
函数释放先前初始化的posnlist
。您可能仍然需要一个正式的初始值设定项函数,除非您愿意使用posnlistxxx={0,0,0}代码>符号无处不在
此代码现在已由GCC清理;最初的版本不是这样的,而且里面有一些bug——这些bug会导致编译器错误
测试代码-请注意,“stderr.h”
不是标准头,而是我习惯使用的错误报告代码。它提供了err\u error()
和err\u setarg0()
函数
#include <stdlib.h>
#include "stderr.h"
typedef struct Position
{
int x;
int y;
} Position;
typedef struct PosnList
{
size_t num_pts;
size_t max_pts;
Position *points;
} PosnList;
extern void add_point(int x, int y, PosnList *p);
extern void zap_posnlist(PosnList *p);
void add_point(int x, int y, PosnList *p)
{
if (p->num_pts >= p->max_pts)
{
size_t new_num = (p->max_pts + 2) * 2;
Position *new_pts = realloc(p->points, new_num * sizeof(Position));
if (new_pts == 0)
err_error("Out of memory (%s:%d - %zu bytes)\n",
__FILE__, __LINE__, new_num * sizeof(Position));
p->max_pts = new_num;
p->points = new_pts;
}
p->points[p->num_pts++] = (Position){ x, y };
}
void zap_posnlist(PosnList *p)
{
free(p->points);
p->num_pts = 0;
p->max_pts = 0;
p->points = 0;
}
#include <stdio.h>
int main(int argc, char **argv)
{
PosnList positions = { 0, 0, 0 };
err_setarg0(argv[0]);
if (argc > 1)
srand(atoi(argv[1]));
for (size_t i = 0; i < 37; i++)
add_point(rand(), rand(), &positions);
for (size_t i = 0; i < positions.num_pts; i++)
printf("%2zu: (%5d, %5d)\n", i, positions.points[i].x, positions.points[i].y);
zap_posnlist(&positions);
return(0);
}
#包括
#包括“stderr.h”
类型定义结构位置
{
int x;
int-y;
}位置;
类型定义结构PosnList
{
大小/数量/分;
最大尺寸;
位置*点;
}PosnList;
外部无效添加点(int x,int y,PosnList*p);
外部无效zap_posnlist(posnlist*p);
无效添加点(整数x,整数y,PosnList*p)
{
如果(p->num\u pts>=p->max\u pts)
{
大小新数量=(p->max\u pts+2)*2;
位置*new_pts=realloc(p->points,new_num*sizeof(位置));
如果(新值==0)
错误(“内存不足(%s:%d-%zu字节)\n”,
__文件uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
p->max\u pts=新数量;
p->点=新点;
}
p->points[p->num_pts++]=(位置){x,y};
}
void zap_posnlist(posnlist*p)
{
免费(p->点数);
p->num_pts=0;
p->max_pts=0;
p->points=0;
}
#包括
int main(int argc,字符**argv)
{
PosnList positions={0,0,0};
err_setarg0(argv[0]);
如果(argc>1)
srand(atoi(argv[1]);
对于(大小i=0;i<37;i++)
添加_点(rand()、rand()和位置);
对于(大小i=0;i
如果您想要stderr.h
和stderr.c
的源代码,请与我联系(请参阅我的个人资料),您是否可以将这些值添加到动态分配的值列表中?您可能需要一个包含x和y元素的结构,并且您需要一个函数来管理这些值的列表?请您向我展示一下该函数的外观,因为我现在不感谢您的帮助。我试过代码,它编译了,但当我执行它时,它说“分段错误,内核掉了”。而且,我很困惑&位置在加分中的价值是什么。再次感谢,代码没有编译干净;它坏了(对不起!)。我已经修复了它,并删除了“买主须知”标志,因为我非常确定它现在是好的。谢谢…它现在可以工作了,虽然我仍然使用第一个代码,但现在正在使用malloc。。。。但它仍然是一样的,我不能把x和y的不同值变成一个方程。它一直使用相同的x和y