19 /* allocate memory for screenbuffer */
20 void init_sprites(void)
25 void shutdown_sprites(void)
31 /* write visible window of playing area into screenbuffer */
32 void show_window(int x
,int y
)
37 if (x
+SCREEN_X
<=0)return;
38 if (y
+SCREEN_Y
<=0)return;
41 roffs
=x
+SCREEN_X
>AREA_X
?x
+SCREEN_X
-AREA_X
:0;
45 case 0: /* normal part */
46 for(a
=(y
<0?-y
:0);a
<SCREEN_Y
&&(y
+a
)<AREA_Y
;a
++)
51 memcpy(screen
+prec
+loffs
,area
+loffs
+x
+prec2
+prec1
,SCREEN_X
-loffs
-roffs
);
52 memcpy(screen_a
+prec
+loffs
,area_a
+loffs
+x
+prec2
+prec1
,SCREEN_X
-loffs
-roffs
);
57 memset(screen2_a
,0,SCREEN_X
*SCREEN_Y
);
58 memset(screen2
,0,SCREEN_X
*SCREEN_Y
);
59 for(a
=(y
<0?-y
:0);a
<SCREEN_Y
&&(y
+a
)<AREA_Y
;a
++)
65 for (b
=0;b
<SCREEN_X
-loffs
-roffs
;b
++)
68 int prec4
=loffs
+x
+prec2
+prec1
+b
;
70 switch((area_a
[prec4
])&240)
77 case TYPE_JUMP_FOREGROUND
:
86 if (offs
!=1&&(!area
[prec4
]||area
[prec4
]==' '))continue;
88 if ((b
+offs
+loffs
)<0||(loffs
+b
+offs
)>=SCREEN_X
)continue;
89 screen2
[prec
+loffs
+b
+offs
]=area
[prec4
];
90 screen2_a
[prec
+loffs
+b
+offs
]=area_a
[prec4
];
96 for(a
=(y
<0?-y
:0);a
<SCREEN_Y
&&(y
+a
)<AREA_Y
;a
++)
101 memcpy(screen
+prec
+loffs
,area
+loffs
+x
+prec2
+prec1
,SCREEN_X
-loffs
-roffs
);
102 memcpy(screen_a
+prec
+loffs
,area_a
+loffs
+x
+prec2
+prec1
,SCREEN_X
-loffs
-roffs
);
108 /* write one scanline of a bitmap to given memory */
114 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
)
117 if (y
>=ys
||y
<0)return;
118 for (a
=0;a
<l
->len
&&x
+a
<xs
;a
++)
120 if (!l
->attr
[a
])continue;
121 if ( plastic
&&x
+a
+b
>=0&&
122 ((attr
[x
+a
+b
]&240)==TYPE_FOREGROUND
||
123 (attr
[x
+a
+b
]&240)==TYPE_JUMP_FOREGROUND
)
126 scrn
[x
+a
+b
]=l
->bmp
[a
];
127 attr
[x
+a
+b
]=(l
->attr
[a
])|type
;
131 /* put image to given memory */
137 _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
)
141 put_line(xs
,ys
,scrn
,attr
,x
+p
->xo
,a
+y
+p
->yo
,p
->lines
+a
,type
,plastic
);
144 /* put image into screenbuffer */
150 put_sprite(int x
, int y
, struct pos
*p
,unsigned char plastic
)
152 unsigned char *sc
,*at
;
154 sc
=(TRI_D_ON
&&tri_d
)?screen2
:screen
;
155 at
=(TRI_D_ON
&&tri_d
)?screen2_a
:screen_a
;
160 _put_sprite(SCREEN_X
,SCREEN_Y
,sc
,at
,x
,y
,p
,0,plastic
);
164 /* ancillary function for converting color from file notation to value 0-15 */
165 int _conv_color(int c
)
168 if (a
>='a'&&a
<='f')return 10+a
-'a';
169 if (a
>='0'&&a
<='9')return a
-'0';
170 /* space or other character is blank attr */
175 /* load sprite from a file */
176 void load_sprite(char * filename
,struct sprite
*s
)
178 #define CURP (s->n_positions-1)
179 #define CURL (s->positions[CURP].n-1)
182 static char buffer
[8192];
184 int step
=0; /*0=expecting 'p', 1=expecting 'l', 2=expecting 'a', 3=expecting 'p' or 's' or 'l', 4=expecting end */
190 if (!(f
=fopen(filename
,"rb")))
192 if (fopen_s(&f
,filename
,"rb") != 0)
196 snprintf(msg
,256,"Error opening file \"%s\"!\n",filename
);
200 while(fgets(buffer
,8191,f
))
203 if (buffer
[x
-1]==10)buffer
[x
-1]=0;
209 case '#': /* comment */
213 if (step
!=0&&step
!=3)
216 snprintf(msg
,256,"Syntax error in file \"%s\".\n",filename
);
222 s
->positions
=mem_realloc(s
->positions
,s
->n_positions
*sizeof(struct pos
));
225 ERROR("Memory allocation error!\n");
228 s
->positions
[CURP
].n
=0;
229 s
->positions
[CURP
].lines
=0;
232 s
->positions
[CURP
].xo
=strtol(buffer
+1,&p
,0);
233 s
->positions
[CURP
].yo
=strtol(p
+1,&q
,0);
237 if (step
!=1&&step
!=3)
240 snprintf(msg
,256,"Syntax error in file \"%s\".\n",filename
);
245 if (!(s
->positions
[CURP
].n
))s
->positions
[CURP
].lines
=DUMMY
;
246 s
->positions
[CURP
].n
++;
247 s
->positions
[CURP
].lines
=mem_realloc(s
->positions
[CURP
].lines
,s
->positions
[CURP
].n
*sizeof(struct line
));
248 if (!s
->positions
[CURP
].lines
)
250 ERROR("Memory allocation error!\n");
253 s
->positions
[CURP
].lines
[CURL
].len
=strlen(buffer
+1);
254 if (!s
->positions
[CURP
].lines
[CURL
].len
)break; /* empty line, we have nothing to do */
255 s
->positions
[CURP
].lines
[CURL
].bmp
=mem_alloc(s
->positions
[CURP
].lines
[CURL
].len
);
256 if (!s
->positions
[CURP
].lines
[CURL
].bmp
)
258 ERROR("Memory allocation error!\n");
261 memcpy(s
->positions
[CURP
].lines
[CURL
].bmp
,buffer
+1,s
->positions
[CURP
].lines
[CURL
].len
);
268 snprintf(msg
,256,"Syntax error in file \"%s\".\n",filename
);
273 /* UGH, co to tady je??????? */
274 if (!s
->positions
[CURP
].lines
)
276 ERROR("Memory allocation error!\n");
279 if (!s
->positions
[CURP
].lines
[CURL
].len
)break; /* empty line, we have nothing to do */
280 s
->positions
[CURP
].lines
[CURL
].attr
=mem_alloc(s
->positions
[CURP
].lines
[CURL
].len
);
281 if (!s
->positions
[CURP
].lines
[CURL
].attr
)
283 ERROR("Memory allocation error!\n");
287 if (x
>s
->positions
[CURP
].lines
[CURL
].len
)x
=s
->positions
[CURP
].lines
[CURL
].len
;
289 s
->positions
[CURP
].lines
[CURL
].attr
[a
]=_conv_color(buffer
[1+a
]);
290 memset(s
->positions
[CURP
].lines
[CURL
].attr
+x
,0,s
->positions
[CURP
].lines
[CURL
].len
-x
);
297 snprintf(msg
,256,"Syntax error in file \"%s\".\n",filename
);
302 for(q
=buffer
,p
=buffer
+1;*q
&&*q
!=10;*q
==','?p
=q
+1:0)
309 snprintf(txt
,256,"Error loading sprite \"%s\". Undefined position %d.\n",filename
,x
);
313 if (!(s
->n_steps
))s
->steps
=DUMMY
;
315 s
->steps
=mem_realloc(s
->steps
,s
->n_steps
*sizeof(unsigned short));
318 ERROR("Memory allocation error!\n");
321 s
->steps
[s
->n_steps
-1]=x
;
328 snprintf(msg
,256,"Syntax error in file \"%s\"!\n",filename
);
338 snprintf(msg
,256,"Unexpected end of file in \"%s\".\n",filename
);
346 static void free_line(struct line
* line
)
350 mem_free(line
->attr
);
356 static void free_pos(struct pos
*pos
)
359 for (a
=0;a
<(pos
->n
);a
++)
360 free_line((pos
->lines
)+a
);
361 mem_free(pos
->lines
);
365 void free_sprite(struct sprite
*sp
)
369 for (a
=0;a
<(sp
->n_positions
);a
++)
370 free_pos((sp
->positions
)+a
);
372 if (sp
->n_steps
){mem_free(sp
->steps
);mem_free(sp
->positions
);}