Debian Codegraph @

terps/plus/atari8c64draw.c

raw symbols SCIP
1 //
2 // atari8c64draw.c
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 Atari 8-bit and C64 RLE graphics
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 "definitions.h"
16 #include "glk.h"
17 #include "graphics.h"
18
19 static void DrawA8C64Pixels(int pattern, int pattern2)
20 {
21 int pix1, pix2, pix3, pix4;
22
23 if (x > (xlen - 3) * 8)
24 return;
25
26 pix1 = (pattern & 0xc0) >> 6;
27 pix2 = (pattern & 0x30) >> 4;
28 pix3 = (pattern & 0x0c) >> 2;
29 pix4 = (pattern & 0x03);
30
31 PutDoublePixel(x, y, pix1);
32 x += 2;
33 PutDoublePixel(x, y, pix2);
34 x += 2;
35 PutDoublePixel(x, y, pix3);
36 x += 2;
37 PutDoublePixel(x, y, pix4);
38 x += 2;
39 y++;
40 x -= 8;
41
42 pix1 = (pattern2 & 0xc0) >> 6;
43 pix2 = (pattern2 & 0x30) >> 4;
44 pix3 = (pattern2 & 0x0c) >> 2;
45 pix4 = (pattern2 & 0x03);
46
47 PutDoublePixel(x, y, pix1);
48 x += 2;
49 PutDoublePixel(x, y, pix2);
50 x += 2;
51 PutDoublePixel(x, y, pix3);
52 x += 2;
53 PutDoublePixel(x, y, pix4);
54 x += 2;
55 y++;
56 x -= 8;
57
58 if (y > ylen) {
59 x += 8;
60 y = yoff;
61 }
62 }
63
64 void SetColor(int32_t index, const RGB *color);
65
66 /* C64 colors */
67 static const RGB black = { 0, 0, 0 };
68 static const RGB white = { 255, 255, 255 };
69 static const RGB red = { 191, 97, 72 };
70 static const RGB purple = { 177, 89, 185 };
71 static const RGB green = { 121, 213, 112 };
72 static const RGB blue = { 95, 72, 233 };
73 static const RGB yellow = { 247, 255, 108 };
74 static const RGB orange = { 186, 134, 32 };
75 static const RGB brown = { 131, 112, 0 };
76 static const RGB lred = { 231, 154, 132 };
77 static const RGB grey = { 167, 167, 167 };
78 static const RGB lgreen = { 192, 255, 185 };
79 static const RGB lblue = { 162, 143, 255 };
80
81 /* Atari 8-bit colors */
82 static const RGB cyan = { 0x9c, 0xe2, 0xc5 };
83 static const RGB lpurple = { 0xdf, 0xaa, 0xff };
84 static const RGB apurple = { 0xdb, 0x47, 0xdd };
85 static const RGB deeppurple = { 0x73, 0x1c, 0x73 };
86 static const RGB dblue = { 0x30, 0x24, 0xff };
87 static const RGB agreen = { 0x08, 0x88, 0x17 };
88 static const RGB dgreen = { 0x4f, 0x74, 0x20 };
89 static const RGB darkergreen = { 0x08, 0x38, 0x00 };
90 static const RGB ablue = { 0x36, 0x6e, 0xff };
91 static const RGB ayellow = { 0xef, 0xf2, 0x58 };
92 static const RGB aorange = { 0xbf, 0x77, 0x30 };
93 static const RGB abrown = { 0xab, 0x51, 0x1f };
94 static const RGB dbrown = { 0x73, 0x2c, 0x00 };
95 static const RGB alred = { 0xc2, 0x52, 0x57 };
96 static const RGB beige = { 0xff, 0x8f, 0x8f };
97 static const RGB dred = { 0xa2, 0x3f, 0x40 };
98 static const RGB agrey = { 0x92, 0x92, 0x92 };
99 static const RGB lgrey = { 0xb4, 0xb5, 0xb4 };
100 static const RGB algreen = { 0x5f, 0x8f, 0x00 };
101 static const RGB tan = { 0xaf, 0x99, 0x3a };
102 static const RGB lilac = { 0x83, 0x58, 0xee };
103
104 static void TranslateAtariColor(int index, uint8_t value)
105 {
106 switch (value) {
107 case 0:
108 case 224:
109 SetColor(index, &black);
110 break;
111 case 5:
112 case 7:
113 SetColor(index, &agrey);
114 break;
115 case 6:
116 case 8:
117 SetColor(index, &ablue);
118 break;
119 case 9:
120 case 10:
121 case 12:
122 SetColor(index, &lgrey);
123 break;
124 case 14:
125 SetColor(index, &white);
126 break;
127 case 17:
128 case 32:
129 case 36:
130 SetColor(index, &dbrown);
131 break;
132 case 40:
133 SetColor(index, &aorange);
134 break;
135 case 54:
136 if (CurrentGame == FANTASTIC4)
137 SetColor(index, &aorange);
138 else
139 SetColor(index, &dred);
140 break;
141 case 56:
142 SetColor(index, &alred);
143 break;
144 case 58:
145 case 60:
146 SetColor(index, &beige);
147 break;
148 case 69:
149 case 71:
150 SetColor(index, &deeppurple);
151 break;
152 case 68:
153 case 103:
154 SetColor(index, &apurple);
155 break;
156 case 89:
157 SetColor(index, &lilac);
158 break;
159 case 85:
160 case 101:
161 SetColor(index, &dblue);
162 break;
163 case 110:
164 SetColor(index, &lpurple);
165 break;
166 case 135:
167 SetColor(index, &ablue);
168 break;
169 case 174:
170 SetColor(index, &cyan);
171 break;
172 case 182:
173 case 196:
174 case 214:
175 case 198:
176 case 200:
177 SetColor(index, &agreen);
178 break;
179 case 194:
180 SetColor(index, &darkergreen);
181 break;
182 case 212:
183 SetColor(index, &dgreen);
184 break;
185 case 199:
186 case 215:
187 case 201:
188 SetColor(index, &algreen);
189 break;
190 case 230:
191 SetColor(index, &tan);
192 break;
193 case 237:
194 SetColor(index, &ayellow);
195 break;
196 case 244:
197 SetColor(index, &abrown);
198 break;
199 case 246:
200 case 248:
201 SetColor(index, &aorange);
202 break;
203 default:
204 fprintf(stderr, "Unknown color %d ", value);
205 break;
206 }
207 }
208
209 /*
210 The values below are determined by looking at the games
211 running in the VICE C64 emulator.
212 I have no idea how the original interpreter calculates
213 them. I might have made some mistakes. */
214
215 static void TranslateC64Color(int index, uint8_t value)
216 {
217 switch (value) {
218 case 0:
219 SetColor(index, &purple);
220 break;
221 case 1:
222 SetColor(index, &blue);
223 break;
224 case 3:
225 SetColor(index, &white);
226 break;
227 case 7:
228 case 8:
229 SetColor(index, &blue);
230 break;
231 case 9:
232 case 10:
233 case 12:
234 case 14:
235 case 15:
236 SetColor(index, &white);
237 break;
238 case 17:
239 SetColor(index, &green);
240 break;
241 case 36:
242 case 40:
243 SetColor(index, &brown);
244 break;
245 case 46:
246 SetColor(index, &yellow);
247 break;
248 case 52:
249 case 54:
250 case 56:
251 case 60:
252 SetColor(index, &orange);
253 break;
254 case 68:
255 case 69:
256 case 71:
257 SetColor(index, &red);
258 break;
259 case 77:
260 case 85:
261 case 87:
262 SetColor(index, &purple);
263 break;
264 case 89:
265 SetColor(index, &lred);
266 break;
267 case 101:
268 case 103:
269 SetColor(index, &purple);
270 break;
271 case 110:
272 SetColor(index, &lblue);
273 break;
274 case 135:
275 case 137:
276 SetColor(index, &blue);
277 break;
278 case 157:
279 SetColor(index, &lblue);
280 break;
281 case 161:
282 SetColor(index, &grey);
283 break;
284 case 179:
285 case 182:
286 case 194:
287 case 196:
288 case 198:
289 case 199:
290 case 200:
291 SetColor(index, &green);
292 break;
293 case 201:
294 SetColor(index, &lgreen);
295 break;
296 case 212:
297 case 214:
298 case 215:
299 SetColor(index, &green);
300 break;
301 case 224:
302 SetColor(index, &purple);
303 break;
304 case 230:
305 case 237:
306 SetColor(index, &yellow);
307 break;
308 case 244:
309 case 246:
310 SetColor(index, &brown);
311 break;
312 case 248:
313 if (CurrentGame == FANTASTIC4)
314 SetColor(index, &orange);
315 else
316 SetColor(index, &brown);
317 break;
318 case 252:
319 SetColor(index, &yellow);
320 break;
321 default:
322 fprintf(stderr, "Unknown color %d ", value);
323 break;
324 }
325 }
326
327 int DrawAtariC64ImageFromData(uint8_t *ptr, size_t datasize)
328 {
329 int work, work2;
330 int c;
331 int i;
332
333 uint8_t *origptr = ptr;
334
335 x = 0;
336 y = 0;
337
338 ptr += 2;
339
340 // For Atari 8-bit, this vaule is already datasize
341 // but on C64, datasize is the actual file size of the image
342 // file on disk
343 work = *ptr++;
344 size_t size = work + *ptr++ * 256;
345 if (size < datasize - 2) {
346 datasize = size + 2;
347 }
348
349 // Get the offset
350 xoff = *ptr++ - 3;
351 if (xoff < 0)
352 xoff = 0;
353 yoff = *ptr++;
354 x = xoff * 8;
355 y = yoff;
356
357 // Get the x length
358 xlen = *ptr++;
359 ylen = *ptr++;
360
361 if (CurrentSys == SYS_ATARI8) {
362 glui32 curheight, curwidth;
363 glk_window_get_size(Graphics, &curwidth, &curheight);
364 if (CurrentGame == SPIDERMAN && ((ylen == 126 && xlen == 39) || (ylen == 158 && xlen == 38))) {
365 ImageHeight = ylen + 2;
366 ImageWidth = xlen * 8;
367 if (ylen == 158)
368 ImageWidth -= 24;
369 else
370 ImageWidth -= 17;
371 }
372
373 int optimal_height = ImageHeight * pixel_size;
374 if (curheight != optimal_height) {
375 x_offset = (curwidth - (ImageWidth * pixel_size)) / 2;
376 right_margin = (ImageWidth * pixel_size) + x_offset;
377 winid_t parent = glk_window_get_parent(Graphics);
378 if (parent)
379 glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed,
380 optimal_height, NULL);
381 }
382 }
383
384 SetColor(0, &black);
385
386 // Get the palette
387 debug_print("Colors: ");
388 for (i = 1; i < 5; i++) {
389 work = *ptr++;
390 if (CurrentSys == SYS_C64)
391 TranslateC64Color(i, work);
392 else
393 TranslateAtariColor(i, work);
394 debug_print("%d ", work);
395 }
396 debug_print("\n");
397
398 while (ptr - origptr < datasize - 2) {
399 // First get count
400 c = *ptr++;
401
402 if ((c & 0x80) == 0x80) { // is a counter
403 c &= 0x7f;
404 work = *ptr++;
405 work2 = *ptr++;
406 for (i = 0; i < c + 1; i++) {
407 DrawA8C64Pixels(work, work2);
408 }
409 } else {
410 // Don't count on the next c characters
411 for (i = 0; i < c + 1 && ptr - origptr < datasize - 1; i++) {
412 work = *ptr++;
413 work2 = *ptr++;
414 DrawA8C64Pixels(work, work2);
415 }
416 }
417 }
418 return 1;
419 }