-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrgwin_graph.c
More file actions
203 lines (165 loc) · 5.46 KB
/
rgwin_graph.c
File metadata and controls
203 lines (165 loc) · 5.46 KB
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
201
202
/* rgwin_graph.c: The graphics window type
for RemGlk, remote-procedure-call implementation of the Glk API.
Designed by Andrew Plotkin <erkyrath@eblong.com>
http://eblong.com/zarf/glk/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "glk.h"
#include "remglk.h"
#include "rgdata.h"
#include "rgwin_graph.h"
window_graphics_t *win_graphics_create(window_t *win)
{
window_graphics_t *dwin = (window_graphics_t *)malloc(sizeof(window_graphics_t));
dwin->owner = win;
dwin->numcontent = 0;
dwin->contentsize = 4;
dwin->content = (data_specialspan_t **)malloc(dwin->contentsize * sizeof(data_specialspan_t *));
dwin->updatemark = 0;
dwin->graphwidth = 0;
dwin->graphheight = 0;
return dwin;
}
void win_graphics_destroy(window_graphics_t *dwin)
{
dwin->owner = NULL;
if (dwin->content) {
long px;
for (px=0; px<dwin->numcontent; px++)
data_specialspan_free(dwin->content[px]);
free(dwin->content);
dwin->content = NULL;
}
free(dwin);
}
void win_graphics_clear(window_t *win)
{
window_graphics_t *dwin = win->data;
/* If the background color has been set, we must retain that entry.
(The last setcolor, if there are several.) */
data_specialspan_t *setcolspan = NULL;
int setcolmarked = FALSE;
/* Discard all the content entries, except for the last setcolor. */
long px;
for (px=0; px<dwin->numcontent; px++) {
data_specialspan_t *span = dwin->content[px];
dwin->content[px] = NULL;
if (span->type == specialtype_SetColor) {
if (setcolspan)
data_specialspan_free(setcolspan);
setcolspan = span;
setcolmarked = (px >= dwin->updatemark);
}
else {
data_specialspan_free(span);
}
}
dwin->numcontent = 0;
dwin->updatemark = 0;
/* Put back the setcolor (if found). Note that contentsize is at
least 4. */
if (setcolspan) {
dwin->content[dwin->numcontent] = setcolspan;
dwin->numcontent++;
if (!setcolmarked)
dwin->updatemark++;
}
/* Clear to background color. */
data_specialspan_t *fillspan = data_specialspan_alloc(specialtype_Fill);
dwin->content[dwin->numcontent] = fillspan;
dwin->numcontent++;
}
void win_graphics_rearrange(window_t *win, grect_t *box, data_metrics_t *metrics)
{
window_graphics_t *dwin = win->data;
dwin->owner->bbox = *box;
int width = box->right - box->left;
int height = box->bottom - box->top;
dwin->graphwidth = (int) floor(width - metrics->graphicsmarginx);
if (dwin->graphwidth < 0)
dwin->graphwidth = 0;
dwin->graphheight = (int) floor(height - metrics->graphicsmarginy);
if (dwin->graphheight < 0)
dwin->graphheight = 0;
}
void win_graphics_putspecial(window_t *win, data_specialspan_t *span)
{
window_graphics_t *dwin = win->data;
if (dwin->numcontent >= dwin->contentsize) {
dwin->contentsize *= 2;
dwin->content = (data_specialspan_t **)realloc(dwin->content,
dwin->contentsize * sizeof(data_specialspan_t *));
}
dwin->content[dwin->numcontent] = span;
dwin->numcontent++;
}
data_content_t *win_graphics_update(window_t *win)
{
window_graphics_t *dwin = win->data;
data_content_t *dat = NULL;
if (dwin->numcontent > dwin->updatemark) {
long px;
dat = data_content_alloc(win->updatetag, win->type);
data_line_t *line = data_line_alloc();
gen_list_append(&dat->lines, line);
for (px=dwin->updatemark; px<dwin->numcontent; px++) {
data_specialspan_t *span = dwin->content[px];
data_line_add_specialspan(line, span);
}
dwin->updatemark = dwin->numcontent;
}
return dat;
}
void win_graphics_trim_buffer(window_t *win)
{
window_graphics_t *dwin = win->data;
/* If a whole-window fill command has been sent, we're going to drop
all commands before it. Except that we save the last setcolor
of the trimmed range. */
long px;
long lastfill = -1;
for (px=0; px<dwin->updatemark; px++) {
data_specialspan_t *span = dwin->content[px];
if (span->type == specialtype_Fill && !span->hasdimensions) {
lastfill = px;
}
}
if (lastfill <= 0) {
return;
}
data_specialspan_t *lastsetcol = NULL;
for (px=0; px<lastfill; px++) {
data_specialspan_t *span = dwin->content[px];
dwin->content[px] = NULL;
if (span->type == specialtype_SetColor) {
if (lastsetcol)
data_specialspan_free(lastsetcol);
lastsetcol = span;
}
else {
data_specialspan_free(span);
}
}
long delta;
if (!lastsetcol) {
delta = lastfill;
if (lastfill > 0 && lastfill < dwin->numcontent)
memmove(dwin->content, &dwin->content[lastfill],
(dwin->numcontent-lastfill) * sizeof(data_specialspan_t *));
}
else {
delta = lastfill-1;
dwin->content[0] = lastsetcol;
if (lastfill > 1 && lastfill < dwin->numcontent)
memmove(&dwin->content[1], &dwin->content[lastfill],
(dwin->numcontent-lastfill) * sizeof(data_specialspan_t *));
}
dwin->numcontent -= delta;
if (dwin->updatemark >= lastfill)
dwin->updatemark -= delta;
else
dwin->updatemark = 0;
}