Debian Codegraph @

terps/scott/saga/atari8c64draw.c

raw symbols SCIP
1 //
2 // atari8c64draw
3 // Part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Routines to draw Atari 8-bit and C64 RLE graphics
6 // Based on Code by David Lodge 29/04/2005
7 //
8 // Original code at https://github.com/tautology0/textadventuregraphics
9
10 #include <stdio.h>
11 #include <stdlib.h>
12
13 #include "sagagraphics.h"
14 #include "scott.h"
15
16 extern int x, y, count;
17 extern int xlen, ylen;
18 extern int xoff, yoff;
19 extern int size;
20
21 typedef uint8_t RGB[3];
22
23 static void DrawA8C64Pixels(int pattern, int pattern2)
24 {
25 int pix1, pix2, pix3, pix4;
26
27 if (x > (xlen - 3) * 8)
28 return;
29
30 pix1 = (pattern & 0xc0) >> 6;
31 pix2 = (pattern & 0x30) >> 4;
32 pix3 = (pattern & 0x0c) >> 2;
33 pix4 = (pattern & 0x03);
34
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 y++;
44 x -= 8;
45
46 pix1 = (pattern2 & 0xc0) >> 6;
47 pix2 = (pattern2 & 0x30) >> 4;
48 pix3 = (pattern2 & 0x0c) >> 2;
49 pix4 = (pattern2 & 0x03);
50
51 PutDoublePixel(x, y, pix1);
52 x += 2;
53 PutDoublePixel(x, y, pix2);
54 x += 2;
55 PutDoublePixel(x, y, pix3);
56 x += 2;
57 PutDoublePixel(x, y, pix4);
58 x += 2;
59 y++;
60 x -= 8;
61
62 if (y > ylen) {
63 x += 8;
64 y = yoff;
65 }
66 }
67
68 /* C64 colors */
69 static const RGB black = { 0, 0, 0 };
70 static const RGB white = { 255, 255, 255 };
71 static const RGB red = { 191, 97, 72 };
72 static const RGB purple = { 177, 89, 185 };
73 static const RGB green = { 121, 213, 112 };
74 static const RGB blue = { 95, 72, 233 };
75 static const RGB yellow = { 247, 255, 108 };
76 static const RGB orange = { 186, 134, 32 };
77 static const RGB brown = { 131, 112, 0 };
78 static const RGB lred = { 231, 154, 132 };
79 static const RGB grey = { 167, 167, 167 };
80 static const RGB lgreen = { 192, 255, 185 };
81 static const RGB lblue = { 162, 143, 255 };
82
83 // clang-format off
84
85 /* Atari 8-bit colors */
86 RGB colors[256] = {
87 { 0x00, 0x00, 0x00 }, // 0
88 { 0x0e, 0x0e, 0x0e }, // 1
89 { 0x1d, 0x1d, 0x1d }, // 2
90
91 { 0x2c, 0x2c, 0x2c }, // 3
92 { 0x3b, 0x3b, 0x3b }, // 4
93 { 0x4a, 0x4a, 0x4a }, // 5
94 { 0x59, 0x59, 0x59 }, // 6
95 { 0x68, 0x68, 0x68 }, // 7
96 { 0x77, 0x77, 0x77 }, // 8
97 { 0x86, 0x86, 0x86 }, // 9
98 { 0x95, 0x95, 0x95 }, // 10
99 { 0xa4, 0xa4, 0xa4 }, // 11
100 { 0xb3, 0xb3, 0xb3 }, // 12
101 { 0xc2, 0xc2, 0xc2 }, // 13
102 // { 0xd1, 0xd1, 0xd1 }, // 14
103 { 0xe0, 0xe0, 0xe0 },
104 { 0xe0, 0xe0, 0xe0 }, // 15
105 { 0x34, 0x00, 0x00 }, // 16
106 { 0x43, 0x0e, 0x00 }, // 17
107 // { 0x52, 0x1d, 0x00 }, // 18
108 { 0xad, 0x5f, 0x64 },
109 { 0x61, 0x2c, 0x00 }, // 19
110 { 0x70, 0x3b, 0x00 }, // 20
111 { 0x7e, 0x4a, 0x00 }, // 21
112 { 0x8d, 0x59, 0x00 }, // 22
113 { 0x9c, 0x68, 0x00 }, // 23
114 { 0xab, 0x77, 0x00 }, // 24
115 { 0xba, 0x86, 0x00 }, // 25
116 { 0xc9, 0x95, 0x0b }, // 26
117 { 0xd8, 0xa4, 0x1a }, // 27
118 { 0xe7, 0xb3, 0x29 }, // 28
119 { 0xee, 0xc2, 0x38 }, // 29
120 { 0xee, 0xd1, 0x46 }, // 30
121 { 0xee, 0xe0, 0x55 }, // 31
122 { 0x3a, 0x00, 0x00 }, // 32
123 { 0x49, 0x01, 0x00 }, // 33
124 { 0x58, 0x10, 0x00 }, // 34
125 { 0x67, 0x1f, 0x00 }, // 35
126 { 0x76, 0x2e, 0x00 }, // 36
127 // { 0x85, 0x3d, 0x00 }, // 37
128 { 0xad, 0x5f, 0x64 },
129 { 0x94, 0x4c, 0x02 }, // 38
130 { 0xa3, 0x5b, 0x11 }, // 39
131 { 0xb2, 0x6a, 0x20 }, // 40
132 { 0xc1, 0x79, 0x2f }, // 41
133 { 0xd0, 0x88, 0x3e }, // 42
134 { 0xdf, 0x97, 0x4d }, // 43
135 { 0xee, 0xa6, 0x5c }, // 44
136 { 0xee, 0xb5, 0x6b }, // 45
137 { 0xee, 0xc4, 0x7a }, // 46
138 { 0xee, 0xd2, 0x89 }, // 47
139 { 0x36, 0x00, 0x00 }, // 48
140 { 0x45, 0x00, 0x00 }, // 49
141 // { 0x54, 0x05, 0x0b }, // 50
142 { 0xad, 0x5f, 0x64 },
143 { 0x62, 0x14, 0x1a }, // 51
144 { 0x71, 0x23, 0x29 }, // 52
145 { 0x80, 0x32, 0x38 }, // 53
146 // { 0x8f, 0x41, 0x46 }, // 54
147 { 0xad, 0x5f, 0x64 },
148 { 0x9e, 0x50, 0x55 }, // 55
149 { 0xad, 0x5f, 0x64 }, // 56
150 { 0xbc, 0x6e, 0x73 }, // 57
151 // { 0xcb, 0x7d, 0x82 }, // 58
152 { 0xad, 0x5f, 0x64 },
153 { 0xda, 0x8c, 0x91 }, // 59
154 { 0xe9, 0x9a, 0xa0 }, // 60
155 { 0xee, 0xa9, 0xaf }, // 61
156 { 0xee, 0xb8, 0xbe }, // 62
157 { 0xee, 0xc7, 0xcd }, // 63
158 { 0x27, 0x00, 0x34 }, // 64
159 { 0x36, 0x00, 0x43 }, // 65
160 { 0x45, 0x00, 0x52 }, // 66
161 { 0x54, 0x0f, 0x61 }, // 67
162 { 0x62, 0x1e, 0x70 }, // 68
163 { 0x71, 0x2d, 0x7e }, // 69
164 { 0x80, 0x3c, 0x8d }, // 70
165 { 0x8f, 0x4b, 0x9c }, // 71
166 { 0x9e, 0x5a, 0xab }, // 72
167 { 0xad, 0x69, 0xba }, // 73
168 { 0xbc, 0x78, 0xc9 }, // 74
169 { 0xcb, 0x87, 0xd8 }, // 75
170 { 0xda, 0x96, 0xe7 }, // 76
171 { 0xe9, 0xa5, 0xee }, // 77
172 { 0xee, 0xb4, 0xee }, // 78
173 { 0xee, 0xc3, 0xee }, // 79
174 { 0x0f, 0x00, 0x71 }, // 80
175 { 0x1e, 0x00, 0x80 }, // 81
176 { 0x2d, 0x00, 0x8f }, // 82
177 { 0x3c, 0x0f, 0x9e }, // 83
178 { 0x4b, 0x1e, 0xad }, // 84
179 { 0x5a, 0x2d, 0xbc }, // 85
180 // { 0x69, 0x3c, 0xcb }, // 86
181 { 0x4b, 0x1e, 0xad },
182 { 0x78, 0x4b, 0xda }, // 87
183 { 0x87, 0x5a, 0xe9 }, // 88
184 { 0x96, 0x69, 0xee }, // 89
185 { 0xa5, 0x78, 0xee }, // 90
186 { 0xb4, 0x87, 0xee }, // 91
187 { 0xc3, 0x96, 0xee }, // 92
188 { 0xd2, 0xa5, 0xee }, // 93
189 { 0xe0, 0xb4, 0xee }, // 94
190 { 0xee, 0xc3, 0xee }, // 95
191 { 0x00, 0x00, 0x99 }, // 96
192 { 0x04, 0x00, 0xa7 }, // 97
193 { 0x13, 0x07, 0xb6 }, // 98
194 { 0x22, 0x16, 0xc5 }, // 99
195 { 0x31, 0x25, 0xd4 }, // 100
196 { 0x40, 0x34, 0xe3 }, // 101
197 { 0x4f, 0x43, 0xee }, // 102
198 { 0x5e, 0x52, 0xee }, // 103
199 { 0x6d, 0x61, 0xee }, // 104
200 { 0x7c, 0x70, 0xee }, // 105
201 { 0x8b, 0x7e, 0xee }, // 106
202 { 0x9a, 0x8d, 0xee }, // 107
203 { 0xa8, 0x9c, 0xee }, // 108
204 { 0xb7, 0xab, 0xee }, // 109
205 { 0xc6, 0xba, 0xee }, // 110
206 { 0xd5, 0xc9, 0xee }, // 111
207 { 0x00, 0x00, 0xa1 }, // 112
208 { 0x00, 0x02, 0xb0 }, // 113
209 { 0x00, 0x11, 0xbf }, // 114
210 { 0x0a, 0x20, 0xce }, // 115
211 { 0x19, 0x2f, 0xdd }, // 116
212 { 0x28, 0x3e, 0xec }, // 117
213 { 0x37, 0x4d, 0xee }, // 118
214 { 0x46, 0x5c, 0xee }, // 119
215 { 0x54, 0x6b, 0xee }, // 120
216 { 0x63, 0x7a, 0xee }, // 121
217 { 0x72, 0x89, 0xee }, // 122
218 { 0x81, 0x98, 0xee }, // 123
219 { 0x90, 0xa7, 0xee }, // 124
220 { 0x9f, 0xb6, 0xee }, // 125
221 { 0xae, 0xc4, 0xee }, // 126
222 { 0xbd, 0xd3, 0xee }, // 127
223 { 0x00, 0x00, 0x8a }, // 128
224 { 0x00, 0x0e, 0x99 }, // 129
225 { 0x00, 0x1d, 0xa8 }, // 130
226 { 0x00, 0x2c, 0xb6 }, // 131
227 { 0x07, 0x3b, 0xc5 }, // 132
228 // { 0x16, 0x4a, 0xd4 }, // 133
229 { 0x34, 0x68, 0xee },
230 { 0x25, 0x59, 0xe3 }, // 134
231 { 0x34, 0x68, 0xee }, // 135
232 { 0x43, 0x77, 0xee }, // 136
233 { 0x52, 0x86, 0xed }, // 137
234 { 0x61, 0x95, 0xee }, // 138
235 { 0x70, 0xa4, 0xee }, // 139
236 { 0x7e, 0xb3, 0xee }, // 140
237 { 0x8d, 0xc2, 0xee }, // 141
238 { 0x9c, 0xd1, 0xee }, // 142
239 { 0xab, 0xe0, 0xee }, // 143
240 { 0x00, 0x0d, 0x56 }, // 144
241 { 0x00, 0x1c, 0x65 }, // 145
242 { 0x00, 0x2a, 0x74 }, // 146
243 { 0x00, 0x39, 0x83 }, // 147
244 { 0x00, 0x48, 0x92 }, // 148
245 { 0x0f, 0x57, 0xa1 }, // 149
246 { 0x1e, 0x66, 0xb0 }, // 150
247 { 0x2d, 0x75, 0xbf }, // 151
248 { 0x3c, 0x84, 0xce }, // 152
249 { 0x4b, 0x93, 0xdd }, // 153
250 { 0x5a, 0xa2, 0xec }, // 154
251 { 0x69, 0xb1, 0xee }, // 155
252 { 0x78, 0xc0, 0xee }, // 156
253 { 0x87, 0xcf, 0xee }, // 157
254 { 0x96, 0xde, 0xee }, // 158
255 { 0xa5, 0xed, 0xee }, // 159
256 { 0x00, 0x18, 0x12 }, // 160
257 { 0x00, 0x27, 0x21 }, // 161
258 { 0x00, 0x36, 0x30 }, // 162
259 { 0x00, 0x45, 0x3f }, // 163
260 { 0x05, 0x54, 0x4e }, // 164
261 { 0x14, 0x62, 0x5d }, // 165
262 { 0x23, 0x71, 0x6c }, // 166
263 { 0x32, 0x80, 0x7b }, // 167
264 { 0x41, 0x8f, 0x8a }, // 168
265 { 0x50, 0x9e, 0x99 }, // 169
266 { 0x5f, 0xad, 0xa8 }, // 170
267 { 0x6e, 0xbc, 0xb6 }, // 171
268 { 0x7d, 0xcb, 0xc5 }, // 172
269 { 0x8c, 0xda, 0xd4 }, // 173
270 { 0x9a, 0xe9, 0xe3 }, // 174
271 { 0xa9, 0xee, 0xee }, // 175
272 { 0x00, 0x1c, 0x00 }, // 176
273 { 0x00, 0x2b, 0x00 }, // 177
274 { 0x00, 0x3a, 0x00 }, // 178
275 { 0x05, 0x49, 0x00 }, // 179
276 { 0x14, 0x58, 0x07 }, // 180
277 { 0x23, 0x67, 0x16 }, // 181
278 { 0x32, 0x76, 0x25 }, // 182
279 { 0x41, 0x85, 0x34 }, // 183
280 { 0x50, 0x94, 0x43 }, // 184
281 { 0x5f, 0xa3, 0x52 }, // 185
282 { 0x6e, 0xb2, 0x61 }, // 186
283 { 0x7d, 0xc1, 0x70 }, // 187
284 { 0x8c, 0xd0, 0x7e }, // 188
285 { 0x9a, 0xdf, 0x8d }, // 189
286 { 0xa9, 0xee, 0x9c }, // 190
287 { 0xb8, 0xee, 0xab }, // 191
288 { 0x00, 0x1c, 0x00 }, // 192
289 { 0x00, 0x2b, 0x00 }, // 193
290 { 0x0e, 0x3a, 0x00 }, // 194
291 { 0x1c, 0x49, 0x00 }, // 195
292 { 0x2b, 0x58, 0x00 }, // 196
293 { 0x3a, 0x67, 0x00 }, // 197
294 // { 0x49, 0x76, 0x00 }, // 198
295 { 0x2b, 0x58, 0x00 },
296 // { 0x58, 0x85, 0x00 }, // 199
297 { 0x3a, 0x67, 0x00 }, // 197
298 { 0x67, 0x94, 0x05 }, // 200
299 { 0x76, 0xa3, 0x14 }, // 201
300 { 0x85, 0xb2, 0x23 }, // 202
301 { 0x94, 0xc1, 0x32 }, // 203
302 { 0xa3, 0xd0, 0x41 }, // 204
303 { 0xb2, 0xdf, 0x50 }, // 205
304 { 0xc1, 0xee, 0x5f }, // 206
305 { 0xd0, 0xee, 0x6e }, // 207
306 { 0x0a, 0x16, 0x00 }, // 208
307 { 0x19, 0x25, 0x00 }, // 209
308 { 0x28, 0x34, 0x00 }, // 210
309 { 0x37, 0x43, 0x00 }, // 211
310 { 0x46, 0x52, 0x00 }, // 212
311 { 0x54, 0x61, 0x00 }, // 213
312 { 0x63, 0x70, 0x00 }, // 214
313 { 0x72, 0x7e, 0x00 }, // 215
314 // { 0x81, 0x8d, 0x00 }, // 216
315 { 0x63, 0x70, 0x00 },
316 { 0x90, 0x9c, 0x00 }, // 217
317 { 0x9f, 0xab, 0x00 }, // 218
318 { 0xae, 0xba, 0x0b }, // 219
319 { 0xbd, 0xc9, 0x1a }, // 220
320 { 0xcc, 0xd8, 0x29 }, // 221
321 { 0xdb, 0xe7, 0x38 }, // 222
322 { 0xea, 0xee, 0x46 }, // 223
323 { 0x22, 0x0c, 0x00 }, // 224
324 { 0x31, 0x1b, 0x00 }, // 225
325 { 0x40, 0x2a, 0x00 }, // 226
326 { 0x4f, 0x38, 0x00 }, // 227
327 // { 0x5e, 0x47, 0x00 }, // 228
328 { 0x94, 0x4c, 0x02 },
329 { 0x6d, 0x56, 0x00 }, // 229
330 { 0x7c, 0x65, 0x00 }, // 230
331 { 0x8b, 0x74, 0x00 }, // 231
332 { 0x9a, 0x83, 0x00 }, // 232
333 { 0xa8, 0x92, 0x00 }, // 233
334 { 0xb7, 0xa1, 0x00 }, // 234
335 { 0xc6, 0xb0, 0x02 }, // 235
336 { 0xd5, 0xbf, 0x11 }, // 236
337 { 0xe4, 0xce, 0x20 }, // 237
338 { 0xee, 0xdd, 0x2f }, // 238
339 { 0xee, 0xec, 0x3e }, // 239
340 { 0x34, 0x00, 0x00 }, // 240
341 { 0x43, 0x0e, 0x00 }, // 241
342 { 0x52, 0x1d, 0x00 }, // 242
343 { 0x61, 0x2c, 0x00 }, // 243
344 { 0x70, 0x3b, 0x00 }, // 244
345 { 0x7e, 0x4a, 0x00 }, // 245
346 { 0x8d, 0x59, 0x00 }, // 246
347 // { 0x9c, 0x68, 0x00 }, // 247
348 { 0xad, 0x5f, 0x64 },
349 // { 0xab, 0x77, 0x00 }, // 248
350 { 0x8d, 0x59, 0x00 },
351 { 0xba, 0x86, 0x00 }, // 249
352 { 0xc9, 0x95, 0x0b }, // 250
353 { 0xd8, 0xa4, 0x1a }, // 251
354 { 0xe7, 0xb3, 0x29 }, // 252
355 { 0xee, 0xc2, 0x38 }, // 253
356 { 0xee, 0xd1, 0x46 }, // 254
357 // { 0xee, 0xe0, 0x55 }, // 255
358 { 0xba, 0x86, 0x00 }
359 };
360
361 // clang-format on
362
363 static void TranslateAtariColorRGB(int index, uint8_t value)
364 {
365 SetColor(index, &colors[value]);
366 }
367
368 /*
369 The values below are determined by looking at the games
370 running in the VICE C64 emulator.
371 I have no idea how the original interpreter calculates
372 them. I might have made some mistakes. */
373
374 static void TranslateC64Color(int index, uint8_t value)
375 {
376 debug_print("Color %d: %d\n", index, value);
377 switch (value) {
378 case 2:
379 case 3:
380 case 4:
381 case 8:
382 case 9:
383 case 10:
384 case 12:
385 case 14:
386 case 15:
387 case 255:
388 case 137:
389 case 142:
390 SetColor(index, &white);
391 break;
392 case 35:
393 case 36:
394 case 38:
395 case 40:
396 case 244:
397 case 246:
398 case 248:
399 SetColor(index, &brown);
400 break;
401 case 24:
402 case 26:
403 case 30:
404 case 46:
405 case 16:
406 case 230:
407 case 237:
408 case 238:
409 case 252:
410 SetColor(index, &yellow);
411 break;
412 case 50:
413 case 51:
414 case 52:
415 case 53:
416 case 54:
417 case 56:
418 case 58:
419 case 59:
420 case 60:
421 case 66:
422 case 62:
423 SetColor(index, &orange);
424 break;
425 case 67:
426 case 68:
427 case 69:
428 case 70:
429 case 71:
430 SetColor(index, &red);
431 break;
432 case 0:
433 case 77:
434 case 81:
435 case 84:
436 case 85:
437 case 86:
438 case 87:
439 case 97:
440 case 101:
441 case 102:
442 case 103:
443 case 105:
444 case 224:
445 SetColor(index, &purple);
446 break;
447 case 89:
448 SetColor(index, &lred);
449 break;
450 case 1:
451 case 7:
452 case 116:
453 case 135:
454 case 148:
455 case 151:
456 SetColor(index, &blue);
457 break;
458 case 110:
459 case 157:
460 SetColor(index, &lblue);
461 break;
462 case 161:
463 SetColor(index, &grey);
464 break;
465 case 17:
466 case 20:
467 case 179:
468 case 182:
469 case 183:
470 case 194:
471 case 195:
472 case 196:
473 case 197:
474 case 198:
475 case 199:
476 case 200:
477 case 212:
478 case 214:
479 case 215:
480 case 216:
481 SetColor(index, &green);
482 break;
483 case 201:
484 SetColor(index, &lgreen);
485 break;
486 default:
487 fprintf(stderr, "Unknown color %d ", value);
488 break;
489 }
490 }
491
492 int DrawAtariC64Image(USImage *image)
493 {
494 uint8_t *ptr = image->imagedata;
495 size_t datasize = image->datasize;
496
497 int work, work2;
498 int c;
499 int i;
500 int countflag = (CurrentGame == COUNT_US || CurrentGame == VOODOO_CASTLE_US);
501
502 uint8_t *origptr = ptr;
503
504 x = 0;
505 y = 0;
506
507 ptr += 2;
508
509 work = *ptr++;
510 size = work + *ptr++ * 256;
511 // Get the offset
512 xoff = *ptr++ - 3;
513 yoff = *ptr++;
514 x = xoff * 8;
515 y = yoff;
516
517 // Get the x length
518 xlen = *ptr++;
519
520 ylen = *ptr++;
521 if (countflag)
522 ylen -= 2;
523
524 glui32 curheight, curwidth;
525 glk_window_get_size(Graphics, &curwidth, &curheight);
526
527 if (image->usage == IMG_ROOM || (xlen == 38 && xoff < 1)) {
528 xlen = xlen - 1 - image->cropright / 8;
529 left_margin = image->cropleft;
530
531 ImageWidth = xlen * 8 - 17 + left_margin;
532 ImageHeight = ylen + 2;
533
534 if (image->index == 19 && image->systype == SYS_ATARI8) {
535 xlen++;
536 ImageWidth = 308;
537 }
538
539 int width_in_pixels = ImageWidth * pixel_size;
540 while (width_in_pixels > curwidth && pixel_size > 1) {
541 pixel_size--;
542 width_in_pixels = ImageWidth * pixel_size;
543 }
544
545 x_offset = (curwidth - width_in_pixels) / 2;
546
547 int optimal_height = ImageHeight * pixel_size;
548
549 if (curheight != optimal_height) {
550 winid_t parent = glk_window_get_parent(Graphics);
551 if (parent)
552 glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed,
553 optimal_height, NULL);
554 }
555 }
556
557 SetColor(0, &black);
558
559 // Get the palette
560 for (i = 1; i < 5; i++) {
561 work = *ptr++;
562 if (image->systype == SYS_C64)
563 TranslateC64Color(i, work);
564 else
565 TranslateAtariColorRGB(i, work);
566 }
567
568 while (ptr - origptr < datasize - 2) {
569 // First get count
570 c = *ptr++;
571
572 if (((c & 0x80) == 0x80) || countflag) { // is a counter
573 if (countflag) {
574 c -= 1;
575 } else {
576 c &= 0x7f;
577 }
578 work = *ptr++;
579 work2 = *ptr++;
580 for (i = 0; i < c + 1; i++) {
581 DrawA8C64Pixels(work, work2);
582 }
583 } else {
584 // Don't count on the next c characters
585 for (i = 0; i < c + 1 && ptr - origptr < datasize - 1; i++) {
586 work = *ptr++;
587 work2 = *ptr++;
588 DrawA8C64Pixels(work, work2);
589 }
590 }
591 }
592 return 1;
593 }