Debian Codegraph @

terps/plus/pcdraw.c

raw symbols SCIP
1 //
2 // pcdraw.h
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 //
5 // Based on code witten by David Lodge on 29/04/2005
6 // Routines to draw the PC graphics in Plus games
7 //
8 // Original code at https://github.com/tautology0/textadventuregraphics
9 //
10
11 #include <stdio.h>
12 #include <stdlib.h>
13
14 #include "common.h"
15 #include "glk.h"
16 #include "graphics.h"
17
18 extern int at_last_line;
19
20 int ycount = 0;
21 int skipy = 1;
22
23 /* palette handler stuff starts here */
24
25 static void DrawDOSPixels(int pattern)
26 {
27 int pix1, pix2, pix3, pix4;
28 // Now get colors
29 pix1 = (pattern & 0xc0) >> 6;
30 pix2 = (pattern & 0x30) >> 4;
31 pix3 = (pattern & 0x0c) >> 2;
32 pix4 = (pattern & 0x03);
33
34 if (!skipy) {
35 PutDoublePixel(x, y, pix1);
36 x += 2;
37 PutDoublePixel(x, y, pix2);
38 x += 2;
39 PutDoublePixel(x, y, pix3);
40 x += 2;
41 PutDoublePixel(x, y, pix4);
42 x += 2;
43 } else {
44 PutPixel(x, y, pix1);
45 x++;
46 PutPixel(x, y, pix2);
47 x++;
48 PutPixel(x, y, pix3);
49 x++;
50 PutPixel(x, y, pix4);
51 x++;
52 }
53
54 if (x >= xlen + xoff) {
55 y += 2;
56 x = xoff;
57 ycount++;
58 }
59
60 if (ycount > ylen) {
61 y = yoff + 1;
62 at_last_line++;
63 ycount = 0;
64 }
65 }
66
67 int DrawDOSImageFromData(uint8_t *ptr, size_t datasize)
68 {
69 x = 0;
70 y = 0;
71 at_last_line = 0;
72
73 xlen = 0;
74 ylen = 0;
75 xoff = 0;
76 yoff = 0;
77 ycount = 0;
78 skipy = 1;
79
80 int work;
81 int c;
82 int i;
83 int rawoffset;
84 // clang-format off
85 RGB black = { 0,0,0 };
86 RGB magenta = { 255,0,255 };
87 RGB cyan = { 0,255,255 };
88 RGB white = { 255,255,255 };
89 // clang-format on
90
91 /* set up the palette */
92 SetColor(0, &black);
93 SetColor(1, &cyan);
94 SetColor(2, &magenta);
95 SetColor(3, &white);
96
97 if (ptr == NULL) {
98 fprintf(stderr, "DrawMSDOSImageFromData: ptr == NULL\n");
99 return 0;
100 }
101
102 uint8_t *origptr = ptr;
103
104 // Get the size of the graphics chunk
105 ptr = origptr + 0x05;
106 work = *ptr++;
107 int imagesize = work + (*ptr * 256);
108
109 // Get whether it is lined
110 ptr = origptr + 0x0d;
111 work = *ptr++;
112 if (work == 0xff)
113 skipy = 0;
114
115 // Get the offset
116 ptr = origptr + 0x0f;
117 work = *ptr++;
118 rawoffset = work + (*ptr * 256);
119 xoff = ((rawoffset % 80) * 4) - 24;
120 yoff = rawoffset / 40;
121 yoff -= (yoff & 1);
122 x = xoff;
123 y = yoff;
124
125 // Get the y length
126 ptr = origptr + 0x11;
127 work = *ptr++;
128 ylen = work + (*ptr * 256);
129 ylen -= rawoffset;
130 ylen /= 80;
131
132 // Get the x length
133 ptr = origptr + 0x13;
134 xlen = *ptr * 4;
135
136 ptr = origptr + 0x17;
137 while (ptr - origptr < imagesize) {
138 // First get count
139 c = *ptr++;
140
141 if ((c & 0x80) == 0x80) { // is a counter
142 work = *ptr++;
143 c &= 0x7f;
144 for (i = 0; i <= c; i++) {
145 DrawDOSPixels(work);
146 }
147 } else {
148 // Don't count on the next j characters
149 for (i = 0; i <= c; i++) {
150 work = *ptr++;
151 DrawDOSPixels(work);
152 }
153 }
154 if (at_last_line > 1)
155 break;
156 }
157 return 1;
158 }