-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphix.c
173 lines (152 loc) · 4.16 KB
/
graphix.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
//******************************************************************************
#include "graphix.h"
#include <X11/Xlib.h>
#include <stdlib.h>
#include <assert.h>
struct graphix
{
int width;
int height;
int colors;
Display *display;
Window window;
GC gc[1];
};
//******************************************************************************
// https://tronche.com/gui/x/xlib/events/processing-overview.html
graphix graphix_create(const char *title, int width, int height,
int colors, const char *color[])
{
assert(width > 0);
assert(height > 0);
assert(colors > 0);
assert(colors < 256);
graphix gx = malloc(sizeof(struct graphix) + (colors - 1) * sizeof(GC));
assert(gx);
gx->width = width;
gx->height = height;
gx->colors = colors;
gx->display = XOpenDisplay(NULL);
assert(gx->display);
XSync(gx->display, False);
int screen = DefaultScreen(gx->display);
gx->window = XCreateSimpleWindow
(
gx->display, RootWindow(gx->display, screen),
0, 0, // x, y
(unsigned int)width, (unsigned int)height, // w, h
1, // border
BlackPixel(gx->display, screen), BlackPixel(gx->display, screen)
);
XStoreName(gx->display, gx->window, title);
XSelectInput(gx->display, gx->window, ExposureMask | StructureNotifyMask);
XMapWindow(gx->display, gx->window);
XFlush(gx->display);
for (int events = 0; events < 4;)
{
XEvent event;
XNextEvent(gx->display, &event);
if (event.type == Expose)
events++;
if (event.type == MapNotify)
events++;
}
XSelectInput(gx->display, gx->window, ButtonPressMask);
Colormap colormap = DefaultColormap(gx->display, screen);
for (int i = 0; i < colors; i++)
{
XColor xcolor;
Status status = XAllocNamedColor(gx->display, colormap, color[i], &xcolor, &xcolor);
assert(status);
XGCValues values;
gx->gc[i] = XCreateGC(gx->display, gx->window, 0L, &values);
assert(gx->gc[i]);
XSetForeground(gx->display, gx->gc[i], xcolor.pixel);
XSetBackground(gx->display, gx->gc[i],
BlackPixel(gx->display, screen));
XSetLineAttributes(gx->display, gx->gc[i], 1L, LineSolid, CapButt, JoinBevel);
XSetFillStyle(gx->display, gx->gc[i], FillSolid);
}
return gx;
}
void graphix_free(graphix gx)
{
assert(gx);
XCloseDisplay(gx->display);
free(gx);
}
int graphix_width(const graphix gx)
{
assert(gx);
return gx->width;
}
int graphix_height(const graphix gx)
{
assert(gx);
return gx->height;
}
//******************************************************************************
void graphix_point(graphix gx, int x, int y, int color)
{
assert(gx);
assert(x >= 0);
assert(x < gx->width);
assert(y >= 0);
assert(y < gx->height);
assert(color >= 0);
assert(color < gx->colors);
XDrawPoint(gx->display, gx->window, gx->gc[color], x, y);
}
void graphix_line(graphix gx, int x, int y, int xx, int yy, int color)
{
assert(gx);
assert(x >= 0);
assert(x < gx->width);
assert(y >= 0);
assert(y < gx->height);
assert(xx >= 0);
assert(xx < gx->width);
assert(yy >= 0);
assert(yy < gx->height);
assert(color >= 0);
assert(color < gx->colors);
XDrawLine(gx->display, gx->window, gx->gc[color], x, y, xx, yy);
}
void graphix_rect(graphix gx, int x, int y, int width, int height, int color)
{
assert(gx);
assert(x >= 0);
assert(x < gx->width);
assert(y >= 0);
assert(y < gx->height);
assert(width >= 0);
assert(x + width <= gx->width);
assert(height >= 0);
assert(y + height <= gx->height);
assert(color >= 0);
assert(color < gx->colors);
XFillRectangle(gx->display, gx->window, gx->gc[color],
x, y, (unsigned int)width, (unsigned int)height);
}
void graphix_flush(graphix gx)
{
assert(gx);
XFlush(gx->display);
}
//******************************************************************************
int graphix_button(graphix gx)
{
assert(gx);
XEvent event;
if (XCheckMaskEvent(gx->display, ButtonPressMask, &event))
return ((XButtonEvent*)&event)->button;
return 0;
}
int graphix_wait(graphix gx)
{
assert(gx);
XEvent event;
XMaskEvent(gx->display, ButtonPressMask, &event);
return ((XButtonEvent*)&event)->button;
}
//******************************************************************************