-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsprite.c
373 lines (329 loc) · 7.82 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef WIN32
#include "config.h"
#endif
#include "sprite.h"
#include "blit.h"
#include "data.h"
#include "console.h"
#include "cfg.h"
#include "error.h"
/* allocate memory for screenbuffer */
void init_sprites(void)
{
init_blit();
}
void shutdown_sprites(void)
{
shutdown_blit();
}
/* write visible window of playing area into screenbuffer */
void show_window(int x,int y)
{
int a,loffs,roffs;
int prec2=y*AREA_X;
if (x+SCREEN_X<=0)return;
if (y+SCREEN_Y<=0)return;
loffs=x<0?-x:0;
roffs=x+SCREEN_X>AREA_X?x+SCREEN_X-AREA_X:0;
#ifdef TRI_D
switch (tri_d)
{
case 0: /* normal part */
for(a=(y<0?-y:0);a<SCREEN_Y&&(y+a)<AREA_Y;a++)
{
int prec=a*SCREEN_X;
int prec1=a*AREA_X;
memcpy(screen+prec+loffs,area+loffs+x+prec2+prec1,SCREEN_X-loffs-roffs);
memcpy(screen_a+prec+loffs,area_a+loffs+x+prec2+prec1,SCREEN_X-loffs-roffs);
}
break;
case 1: /* 3d part */
memset(screen2_a,0,SCREEN_X*SCREEN_Y);
memset(screen2,0,SCREEN_X*SCREEN_Y);
for(a=(y<0?-y:0);a<SCREEN_Y&&(y+a)<AREA_Y;a++)
{
int b;
int prec=a*SCREEN_X;
int prec1=a*AREA_X;
for (b=0;b<SCREEN_X-loffs-roffs;b++)
{
int offs;
int prec4=loffs+x+prec2+prec1+b;
switch((area_a[prec4])&240)
{
case TYPE_BACKGROUND:
offs=-1;
break;
case TYPE_FOREGROUND:
case TYPE_JUMP_FOREGROUND:
offs=1;
break;
default:
offs=0;
break;
}
if (offs!=1&&(!area[prec4]||area[prec4]==' '))continue;
if ((b+offs+loffs)<0||(loffs+b+offs)>=SCREEN_X)continue;
screen2[prec+loffs+b+offs]=area[prec4];
screen2_a[prec+loffs+b+offs]=area_a[prec4];
}
}
break;
}
#else
for(a=(y<0?-y:0);a<SCREEN_Y&&(y+a)<AREA_Y;a++)
{
int prec=a*SCREEN_X;
int prec1=a*AREA_X;
memcpy(screen+prec+loffs,area+loffs+x+prec2+prec1,SCREEN_X-loffs-roffs);
memcpy(screen_a+prec+loffs,area_a+loffs+x+prec2+prec1,SCREEN_X-loffs-roffs);
}
#endif
}
/* write one scanline of a bitmap to given memory */
#ifdef HAVE_INLINE
inline void
#else
void
#endif
put_line(int xs, int ys,unsigned char *scrn,unsigned char *attr,int x,int y,struct line *l,unsigned char type,unsigned char plastic)
{
int a,b=xs*y;
if (y>=ys||y<0)return;
for (a=0;a<l->len&&x+a<xs;a++)
{
if (!l->attr[a])continue;
if ( plastic&&x+a+b>=0&&
((attr[x+a+b]&240)==TYPE_FOREGROUND||
(attr[x+a+b]&240)==TYPE_JUMP_FOREGROUND)
) continue;
if (x+a<0)continue;
scrn[x+a+b]=l->bmp[a];
attr[x+a+b]=(l->attr[a])|type;
}
}
/* put image to given memory */
#ifdef HAVE_INLINE
inline void
#else
void
#endif
_put_sprite(int xs, int ys, unsigned char *scrn,unsigned char *attr,int x,int y,struct pos *p,unsigned char type,unsigned char plastic)
{
int a;
for (a=0;a<p->n;a++)
put_line(xs,ys,scrn,attr,x+p->xo,a+y+p->yo,p->lines+a,type,plastic);
}
/* put image into screenbuffer */
#ifdef HAVE_INLINE
inline void
#else
void
#endif
put_sprite(int x, int y, struct pos *p,unsigned char plastic)
{
unsigned char *sc,*at;
#ifdef TRI_D
sc=(TRI_D_ON&&tri_d)?screen2:screen;
at=(TRI_D_ON&&tri_d)?screen2_a:screen_a;
#else
sc=screen;
at=screen_a;
#endif
_put_sprite(SCREEN_X,SCREEN_Y,sc,at,x,y,p,0,plastic);
}
/* ancillary function for converting color from file notation to value 0-15 */
int _conv_color(int c)
{
int a=tolower(c);
if (a>='a'&&a<='f')return 10+a-'a';
if (a>='0'&&a<='9')return a-'0';
/* space or other character is blank attr */
return 0;
}
/* load sprite from a file */
void load_sprite(char * filename,struct sprite *s)
{
#define CURP (s->n_positions-1)
#define CURL (s->positions[CURP].n-1)
FILE *f;
int x,a;
static char buffer[8192];
char *p,*q;
int step=0; /*0=expecting 'p', 1=expecting 'l', 2=expecting 'a', 3=expecting 'p' or 's' or 'l', 4=expecting end */
s->positions=DUMMY;
s->n_positions=0;
#ifndef WIN32
if (!(f=fopen(filename,"rb")))
#else
if (fopen_s(&f,filename,"rb") != 0)
#endif
{
char msg[256];
snprintf(msg,256,"Error opening file \"%s\"!\n",filename);
ERROR(msg);
EXIT(1);
}
while(fgets(buffer,8191,f))
{
x=strlen(buffer);
if (buffer[x-1]==10)buffer[x-1]=0;
switch(*buffer)
{
case 0:
goto skip;
case '#': /* comment */
break;
case 'p':
if (step!=0&&step!=3)
{
char msg[256];
snprintf(msg,256,"Syntax error in file \"%s\".\n",filename);
ERROR(msg);
EXIT(1);
}
step=1;
s->n_positions++;
s->positions=mem_realloc(s->positions,s->n_positions*sizeof(struct pos));
if (!s->positions)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
s->positions[CURP].n=0;
s->positions[CURP].lines=0;
s->n_steps=0;
s->steps=0;
s->positions[CURP].xo=strtol(buffer+1,&p,0);
s->positions[CURP].yo=strtol(p+1,&q,0);
break;
case 'l':
if (step!=1&&step!=3)
{
char msg[256];
snprintf(msg,256,"Syntax error in file \"%s\".\n",filename);
ERROR(msg);
EXIT(1);
}
step=2;
if (!(s->positions[CURP].n))s->positions[CURP].lines=DUMMY;
s->positions[CURP].n++;
s->positions[CURP].lines=mem_realloc(s->positions[CURP].lines,s->positions[CURP].n*sizeof(struct line));
if (!s->positions[CURP].lines)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
s->positions[CURP].lines[CURL].len=strlen(buffer+1);
if (!s->positions[CURP].lines[CURL].len)break; /* empty line, we have nothing to do */
s->positions[CURP].lines[CURL].bmp=mem_alloc(s->positions[CURP].lines[CURL].len);
if (!s->positions[CURP].lines[CURL].bmp)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
memcpy(s->positions[CURP].lines[CURL].bmp,buffer+1,s->positions[CURP].lines[CURL].len);
break;
case 'a':
if (step!=2)
{
char msg[256];
snprintf(msg,256,"Syntax error in file \"%s\".\n",filename);
ERROR(msg);
EXIT(1);
}
step=3;
/* UGH, co to tady je??????? */
if (!s->positions[CURP].lines)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
if (!s->positions[CURP].lines[CURL].len)break; /* empty line, we have nothing to do */
s->positions[CURP].lines[CURL].attr=mem_alloc(s->positions[CURP].lines[CURL].len);
if (!s->positions[CURP].lines[CURL].attr)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
x=strlen(buffer+1);
if (x>s->positions[CURP].lines[CURL].len)x=s->positions[CURP].lines[CURL].len;
for (a=0;a<x;a++)
s->positions[CURP].lines[CURL].attr[a]=_conv_color(buffer[1+a]);
memset(s->positions[CURP].lines[CURL].attr+x,0,s->positions[CURP].lines[CURL].len-x);
break;
case 's':
if (step!=3)
{
char msg[256];
snprintf(msg,256,"Syntax error in file \"%s\".\n",filename);
ERROR(msg);
EXIT(1);
}
step=4;
for(q=buffer,p=buffer+1;*q&&*q!=10;*q==','?p=q+1:0)
{
x=strtol(p,&q,0);
if (x<0||x>CURP)
{
char txt[256];
snprintf(txt,256,"Error loading sprite \"%s\". Undefined position %d.\n",filename,x);
ERROR(txt);
EXIT(1);
}
if (!(s->n_steps))s->steps=DUMMY;
s->n_steps++;
s->steps=mem_realloc(s->steps,s->n_steps*sizeof(unsigned short));
if (!s->steps)
{
ERROR("Memory allocation error!\n");
EXIT(1);
}
s->steps[s->n_steps-1]=x;
}
break;
default:
{
char msg[256];
snprintf(msg,256,"Syntax error in file \"%s\"!\n",filename);
ERROR(msg);
EXIT(1);
}
}
skip: ;
}
if (step!=4)
{
char msg[256];
snprintf(msg,256,"Unexpected end of file in \"%s\".\n",filename);
ERROR(msg);
EXIT(1);
}
fclose(f);
}
static void free_line(struct line* line)
{
if(line->len)
{
mem_free(line->attr);
mem_free(line->bmp);
}
}
static void free_pos(struct pos *pos)
{
int a;
for (a=0;a<(pos->n);a++)
free_line((pos->lines)+a);
mem_free(pos->lines);
}
void free_sprite(struct sprite*sp)
{
int a;
for (a=0;a<(sp->n_positions);a++)
free_pos((sp->positions)+a);
if (sp->n_steps){mem_free(sp->steps);mem_free(sp->positions);}
}