Debian Codegraph @

terps/scott/ai_uk/sagadraw.c

raw symbols SCIP
1 /* Sagadraw v2.5
2 Originally by David Lodge
3
4 With help from Paul David Doherty (including the colour code)
5 */
6
7 #include "glk.h"
8 #include <math.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "sagagraphics.h"
14 #include "scott.h"
15 #include "scottdefines.h"
16 #include "seasofblood.h"
17
18 #include "sagadraw.h"
19
20 int draw_to_buffer = 0;
21
22 uint8_t sprite[256][8];
23 uint8_t screenchars[768][8];
24 uint8_t buffer[384][9];
25
26 Image *images = NULL;
27
28 int white_colour = 15;
29 int blue_colour = 9;
30 glui32 dice_colour = 0xff0000;
31
32 int32_t errorcount = 0;
33
34 palette_type palchosen = NO_PALETTE;
35
36 #define STRIDE 765 /* 255 pixels * 3 (r, g, b) */
37
38 #define INVALIDCOLOR 16
39
40 const char *flipdescription[] = { "none", "90°", "180°", "270°", "ERROR" };
41
42 void DefinePalette(void)
43 {
44 /* set up the palette */
45 if (palchosen == VGA) {
46 RGB black = { 0, 0, 0 };
47 RGB blue = { 0, 0, 255 };
48 RGB red = { 255, 0, 0 };
49 RGB magenta = { 255, 0, 255 };
50 RGB green = { 0, 255, 0 };
51 RGB cyan = { 0, 255, 255 };
52 RGB yellow = { 255, 255, 0 };
53 RGB white = { 255, 255, 255 };
54 RGB brblack = { 0, 0, 0 };
55 RGB brblue = { 0, 0, 255 };
56 RGB brred = { 255, 0, 0 };
57 RGB brmagenta = { 255, 0, 255 };
58 RGB brgreen = { 0, 255, 0 };
59 RGB brcyan = { 0, 255, 255 };
60 RGB bryellow = { 255, 255, 0 };
61 RGB brwhite = { 255, 255, 255 };
62
63 SetColor(0, &black);
64 SetColor(1, &blue);
65 SetColor(2, &red);
66 SetColor(3, &magenta);
67 SetColor(4, &green);
68 SetColor(5, &cyan);
69 SetColor(6, &yellow);
70 SetColor(7, &white);
71 SetColor(8, &brblack);
72 SetColor(9, &brblue);
73 SetColor(10, &brred);
74 SetColor(11, &brmagenta);
75 SetColor(12, &brgreen);
76 SetColor(13, &brcyan);
77 SetColor(14, &bryellow);
78 SetColor(15, &brwhite);
79 } else if (palchosen == ZX) {
80 /* corrected Sinclair ZX palette (pretty dull though) */
81 RGB black = { 0, 0, 0 };
82 RGB blue = { 0, 0, 154 };
83 RGB red = { 154, 0, 0 };
84 RGB magenta = { 154, 0, 154 };
85 RGB green = { 0, 154, 0 };
86 RGB cyan = { 0, 154, 154 };
87 RGB yellow = { 154, 154, 0 };
88 RGB white = { 154, 154, 154 };
89 RGB brblack = { 0, 0, 0 };
90 RGB brblue = { 0, 0, 170 };
91 RGB brred = { 186, 0, 0 };
92 RGB brmagenta = { 206, 0, 206 };
93 RGB brgreen = { 0, 206, 0 };
94 RGB brcyan = { 0, 223, 223 };
95 RGB bryellow = { 239, 239, 0 };
96 RGB brwhite = { 255, 255, 255 };
97
98 SetColor(0, &black);
99 SetColor(1, &blue);
100 SetColor(2, &red);
101 SetColor(3, &magenta);
102 SetColor(4, &green);
103 SetColor(5, &cyan);
104 SetColor(6, &yellow);
105 SetColor(7, &white);
106 SetColor(8, &brblack);
107 SetColor(9, &brblue);
108 SetColor(10, &brred);
109 SetColor(11, &brmagenta);
110 SetColor(12, &brgreen);
111 SetColor(13, &brcyan);
112 SetColor(14, &bryellow);
113 SetColor(15, &brwhite);
114
115 white_colour = 15;
116 blue_colour = 9;
117 dice_colour = 0xff0000;
118 } else if (palchosen == ZXOPT) {
119 /* optimized but not realistic Sinclair ZX palette (SPIN emu) */
120 RGB black = { 0, 0, 0 };
121 RGB blue = { 0, 0, 202 };
122 RGB red = {
123 202,
124 0,
125 0,
126 };
127 RGB magenta = { 202, 0, 202 };
128 RGB green = { 0, 202, 0 };
129 RGB cyan = { 0, 202, 202 };
130 RGB yellow = { 202, 202, 0 };
131 RGB white = { 202, 202, 202 };
132 /*
133 old David Lodge palette:
134
135 RGB black = { 0, 0, 0 };
136 RGB blue = { 0, 0, 214 };
137 RGB red = { 214, 0, 0 };
138 RGB magenta = { 214, 0, 214 };
139 RGB green = { 0, 214, 0 };
140 RGB cyan = { 0, 214, 214 };
141 RGB yellow = { 214, 214, 0 };
142 RGB white = { 214, 214, 214 };
143 */
144 RGB brblack = { 0, 0, 0 };
145 RGB brblue = {
146 0,
147 0,
148 255,
149 };
150 RGB brred = { 255, 0, 20 };
151 RGB brmagenta = { 255, 0, 255 };
152 RGB brgreen = { 0, 255, 0 };
153 RGB brcyan = { 0, 255, 255 };
154 RGB bryellow = { 255, 255, 0 };
155 RGB brwhite = { 255, 255, 255 };
156
157 SetColor(0, &black);
158 SetColor(1, &blue);
159 SetColor(2, &red);
160 SetColor(3, &magenta);
161 SetColor(4, &green);
162 SetColor(5, &cyan);
163 SetColor(6, &yellow);
164 SetColor(7, &white);
165 SetColor(8, &brblack);
166 SetColor(9, &brblue);
167 SetColor(10, &brred);
168 SetColor(11, &brmagenta);
169 SetColor(12, &brgreen);
170 SetColor(13, &brcyan);
171 SetColor(14, &bryellow);
172 SetColor(15, &brwhite);
173
174 white_colour = 15;
175 blue_colour = 9;
176 dice_colour = 0xff0000;
177
178 } else if (palchosen >= C64A) {
179 /* and now: C64 palette (pepto/VICE) */
180 RGB black = { 0, 0, 0 };
181 RGB white = { 255, 255, 255 };
182 RGB red = { 191, 97, 72 };
183 RGB cyan = { 153, 230, 249 };
184 RGB purple = { 177, 89, 185 };
185 RGB green = { 121, 213, 112 };
186 RGB blue = { 95, 72, 233 };
187 RGB yellow = { 247, 255, 108 };
188 RGB orange = { 186, 134, 32 };
189 RGB brown = { 116, 105, 0 };
190 RGB lred = { 231, 154, 132 };
191 RGB dgrey = { 69, 69, 69 };
192 RGB grey = { 167, 167, 167 };
193 RGB lgreen = { 192, 255, 185 };
194 RGB lblue = { 162, 143, 255 };
195 RGB lgrey = { 200, 200, 200 };
196
197 SetColor(0, &black);
198 SetColor(1, &white);
199 SetColor(2, &red);
200 SetColor(3, &cyan);
201 SetColor(4, &purple);
202 SetColor(5, &green);
203 SetColor(6, &blue);
204 SetColor(7, &yellow);
205 SetColor(8, &orange);
206 SetColor(9, &brown);
207 SetColor(10, &lred);
208 SetColor(11, &dgrey);
209 SetColor(12, &grey);
210 SetColor(13, &lgreen);
211 SetColor(14, &lblue);
212 SetColor(15, &lgrey);
213
214 white_colour = 1;
215 blue_colour = 6;
216 dice_colour = 0x5f48e9;
217 }
218 }
219
220 const char *colortext(int32_t col)
221 {
222 const char *zxcolorname[] = {
223 "black",
224 "blue",
225 "red",
226 "magenta",
227 "green",
228 "cyan",
229 "yellow",
230 "white",
231 "bright black",
232 "bright blue",
233 "bright red",
234 "bright magenta",
235 "bright green",
236 "bright cyan",
237 "bright yellow",
238 "bright white",
239 "INVALID",
240 };
241
242 const char *c64colorname[] = {
243 "black",
244 "white",
245 "red",
246 "cyan",
247 "purple",
248 "green",
249 "blue",
250 "yellow",
251 "orange",
252 "brown",
253 "light red",
254 "dark grey",
255 "grey",
256 "light green",
257 "light blue",
258 "light grey",
259 "INVALID",
260 };
261
262 if (palchosen >= C64A)
263 return (c64colorname[col]);
264 else
265 return (zxcolorname[col]);
266 }
267
268 int32_t Remap(int32_t color)
269 {
270 int32_t mapcol;
271
272 if ((palchosen == ZX) || (palchosen == ZXOPT)) {
273 /* nothing to remap here; shows that the gfx were created on a ZX */
274 mapcol = (((color >= 0) && (color <= 15)) ? color : INVALIDCOLOR);
275 } else if (palchosen == C64A) {
276 /* remap A determined from Golden Baton, applies to S1/S3/S13 too (8col) */
277 int32_t c64remap[] = {
278 0,
279 6,
280 2,
281 4,
282 5,
283 3,
284 7,
285 1,
286 8,
287 1,
288 1,
289 1,
290 7,
291 12,
292 8,
293 7,
294 };
295 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
296 } else if (palchosen == C64B) {
297 /* remap B determined from Spiderman (16col) */
298 int32_t c64remap[] = {
299 0,
300 6,
301 9,
302 4,
303 5,
304 14,
305 8,
306 12,
307 0,
308 6,
309 2,
310 4,
311 5,
312 3,
313 7,
314 1,
315 };
316 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
317 } else if (palchosen == C64C) {
318 /* remap C determined from Spiderman C64 */
319 int32_t c64remap[] = {
320 0,
321 6,
322 2,
323 4,
324 5,
325 14,
326 8,
327 12,
328 0,
329 6,
330 2,
331 4,
332 5,
333 3,
334 7,
335 1,
336 };
337 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
338 } else if (palchosen == C64D) {
339 /* remap D determined from Seas of Blood C64 */
340 int32_t c64remap[] = {
341 0,
342 6,
343 2,
344 4,
345 5,
346 3,
347 8,
348 1,
349 0,
350 6,
351 2,
352 4,
353 5,
354 3,
355 7,
356 1,
357 };
358 mapcol = (((color >= 0) && (color <= 15)) ? c64remap[color] : INVALIDCOLOR);
359 } else
360 mapcol = (((color >= 0) && (color <= 15)) ? color : INVALIDCOLOR);
361
362 return (mapcol);
363 }
364
365 /* real code starts here */
366
367 void Flip(uint8_t character[])
368 {
369 int32_t i, j;
370 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
371
372 for (i = 0; i < 8; i++)
373 for (j = 0; j < 8; j++)
374 if ((character[i] & (1 << j)) != 0)
375 work2[i] += 1 << (7 - j);
376 for (i = 0; i < 8; i++)
377 character[i] = work2[i];
378 }
379
380 void rot90(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[j] & (1 << i)) != 0)
388 work2[7 - i] += 1 << j;
389
390 for (i = 0; i < 8; i++)
391 character[i] = work2[i];
392 }
393
394 void rot270(uint8_t character[])
395 {
396 int32_t i, j;
397 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
398
399 for (i = 0; i < 8; i++)
400 for (j = 0; j < 8; j++)
401 if ((character[j] & (1 << i)) != 0)
402 work2[i] += 1 << (7 - j);
403
404 for (i = 0; i < 8; i++)
405 character[i] = work2[i];
406 }
407
408 void rot180(uint8_t character[])
409 {
410 int32_t i, j;
411 uint8_t work2[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
412
413 for (i = 0; i < 8; i++)
414 for (j = 0; j < 8; j++)
415 if ((character[i] & (1 << j)) != 0)
416 work2[7 - i] += 1 << (7 - j);
417
418 for (i = 0; i < 8; i++)
419 character[i] = work2[i];
420 }
421
422 void transform(int32_t character, int32_t flip_mode, int32_t ptr)
423 {
424 if (character > 255)
425 return;
426 uint8_t work[8];
427 int32_t i;
428
429 #ifdef DRAWDEBUG
430 debug_print("Plotting char: %d with flip: %02x (%s) at %d: %d,%d\n",
431 character, flip_mode, flipdescription[(flip_mode & 48) >> 4], ptr,
432 ptr % 0x20, ptr / 0x20);
433 #endif
434
435 // first copy the character into work
436 for (i = 0; i < 8; i++)
437 work[i] = sprite[character][i];
438
439 // Now flip it
440 if ((flip_mode & 0x30) == 0x10) {
441 rot90(work);
442 // debug_print("rot 90 character %d\n",character);
443 }
444 if ((flip_mode & 0x30) == 0x20) {
445 rot180(work);
446 // debug_print("rot 180 character %d\n",character);
447 }
448 if ((flip_mode & 0x30) == 0x30) {
449 rot270(work);
450 // debug_print("rot 270 character %d\n",character);
451 }
452 if ((flip_mode & 0x40) != 0) {
453 Flip(work);
454 /* fprintf("flipping character %d\n",character); */
455 }
456 Flip(work);
457
458 // Now mask it onto the previous character
459 for (i = 0; i < 8; i++) {
460 if ((flip_mode & 0x0c) == 12)
461 screenchars[ptr][i] ^= work[i];
462 else if ((flip_mode & 0x0c) == 8)
463 screenchars[ptr][i] &= work[i];
464 else if ((flip_mode & 0x0c) == 4)
465 screenchars[ptr][i] |= work[i];
466 else
467 screenchars[ptr][i] = work[i];
468 }
469 }
470
471 void RectFill(int32_t x, int32_t y, int32_t width, int32_t height,
472 int32_t color)
473 {
474 int bufferpos = (y / 8) * 32 + (x / 8);
475 if (bufferpos >= 0xD80)
476 return;
477 buffer[bufferpos][8] = buffer[bufferpos][8] | (color << 3);
478
479 glui32 glk_color = ((pal[color][0] << 16)) | ((pal[color][1] << 8)) | (pal[color][2]);
480
481 glk_window_fill_rect(Graphics, glk_color, x * pixel_size + x_offset,
482 y * pixel_size + y_offset, width * pixel_size,
483 height * pixel_size);
484 }
485
486 void background(int32_t x, int32_t y, int32_t color)
487 {
488 /* Draw the background */
489 RectFill(x * 8, y * 8, 8, 8, color);
490 }
491
492 void plotsprite(int32_t character, int32_t x, int32_t y, int32_t fg,
493 int32_t bg)
494 {
495 int32_t i, j;
496 background(x, y, bg);
497 for (i = 0; i < 8; i++) {
498 for (j = 0; j < 8; j++)
499 if ((screenchars[character][i] & (1 << j)) != 0)
500 PutPixel(x * 8 + j, y * 8 + i, fg);
501 }
502 }
503
504 int isNthBitSet(unsigned const char c, int n)
505 {
506 static unsigned const char mask[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
507 return ((c & mask[n]) != 0);
508 }
509
510 uint8_t *DrawSagaPictureFromData(uint8_t *dataptr, int xsize, int ysize,
511 int xoff, int yoff);
512
513 struct image_patch {
514 GameIDType id;
515 int picture_number;
516 int offset;
517 int number_of_bytes;
518 const char *patch;
519 };
520
521 struct image_patch image_patches[] = {
522 { UNKNOWN_GAME, 0, 0, 0, "" },
523 { CLAYMORGUE_C64, 12, 378, 9, "\x20\xa4\x02\x80\x20\x82\x01\x20\x84" },
524 { CLAYMORGUE_C64, 28, 584, 1, "\x90" },
525 { CLAYMORGUE_C64, 16, 0, 12,
526 "\x82\xfd\x00\x82\x81\x00\xff\x05\xff\x05\xff\x05" },
527 { CLAYMORGUE, 14, 0, 12,
528 "\x82\xfd\x00\x82\x81\x00\xff\x05\xff\x05\xff\x05" },
529 { GREMLINS_C64, 21, 10, 5, "\x01\xa0\x03\x00\x01" },
530 { GREMLINS_C64, 44, 304, 1, "\xb1" },
531 { GREMLINS_C64, 81, 176, 1, "\xa0" },
532 { GREMLINS_C64, 85, 413, 1, "\x94" },
533 { GREMLINS_GERMAN_C64, 20, 191, 9, "\x03\x00\x94\x0c\xb0\x02\x94\x0d\xb0" },
534 { SPIDERMAN_C64, 9, 1241, 1, "\x81" },
535 { SECRET_MISSION_C64, 11, 88, 2, "\x90\x18" },
536 { SECRET_MISSION, 33, 2, 1, "\xa0" },
537 { SECRET_MISSION_C64, 33, 2, 1, "\xa0" },
538 { SECRET_MISSION, 38, 53, 1, "\x53" },
539 { SECRET_MISSION_C64, 38, 53, 1, "\x53" },
540 { SEAS_OF_BLOOD_C64, 34, 471, 1, "\xa0" },
541 { SEAS_OF_BLOOD_C64, 35, 16, 1, "\xcc" },
542 { NUMGAMES, 0, 0, 0, "" },
543 };
544
545 int FindImagePatch(GameIDType game, int image_number, int start)
546 {
547 for (int i = start + 1; image_patches[i].id != NUMGAMES; i++) {
548 if (image_patches[i].id == game && image_patches[i].picture_number == image_number) {
549 return i;
550 }
551 }
552 return 0;
553 }
554
555 void Patch(uint8_t *offset, int patch_number)
556 {
557 struct image_patch *patch = &image_patches[patch_number];
558 for (int i = 0; i < patch->number_of_bytes; i++) {
559 uint8_t newval = patch->patch[i];
560 // debug_print("Patch: changing offset %d in image %d from %x to %x.\n", i + patch->offset, patch->picture_number, offset[i + patch->offset], newval);
561 offset[i + patch->offset] = newval;
562 }
563 }
564
565 void PatchOutBrokenClaymorgueImagesC64(void)
566 {
567 Output("[This copy of The Sorcerer of Claymorgue Castle has 16 broken or "
568 "missing pictures. These have been patched out.]\n\n");
569 for (int i = 12; i < 28; i++) {
570 if (i != 16)
571 for (int j = 0; j < GameHeader.NumRooms; j++) {
572 if (Rooms[j].Image == i) {
573 Rooms[j].Image = 255;
574 }
575 }
576 }
577 }
578
579 void PatchOutBrokenClaymorgueImagesZX(void)
580 {
581 Output("[This copy of The Sorcerer of Claymorgue Castle has 26 broken or "
582 "missing pictures. These have been patched out.]\n\n");
583 for (int i = 9; i < 36; i++) {
584 if (i != 14)
585 for (int j = 0; j < GameHeader.NumRooms; j++) {
586 if (Rooms[j].Image == i) {
587 Rooms[j].Image = 255;
588 }
589 }
590 }
591 }
592
593 size_t hulk_coordinates = 0x26db;
594 size_t hulk_item_image_offsets = 0x2798;
595 size_t hulk_look_image_offsets = 0x27bc;
596 size_t hulk_special_image_offsets = 0x276e;
597 size_t hulk_image_offset = 0x441b;
598
599 void SagaSetup(size_t imgoffset)
600 {
601 int32_t i, y;
602
603 uint16_t image_offsets[Game->number_of_pictures];
604
605 if (palchosen == NO_PALETTE) {
606 palchosen = Game->palette;
607 }
608
609 if (palchosen == NO_PALETTE) {
610 debug_print("unknown palette\n");
611 exit(EXIT_FAILURE);
612 }
613
614 DefinePalette();
615
616 int version = Game->picture_format_version;
617
618 int32_t CHAR_START = Game->start_of_characters + file_baseline_offset;
619 int32_t OFFSET_TABLE_START = Game->start_of_image_data + file_baseline_offset;
620
621 if (Game->start_of_image_data == FOLLOWS) {
622 OFFSET_TABLE_START = CHAR_START + 0x800;
623 }
624
625 int32_t DATA_OFFSET = Game->image_address_offset + file_baseline_offset;
626 if (imgoffset)
627 DATA_OFFSET = imgoffset;
628 uint8_t *pos;
629 int numgraphics = Game->number_of_pictures;
630 pos = SeekToPos(entire_file, CHAR_START);
631
632 #ifdef DRAWDEBUG
633 debug_print("Grabbing Character details\n");
634 debug_print("Character Offset: %04x\n",
635 CHAR_START - file_baseline_offset);
636 #endif
637 for (i = 0; i < 256; i++) {
638 for (y = 0; y < 8; y++) {
639 sprite[i][y] = *(pos++);
640 }
641 }
642
643 /* Now we have hopefully read the character data */
644 /* Time for the image offsets */
645
646 images = (Image *)MemAlloc(sizeof(Image) * numgraphics);
647 Image *img = images;
648
649 pos = SeekToPos(entire_file, OFFSET_TABLE_START);
650
651 int broken_claymorgue_pictures_c64 = 0;
652 int broken_claymorgue_pictures_zx = 0;
653
654 for (i = 0; i < numgraphics; i++) {
655 if (Game->picture_format_version == 0) {
656 uint16_t address;
657
658 if (i < 11) {
659 address = Game->start_of_image_data + (i * 2);
660 } else if (i < 28) {
661 address = hulk_item_image_offsets + (i - 10) * 2;
662 } else if (i < 34) {
663 address = hulk_look_image_offsets + (i - 28) * 2;
664 } else {
665 address = hulk_special_image_offsets + (i - 34) * 2;
666 }
667
668 address += file_baseline_offset;
669 address = entire_file[address] + entire_file[address + 1] * 0x100;
670
671 image_offsets[i] = address + hulk_image_offset;
672 } else {
673 image_offsets[i] = *(pos++);
674 image_offsets[i] += *(pos++) * 0x100;
675 }
676 }
677
678 for (int picture_number = 0; picture_number < numgraphics; picture_number++) {
679 pos = SeekToPos(entire_file, image_offsets[picture_number] + DATA_OFFSET);
680 if (pos == 0)
681 return;
682
683 img->width = *(pos++);
684 if (img->width > 32)
685 img->width = 32;
686
687 img->height = *(pos++);
688 if (img->height > 12)
689 img->height = 12;
690
691 if (version > 0) {
692 img->xoff = *(pos++);
693 if (img->xoff > 32)
694 img->xoff = 4;
695 img->yoff = *(pos++);
696 if (img->yoff > 12)
697 img->yoff = 0;
698 } else {
699 if (picture_number > 9 && picture_number < 28) {
700 img->xoff = entire_file[hulk_coordinates + picture_number - 10 + file_baseline_offset];
701 img->yoff = entire_file[hulk_coordinates + 18 + picture_number - 10 + file_baseline_offset];
702 } else {
703 img->xoff = img->yoff = 0;
704 }
705 }
706
707 if (CurrentGame == CLAYMORGUE_C64 && img->height == 0 && img->width == 0 && picture_number == 13) {
708 PatchOutBrokenClaymorgueImagesC64();
709 broken_claymorgue_pictures_c64 = 1;
710 }
711
712 if (broken_claymorgue_pictures_c64 && (picture_number == 16 || picture_number == 28)) {
713 img->height = 12;
714 img->width = 32;
715 img->xoff = 4;
716 img->yoff = 0;
717 if (picture_number == 28)
718 pos = entire_file + 0x2005;
719 }
720
721 if (CurrentGame == CLAYMORGUE && img->height == 0 && img->width == 0 && picture_number == 9) {
722 PatchOutBrokenClaymorgueImagesZX();
723 broken_claymorgue_pictures_zx = 1;
724 }
725
726 if (broken_claymorgue_pictures_zx && (picture_number == 14)) {
727 img->height = 12;
728 img->width = 32;
729 img->xoff = 4;
730 img->yoff = 0;
731 }
732
733 img->imagedata = pos;
734
735 int patch = FindImagePatch(CurrentGame, picture_number, 0);
736 while (patch) {
737 Patch(img->imagedata, patch);
738 patch = FindImagePatch(CurrentGame, picture_number, patch);
739 }
740
741 img++;
742 }
743 }
744
745 void PrintImageContents(int index, uint8_t *data, size_t size)
746 {
747 debug_print("/* image %d ", index);
748 debug_print(
749 "width: %d height: %d xoff: %d yoff: %d size: %zu bytes*/\n{ ",
750 images[index].width, images[index].height, images[index].xoff,
751 images[index].yoff, size);
752 for (int i = 0; i < size; i++) {
753 debug_print("0x%02x, ", data[i]);
754 if (i % 8 == 7)
755 debug_print("\n ");
756 }
757
758 debug_print(" },\n");
759 return;
760 }
761
762 void debugdrawcharacter(int character)
763 {
764 debug_print("Contents of character %d of 256:\n", character);
765 for (int row = 0; row < 8; row++) {
766 for (int n = 0; n < 8; n++) {
767 if (isNthBitSet(sprite[character][row], n))
768 debug_print("■");
769 else
770 debug_print("0");
771 }
772 debug_print("\n");
773 }
774 if (character != 255)
775 debugdrawcharacter(255);
776 }
777
778 void debugdraw(int on, int character, int xoff, int yoff, int width)
779 {
780 if (on) {
781 int x = character % width;
782 int y = character / width;
783 plotsprite(character, x + xoff, y + yoff, 0, 15);
784 debug_print("Contents of character position %d:\n", character);
785 for (int row = 0; row < 8; row++) {
786 for (int n = 0; n < 8; n++) {
787 if (isNthBitSet(screenchars[character][row], n))
788 debug_print("■");
789 else
790 debug_print("0");
791 }
792 debug_print("\n");
793 }
794 }
795 }
796
797 uint8_t *DrawSagaPictureFromData(uint8_t *dataptr, int xsize, int ysize,
798 int xoff, int yoff)
799 {
800 int32_t offset = 0, cont = 0;
801 int32_t i, x, y, mask_mode;
802 uint8_t data, data2, old = 0;
803 int32_t ink[0x22][14], paper[0x22][14];
804
805 // uint8_t *origptr = dataptr;
806 int version = Game->picture_format_version;
807
808 offset = 0;
809 int32_t character = 0;
810 int32_t count;
811 do {
812 count = 1;
813
814 /* first handle mode */
815 data = *dataptr++;
816 if (data < 0x80) {
817 if (character > 127 && version > 2) {
818 data += 128;
819 }
820 character = data;
821 #ifdef DRAWDEBUG
822 debug_print("******* SOLO CHARACTER: %04x\n", character);
823 #endif
824 transform(character, 0, offset);
825 offset++;
826 if (offset > 767)
827 break;
828 } else {
829 // first check for a count
830 if ((data & 2) == 2) {
831 count = (*dataptr++) + 1;
832 }
833
834 // Next get character and plot (count) times
835 character = *dataptr++;
836
837 // Plot the initial character
838 if ((data & 1) == 1 && character < 128)
839 character += 128;
840
841 for (i = 0; i < count; i++)
842 transform(character, (data & 0x0c) ? (data & 0xf3) : data, offset + i);
843
844 // Now check for overlays
845 if ((data & 0xc) != 0) {
846 // We have overlays so grab each member of the stream and work out what
847 // to do with it
848
849 mask_mode = (data & 0xc);
850 data2 = *dataptr++;
851 old = data;
852 do {
853 cont = 0;
854 if (data2 < 0x80) {
855 if (version == 4 && (old & 1) == 1)
856 data2 += 128;
857 #ifdef DRAWDEBUG
858 debug_print("Plotting %d directly (overlay) at %d\n", data2,
859 offset);
860 #endif
861 for (i = 0; i < count; i++)
862 transform(data2, old & 0x0c, offset + i);
863 } else {
864 character = *dataptr++;
865 if ((data2 & 1) == 1)
866 character += 128;
867 #ifdef DRAWDEBUG
868 debug_print("Plotting %d with flip %02x (%s) at %d %d\n",
869 character, (data2 | mask_mode),
870 flipdescription[((data2 | mask_mode) & 48) >> 4], offset,
871 count);
872 #endif
873 for (i = 0; i < count; i++)
874 transform(character, (data2 & 0xf3) | mask_mode, offset + i);
875 if ((data2 & 0x0c) != 0) {
876 mask_mode = (data2 & 0x0c);
877 old = data2;
878 cont = 1;
879 data2 = *dataptr++;
880 }
881 }
882 } while (cont != 0);
883 }
884 offset += count;
885 }
886 } while (offset < xsize * ysize);
887
888 y = 0;
889 x = 0;
890
891 // debug_print("Attribute data begins at offset %ld\n", dataptr -
892 // origptr);
893
894 uint8_t colour = 0;
895 // Note version 3 count is inverse it is repeat previous colour
896 // Whilst version0-2 count is repeat next character
897 while (y < ysize) {
898 if (dataptr - entire_file > file_length)
899 return dataptr - 1;
900 data = *dataptr++;
901 if ((data & 0x80)) {
902 count = (data & 0x7f) + 1;
903 if (version >= 3) {
904 count--;
905 } else {
906 colour = *dataptr++;
907 }
908 } else {
909 count = 1;
910 colour = data;
911 }
912 while (count > 0) {
913 // Now split up depending on which version we're using
914
915 // For version 3+
916
917 if (draw_to_buffer)
918 buffer[(yoff + y) * 32 + (xoff + x)][8] = colour;
919 else {
920 if (version > 2) {
921 if (x > 33)
922 return NULL;
923 // ink is 0-2, screen is 3-5, 6 in bright flag
924 ink[x][y] = colour & 0x07;
925 paper[x][y] = (colour & 0x38) >> 3;
926
927 if ((colour & 0x40) == 0x40) {
928 paper[x][y] += 8;
929 ink[x][y] += 8;
930 }
931 } else {
932 if (x > 33)
933 return NULL;
934 paper[x][y] = colour & 0x07;
935 ink[x][y] = ((colour & 0x70) >> 4);
936
937 if ((colour & 0x08) == 0x08 || version < 2) {
938 paper[x][y] += 8;
939 ink[x][y] += 8;
940 }
941 }
942 }
943
944 x++;
945 if (x == xsize) {
946 x = 0;
947 y++;
948 }
949 count--;
950 }
951 }
952 offset = 0;
953 int32_t xoff2;
954 for (y = 0; y < ysize; y++)
955 for (x = 0; x < xsize; x++) {
956 xoff2 = xoff;
957 if (version > 0 && version < 3)
958 xoff2 = xoff - 4;
959
960 if (draw_to_buffer) {
961 for (i = 0; i < 8; i++)
962 buffer[(y + yoff) * 32 + x + xoff2][i] = screenchars[offset][i];
963 } else {
964 plotsprite(offset, x + xoff2, y + yoff, Remap(ink[x][y]),
965 Remap(paper[x][y]));
966 }
967
968 #ifdef DRAWDEBUG
969 debug_print("(gfx#:plotting %d,%d:paper=%s,ink=%s)\n", x + xoff2,
970 y + yoff, colortext(remap(paper[x][y])),
971 colortext(remap(ink[x][y])));
972 #endif
973 offset++;
974 if (offset > 766)
975 break;
976 }
977 return dataptr;
978 }
979
980 void DrawSagaPictureNumber(int picture_number)
981 {
982 int numgraphics = Game->number_of_pictures;
983 if (picture_number >= numgraphics) {
984 debug_print("Invalid image number %d! Last image:%d\n", picture_number,
985 numgraphics - 1);
986 return;
987 }
988
989 Image img = images[picture_number];
990
991 if (img.imagedata == NULL)
992 return;
993
994 DrawSagaPictureFromData(img.imagedata, img.width, img.height, img.xoff,
995 img.yoff);
996
997 last_image_index = picture_number;
998 }
999
1000 void DrawSagaPictureAtPos(int picture_number, int x, int y)
1001 {
1002 Image img = images[picture_number];
1003
1004 DrawSagaPictureFromData(img.imagedata, img.width, img.height, x, y);
1005 }
1006
1007 void SwitchPalettes(int pal1, int pal2)
1008 {
1009 uint8_t temp[3];
1010
1011 temp[0] = pal[pal1][0];
1012 temp[1] = pal[pal1][1];
1013 temp[2] = pal[pal1][2];
1014
1015 pal[pal1][0] = pal[pal2][0];
1016 pal[pal1][1] = pal[pal2][1];
1017 pal[pal1][2] = pal[pal2][2];
1018
1019 pal[pal2][0] = temp[0];
1020 pal[pal2][1] = temp[1];
1021 pal[pal2][2] = temp[2];
1022 }
1023
1024 void DrawSagaPictureFromBuffer(void)
1025 {
1026 for (int line = 0; line < 12; line++) {
1027 for (int col = 0; col < 32; col++) {
1028
1029 uint8_t colour = buffer[col + line * 32][8];
1030
1031 int paper = (colour >> 3) & 0x7;
1032 paper += 8 * ((colour & 0x40) == 0x40);
1033 paper = Remap(paper);
1034 int ink = (colour & 0x7);
1035 ink += 8 * ((colour & 0x40) == 0x40);
1036 ink = Remap(ink);
1037
1038 background(col, line, paper);
1039
1040 for (int i = 0; i < 8; i++) {
1041 if (buffer[col + line * 32][i] == 0)
1042 continue;
1043 if (buffer[col + line * 32][i] == 255) {
1044
1045 glui32 glk_color = (pal[ink][0] << 16) | (pal[ink][1] << 8) | pal[ink][2];
1046
1047 glk_window_fill_rect(
1048 Graphics, glk_color, col * 8 * pixel_size + x_offset,
1049 (line * 8 + i) * pixel_size, 8 * pixel_size, pixel_size);
1050 continue;
1051 }
1052 for (int j = 0; j < 8; j++)
1053 if ((buffer[col + line * 32][i] & (1 << j)) != 0) {
1054 int ypos = line * 8 + i;
1055 PutPixel(col * 8 + j, ypos, ink);
1056 }
1057 }
1058 }
1059 }
1060 }