Debian Codegraph @

terps/taylor/sagadraw.c

raw symbols SCIP
1 //
2 // sagadraw.c
3 // Part of TaylorMade, an interpreter for Adventure Soft UK games
4 //
5 // Based on code by David Lodge
6 // with help from Paul David Doherty (including the colour code)
7 //
8 // Original code at https://github.com/tautology0/textadventuregraphics
9 //
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "glk.h"
17 #include "taylor.h"
18 #include "utility.h"
19
20 #include "sagadraw.h"
21
22 int draw_to_buffer = 1;
23
24 uint8_t sprite[256][8];
25 uint8_t screenchars[768][8];
26 uint8_t screenbuf[768][9];
27
28 static Image *images = NULL;
29
30 glui32 pixel_size;
31 glui32 x_offset;
32
33 static uint8_t *taylor_image_data;
34 static uint8_t *EndOfGraphicsData;
35 /* palette handler stuff starts here */
36
37 typedef uint8_t RGB[3];
38
39 typedef RGB PALETTE[16];
40
41 PALETTE pal;
42
43 int white_colour = 15;
44 int blue_colour = 9;
45
46 int32_t errorcount = 0;
47
48 palette_type palchosen = NO_PALETTE;
49
50 #define INVALIDCOLOR 16
51
52 //static const char *flipdescription[] = { "none", "90°", "180°", "270°", "ERROR" };
53
54 //#define DRAWDEBUG
55
56 //static void colrange(int32_t c)
57 //{
58 // if ((c < 0) || (c > 15)) {
59 //#ifdef DRAWDEBUG
60 // fprintf(stderr, "# col out of range: %d\n", c);
61 //#endif
62 // errorcount++;
63 // }
64 //}
65
66 //static void checkrange(int32_t x, int32_t y)
67 //{
68 // if ((x < 0) || (x > 254)) {
69 //#ifdef DRAWDEBUG
70 // fprintf(stderr, "# x out of range: %d\n", x);
71 //#endif
72 // errorcount++;
73 // }
74 // if ((y < 96) || (y > 191)) {
75 //#ifdef DRAWDEBUG
76 // fprintf(stderr, "# y out of range: %d\n", y);
77 //#endif
78 // errorcount++;
79 // }
80 //}
81
82 //static void do_palette(const char *palname)
83 //{
84 // if (strcmp("zx", palname) == 0)
85 // palchosen = ZX;
86 // else if (strcmp("zxopt", palname) == 0)
87 // palchosen = ZXOPT;
88 // else if (strcmp("c64a", palname) == 0)
89 // palchosen = C64A;
90 // else if (strcmp("c64b", palname) == 0)
91 // palchosen = C64B;
92 // else if (strcmp("vga", palname) == 0)
93 // palchosen = VGA;
94 //}
95
96 static void set_color(int32_t index, RGB *colour)
97 {
98 pal[index][0] = (*colour)[0];
99 pal[index][1] = (*colour)[1];
100 pal[index][2] = (*colour)[2];
101 }
102
103 void DefinePalette(void)
104 {
105 /* set up the palette */
106 if (palchosen == VGA) {
107 RGB black = { 0, 0, 0 };
108 RGB blue = { 0, 0, 255 };
109 RGB red = { 255, 0, 0 };
110 RGB magenta = { 255, 0, 255 };
111 RGB green = { 0, 255, 0 };
112 RGB cyan = { 0, 255, 255 };
113 RGB yellow = { 255, 255, 0 };
114 RGB white = { 255, 255, 255 };
115 RGB brblack = { 0, 0, 0 };
116 RGB brblue = { 0, 0, 255 };
117 RGB brred = { 255, 0, 0 };
118 RGB brmagenta = { 255, 0, 255 };
119 RGB brgreen = { 0, 255, 0 };
120 RGB brcyan = { 0, 255, 255 };
121 RGB bryellow = { 255, 255, 0 };
122 RGB brwhite = { 255, 255, 255 };
123
124 set_color(0, &black);
125 set_color(1, &blue);
126 set_color(2, &red);
127 set_color(3, &magenta);
128 set_color(4, &green);
129 set_color(5, &cyan);
130 set_color(6, &yellow);
131 set_color(7, &white);
132 set_color(8, &brblack);
133 set_color(9, &brblue);
134 set_color(10, &brred);
135 set_color(11, &brmagenta);
136 set_color(12, &brgreen);
137 set_color(13, &brcyan);
138 set_color(14, &bryellow);
139 set_color(15, &brwhite);
140 } else if (palchosen == ZX) {
141 /* corrected Sinclair ZX palette (pretty dull though) */
142 RGB black = { 0, 0, 0 };
143 RGB blue = { 0, 0, 154 };
144 RGB red = { 154, 0, 0 };
145 RGB magenta = { 154, 0, 154 };
146 RGB green = { 0, 154, 0 };
147 RGB cyan = { 0, 154, 154 };
148 RGB yellow = { 154, 154, 0 };
149 RGB white = { 154, 154, 154 };
150 RGB brblack = { 0, 0, 0 };
151 RGB brblue = { 0, 0, 170 };
152 RGB brred = { 186, 0, 0 };
153 RGB brmagenta = { 206, 0, 206 };
154 RGB brgreen = { 0, 206, 0 };
155 RGB brcyan = { 0, 223, 223 };
156 RGB bryellow = { 239, 239, 0 };
157 RGB brwhite = { 255, 255, 255 };
158
159 set_color(0, &black);
160 set_color(1, &blue);
161 set_color(2, &red);
162 set_color(3, &magenta);
163 set_color(4, &green);
164 set_color(5, &cyan);
165 set_color(6, &yellow);
166 set_color(7, &white);
167 set_color(8, &brblack);
168 set_color(9, &brblue);
169 set_color(10, &brred);
170 set_color(11, &brmagenta);
171 set_color(12, &brgreen);
172 set_color(13, &brcyan);
173 set_color(14, &bryellow);
174 set_color(15, &brwhite);
175
176 white_colour = 15;
177 blue_colour = 9;
178
179 } else if (palchosen == ZXOPT) {
180 /* optimized but not realistic Sinclair ZX palette (SPIN emu) */
181 RGB black = { 0, 0, 0 };
182 RGB blue = { 0, 0, 202 };
183 RGB red = {
184 202,
185 0,
186 0,
187 };
188 RGB magenta = { 202, 0, 202 };
189 RGB green = { 0, 202, 0 };
190 RGB cyan = { 0, 202, 202 };
191 RGB yellow = { 202, 202, 0 };
192 RGB white = { 202, 202, 202 };
193 /*
194 old David Lodge palette:
195
196 RGB black = { 0, 0, 0 };
197 RGB blue = { 0, 0, 214 };
198 RGB red = { 214, 0, 0 };
199 RGB magenta = { 214, 0, 214 };
200 RGB green = { 0, 214, 0 };
201 RGB cyan = { 0, 214, 214 };
202 RGB yellow = { 214, 214, 0 };
203 RGB white = { 214, 214, 214 };
204 */
205 RGB brblack = { 0, 0, 0 };
206 RGB brblue = { 0, 0, 255 };
207 RGB brred = { 255, 0, 20 };
208 RGB brmagenta = { 255, 0, 255 };
209 RGB brgreen = { 0, 255, 0 };
210 RGB brcyan = { 0, 255, 255 };
211 RGB bryellow = { 255, 255, 0 };
212 RGB brwhite = { 255, 255, 255 };
213
214 set_color(0, &black);
215 set_color(1, &blue);
216 set_color(2, &red);
217 set_color(3, &magenta);
218 set_color(4, &green);
219 set_color(5, &cyan);
220 set_color(6, &yellow);
221 set_color(7, &white);
222 set_color(8, &brblack);
223 set_color(9, &brblue);
224 set_color(10, &brred);
225 set_color(11, &brmagenta);
226 set_color(12, &brgreen);
227 set_color(13, &brcyan);
228 set_color(14, &bryellow);
229 set_color(15, &brwhite);
230
231 white_colour = 15;
232 blue_colour = 9;
233
234 } else if ((palchosen == C64A) || (palchosen == C64B)) {
235 /* and now: C64 palette (pepto/VICE) */
236 RGB black = { 0, 0, 0 };
237 RGB white = { 255, 255, 255 };
238 RGB red = { 191, 97, 72 };
239 RGB cyan = { 153, 230, 249 };
240 RGB purple = { 177, 89, 185 };
241 RGB green = { 121, 213, 112 };
242 RGB blue = { 95, 72, 233 };
243 RGB yellow = { 247, 255, 108 };
244 RGB orange = { 186, 134, 32 };
245 RGB brown = { 116, 105, 0 };
246 RGB lred = { 231, 154, 132 };
247 RGB dgrey = { 69, 69, 69 };
248 RGB grey = { 167, 167, 167 };
249 RGB lgreen = { 192, 255, 185 };
250 RGB lblue = { 162, 143, 255 };
251 RGB lgrey = { 200, 200, 200 };
252
253 set_color(0, &black);
254 set_color(1, &white);
255 set_color(2, &red);
256 set_color(3, &cyan);
257 set_color(4, &purple);
258 set_color(5, &green);
259 set_color(6, &blue);
260 set_color(7, &yellow);
261 set_color(8, &orange);
262 set_color(9, &brown);
263 set_color(10, &lred);
264 set_color(11, &dgrey);
265 set_color(12, &grey);
266 set_color(13, &lgreen);
267 set_color(14, &lblue);
268 set_color(15, &lgrey);
269
270 white_colour = 1;
271 blue_colour = 6;
272 }
273 }
274
275 //static const char *colortext(int32_t col)
276 //{
277 // const char *zxcolorname[] = {
278 // "black",
279 // "blue",
280 // "red",
281 // "magenta",
282 // "green",
283 // "cyan",
284 // "yellow",
285 // "white",
286 // "bright black",
287 // "bright blue",
288 // "bright red",
289 // "bright magenta",
290 // "bright green",
291 // "bright cyan",
292 // "bright yellow",
293 // "bright white",
294 // "INVALID",
295 // };
296 //
297 // const char *c64colorname[] = {
298 // "black",
299 // "white",
300 // "red",
301 // "cyan",
302 // "purple",
303 // "green",
304 // "blue",
305 // "yellow",
306 // "orange",
307 // "brown",
308 // "light red",
309 // "dark grey",
310 // "grey",
311 // "light green",
312 // "light blue",
313 // "light grey",
314 // "INVALID",
315 // };
316 //
317 // if ((palchosen == C64A) || (palchosen == C64B))
318 // return (c64colorname[col]);
319 // else
320 // return (zxcolorname[col]);
321 //}
322
323 int32_t Remap(int32_t color)
324 {
325 int32_t mapcol;
326
327 if ((palchosen == ZX) || (palchosen == ZXOPT)) {
328 /* nothing to remap here; shows that the gfx were created on a ZX */
329 mapcol = (((color >= 0) && (color <= 15)) ? color : INVALIDCOLOR);
330 } else if (palchosen == C64A) {
331 /* remap A determined from Golden Baton, applies to S1/S3/S13 too (8col) */
332 int32_t c64remap[] = {
333 0, // black
334 6, // blue
335 2, // red
336 4, // magenta
337 5, // green
338 3, // cyan
339 7, // yellow
340 1, // white
341 0, // bright black
342 6, // bright blue
343 2, // bright red
344 4, // bright magenta
345 5, // bright green
346 3, // bright cyan
347 7, // bright yellow
348 1, // bright white
349 };
350 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
351 } else if (palchosen == C64B) {
352 /* remap B determined from Spiderman (16col) */
353 int32_t c64remap[] = {
354 0,
355 6,
356 9,
357 4,
358 5,
359 12,
360 8,
361 15,
362 0,
363 14,
364 2,
365 10,
366 13,
367 3,
368 7,
369 1,
370 };
371 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
372 } else
373 mapcol = (((color >= 0) && (color <= 15)) ? color : INVALIDCOLOR);
374
375 return (mapcol);
376 }
377
378 /* real code starts here */
379
380 static void Flip(uint8_t character[])
381 {
382 int32_t i, j;
383 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
384
385 for (i = 0; i < 8; i++)
386 for (j = 0; j < 8; j++)
387 if ((character[i] & (1 << j)) != 0)
388 work2[i] += 1 << (7 - j);
389 for (i = 0; i < 8; i++)
390 character[i] = work2[i];
391 }
392
393 static void rot90(uint8_t character[])
394 {
395 int32_t i, j;
396 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
397
398 for (i = 0; i < 8; i++)
399 for (j = 0; j < 8; j++)
400 if ((character[j] & (1 << i)) != 0)
401 work2[7 - i] += 1 << j;
402
403 for (i = 0; i < 8; i++)
404 character[i] = work2[i];
405 }
406
407 static void rot270(uint8_t character[])
408 {
409 int32_t i, j;
410 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
411
412 for (i = 0; i < 8; i++)
413 for (j = 0; j < 8; j++)
414 if ((character[j] & (1 << i)) != 0)
415 work2[i] += 1 << (7 - j);
416
417 for (i = 0; i < 8; i++)
418 character[i] = work2[i];
419 }
420
421 static void rot180(uint8_t character[])
422 {
423 int32_t i, j;
424 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
425
426 for (i = 0; i < 8; i++)
427 for (j = 0; j < 8; j++)
428 if ((character[i] & (1 << j)) != 0)
429 work2[7 - i] += 1 << (7 - j);
430
431 for (i = 0; i < 8; i++)
432 character[i] = work2[i];
433 }
434
435 static void transform(int32_t character, int32_t flip_mode, int32_t ptr)
436 {
437 uint8_t work[8];
438 int32_t i;
439
440 #ifdef DRAWDEBUG
441 fprintf(stderr, "Plotting char: %d with flip: %02x (%s) at %d: %d,%d\n",
442 character, flip_mode, flipdescription[(flip_mode & 48) >> 4], ptr,
443 ptr % 0x20, ptr / 0x20);
444 #endif
445
446 // first copy the character into work
447 for (i = 0; i < 8; i++)
448 work[i] = sprite[character][i];
449
450 // Now flip it
451 if ((flip_mode & 0x30) == 0x10) {
452 rot90(work);
453 // fprintf(stderr, "rot 90 character %d\n",character);
454 }
455 if ((flip_mode & 0x30) == 0x20) {
456 rot180(work);
457 // fprintf(stderr, "rot 180 character %d\n",character);
458 }
459 if ((flip_mode & 0x30) == 0x30) {
460 rot270(work);
461 // fprintf(stderr, "rot 270 character %d\n",character);
462 }
463 if ((flip_mode & 0x40) != 0) {
464 Flip(work);
465 /* fprintf("flipping character %d\n",character); */
466 }
467 Flip(work);
468
469 // Now mask it onto the previous character
470 for (i = 0; i < 8; i++) {
471 if ((flip_mode & 0x0c) == 12)
472 screenchars[ptr][i] ^= work[i];
473 else if ((flip_mode & 0x0c) == 8)
474 screenchars[ptr][i] &= work[i];
475 else if ((flip_mode & 0x0c) == 4)
476 screenchars[ptr][i] |= work[i];
477 else
478 screenchars[ptr][i] = work[i];
479 }
480 }
481
482 void PutPixel(glsi32 x, glsi32 y, int32_t color)
483 {
484 int y_offset = 0;
485
486 glui32 glk_color = ((pal[color][0] << 16)) | ((pal[color][1] << 8)) | (pal[color][2]);
487
488 glk_window_fill_rect(Graphics, glk_color, x * pixel_size + x_offset,
489 y * pixel_size + y_offset, pixel_size, pixel_size);
490 }
491
492 void RectFill(int32_t x, int32_t y, int32_t width, int32_t height,
493 int32_t color, int usebuffer)
494 {
495 glui32 y_offset = 0;
496
497 if (usebuffer) {
498 int bufferpos = (y / 8) * 32 + (x / 8);
499 if (bufferpos >= 0xD80)
500 return;
501 screenbuf[bufferpos][8] = (uint8_t)(screenbuf[bufferpos][8] | (color << 3));
502 }
503
504 glui32 glk_color = ((pal[color][0] << 16)) | ((pal[color][1] << 8)) | (pal[color][2]);
505
506 glk_window_fill_rect(Graphics, glk_color, x * pixel_size + x_offset,
507 y * pixel_size + y_offset, width * pixel_size,
508 height * pixel_size);
509 }
510
511 void background(int32_t x, int32_t y, int32_t color)
512 {
513 /* Draw the background */
514 RectFill(x * 8, y * 8, 8, 8, color, 0);
515 }
516
517 void plotsprite(int32_t character, int32_t x, int32_t y, int32_t fg,
518 int32_t bg)
519 {
520 if (fg > 15)
521 fg = 0;
522 if (bg > 15)
523 bg = 0;
524 int32_t i, j;
525 background(x, y, bg);
526 for (i = 0; i < 8; i++) {
527 for (j = 0; j < 8; j++)
528 if ((screenchars[character][i] & (1 << j)) != 0)
529 PutPixel(x * 8 + j, y * 8 + i, fg);
530 }
531 }
532
533 static int isNthBitSet(unsigned const char c, int n)
534 {
535 static unsigned const char mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
536 return ((c & mask[n]) != 0);
537 }
538
539 uint8_t *DrawSagaPictureFromData(uint8_t *dataptr, int xsize, int ysize,
540 int xoff, int yoff, size_t datasize);
541
542 struct image_patch {
543 GameIDType id;
544 int picture_number;
545 int offset;
546 int number_of_bytes;
547 const char *patch;
548 };
549
550 static const struct image_patch image_patches[] = {
551 { UNKNOWN_GAME, 0, 0, 0, "" },
552 { QUESTPROBE3, 55, 604, 3, "\xff\xff\x82" },
553 { QUESTPROBE3, 56, 357, 46, "\x79\x81\x78\x79\x7b\x83\x47\x79\x82\x78\x79\x7b\x83\x47\x79\x83"
554 "\x78\x79\x7b\x81\x79\x47\x79\x84\x7b\x83\x47\x79\x84\x58\x83\x47\x7a\x84\x5f\x18\x81\x5f"
555 "\x47\x50\x84\x5f\x18\x81\x5f\x47" },
556 { NUMGAMES, 0, 0, 0, "" },
557 };
558
559 static int FindImagePatch(GameIDType game, int image_number, int start)
560 {
561 for (int i = start + 1; image_patches[i].id != NUMGAMES; i++) {
562 if (image_patches[i].id == game && image_patches[i].picture_number == image_number) {
563 return i;
564 }
565 }
566 return 0;
567 }
568
569 static void Patch(uint8_t *offset, int patch_number)
570 {
571 const struct image_patch *patch = &image_patches[patch_number];
572 for (int i = 0; i < patch->number_of_bytes; i++) {
573 const char newval = patch->patch[i];
574 offset[i + patch->offset] = (uint8_t)newval;
575 }
576 }
577
578 void DrawTaylor(int loc);
579
580 static void Q3Init(size_t *base, size_t *offsets, size_t *imgdata)
581 {
582 *base = FindCode("\x00\x01\x01\x02\x03\x04\x05\x06\x02\x02", 0, 10);
583 *offsets = FindCode("\x00\x00\xa7\x02\xa7\x03\xb9\x08\xd7\x0b", 0, 10);
584 *imgdata = FindCode("\x20\x0c\x00\x00\x8a\x01\x44\xa0\x17\x8a", *offsets, 10);
585 }
586
587 static uint8_t *Q3Image(int imgnum, size_t base, size_t offsets, size_t imgdata)
588 {
589 uint16_t offset_addr = (FileImage[base + imgnum] & 0x7f) * 2 + offsets;
590 uint16_t image_addr = imgdata + FileImage[offset_addr] + FileImage[offset_addr + 1] * 256;
591 return &FileImage[image_addr];
592 }
593
594 static void RepeatOpcode(int *number, uint8_t *instructions, uint8_t repeatcount)
595 {
596 int i = *number - 1;
597 instructions[i++] = 0x82;
598 instructions[i++] = repeatcount;
599 instructions[i++] = 0;
600 *number = i;
601 }
602
603 static size_t FindCharacterStart(void)
604 {
605 /* Look for the character data */
606 size_t pos = FindCode("\x00\x00\x00\x00\x00\x00\x00\x00\x80\x80\x80\x80\x80\x80\x80\x80\x40\x40\x40\x40\x40\x40\x40\x40", 0, 24);
607 if (pos == -1) {
608 fprintf(stderr, "Cannot find character data.\n");
609 return 0;
610 }
611 #ifdef DEBUG
612 fprintf(stderr, "Found characters at pos %zx\n", pos);
613 #endif
614 return pos;
615 }
616
617 void SagaSetup(void)
618 {
619 if (images != NULL)
620 return;
621
622 if (Game->number_of_pictures == 0) {
623 NoGraphics = 1;
624 return;
625 }
626
627 EndOfGraphicsData = FileImage + FileImageLen;
628
629 int32_t i, y;
630
631 if (palchosen == NO_PALETTE) {
632 palchosen = Game->palette;
633 }
634
635 if (palchosen == NO_PALETTE) {
636 fprintf(stderr, "unknown palette\n");
637 exit(EXIT_FAILURE);
638 }
639
640 DefinePalette();
641
642 size_t CHAR_START = FindCharacterStart();
643 if (!CHAR_START)
644 CHAR_START = Game->start_of_characters + FileBaselineOffset;
645 #ifdef DRAWDEBUG
646 fprintf(stderr, "CHAR_START: %zx (%zu)\n", Game->start_of_characters + FileBaselineOffset, Game->start_of_characters + FileBaselineOffset);
647 #endif
648 uint8_t *pos;
649 int numgraphics = Game->number_of_pictures;
650 pos = SeekToPos(FileImage, CHAR_START);
651
652 #ifdef DRAWDEBUG
653 fprintf(stderr, "Grabbing Character details\n");
654 fprintf(stderr, "Character Offset: %04lx\n",
655 CHAR_START - FileBaselineOffset);
656 #endif
657 for (i = 0; i < 246; i++) {
658 for (y = 0; y < 8 && pos < EndOfGraphicsData; y++) {
659 sprite[i][y] = *(pos++);
660 }
661 }
662
663 /* Now we have hopefully read the character data */
664 /* Time for the image offsets */
665
666 images = (Image *)MemAlloc(sizeof(Image) * numgraphics);
667 Image *img = images;
668 size_t image_blocks_start_address = Game->start_of_image_blocks + FileBaselineOffset;
669
670 size_t patterns_lookup = Game->image_patterns_lookup + FileBaselineOffset;
671
672 pos = SeekToPos(FileImage, image_blocks_start_address);
673
674 size_t base = 0, offsets = 0, imgdata = 0;
675
676 if (Version == QUESTPROBE3_TYPE)
677 Q3Init(&base, &offsets, &imgdata);
678
679 for (int picture_number = 0; picture_number < numgraphics; picture_number++) {
680
681 if (Version == QUESTPROBE3_TYPE) {
682 pos = Q3Image(picture_number, base, offsets, imgdata);
683 if (pos > EndOfData - 4 || pos < FileImage) {
684 fprintf(stderr, "Image %d out of range!\n", picture_number);
685 img->imagedata = NULL;
686 img++;
687 continue;
688 }
689 img->width = *pos++;
690 img->height = *pos++;
691 img->xoff = *pos++;
692 img->yoff = *pos++;
693 img->imagedata = pos;
694 img->datasize = EndOfGraphicsData - pos;
695 if (picture_number == 17) {
696 img->imagedata = MemAlloc(608);
697 img->datasize = 608;
698 memcpy(img->imagedata, pos, MIN(EndOfGraphicsData - pos, 608));
699 int patch = FindImagePatch(QUESTPROBE3, 55, 0);
700 Patch(img->imagedata, patch);
701 } else if (picture_number == 55 || (picture_number >= 18 && picture_number <= 20)) {
702 img->imagedata = images[17].imagedata;
703 img->datasize = images[17].datasize;
704 } else if (picture_number == 56) {
705 img->imagedata = MemAlloc(403);
706 img->datasize = 403;
707 memcpy(img->imagedata, pos, MIN(EndOfGraphicsData - pos, 403));
708 int patch = FindImagePatch(QUESTPROBE3, 56, 0);
709 Patch(img->imagedata, patch);
710 }
711 img++;
712 continue;
713 }
714
715 uint8_t widthheight = *pos++;
716 img->width = ((widthheight & 0xf0) >> 4) + 1;
717 img->height = (widthheight & 0x0f) + 1;
718 if (CurrentGame == BLIZZARD_PASS) {
719 switch (picture_number) {
720 case 13:
721 case 15:
722 case 17:
723 case 34:
724 case 85:
725 case 111:
726 img->width += 16;
727 break;
728 default:
729 break;
730 }
731 }
732 uint8_t instructions[2048];
733 int number = 0;
734 uint8_t *copied_bytes = NULL;
735 uint8_t *stored_pointer = NULL;
736 do {
737 instructions[number++] = *pos;
738 uint8_t opcode = *pos;
739 if (Version != HEMAN_TYPE) {
740 switch (opcode) {
741 case 0xfb:
742 number--;
743 if (!copied_bytes || copied_bytes[0] == 0) {
744 pos = stored_pointer;
745 if (copied_bytes != NULL)
746 free(copied_bytes);
747 copied_bytes = NULL;
748 } else {
749 copied_bytes[0]--;
750 pos = copied_bytes;
751 }
752 break;
753 case 0xef:
754 RepeatOpcode(&number, instructions, 1);
755 break;
756 case 0xee:
757 RepeatOpcode(&number, instructions, 2);
758 break;
759 case 0xeb:
760 RepeatOpcode(&number, instructions, 3);
761 break;
762 case 0xf3:
763 pos++;
764 RepeatOpcode(&number, instructions, *pos);
765 break;
766 case 0xfa:
767 number--;
768 pos++;
769 int numbytes = *pos++;
770 stored_pointer = pos;
771 if (copied_bytes != NULL)
772 free(copied_bytes);
773 copied_bytes = MemAlloc(numbytes + 1);
774 memcpy(copied_bytes, pos, numbytes);
775 copied_bytes[0]--;
776 copied_bytes[numbytes] = 0xfb;
777 pos = copied_bytes;
778 break;
779 }
780 } else if (Game->number_of_patterns) {
781 for (i = 0; i < Game->number_of_patterns; i++) {
782 if (*pos == FileImage[patterns_lookup + i]) {
783 number--;
784 size_t baseoffset = patterns_lookup + Game->number_of_patterns + i * 2;
785 size_t newoffset = FileImage[baseoffset] + FileImage[baseoffset + 1] * 256 - 0x4000 + FileBaselineOffset;
786 while (FileImage[newoffset] != Game->pattern_end_marker) {
787 instructions[number++] = FileImage[newoffset++];
788 }
789 break;
790 }
791 }
792 }
793 pos++;
794 } while (*pos != 0xfe);
795
796 instructions[number++] = 0xfe;
797
798 img->imagedata = MemAlloc(number);
799 img->datasize = number;
800 memcpy(img->imagedata, instructions, number);
801
802 pos++;
803 img++;
804 }
805
806 taylor_image_data = &FileImage[Game->start_of_image_instructions + FileBaselineOffset];
807 }
808
809 void PrintImageContents(int index, uint8_t *data, size_t size)
810 {
811 fprintf(stderr, "/* image %d ", index);
812 fprintf(stderr,
813 "width: %d height: %d xoff: %d yoff: %d size: %zu bytes*/\n{ ",
814 images[index].width, images[index].height, images[index].xoff,
815 images[index].yoff, size);
816 for (int i = 0; i < size; i++) {
817 fprintf(stderr, "0x%02x, ", data[i]);
818 if (i % 8 == 7)
819 fprintf(stderr, "\n ");
820 }
821
822 fprintf(stderr, " },\n");
823 return;
824 }
825
826 void debugdrawcharacter(int character)
827 {
828 fprintf(stderr, "Contents of character %d of 256:\n", character);
829 for (int row = 0; row < 8; row++) {
830 for (int n = 0; n < 8; n++) {
831 if (isNthBitSet(sprite[character][row], n))
832 fprintf(stderr, "■");
833 else
834 fprintf(stderr, "0");
835 }
836 fprintf(stderr, "\n");
837 }
838 }
839
840 void debugdraw(int on, int character, int xoff, int yoff, int width)
841 {
842 if (on) {
843 int x = character % width;
844 int y = character / width;
845 plotsprite(character, x + xoff, y + yoff, 0, 15);
846 fprintf(stderr, "Contents of character position %d:\n", character);
847 for (int row = 0; row < 8; row++) {
848 for (int n = 0; n < 8; n++) {
849 if (isNthBitSet(screenchars[character][row], n))
850 fprintf(stderr, "■");
851 else
852 fprintf(stderr, "0");
853 }
854 fprintf(stderr, "\n");
855 }
856 }
857 }
858
859 #pragma mark Taylorimage
860
861 static void mirror_area(int x1, int y1, int width, int y2)
862 {
863 for (int line = y1; line < y2; line++) {
864 int source = line * 32 + x1;
865 int target = source + width - 1;
866 for (int col = 0; col < width / 2; col++) {
867 screenbuf[target][8] = screenbuf[source][8];
868 for (int pixrow = 0; pixrow < 8; pixrow++)
869 screenbuf[target][pixrow] = screenbuf[source][pixrow];
870 Flip(screenbuf[target]);
871 source++;
872 target--;
873 }
874 }
875 }
876
877 static void mirror_top_half(void)
878 {
879 for (int line = 0; line < 6; line++) {
880 for (int col = 0; col < 32; col++) {
881 screenbuf[(11 - line) * 32 + col][8] = screenbuf[line * 32 + col][8];
882 for (int pixrow = 0; pixrow < 8; pixrow++)
883 screenbuf[(11 - line) * 32 + col][7 - pixrow] = screenbuf[line * 32 + col][pixrow];
884 }
885 }
886 }
887
888 static void flip_image_horizontally(void)
889 {
890 uint8_t mirror[384][9];
891
892 for (int line = 0; line < 12; line++) {
893 for (int col = 32; col > 0; col--) {
894 for (int pixrow = 0; pixrow < 9; pixrow++)
895 mirror[line * 32 + col - 1][pixrow] = screenbuf[line * 32 + (32 - col)][pixrow];
896 Flip(mirror[line * 32 + col - 1]);
897 }
898 }
899
900 memcpy(screenbuf, mirror, 384 * 9);
901 }
902
903 static void flip_image_vertically(void)
904 {
905 uint8_t mirror[384][9];
906
907 for (int line = 0; line < 12; line++) {
908 for (int col = 0; col < 32; col++) {
909 for (int pixrow = 0; pixrow < 8; pixrow++)
910 mirror[(11 - line) * 32 + col][7 - pixrow] = screenbuf[line * 32 + col][pixrow];
911 mirror[(11 - line) * 32 + col][8] = screenbuf[line * 32 + col][8];
912 }
913 }
914 memcpy(screenbuf, mirror, 384 * 9);
915 }
916
917 static void flip_area_vertically(uint8_t x1, uint8_t y1, uint8_t width, uint8_t y2)
918 {
919 // fprintf(stderr, "flip_area_vertically x1: %d: y1: %d width: %d y2 %d\n", x1, y1, width, y2);
920 uint8_t mirror[384][9];
921
922 for (int line = 0; line <= y2; line++) {
923 for (int col = x1; col < x1 + width; col++) {
924 for (int pixrow = 0; pixrow < 8; pixrow++)
925 mirror[(y2 - line) * 32 + col][7 - pixrow] = screenbuf[(y1 + line) * 32 + col][pixrow];
926 mirror[(y2 - line) * 32 + col][8] = screenbuf[(y1 + line) * 32 + col][8];
927 }
928 }
929 for (int line = y1; line <= y2; line++) {
930 for (int col = x1; col < x1 + width; col++) {
931 for (int pixrow = 0; pixrow < 8; pixrow++)
932 screenbuf[line * 32 + col][pixrow] = mirror[line * 32 + col][pixrow];
933 screenbuf[line * 32 + col][8] = mirror[line * 32 + col][8];
934 }
935 }
936 }
937
938 static void mirror_area_vertically(uint8_t x1, uint8_t y1, uint8_t width, uint8_t y2)
939 {
940 for (int line = 0; line <= y2 / 2; line++) {
941 for (int col = x1; col < x1 + width; col++) {
942 screenbuf[(y2 - line) * 32 + col][8] = screenbuf[(y1 + line) * 32 + col][8];
943 for (int pixrow = 0; pixrow < 8; pixrow++)
944 screenbuf[(y2 - line) * 32 + col][7 - pixrow] = screenbuf[(y1 + line) * 32 + col][pixrow];
945 }
946 }
947 }
948
949 static void flip_area_horizontally(uint8_t x1, uint8_t y1, uint8_t width, uint8_t y2)
950 {
951 // fprintf(stderr, "flip_area_horizontally x1: %d: y1: %d width: %d y2 %d\n", x1, y1, width, y2);
952 uint8_t mirror[384][9];
953
954 for (int line = y1; line < y2; line++) {
955 for (int col = 0; col < width; col++) {
956 for (int pixrow = 0; pixrow < 9; pixrow++)
957 mirror[line * 32 + x1 + col][pixrow] = screenbuf[line * 32 + (x1 + width - 1) - col][pixrow];
958 Flip(mirror[line * 32 + x1 + col]);
959 }
960 }
961
962 for (int line = y1; line < y2; line++) {
963 for (int col = x1; col < x1 + width; col++) {
964 for (int pixrow = 0; pixrow < 8; pixrow++)
965 screenbuf[line * 32 + col][pixrow] = mirror[line * 32 + col][pixrow];
966 screenbuf[line * 32 + col][8] = mirror[line * 32 + col][8];
967 }
968 }
969 }
970
971 static void draw_colour_old(uint8_t x, uint8_t y, uint8_t colour, uint8_t length)
972 {
973 for (int i = 0; i < length; i++) {
974 screenbuf[y * 32 + x + i][8] = colour;
975 }
976 }
977
978 static void draw_colour(uint8_t colour, uint8_t x, uint8_t y, uint8_t width, uint8_t height)
979 {
980 for (int h = 0; h < height; h++) {
981 for (int w = 0; w < width; w++) {
982 screenbuf[(y + h) * 32 + x + w][8] = colour;
983 }
984 }
985 }
986
987 static void make_light(void)
988 {
989 for (int i = 0; i < 384; i++) {
990 screenbuf[i][8] = screenbuf[i][8] | 0x40;
991 }
992 }
993
994 static void replace_colour(uint8_t before, uint8_t after)
995 {
996 uint8_t beforeink = before & 7;
997 uint8_t afterink = after & 7;
998 uint8_t inkmask = 0x07; // 0000 0111
999
1000 uint8_t beforepaper = beforeink << 3;
1001 uint8_t afterpaper = afterink << 3;
1002 uint8_t papermask = 0x38; // 0011 1000
1003
1004 for (int j = 0; j < 384; j++) {
1005 if ((screenbuf[j][8] & inkmask) == beforeink) {
1006 screenbuf[j][8] = (screenbuf[j][8] & ~inkmask) | afterink;
1007 }
1008
1009 if ((screenbuf[j][8] & papermask) == beforepaper) {
1010 screenbuf[j][8] = (screenbuf[j][8] & ~papermask) | afterpaper;
1011 }
1012 }
1013 }
1014
1015 static uint8_t ink2paper(uint8_t ink)
1016 {
1017 uint8_t paper = (ink & 0x07) << 3; // 0000 0111 mask ink
1018 paper = paper & 0x38; // 0011 1000 mask paper
1019 return (ink & 0x40) | paper; // 0x40 = 0100 0000 preserve brightness bit from ink
1020 }
1021
1022 static void replace(uint8_t before, uint8_t after, uint8_t mask)
1023 {
1024 for (int j = 0; j < 384; j++) {
1025 uint8_t col = screenbuf[j][8] & mask;
1026 if (col == before) {
1027 uint8_t newcol = screenbuf[j][8] | mask;
1028 newcol = newcol ^ mask;
1029 screenbuf[j][8] = newcol | after;
1030 }
1031 }
1032 }
1033
1034 static void replace_paper_and_ink(uint8_t before, uint8_t after)
1035 {
1036 uint8_t beforeink = before & 0x47; // 0100 0111 ink and brightness
1037 replace(beforeink, after, 0x47);
1038 uint8_t beforepaper = ink2paper(before);
1039 uint8_t afterpaper = ink2paper(after);
1040 replace(beforepaper, afterpaper, 0x78); // 0111 1000 mask paper and brightness
1041 }
1042
1043 void ClearGraphMem(void)
1044 {
1045 memset(screenbuf, 0, 384 * 9);
1046 }
1047
1048 void DrawTaylor(int loc)
1049 {
1050 uint8_t *ptr = taylor_image_data;
1051 for (int i = 0; i < loc; i++) {
1052 while (*(ptr) != 0xff)
1053 ptr++;
1054 ptr++;
1055 }
1056
1057 while (ptr < EndOfGraphicsData) {
1058 // fprintf(stderr, "DrawTaylorRoomImage: Instruction %d: 0x%02x\n", instruction++, *ptr);
1059 switch (*ptr) {
1060 case 0xff:
1061 // fprintf(stderr, "End of picture\n");
1062 return;
1063 case 0xfe:
1064 // fprintf(stderr, "0xfe mirror_left_half\n");
1065 mirror_area(0, 0, 32, 12);
1066 break;
1067 case 0xfd:
1068 // fprintf(stderr, "0xfd Replace colour %x with %x\n", *(ptr + 1), *(ptr + 2));
1069 replace_colour(*(ptr + 1), *(ptr + 2));
1070 ptr += 2;
1071 break;
1072 case 0xfc: // Draw colour: x, y, attribute, length 7808
1073 if (Version != HEMAN_TYPE) {
1074 // fprintf(stderr, "0xfc (7808) Draw attribute %x at %d,%d length %d\n", *(ptr + 3), *(ptr + 1), *(ptr + 2), *(ptr + 4));
1075 draw_colour_old(*(ptr + 1), *(ptr + 2), *(ptr + 3), *(ptr + 4));
1076 ptr = ptr + 4;
1077 } else {
1078 // fprintf(stderr, "0xfc (7808) Draw attribute %x at %d,%d height %d width %d\n", *(ptr + 4), *(ptr + 2), *(ptr + 1), *(ptr + 3), *(ptr + 5));
1079 draw_colour(*(ptr + 4), *(ptr + 2), *(ptr + 1), *(ptr + 5), *(ptr + 3));
1080 ptr = ptr + 5;
1081 }
1082 break;
1083 case 0xfb: // Make all screen colours bright 713e
1084 // fprintf(stderr, "Make colours in picture area bright\n");
1085 make_light();
1086 break;
1087 case 0xfa: // Flip entire image horizontally 7646
1088 // fprintf(stderr, "0xfa Flip entire image horizontally\n");
1089 flip_image_horizontally();
1090 break;
1091 case 0xf9: //0xf9 Draw picture n recursively;
1092 // fprintf(stderr, "Draw Room Image %d recursively\n", *(ptr + 1));
1093 DrawTaylor(*(ptr + 1));
1094 ptr++;
1095 break;
1096 case 0xf8:
1097 // fprintf(stderr, "0xf8: Skip rest of picture if object %d is not present\n", *(ptr + 1));
1098 ptr++;
1099 if (CurrentGame == BLIZZARD_PASS || BaseGame == REBEL_PLANET) {
1100 if (ObjectLoc[*ptr] == MyLoc) {
1101 DrawSagaPictureAtPos(*(ptr + 1), *(ptr + 2), *(ptr + 3));
1102 }
1103 ptr += 3;
1104 } else {
1105 if (ObjectLoc[*ptr] != MyLoc)
1106 return;
1107 }
1108 break;
1109 case 0xf4: // End if object arg1 is present
1110 // fprintf(stderr, "0xf4: Skip rest of picture if object %d IS present\n", *(ptr + 1));
1111 if (ObjectLoc[*(ptr + 1)] == MyLoc)
1112 return;
1113 ptr++;
1114 break;
1115 case 0xf3:
1116 // fprintf(stderr, "0xf3: goto 753d Mirror top half vertically\n");
1117 mirror_top_half();
1118 break;
1119 case 0xf2: // arg1 arg2 arg3 arg4 Mirror horizontally
1120 // fprintf(stderr, "0xf2: Mirror area x: %d y: %d width:%d y2:%d horizontally\n", *(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1121 mirror_area(*(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1122 ptr = ptr + 4;
1123 break;
1124 case 0xf1: // arg1 arg2 arg3 arg4 Mirror vertically
1125 // fprintf(stderr, "0xf1: Mirror area x: %d y: %d width:%d y2:%d vertically\n", *(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1126 mirror_area_vertically(*(ptr + 1), *(ptr + 2), *(ptr + 4), *(ptr + 3));
1127 ptr = ptr + 4;
1128 break;
1129 case 0xee: // arg1 arg2 arg3 arg4 Flip area horizontally
1130 // fprintf(stderr, "0xf1: Flip area x: %d y: %d width:%d y2:%d horizontally\n", *(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1131 flip_area_horizontally(*(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1132 ptr = ptr + 4;
1133 break;
1134 case 0xed:
1135 // fprintf(stderr, "0xed: Flip entire image vertically\n");
1136 flip_image_vertically();
1137 break;
1138 case 0xec: // Flip area vertically
1139 // fprintf(stderr, "0xf1: Flip area x: %d y: %d width:%d y2:%d vertically\n", *(ptr + 2), *(ptr + 1), *(ptr + 4), *(ptr + 3));
1140 flip_area_vertically(*(ptr + 1), *(ptr + 2), *(ptr + 4), *(ptr + 3));
1141 ptr = ptr + 4;
1142 break;
1143 case 0xeb:
1144 case 0xea:
1145 fprintf(stderr, "Unimplemented draw instruction 0x%02x!\n", *ptr);
1146 break;
1147 case 0xe9:
1148 // fprintf(stderr, "0xe9: (77ac) replace paper and ink %d for colour %d?\n", *(ptr + 1), *(ptr + 2));
1149 replace_paper_and_ink(*(ptr + 1), *(ptr + 2));
1150 ptr = ptr + 2;
1151 break;
1152 case 0xe8:
1153 // fprintf(stderr, "Clear graphics memory\n");
1154 ClearGraphMem();
1155 break;
1156 case 0xf7: // set A to 0c and call 70b7, but A seems to not be used. Vestigial code?
1157 if (BaseGame == REBEL_PLANET && MyLoc == 43 && ObjectLoc[131] == 252)
1158 return;
1159 case 0xf6: // set A to 04 and call 70b7. See 0xf7 above.
1160 case 0xf5: // set A to 08 and call 70b7. See 0xf7 above.
1161 // fprintf(stderr, "0x%02x: set A to unused value and draw image block %d at %d, %d\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));
1162 ptr++; // Deliberate fallthrough
1163 default: // else draw image *ptr at x, y
1164 // fprintf(stderr, "Default: Draw image block %d at %d,%d\n", *ptr, *(ptr + 1), *(ptr + 2));
1165 DrawSagaPictureAtPos(*ptr, *(ptr + 1), *(ptr + 2));
1166 ptr = ptr + 2;
1167 break;
1168 }
1169 ptr++;
1170 }
1171 }
1172
1173 uint8_t *DrawSagaPictureFromData(uint8_t *dataptr, int xsize, int ysize,
1174 int xoff, int yoff, size_t datasize)
1175 {
1176 if (dataptr == NULL)
1177 return NULL;
1178
1179 int32_t offset = 0, cont = 0;
1180 int32_t i, x, y, mask_mode;
1181 uint8_t data, data2, old = 0;
1182 int32_t ink[0x22][14], paper[0x22][14];
1183
1184 int version = 4;
1185
1186 uint8_t *origptr = dataptr;
1187
1188 offset = 0;
1189 int32_t character = 0;
1190 int32_t count;
1191 int offsetlimit = xsize * ysize;
1192 do {
1193 count = 1;
1194
1195 /* first handle mode */
1196 data = *dataptr++;
1197 if (data < 0x80) {
1198 if (character > 127 && version > 2) {
1199 data += 128;
1200 }
1201 character = data;
1202 #ifdef DRAWDEBUG
1203 fprintf(stderr, "******* SOLO CHARACTER: %04x\n", character);
1204 #endif
1205 transform(character, 0, offset);
1206 offset++;
1207 if (offset > offsetlimit)
1208 break;
1209 } else {
1210 // first check for a count
1211 if ((data & 2) == 2) {
1212 count = (*dataptr++) + 1;
1213 }
1214
1215 // Next get character and plot (count) times
1216 character = *dataptr++;
1217
1218 // Plot the initial character
1219 if ((data & 1) == 1 && character < 128)
1220 character += 128;
1221
1222 for (i = 0; i < count; i++) {
1223 if (offset + i > offsetlimit)
1224 goto draw_attributes;
1225 transform(character, (data & 0x0c) ? (data & 0xf3) : data, offset + i);
1226 }
1227
1228 // Now check for overlays
1229 if ((data & 0xc) != 0) {
1230 // We have overlays so grab each member of the stream and work out what
1231 // to do with it
1232
1233 mask_mode = (data & 0xc);
1234 data2 = *dataptr++;
1235 old = data;
1236 do {
1237 cont = 0;
1238 if (data2 < 0x80) {
1239 if (version == 4 && (old & 1) == 1)
1240 data2 += 128;
1241 #ifdef DRAWDEBUG
1242 fprintf(stderr, "Plotting %d directly (overlay) at %d\n", data2,
1243 offset);
1244 #endif
1245 for (i = 0; i < count; i++)
1246 transform(data2, old & 0x0c, offset + i);
1247 } else {
1248 character = *dataptr++;
1249 if ((data2 & 1) == 1)
1250 character += 128;
1251 #ifdef DRAWDEBUG
1252 fprintf(stderr, "Plotting %d with flip %02x (%s) at %d %d\n",
1253 character, (data2 | mask_mode),
1254 flipdescription[((data2 | mask_mode) & 48) >> 4], offset,
1255 count);
1256 #endif
1257 for (i = 0; i < count; i++)
1258 transform(character, (data2 & 0xf3) | mask_mode, offset + i);
1259 if ((data2 & 0x0c) != 0) {
1260 mask_mode = (data2 & 0x0c);
1261 old = data2;
1262 cont = 1;
1263 data2 = *dataptr++;
1264 }
1265 }
1266 } while (cont != 0);
1267 }
1268 offset += count;
1269 }
1270 } while (offset < offsetlimit);
1271
1272 draw_attributes:
1273
1274 y = 0;
1275 x = 0;
1276
1277 // fprintf(stderr, "Attribute data begins at offset %ld\n", dataptr -
1278 // origptr);
1279
1280 uint8_t colour = 0;
1281 // Note version 3 count is inverse it is repeat previous colour
1282 // Whilst version0-2 count is repeat next character
1283 while (y < ysize) {
1284 if (dataptr - origptr > datasize) {
1285 fprintf(stderr, "DrawSagaPictureFromData: data offset %zu out of range! Image size %zu. Bailing!\n", dataptr - origptr, datasize);
1286 return dataptr;
1287 }
1288 data = *dataptr++;
1289 // fprintf(stderr, "%03ld: read attribute data byte %02x\n", dataptr -
1290 // origptr - 1, data);
1291 if ((data & 0x80)) {
1292 count = (data & 0x7f) + 1;
1293 if (version >= 3) {
1294 count--;
1295 } else {
1296 colour = *dataptr++;
1297 }
1298 } else {
1299 count = 1;
1300 colour = data;
1301 }
1302 while (count > 0) {
1303 // Now split up depending on which version we're using
1304
1305 // For version 3+
1306
1307 if (draw_to_buffer)
1308 screenbuf[(yoff + y) * 32 + (xoff + x)][8] = colour;
1309 else {
1310 if (version > 2) {
1311 if (x > 33)
1312 return NULL;
1313 // ink is 0-2, screen is 3-5, 6 in bright flag
1314 ink[x][y] = colour & 0x07;
1315 paper[x][y] = (colour & 0x38) >> 3;
1316
1317 if ((colour & 0x40) == 0x40) {
1318 paper[x][y] += 8;
1319 ink[x][y] += 8;
1320 }
1321 } else {
1322 if (x > 33)
1323 return NULL;
1324 paper[x][y] = colour & 0x07;
1325 ink[x][y] = ((colour & 0x70) >> 4);
1326
1327 if ((colour & 0x08) == 0x08 || version < 2) {
1328 paper[x][y] += 8;
1329 ink[x][y] += 8;
1330 }
1331 }
1332 }
1333
1334 x++;
1335 if (x == xsize) {
1336 x = 0;
1337 y++;
1338 if (y > ysize)
1339 break;
1340 }
1341 count--;
1342 }
1343 }
1344 offset = 0;
1345 int32_t xoff2;
1346 for (y = 0; y < ysize; y++)
1347 for (x = 0; x < xsize; x++) {
1348 xoff2 = xoff;
1349 if (version > 0 && version < 3)
1350 xoff2 = xoff - 4;
1351
1352 if (draw_to_buffer) {
1353 for (i = 0; i < 8; i++)
1354 screenbuf[(y + yoff) * 32 + x + xoff2][i] = screenchars[offset][i];
1355 } else {
1356 plotsprite(offset, x + xoff2, y + yoff, Remap(ink[x][y]),
1357 Remap(paper[x][y]));
1358 }
1359
1360 #ifdef DRAWDEBUG
1361 uint8_t colour = screenbuf[(yoff + y) * 32 + (xoff + x)][8];
1362
1363 int paper = (colour >> 3) & 0x7;
1364 paper += 8 * ((colour & 0x40) == 0x40);
1365 paper = Remap(paper);
1366 int ink = (colour & 0x7);
1367 ink += 8 * ((colour & 0x40) == 0x40);
1368 ink = Remap(ink);
1369
1370 fprintf(stderr, "(gfx#:plotting %d,%d:paper=%s,ink=%s)\n", x + xoff2,
1371 y + yoff, colortext(paper),
1372 colortext(ink));
1373 #endif
1374 offset++;
1375 if (offset > offsetlimit)
1376 break;
1377 }
1378 return dataptr;
1379 }
1380
1381 void DrawSagaPictureNumber(int picture_number)
1382 {
1383 if (Game->number_of_pictures == 0)
1384 return;
1385 // int numgraphics = Game->number_of_pictures;
1386 // if (picture_number >= numgraphics) {
1387 // fprintf(stderr, "Invalid image number %d! Last image:%d\n", picture_number,
1388 // numgraphics - 1);
1389 // return;
1390 // }
1391
1392 Image img = images[picture_number];
1393
1394 if (img.imagedata == NULL)
1395 return;
1396
1397 DrawSagaPictureFromData(img.imagedata, img.width, img.height, img.xoff,
1398 img.yoff, img.datasize);
1399 }
1400
1401 void DrawSagaPictureAtPos(int picture_number, int x, int y)
1402 {
1403 Image img = images[picture_number];
1404
1405 DrawSagaPictureFromData(img.imagedata, img.width, img.height, x, y, img.datasize);
1406 }
1407
1408 void DrawSagaPictureFromBuffer(void)
1409 {
1410 for (int line = 0; line < 12; line++) {
1411 for (int col = 0; col < 32; col++) {
1412
1413 uint8_t colour = screenbuf[col + line * 32][8];
1414
1415 int paper = (colour >> 3) & 0x7;
1416 paper += 8 * ((colour & 0x40) == 0x40);
1417 paper = Remap(paper);
1418 int ink = (colour & 0x7);
1419 ink += 8 * ((colour & 0x40) == 0x40);
1420 ink = Remap(ink);
1421
1422 background(col, line, paper);
1423
1424 for (int i = 0; i < 8; i++) {
1425 if (screenbuf[col + line * 32][i] == 0)
1426 continue;
1427 if (screenbuf[col + line * 32][i] == 255) {
1428 glui32 glk_color = (glui32)((pal[ink][0] << 16) | (pal[ink][1] << 8) | pal[ink][2]);
1429
1430 glk_window_fill_rect(
1431 Graphics, glk_color, (glsi32)(col * 8 * pixel_size + x_offset),
1432 (glsi32)(line * 8 + i) * pixel_size, 8 * pixel_size, pixel_size);
1433 continue;
1434 }
1435 for (int j = 0; j < 8; j++)
1436 if ((screenbuf[col + line * 32][i] & (1 << j)) != 0) {
1437 int ypos = line * 8 + i;
1438 PutPixel(col * 8 + j, ypos, ink);
1439 }
1440 }
1441 }
1442 }
1443 }
1444
1445 void PatchAndDrawQP3Cannon(void) {
1446 DrawSagaPictureNumber(46);
1447 screenbuf[32 * 8 + 25][8] &= 191;
1448 screenbuf[32 * 9 + 25][8] &= 191;
1449 screenbuf[32 * 9 + 26][8] &= 191;
1450 screenbuf[32 * 9 + 27][8] &= 191;
1451 DrawSagaPictureFromBuffer();
1452 }