/
Disp.c
200 lines (156 loc) · 6.35 KB
/
Disp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "DispP.h"
#include "etc.h"
Display * Disp_GetDisplay(Disp disp) { return (disp->display); }
Colormap Disp_GetColormap(Disp disp) { return (disp->colormap); }
int Disp_GetWidth( Disp disp) { return (disp->width ); }
int Disp_GetHeight(Disp disp) { return (disp->height); }
/*===========================================================================*/
/* 色の取得 */
/*===========================================================================*/
unsigned long Disp_GetPixel(Disp disp, char * color_name)
{
XColor c0, c1;
XAllocNamedColor(disp->display, disp->colormap, color_name, &c0, &c1);
return (c0.pixel);
}
/*===========================================================================*/
/* GC の作成と解放 */
/*===========================================================================*/
GC Disp_CreateGC(Disp disp)
{
GC gc;
gc = XCreateGC(disp->display, disp->root_window, 0, 0);
return (gc);
}
int Disp_DestroyGC(Disp disp, GC gc)
{
XFreeGC(disp->display, gc);
return (0);
}
/*===========================================================================*/
/* オブジェクトの生成と削除 */
/*===========================================================================*/
/*---------------------------------------------------------------------------*/
/* オブジェクトの生成 */
/*---------------------------------------------------------------------------*/
Disp Disp_Create(char * display_name, int direct_draw)
{
Disp disp;
unsigned long int pixel;
disp = (Disp)malloc(sizeof(_Disp));
if (!disp) Error("Disp_Create", "Cannot allocate memory.");
disp->display = XOpenDisplay(display_name);
if (disp->display == NULL) Error("Disp_Create", "Cannot open display.");
disp->screen = DefaultScreen(disp->display);
disp->root_window = RootWindow(disp->display, disp->screen);
disp->colormap = DefaultColormap(disp->display, disp->screen);
disp->width = DisplayWidth( disp->display, disp->screen);
disp->height = DisplayHeight(disp->display, disp->screen);
disp->depth = DefaultDepth(disp->display, disp->screen);
disp->direct_draw = direct_draw;
if (disp->direct_draw) {
disp->pixmap = NULL;
} else {
disp->pixmap = XCreatePixmap(disp->display, disp->root_window,
disp->width, disp->height, disp->depth);
}
return (disp);
}
/*---------------------------------------------------------------------------*/
/* オブジェクトの削除 */
/*---------------------------------------------------------------------------*/
Disp Disp_Destroy(Disp disp)
{
if (!disp) return (NULL);
if (disp->pixmap) XFreePixmap(disp->display, disp->pixmap);
if (disp->display) XCloseDisplay(disp->display);
free(disp);
return (NULL);
}
/*---------------------------------------------------------------------------*/
/* フラッシュ */
/*---------------------------------------------------------------------------*/
int Disp_Flush(Disp disp)
{
XEvent ev;
if (!disp->direct_draw) {
#if 0
XSetWindowBackgroundPixmap(disp->display, disp->root_window, disp->pixmap);
#endif
XClearWindow(disp->display, disp->root_window);
}
/* イベントのフラッシュ */
while (XPending(disp->display)) {
XNextEvent(disp->display, &ev);
}
XFlush(disp->display);
return (0);
}
/*---------------------------------------------------------------------------*/
/* X サーバとの Sync.(イベントキューを廃棄する) */
/*---------------------------------------------------------------------------*/
int Disp_Sync(Disp disp)
{
Disp_Flush(disp); /* リクエストのフラッシュ */
#if 1
XSync(disp->display, True); /* イベントキュー内のイベントを廃棄する */
#else
XSync(disp->display, False); /* イベントキュー内のイベントを廃棄しない */
#endif
return (0);
}
/*---------------------------------------------------------------------------*/
/* ピックスマップのクリア */
/*---------------------------------------------------------------------------*/
int Disp_ClearPixmap(Disp disp, GC gc)
{
if (disp->pixmap)
XFillRectangle(disp->display, disp->pixmap, gc,
0, 0, disp->width, disp->height);
return (0);
}
/*---------------------------------------------------------------------------*/
/* ディスプレイのクリア */
/*---------------------------------------------------------------------------*/
int Disp_ClearDisplay(Disp disp, unsigned long pixel)
{
if (disp->direct_draw) {
XSetWindowBackground(disp->display, disp->root_window, pixel);
} else {
XSetWindowBackgroundPixmap(disp->display, disp->root_window, disp->pixmap);
}
XClearWindow(disp->display, disp->root_window);
Disp_Flush(disp);
return (0);
}
/*---------------------------------------------------------------------------*/
/* 円の描画 */
/*---------------------------------------------------------------------------*/
int Disp_DrawFilledCircle(Disp disp, GC gc, int x, int y, int r)
{
if (x + r < 0 || disp->width - 1 < x - r ||
y + r < 0 || disp->height - 1 < y - r) return (0);
if (disp->direct_draw)
XFillArc(disp->display, disp->root_window, gc,
x - r, y - r, r * 2 + 1, r * 2 + 1, 0, 360 * 64);
else
XFillArc(disp->display, disp->pixmap, gc,
x - r, y - r, r * 2 + 1, r * 2 + 1, 0, 360 * 64);
return (0);
}
int Disp_DrawFilledCircles(Disp disp, GC gc, XArc * arcs, int n)
{
if (n > 0) {
if (disp->direct_draw)
XFillArcs(disp->display, disp->root_window, gc, arcs, n);
else
XFillArcs(disp->display, disp->pixmap, gc, arcs, n);
}
return (0);
}
/*****************************************************************************/
/* ここまで */
/*****************************************************************************/
/*****************************************************************************/
/* End of File. */
/*****************************************************************************/