-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.c
327 lines (266 loc) · 6.11 KB
/
sprite.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* $Id: sprite.c,v 1.11 2002/01/24 16:24:51 nonaka Exp $ */
/*
* Copyright (c) 2001, 2002 NONAKA Kimihiro <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer as
* the first lines of this file unmodified.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "nscr.h"
#define MAX_CHR 3
typedef struct sprite_manage {
int flag;
image_t *image;
point_t point;
} sprmgr_t;
static sprmgr_t spr[MAX_SPRITE];
static sprmgr_t chr[MAX_CHR];
static long chr_priority = 25;
static long chr_underline = 479;
void
load_chr(unsigned char kind, unsigned char *filename)
{
int n;
_ASSERT(filename != NULL);
switch (kind) {
case 'c': case 'C':
n = 0;
break;
case 'l': case 'L':
n = 1;
break;
case 'r': case 'R':
n = 2;
break;
default:
_ASSERT(0);
return;
}
_ASSERT(0 <= n && n < MAX_CHR);
DPRINTF(("load character '%c'\n", kind));
if (chr[n].image)
img_destroy(chr[n].image);
chr[n].image = image_open(filename, TRANSMODE_TAG);
chr[n].flag = 1;
screen_update();
}
void
hide_chr(unsigned char kind)
{
int i;
switch (kind) {
case 'c': case 'C':
chr[0].flag = 0;
break;
case 'l': case 'L':
chr[1].flag = 0;
break;
case 'r': case 'R':
chr[2].flag = 0;
break;
case 'a': case 'A':
for (i = 0; i < MAX_CHR; i++)
chr[i].flag = 0;
break;
default:
_ASSERT(0);
return;
}
DPRINTF(("hide character '%c'\n", kind));
screen_update();
}
void
merge_chr(unsigned char kind, image_t *dest)
{
int n;
int xoff, yoff;
_ASSERT(dest != NULL);
switch (kind) {
case 'c': case 'C':
if (chr[0].image == NULL)
return;
n = 0;
xoff = (SCREEN_WIDTH - chr[0].image->width) / 2;
break;
case 'l': case 'L':
if (chr[1].image == NULL)
return;
n = 1;
xoff = (SCREEN_WIDTH / 4) - chr[1].image->width / 2;
break;
case 'r': case 'R':
if (chr[2].image == NULL)
return;
n = 2;
xoff = ((SCREEN_WIDTH / 4) * 3) - chr[2].image->width / 2;
break;
default:
_ASSERT(0);
return;
}
_ASSERT(0 <= n && n < MAX_CHR);
if (chr[n].flag) {
DPRINTF(("merge chr %d\n", n));
if (chr[n].image->height < chr_underline + 1) {
yoff = chr_underline + 1 - chr[n].image->height;
} else {
yoff = 0;
}
img_compose(dest, chr[n].image, 0, xoff, yoff);
}
}
void
chr_set_clarity(unsigned char kind, long v)
{
int n;
_ASSERT(v >= 0 && v <= 255);
switch (kind) {
case 'c': case 'C':
n = 0;
break;
case 'l': case 'L':
n = 1;
break;
case 'r': case 'R':
n = 2;
break;
default:
_ASSERT(0);
return;
}
if (chr[n].image == NULL)
return;
if (v == 255) {
chr[n].image->has_clarity = 0;
chr[n].image->clarity = 255;
} else {
chr[n].image->has_clarity = 1;
chr[n].image->clarity = v;
}
}
void
chr_set_priority(long v)
{
_ASSERT(v >= 0 && v < 50);
chr_priority = v;
}
void
chr_set_underline(long v)
{
chr_underline = v;
}
void
load_sprite(int n, unsigned char *filename, point_t *p, int show)
{
_ASSERT(n >= 0 && n < MAX_SPRITE);
_ASSERT(filename != NULL);
_ASSERT(p != NULL);
if (spr[n].image)
img_destroy(spr[n].image);
spr[n].image = image_open(filename, TRANSMODE_TAG);
spr[n].flag = show;
spr[n].point = *p;
}
void
set_sprite(int n, point_t *point, int abs, long maskv)
{
_ASSERT(n >= 0 && n < MAX_SPRITE);
_ASSERT(point != NULL);
spr[n].point.x += point->x;
spr[n].point.y += point->y;
if (abs) {
_ASSERT(maskv >= 0 && maskv <= 255);
if (maskv == 255) {
spr[n].image->has_clarity = 0;
spr[n].image->clarity = 255;
} else {
spr[n].image->has_clarity = 1;
spr[n].image->clarity = maskv;
}
} else {
_ASSERT(maskv >= -255 && maskv <= 255);
if (maskv > 0) {
if (spr[n].image->clarity + maskv >= 255) {
spr[n].image->has_clarity = 0;
spr[n].image->clarity = 255;
} else {
spr[n].image->has_clarity = 1;
spr[n].image->clarity += maskv;
}
} else if (maskv < 0) {
maskv = -maskv;
spr[n].image->has_clarity = 1;
if (spr[n].image->clarity > maskv) {
spr[n].image->clarity -= maskv;
} else {
spr[n].image->clarity = 0;
}
}
}
}
void
show_sprite(int n, int show)
{
_ASSERT(n >= 0 && n < MAX_SPRITE);
spr[n].flag = show;
}
void
clear_sprite(int n)
{
int i;
_ASSERT(n == -1 || (n >= 0 && n < MAX_SPRITE));
if (n == -1) {
for (i = 0; i < MAX_SPRITE; i++) {
if (spr[i].image) {
img_destroy(spr[i].image);
spr[i].image = NULL;
}
}
} else {
if (spr[n].image) {
img_destroy(spr[n].image);
spr[n].image = NULL;
}
}
}
void
merge_sprite(image_t *dest)
{
int i;
_ASSERT(dest != NULL);
for (i = MAX_SPRITE - 1; i >= 0; i--) {
if (spr[i].image) {
if (spr[i].flag) {
DPRINTF(("merge sprite %d\n", i));
img_compose(dest, spr[i].image, 0,
spr[i].point.x, spr[i].point.y);
}
}
if (i == chr_priority) {
DPRINTF(("merge character %d\n", i));
merge_chr('c', dest);
merge_chr('l', dest);
merge_chr('r', dest);
}
}
}