-
Notifications
You must be signed in to change notification settings - Fork 1
/
sprite.c
907 lines (769 loc) · 19.7 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2003 Stuart George and Claudio Matsuoka
*
* $Id: sprite.c,v 1.84 2003/10/12 17:39:36 cmatsuoka Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; see docs/COPYING for further details.
*/
#include <string.h> /* for memcpy() */
#include "sarien.h"
#include "list.h"
#include "agi.h"
#include "sprite.h"
#include "graphics.h"
#include "text.h"
#include "savegame.h"
/**
* Sprite structure.
* This structure holds information on visible and priority data of
* a rectangular area of the AGI screen. Sprites are chained in two
* circular lists, one for updating and other for non-updating sprites.
*/
struct sprite {
struct list_head list;
struct vt_entry *v; /**< pointer to view table entry */
SINT16 x_pos; /**< x coordinate of the sprite */
SINT16 y_pos; /**< y coordinate of the sprite */
SINT16 x_size; /**< width of the sprite */
SINT16 y_size; /**< height of the sprite */
UINT8 *buffer; /**< buffer to store background data */
#ifdef USE_HIRES
UINT8 *hires; /**< buffer for hi-res background */
#endif
};
/*
* Sprite pool replaces dynamic allocation
*/
#undef ALLOC_DEBUG
#ifdef USE_HIRES
# ifndef __TURBOC__
# define POOL_SIZE 68000 /* Gold Rush mine room needs > 50000 */
/* Speeder bike challenge needs > 67000 */
# else
# define POOL_SIZE 32000 /* Can't afford that much in Turbo C! */
# endif
#else
# define POOL_SIZE 25000
#endif
static UINT8 *sprite_pool;
static UINT8 *pool_top;
#ifdef ALLOC_DEBUG
#include <stdio.h>
static int max_alloc = 0;
#endif
static void *pool_alloc (int size)
{
UINT8 *x;
/* Adjust size to 32-bit boundary to prevent data misalignment
* errors.
*/
size = (size + 3) & ~3;
x = pool_top;
pool_top += size;
#ifdef ALLOC_DEBUG
if ((int)(pool_top - (UINT8 *)sprite_pool) > max_alloc)
max_alloc = (int)(pool_top - (UINT8 *)sprite_pool);
printf ("pool_alloc: %d bytes @ %d (used memory: %d, max: %d)\n",
size, (x - (UINT8 *)sprite_pool),
(int)(pool_top - (UINT8 *)sprite_pool), max_alloc);
#endif
if (pool_top >= (UINT8 *)sprite_pool + POOL_SIZE) {
_D (_D_CRIT "not enough memory");
pool_top = x;
return NULL;
}
return x;
}
static void pool_release (void *s)
{
#ifdef ALLOC_DEBUG
printf ("pool_release: %d\n", (int)(s - (void *)sprite_pool));
if (s >= (void *)pool_top) {
printf ("s = %d\n", (int)(s - (void *)sprite_pool));
printf ("top = %d\n", (int)(pool_top - (UINT8 *)sprite_pool));
abort ();
}
#endif
pool_top = s;
}
/*
* Blitter functions
*/
/* Blit one pixel considering the priorities */
static void
blit_pixel (UINT8 *p, UINT8 *end, UINT8 col, int spr, int width, int *hidden)
{
int epr, pr; /* effective and real priorities */
/* CM: priority 15 overrides control lines and is ignored when
* tracking effective priority. This tweak is needed to fix
* bug #451768, and should not affect Sierra games because
* sprites shouldn't have priority 15 (like the AGI Mouse
* demo "mouse pointer")
*
* Update: this solution breaks other games, and can't be used.
*/
if (p >= end)
return;
/* Check if we're on a control line */
if ((pr = *p & 0xf0) < 0x30) {
UINT8 *p1;
/* Yes, get effective priority going down */
for (p1 = p; p1 < end && (epr = *p1 & 0xf0) < 0x30; p1 += width);
if (p1 >= end)
epr = 0x40;
} else {
epr = pr;
}
if (spr >= epr) {
/* Keep control line information visible, but put our
* priority over water (0x30) surface
*/
*p = (pr < 0x30 ? pr : spr) | col;
*hidden = FALSE;
/* Except if our priority is 15, which should never happen
* (fixes bug #451768)
*
* Update: breaks other games, can't be used
*
* if (spr == 0xf0)
* *p = spr | col;
*/
}
}
#ifdef USE_HIRES
#define X_FACT 2 /* Horizontal hires factor */
static int blit_hires_cel (int x, int y, int spr, struct view_cel *c)
{
UINT8 *q = NULL;
UINT8 *h0, *h, *end;
int i, j, t, m, col;
int hidden = TRUE;
q = c->data;
t = c->transparency;
m = c->mirror;
spr <<= 4;
h0 = &game.hires[(x + y * _WIDTH + m * (c->width - 1)) * X_FACT];
end = game.hires + _WIDTH * X_FACT * _HEIGHT;
for (i = 0; i < c->height; i++) {
h = h0;
while (*q) {
col = (*q & 0xf0) >> 4;
for (j = *q & 0x0f; j; j--, h += X_FACT * (1 - 2 * m)) {
if (col != t) {
blit_pixel (h, end, col, spr,
_WIDTH * X_FACT, &hidden);
blit_pixel (h + 1, end, col, spr,
_WIDTH * X_FACT, &hidden);
}
}
q++;
}
h0 += _WIDTH * X_FACT;
q++;
}
return hidden;
}
#endif
static int blit_cel (int x, int y, int spr, struct view_cel *c)
{
UINT8 *p0, *p, *q = NULL, *end;
int i, j, t, m, col;
int hidden = TRUE;
/* Fixes bug #477841 (crash in PQ1 map C4 when y == -2) */
if (y < 0) y = 0;
if (x < 0) x = 0;
if (y >= _HEIGHT) y = _HEIGHT - 1;
if (x >= _WIDTH) x = _WIDTH - 1;
#ifdef USE_HIRES
if (opt.hires)
blit_hires_cel (x, y, spr, c);
#endif
q = c->data;
t = c->transparency;
m = c->mirror;
spr <<= 4;
p0 = &game.sbuf[x + y * _WIDTH + m * (c->width - 1)];
end = game.sbuf + _WIDTH * _HEIGHT;
for (i = 0; i < c->height; i++) {
p = p0;
while (*q) {
col = (*q & 0xf0) >> 4;
for (j = *q & 0x0f; j; j--, p += 1 - 2 * m) {
if (col != t) {
blit_pixel (p, end, col, spr,
_WIDTH, &hidden);
}
}
q++;
}
p0 += _WIDTH;
q++;
}
return hidden;
}
static void objs_savearea (struct sprite *s)
{
int y;
SINT16 x_pos = s->x_pos, y_pos = s->y_pos;
SINT16 x_size = s->x_size, y_size = s->y_size;
UINT8 *p0, *q;
#ifdef USE_HIRES
UINT8 *h0, *k;
#endif
if (x_pos + x_size > _WIDTH)
x_size = _WIDTH-x_pos;
if (x_pos < 0) {
x_size += x_pos;
x_pos = 0;
}
if (y_pos + y_size > _HEIGHT)
y_size = _HEIGHT-y_pos;
if (y_pos < 0) {
y_size += y_pos;
y_pos = 0;
}
if (x_size <= 0 || y_size <= 0)
return;
p0 = &game.sbuf[x_pos + y_pos * _WIDTH];
q = s->buffer;
#ifdef USE_HIRES
h0 = &game.hires[(x_pos + y_pos * _WIDTH) * 2];
k = s->hires;
#endif
for (y = 0; y < y_size; y++) {
memcpy (q, p0, x_size);
q += x_size;
p0 += _WIDTH;
#ifdef USE_HIRES
memcpy (k, h0, x_size * 2);
k += x_size * 2;
h0 += _WIDTH * 2;
#endif
}
}
static void objs_restorearea (struct sprite *s)
{
int y, offset;
SINT16 x_pos = s->x_pos, y_pos = s->y_pos;
SINT16 x_size = s->x_size, y_size = s->y_size;
UINT8 *p0, *q;
#ifdef USE_HIRES
UINT8 *h0, *k;
#endif
if (x_pos + x_size > _WIDTH)
x_size = _WIDTH - x_pos;
if (x_pos < 0) {
x_size += x_pos;
x_pos = 0;
}
if (y_pos + y_size > _HEIGHT)
y_size = _HEIGHT-y_pos;
if (y_pos < 0) {
y_size += y_pos;
y_pos = 0;
}
if (x_size <= 0 || y_size <= 0)
return;
p0 = &game.sbuf[x_pos + y_pos * _WIDTH];
q = s->buffer;
#ifdef USE_HIRES
h0 = &game.hires[(x_pos + y_pos * _WIDTH) * 2];
k = s->hires;
#endif
offset = game.line_min_print * CHAR_LINES;
for (y = 0; y < y_size; y++) {
memcpy (p0, q, x_size);
put_pixels_a (x_pos, y_pos + y + offset, x_size, p0);
q += x_size;
p0 += _WIDTH;
#ifdef USE_HIRES
memcpy (h0, k, x_size * 2);
if (opt.hires) {
put_pixels_hires (x_pos * 2,
y_pos + y + offset, x_size * 2, h0);
}
k += x_size * 2;
h0 += _WIDTH * 2;
#endif
}
}
/*
* Sprite management functions
*/
static LIST_HEAD(spr_upd_head);
static LIST_HEAD(spr_nonupd_head);
/**
* Condition to determine whether a sprite will be in the 'updating' list.
*/
static int test_updating (struct vt_entry *v)
{
/* Sanity check (see bug #779302) */
if (~game.dir_view[v->current_view].flags & RES_LOADED)
return 0;
return (v->flags & (ANIMATED|UPDATE|DRAWN)) ==
(ANIMATED|UPDATE|DRAWN);
}
/**
* Condition to determine whether a sprite will be in the 'non-updating' list.
*/
static int test_not_updating (struct vt_entry *v)
{
/* Sanity check (see bug #779302) */
if (~game.dir_view[v->current_view].flags & RES_LOADED)
return 0;
return (v->flags & (ANIMATED|UPDATE|DRAWN)) == (ANIMATED|DRAWN);
}
/**
* Convert sprite priority to y value.
*/
static INLINE int prio_to_y (int p)
{
int i;
if (p == 0)
return -1;
for (i = 167; i >= 0; i--) {
if (game.pri_table[i] < p)
return i;
}
return -1; /* (p - 5) * 12 + 48; */
}
/**
* Create and initialize a new sprite structure.
*/
static struct sprite *new_sprite (struct vt_entry *v)
{
struct sprite *s;
s = (struct sprite *)pool_alloc (sizeof (struct sprite));
if (s == NULL)
return NULL;
s->v = v; /* link sprite to associated view table entry */
s->x_pos = v->x_pos;
s->y_pos = v->y_pos - v->y_size + 1;
s->x_size = v->x_size;
s->y_size = v->y_size;
s->buffer = pool_alloc (s->x_size * s->y_size);
#ifdef USE_HIRES
s->hires = pool_alloc (s->x_size * s->y_size * 2);
#endif
v->s = s; /* link view table entry to this sprite */
return s;
}
/**
* Insert sprite in the specified sprite list.
*/
static void spr_addlist (struct list_head *head, struct vt_entry *v)
{
struct sprite *s;
s = new_sprite (v);
list_add_tail (&s->list, head);
}
/**
* Sort sprites from lower y values to build a sprite list.
*/
static struct list_head *
build_list (struct list_head *head, int (*test)(struct vt_entry *))
{
int i, j, k;
struct vt_entry *v;
struct vt_entry *entry[0x100];
int y_val[0x100];
int min_y = 0xff, min_index = 0;
/* fill the arrays with all sprites that satisfy the 'test'
* condition and their y values
*/
i = 0;
for_each_vt_entry(v) {
if (test (v)) {
entry[i] = v;
y_val[i] = v->flags & FIXED_PRIORITY ?
prio_to_y (v->priority) : v->y_pos;
i++;
}
}
/* now look for the smallest y value in the array and put that
* sprite in the list
*/
for (j = 0; j < i; j++) {
min_y = 0xff;
for (k = 0; k < i; k++) {
if (y_val[k] < min_y) {
min_index = k;
min_y = y_val[k];
}
}
y_val[min_index] = 0xff;
spr_addlist (head, entry[min_index]);
}
return head;
}
/**
* Build list of updating sprites.
*/
static struct list_head *build_upd_blitlist ()
{
return build_list (&spr_upd_head, test_updating);
}
/**
* Build list of non-updating sprites.
*/
static struct list_head *build_nonupd_blitlist ()
{
return build_list (&spr_nonupd_head, test_not_updating);
}
/**
* Clear the given sprite list.
*/
static void free_list (struct list_head *head)
{
struct list_head *h;
struct sprite *s;
list_for_each (h, head, prev) {
s = list_entry (h, struct sprite, list);
list_del (h);
#ifdef USE_HIRES
pool_release (s->hires);
#endif
pool_release (s->buffer);
pool_release (s);
}
}
/**
* Copy sprites from the pic buffer to the screen buffer, and check if
* sprites of the given list have moved.
*/
static void commit_sprites (struct list_head *head)
{
struct list_head *h;
list_for_each (h, head, next) {
struct sprite *s = list_entry (h, struct sprite, list);
int x1, y1, x2, y2, w, h;
w = (s->v->cel_data->width > s->v->cel_data_2->width) ?
s->v->cel_data->width : s->v->cel_data_2->width;
h = (s->v->cel_data->height > s->v->cel_data_2->height) ?
s->v->cel_data->height : s->v->cel_data_2->height;
s->v->cel_data_2 = s->v->cel_data;
if (s->v->x_pos < s->v->x_pos2) {
x1 = s->v->x_pos;
x2 = s->v->x_pos2 + w - 1;
} else {
x1 = s->v->x_pos2;
x2 = s->v->x_pos + w - 1;
}
if (s->v->y_pos < s->v->y_pos2) {
y1 = s->v->y_pos - h + 1;
y2 = s->v->y_pos2;
} else {
y1 = s->v->y_pos2 - h + 1;
y2 = s->v->y_pos;
}
commit_block (x1, y1, x2, y2);
if (s->v->step_time_count != s->v->step_time)
continue;
if (s->v->x_pos == s->v->x_pos2 && s->v->y_pos == s->v->y_pos2){
s->v->flags |= DIDNT_MOVE;
continue;
}
s->v->x_pos2 = s->v->x_pos;
s->v->y_pos2 = s->v->y_pos;
s->v->flags &= ~DIDNT_MOVE;
}
#ifdef USE_CONSOLE
if (debug.statusline)
write_status ();
#endif
}
/**
* Erase all sprites in the given list.
*/
static void erase_sprites (struct list_head *head)
{
struct list_head *h;
list_for_each (h, head, prev) {
struct sprite *s = list_entry (h, struct sprite, list);
objs_restorearea (s);
}
free_list (head);
}
/**
* Blit all sprites in the given list.
*/
static void blit_sprites (struct list_head *head)
{
struct list_head *h = NULL;
int hidden;
list_for_each (h, head, next) {
struct sprite *s = list_entry (h, struct sprite, list);
objs_savearea (s);
/* _D ("s->v->entry = %d (prio %d)", s->v->entry,
s->v->priority); */
hidden = blit_cel (s->x_pos, s->y_pos, s->v->priority,
s->v->cel_data);
if (s->v->entry == 0) { /* if ego, update f1 */
setflag (F_ego_invisible, hidden);
}
}
}
/*
* Public functions
*/
void commit_upd_sprites ()
{
commit_sprites (&spr_upd_head);
}
void commit_nonupd_sprites ()
{
commit_sprites (&spr_nonupd_head);
}
/* check moves in both lists */
void commit_both ()
{
commit_upd_sprites ();
commit_nonupd_sprites ();
}
/**
* Erase updating sprites.
* This function follows the list of all updating sprites and restores
* the visible and priority data of their background buffers back to
* the AGI screen.
*
* @see erase_nonupd_sprites()
* @see erase_both()
*/
void erase_upd_sprites ()
{
erase_sprites (&spr_upd_head);
}
/**
* Erase non-updating sprites.
* This function follows the list of all non-updating sprites and restores
* the visible and priority data of their background buffers back to
* the AGI screen.
*
* @see erase_upd_sprites()
* @see erase_both()
*/
void erase_nonupd_sprites ()
{
erase_sprites (&spr_nonupd_head);
}
/**
* Erase all sprites.
* This function follows the lists of all updating and non-updating
* sprites and restores the visible and priority data of their background
* buffers back to the AGI screen.
*
* @see erase_upd_sprites()
* @see erase_nonupd_sprites()
*/
void erase_both ()
{
erase_upd_sprites ();
erase_nonupd_sprites ();
}
/**
* Blit updating sprites.
* This function follows the list of all updating sprites and blits
* them on the AGI screen.
*
* @see blit_nonupd_sprites()
* @see blit_both()
*/
void blit_upd_sprites ()
{
/* _D("blit updating"); */
blit_sprites (build_upd_blitlist ());
}
/**
* Blit non-updating sprites.
* This function follows the list of all non-updating sprites and blits
* them on the AGI screen.
*
* @see blit_upd_sprites()
* @see blit_both()
*/
void blit_nonupd_sprites ()
{
/* _D("blit non-updating"); */
blit_sprites (build_nonupd_blitlist ());
}
/**
* Blit all sprites.
* This function follows the lists of all updating and non-updating
* sprites and blits them on the AGI screen.
*
* @see blit_upd_sprites()
* @see blit_nonupd_sprites()
*/
void blit_both ()
{
blit_nonupd_sprites ();
blit_upd_sprites ();
}
/**
* Add view to picture.
* This function is used to implement the add.to.pic AGI command. It
* copies the specified cel from a view resource on the current picture.
* This cel is not a sprite, it can't be moved or removed.
* @param view number of view resource
* @param loop number of loop in the specified view resource
* @param cel number of cel in the specified loop
* @param x x coordinate to place the view
* @param y y coordinate to place the view
* @param pri priority to use
* @param mar if < 4, create a margin around the the base of the cel
*/
void add_to_pic (int view, int loop, int cel, int x, int y, int pri, int mar)
{
struct view_cel *c = NULL;
int x1, y1, x2, y2, y3;
UINT8 *p1, *p2;
_D ("v=%d, l=%d, c=%d, x=%d, y=%d, p=%d, m=%d",
view, loop, cel, x, y, pri, mar);
record_image_stack_call(ADD_VIEW, view, loop, cel, x, y, pri, mar);
/*
* Was hardcoded to 8, changed to pri_table[y] to fix Gold
* Rush (see bug #587558)
*/
if (pri == 0)
pri = game.pri_table[y];
c = &game.views[view].loop[loop].cel[cel];
x1 = x;
y1 = y - c->height + 1;
x2 = x + c->width - 1;
y2 = y;
if (x1 < 0) {
x2 -= x1;
x1 = 0;
}
if (y1 < 0) {
y2 -= y1;
y1 = 0;
}
if (x2 >= _WIDTH) x2 = _WIDTH - 1;
if (y2 >= _HEIGHT) y2 = _HEIGHT - 1;
erase_both ();
_D (_D_WARN "blit_cel (%d, %d, %d, c)", x, y, pri);
blit_cel (x1, y1, pri, c);
/* If margin is 0, 1, 2, or 3, the base of the cel is
* surrounded with a rectangle of the corresponding priority.
* If margin >= 4, this extra margin is not shown.
*/
if (mar < 4) {
/* add rectangle around object, don't clobber control
* info in priority data. The box extends to the end of
* its priority band!
*
* SQ1 needs +1 (see bug #810331)
*/
y3 = (y2 / 12) * 12 + 1;
p1 = &game.sbuf[x1 + y3 * _WIDTH];
p2 = &game.sbuf[x2 + y3 * _WIDTH];
for (y = y3; y <= y2; y++) {
if ((*p1 >> 4) >= 4) *p1 = (mar << 4) | (*p1 & 0x0f);
if ((*p2 >> 4) >= 4) *p2 = (mar << 4) | (*p2 & 0x0f);
p1 += _WIDTH;
p2 += _WIDTH;
}
_D (_D_WARN "pri box: %d %d %d %d (%d)", x1, y3, x2, y2, mar);
p1 = &game.sbuf[x1 + y3 * _WIDTH];
p2 = &game.sbuf[x1 + y2 * _WIDTH];
for (x = x1; x <= x2; x++) {
if ((*p1 >> 4) >= 4) *p1 = (mar << 4) | (*p1 & 0x0f);
if ((*p2 >> 4) >= 4) *p2 = (mar << 4) | (*p2 & 0x0f);
p1++;
p2++;
}
}
blit_both ();
_D (_D_WARN "commit_block (%d, %d, %d, %d)", x1, y1, x2, y2);
commit_block (x1, y1, x2, y2);
}
/**
* Show object and description
* This function shows an object from the player's inventory, displaying
* a message box with the object description.
* @param n Number of the object to show
*/
void show_obj (n)
{
struct view_cel *c;
struct sprite s;
int x1, y1, x2, y2;
agi_load_resource (rVIEW, n);
if (!(c = &game.views[n].loop[0].cel[0]))
return;
x1 = (_WIDTH - c->width) / 2;
y1 = 112;
x2 = x1 + c->width - 1;
y2 = y1 + c->height - 1;
s.x_pos = x1;
s.y_pos = y1;
s.x_size = c->width;
s.y_size = c->height;
s.buffer = malloc (s.x_size * s.y_size);
#ifdef USE_HIRES
s.hires = malloc (s.x_size * s.y_size * 2);
#endif
objs_savearea (&s);
blit_cel (x1, y1, s.x_size, c);
commit_block (x1, y1, x2, y2);
message_box (game.views[n].descr);
objs_restorearea (&s);
commit_block (x1, y1, x2, y2);
free (s.buffer);
/* Added to fix a memory leak --Vasyl */
#ifdef USE_HIRES
free(s.hires);
#endif
}
void commit_block (int x1, int y1, int x2, int y2)
{
int i, w, offset;
UINT8 *q;
#ifdef USE_HIRES
UINT8 *h;
#endif
if (!game.picture_shown)
return;
/* Clipping */
if (x1 < 0) x1 = 0;
if (x2 < 0) x2 = 0;
if (y1 < 0) y1 = 0;
if (y2 < 0) y2 = 0;
if (x1 >= _WIDTH) x1 = _WIDTH - 1;
if (x2 >= _WIDTH) x2 = _WIDTH - 1;
if (y1 >= _HEIGHT) y1 = _HEIGHT - 1;
if (y2 >= _HEIGHT) y2 = _HEIGHT - 1;
/* _D ("%d, %d, %d, %d", x1, y1, x2, y2); */
w = x2 - x1 + 1;
q = &game.sbuf[x1 + _WIDTH * y1];
#ifdef USE_HIRES
h = &game.hires[(x1 + _WIDTH * y1) * 2];
#endif
offset = game.line_min_print * CHAR_LINES;
for (i = y1; i <= y2; i++) {
put_pixels_a (x1, i + offset, w, q);
q += _WIDTH;
#ifdef USE_HIRES
if (opt.hires) {
put_pixels_hires (x1 * 2, i + offset, w * 2, h);
}
h += _WIDTH * 2;
#endif
}
flush_block_a (x1, y1 + offset, x2, y2 + offset);
}
int init_sprites ()
{
if ((sprite_pool = malloc (POOL_SIZE)) == NULL)
return err_NotEnoughMemory;
pool_top = sprite_pool;
return err_OK;
}
void deinit_sprites ()
{
free (sprite_pool);
}
/* end: sprite.c */