-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsheet.c
304 lines (277 loc) · 9.27 KB
/
sheet.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "bootpack.h"
#define SHEET_USE 1
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)
{
struct SHTCTL *ctl;
int i;
ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof(struct SHTCTL));
if (ctl == 0) {
goto err;
}
ctl->map = (unsigned char *) memman_alloc_4k(memman, xsize * ysize);
if (ctl->map == 0) {
memman_free_4k(memman, (int) ctl, sizeof(struct SHTCTL));
goto err;
}
ctl->vram = vram;
ctl->xsize = xsize;
ctl->ysize = ysize;
ctl->top = -1;
for (i = 0; i < MAX_SHEETS; i++) {
ctl->sheets0[i].flags = 0; /* 未使用マーク */
ctl->sheets0[i].ctl = ctl; /* 所属を記録 */
}
err:
return ctl;
}
struct SHEET *sheet_alloc(struct SHTCTL *ctl)
{
struct SHEET *sht;
int i;
for (i = 0; i < MAX_SHEETS; i++) {
if (ctl->sheets0[i].flags == 0) {
sht = &ctl->sheets0[i];
sht->flags = SHEET_USE; /* 使用中マーク */
sht->height = -1; /* 非表示中 */
sht->task = 0; /* 自動で閉じる機能を使わない */
return sht;
}
}
return 0; /* 全てのシートが使用中だった */
}
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)
{
sht->buf = buf;
sht->bxsize = xsize;
sht->bysize = ysize;
sht->col_inv = col_inv;
return;
}
void sheet_updown(struct SHEET *sht, int height)
{
struct SHTCTL *ctl = sht->ctl;
int h, old = sht->height; /* 設定前の高さを記憶する */
/* 指定が低すぎや高すぎだったら、修正する */
if (height > ctl->top + 1) {
height = ctl->top + 1;
}
if (height < -1) {
height = -1;
}
sht->height = height; /* 高さを設定 */
/* 以下は主にsheet[]の並べ替え */
if (old > height) { /* 以前より低くなる */
if (height >= 0) {
/* 間のものを引き上げる */
for (h = old; h > height; h--) {
ctl->sheets[h] = ctl->sheets[h - 1];
ctl->sheets[h]->height = h;
}
ctl->sheets[height] = sht;
/* 新しい下敷きの情報に沿って画面を描き直す */
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height + 1);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height + 1, old);
} else { /* 非表示化 */
if (ctl->top > old) {
/* 上になっているものを下ろす */
for (h = old; h < ctl->top; h++) {
ctl->sheets[h] = ctl->sheets[h + 1];
ctl->sheets[h]->height = h;
}
}
ctl->top--; /* 表示中の下敷きが1つ減るので一番上の高さが減る */
/* 新しい下敷きの情報に沿って画面を描き直す */
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, 0);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, 0, old - 1);
}
} else if (old < height) { /* 以前より高くなる */
if (old >= 0) {
/* 間のものを押し下げる */
for (h = old; h < height; h++) {
ctl->sheets[h] = ctl->sheets[h + 1];
ctl->sheets[h]->height = h;
}
ctl->sheets[height] = sht;
} else {
/* 上になるものを持ち上げる */
for (h = ctl->top; h >= height; h--) {
ctl->sheets[h + 1] = ctl->sheets[h];
ctl->sheets[h + 1]->height = h + 1;
}
ctl->sheets[height] = sht;
ctl->top++; /* 表示中の下敷きが1つ増えるので、一番上の高さを変える */
}
/* 新しい下敷きの情報に沿って画面を描き直す */
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize, height, height);
}
return;
}
void sheet_refresh(struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
if (sht->height >= 0) {
sheet_refreshsub(sht->ctl, sht->vx0 + bx0, sht->vy0 + by0,
sht->vx0 + bx1, sht->vy0 + by1, sht->height, sht->height);
}
return;
}
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0, int h1)
{
int h, bx, by, vx, vy, bx0, by0, bx1, by1, bx2, sid4, i, i1, *p, *q, *r;
unsigned char *buf, c, *vram = ctl->vram, *map = ctl->map, sid;
struct SHEET *sht;
/* リフレッシュ範囲が画面外にはみ出していたら修正。 */
if (vx0 < 0) { vx0 = 0; }
if (vy0 < 0) { vy0 = 0; }
if (vx1 > ctl->xsize) { vx1 = ctl->xsize; }
if (vy1 > ctl->ysize) { vy1 = ctl->ysize; }
for (h = h0; h <= h1; h++) {
sht = ctl->sheets[h];
buf = sht->buf;
sid = sht - ctl->sheets0;
/* vx0〜vy1を使ってbx0〜by1を逆算する */
bx0 = vx0 - sht->vx0;
by0 = vy0 - sht->vy0;
bx1 = vx1 - sht->vx0;
by1 = vy1 - sht->vy0;
if (bx0 < 0) { bx0 = 0; }
if (by0 < 0) { by0 = 0; }
if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
if (by1 > sht->bysize) { by1 = sht->bysize; }
if ((sht->vx0 & 3) == 0) {
/* 4バイト型 */
i = (bx0 + 3) / 4; /* bx0を4バイト単位にする(端数切り上げ) */
i1 = bx1 / 4; /* bx1を4バイト単位にする(端数切捨て) */
i1 = i1 - i;
sid4 = sid | sid << 8 | sid << 16 | sid << 24;
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1 && (bx & 3) != 0; bx++) { /* 前の端数を1バイトずつ */
vx = sht->vx0 + bx;
if (map[vy * ctl->xsize + vx] == sid) {
vram[vy * ctl->xsize + vx] = buf[by * sht->bxsize + bx];
}
}
vx = sht->vx0 + bx;
p = (int *) &map[vy * ctl->xsize + vx];
q = (int *) &vram[vy * ctl->xsize + vx];
r = (int *) &buf[by * sht->bxsize + bx];
for (i = 0; i < i1; i++) { /* 4の倍数部分 */
if (p[i] == sid4) {
q[i] = r[i];
} else {
bx2 = bx + i * 4;
vx = sht->vx0 + bx2;
if (map[vy * ctl->xsize + vx + 0] == sid) {
vram[vy * ctl->xsize + vx + 0] = buf[by * sht->bxsize + bx2 + 0];
}
if (map[vy * ctl->xsize + vx + 1] == sid) {
vram[vy * ctl->xsize + vx + 1] = buf[by * sht->bxsize + bx2 + 1];
}
if (map[vy * ctl->xsize + vx + 2] == sid) {
vram[vy * ctl->xsize + vx + 2] = buf[by * sht->bxsize + bx2 + 2];
}
if (map[vy * ctl->xsize + vx + 3] == sid) {
vram[vy * ctl->xsize + vx + 3] = buf[by * sht->bxsize + bx2 + 3];
}
}
}
for (bx += i1 * 4; bx < bx1; bx++) { /* 後ろの端数を1バイトずつ */
vx = sht->vx0 + bx;
if (map[vy * ctl->xsize + vx] == sid) {
vram[vy * ctl->xsize + vx] = buf[by * sht->bxsize + bx];
}
}
}
} else {
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1; bx++) {
vx = sht->vx0 + bx;
if (map[vy * ctl->xsize + vx] == sid) {
vram[vy * ctl->xsize + vx] = buf[by * sht->bxsize + bx];
}
}
}
}
}
return;
}
void sheet_refreshmap(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0)
{
int h, bx, by, vx, vy, bx0, by0, bx1, by1, sid4, *p;
unsigned char *buf, sid, *map = ctl->map;
struct SHEET *sht;
if (vx0 < 0) { vx0 = 0; }
if (vy0 < 0) { vy0 = 0; }
if (vx1 > ctl->xsize) { vx1 = ctl->xsize; }
if (vy1 > ctl->ysize) { vy1 = ctl->ysize; }
for (h = h0; h <= ctl->top; h++) {
sht = ctl->sheets[h];
sid = sht - ctl->sheets0;
buf = sht->buf;
bx0 = vx0 - sht->vx0;
by0 = vy0 - sht->vy0;
bx1 = vx1 - sht->vx0;
by1 = vy1 - sht->vy0;
if (bx0 < 0) { bx0 = 0; }
if (by0 < 0) { by0 = 0; }
if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
if (by1 > sht->bysize) { by1 = sht->bysize; }
if (sht->col_inv == -1) {
if ((sht->vx0 & 3) == 0 && (bx0 & 3) == 0 && (bx1 & 3) == 0) {
/* 透明色なし専用の高速版(4バイト型) */
bx1 = (bx1 - bx0) / 4; /* MOV回数 */
sid4 = sid | sid << 8 | sid << 16 | sid << 24;
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
vx = sht->vx0 + bx0;
p = (int *) &map[vy * ctl->xsize + vx];
for (bx = 0; bx < bx1; bx++) {
p[bx] = sid4;
}
}
} else {
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1; bx++) {
vx = sht->vx0 + bx;
map[vy * ctl->xsize + vx] = sid;
}
}
}
} else {
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1; bx++) {
vx = sht->vx0 + bx;
if (buf[by * sht->bxsize + bx] != sht->col_inv) {
map[vy * ctl->xsize + vx] = sid;
}
}
}
}
}
return;
}
void sheet_slide(struct SHEET *sht, int vx0, int vy0)
{
int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
sht->vx0 = vx0;
sht->vy0 = vy0;
if (sht->height >= 0) { /* もしも表示中なら、新しい下敷き情報に従って画面を描き直す */
sheet_refreshmap(sht->ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize, 0);
sheet_refreshmap(sht->ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height);
sheet_refreshsub(sht->ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize, 0, sht->height - 1);
sheet_refreshsub(sht->ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height, sht->height);
}
return;
}
void sheet_free(struct SHEET *sht)
{
if (sht->height >= 0) {
sheet_updown(sht, -1); /* 表示中ならまず非表示にする */
}
sht->flags = 0; /* 未使用マーク */
return;
}