Debian Codegraph @

terps/plus/stdraw.c

raw symbols SCIP
1 //
2 // stdraw.c
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 // Routines to draw Atari ST graphics
5 //
6 // Created by Petter Sjölund on 2022-01-30.
7 //
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "animations.h"
14 #include "common.h"
15 #include "definitions.h"
16 #include "glk.h"
17 #include "graphics.h"
18
19 typedef struct {
20 uint16_t x;
21 uint16_t y;
22 uint16_t width;
23 } Pixel;
24
25 typedef struct {
26 uint8_t ColIdx;
27 uint8_t CurCol;
28 uint8_t NumCol;
29 glui32 *Colors;
30 int StartOffset;
31 int Rate;
32 int NumPix;
33 Pixel *Pixels;
34 } AnimationColor;
35
36 static int IsSTBitSet(int bit, uint8_t byte)
37 {
38 return ((byte & (1 << bit)) != 0);
39 }
40
41 static uint32_t NumAnimCols, NumOldAnimCols = 0, ImgAddrOffs, NibblesWide, NxtLineStart, InitialMask, LastMask, CurAddr, LineNibblesLeft;
42
43 static uint32_t ColorCycle = 0;
44
45 static glui32 StColToGlk(uint8_t hi, uint8_t lo)
46 {
47 int blue = lo & 0xf;
48 int green = (lo >> 4) & 0xf;
49 int red = hi & 0xf;
50
51 red = red * 35.7;
52 green = green * 35.7;
53 blue = blue * 35.7;
54
55 return (red << 16) | (green << 8) | blue;
56 }
57
58 AnimationColor *AnimColors = NULL;
59
60 static void FreeAnimCols(void)
61 {
62 if (AnimColors == NULL)
63 return;
64 for (int i = 0; i < NumOldAnimCols; i++) {
65 AnimationColor col = AnimColors[i];
66 if (col.Colors != NULL)
67 free(col.Colors);
68 col.Colors = NULL;
69 if (col.Pixels != NULL)
70 free(col.Pixels);
71 col.Pixels = NULL;
72 }
73 if (AnimColors != NULL)
74 free(AnimColors);
75 AnimColors = NULL;
76 NumAnimCols = 0;
77 }
78
79 static uint8_t *SetPaletteAnimation(uint8_t *ptr)
80 {
81 if (NumAnimCols != 0) {
82 if (AnimColors != NULL) {
83 FreeAnimCols();
84 }
85 AnimColors = MemAlloc(sizeof(AnimationColor) * (NumAnimCols + NumOldAnimCols));
86 for (int i = NumAnimCols; i < NumAnimCols + NumOldAnimCols; i++) {
87 AnimColors[i].Colors = NULL;
88 AnimColors[i].Pixels = NULL;
89 }
90 AnimationColor *col = AnimColors;
91 for (int i = 0; i < NumAnimCols; i++, col++) {
92 uint8_t val = *ptr++;
93 col->ColIdx = 1 + (val >> 4);
94 col->CurCol = 0;
95 col->NumPix = 0;
96 col->Pixels = NULL;
97 col->StartOffset = *ptr++;
98 uint8_t ratehi = *ptr++;
99 col->Rate = *ptr++ + ratehi * 256;
100 col->Rate = 1 + col->Rate;
101 val = val & 0xf;
102 col->NumCol = val;
103 col->Colors = MemAlloc(col->NumCol * sizeof(glui32));
104 for (int j = 0; j < val; j++) {
105 uint8_t hi = *ptr++;
106 uint8_t lo = *ptr++;
107 col->Colors[j] = StColToGlk(hi, lo);
108 };
109 }
110 }
111 return ptr;
112 }
113
114 void PutDoublePixel(glsi32 x, glsi32 y, int32_t color);
115
116 uint8_t *PatternLookup = NULL;
117
118 static void GeneratePatternLookup(void)
119 {
120 uint32_t pattern;
121 uint16_t previous;
122
123 pattern = 0xffff0000;
124 PatternLookup = MemAlloc(1024);
125 uint8_t *ptr = PatternLookup;
126 do {
127 for (int i = 0; i < 4; i++) {
128 ptr[i] = 0;
129 if ((pattern & 1 << i) != 0) {
130 ptr[i] = ptr[i] | 0x33;
131 }
132 if ((pattern & 1 << (i + 4)) != 0) {
133 ptr[i] = ptr[i] | 0xcc;
134 }
135 }
136 ptr = ptr + 4;
137 previous = pattern + 1;
138 pattern = (pattern & 0xffff0000) | previous;
139 } while (previous != 256);
140 }
141
142 static void DrawSTNibble(uint8_t byte, uint8_t mask, int xpos, int ypos, Pixel **pixels)
143 {
144 uint16_t offs = byte << 2;
145 int startbit = 7;
146 int endbit = 4;
147 if (mask == 0xf) {
148 startbit = 3;
149 endbit = 0;
150 }
151
152 for (int i = startbit; i >= endbit; i -= 2) {
153 uint8_t col = 0;
154 for (int j = 0; j < 4; j++) {
155 if (IsSTBitSet(i, PatternLookup[offs + j]))
156 col |= 1 << (3 - j);
157 }
158
159 if (col) {
160 for (int c = 0; c < NumAnimCols; c++) {
161 if (col == AnimColors[c].ColIdx && AnimColors[c].NumPix < 3000) {
162 if (AnimColors[c].NumPix && pixels[c][AnimColors[c].NumPix - 1].y == ypos && pixels[c][AnimColors[c].NumPix - 1].x == xpos + 5 - i) {
163 pixels[c][AnimColors[c].NumPix - 1].width += 2;
164 break;
165 }
166
167 pixels[c][AnimColors[c].NumPix].x = xpos + 7 - i;
168 pixels[c][AnimColors[c].NumPix].y = ypos;
169 pixels[c][AnimColors[c].NumPix].width = 2;
170
171 AnimColors[c].NumPix++;
172
173 break;
174 }
175 }
176 PutDoublePixel(xpos + 7 - i, ypos, col);
177 }
178 }
179 }
180
181 static void DrawPattern(uint8_t pattern, Pixel **pixels)
182 {
183 // mask and LastMask alternate between 0x0f and 0xf0
184 uint8_t mask = LastMask ^ 0xff;
185
186 // CurAddr: screen memory where we're currently writing
187 int y1 = CurAddr / 160;
188 int x1 = 2 * (CurAddr % 160) + 6 * (CurAddr & 1);
189
190 DrawSTNibble(pattern, mask, x1, y1, pixels);
191
192 mask = mask ^ 0xff;
193 LastMask = LastMask ^ 0xff;
194 if (mask != 0xf) {
195 CurAddr += 1 + 6 * (CurAddr & 1);
196 }
197
198 // LineBytesLeft = bytes remaining to draw this line
199
200 LineNibblesLeft--;
201 if (LineNibblesLeft != 0)
202 return;
203
204 // Reached end of line
205 // NxtLine: start address of next line
206 NxtLineStart += 160; // move down one line
207 CurAddr = NxtLineStart;
208 // InitialMask: mask at start of line
209 mask = InitialMask;
210 LastMask = mask ^ 0xff;
211 LineNibblesLeft = NibblesWide;
212 }
213
214 void SetRGB(int32_t index, int red, int green, int blue);
215
216 int ColorCyclingRunning = 0;
217
218 int Intersects(Pixel pix, int xpos, int ypos, int width, int height)
219 {
220 return MAX(pix.x, xpos) <= MIN(pix.x + pix.width, xpos + width)
221 && MAX(pix.y, ypos) <= MIN(pix.y, ypos + height);
222 }
223
224 void CopyPixel(Pixel *a, Pixel *b)
225 {
226 a->width = b->width;
227 a->x = b->x;
228 a->y = b->y;
229 }
230
231 void CopyAnimCol(AnimationColor *a, AnimationColor *b)
232 {
233 a->ColIdx = b->ColIdx;
234 a->CurCol = b->CurCol;
235 a->NumCol = b->NumCol;
236 int datasize = b->NumCol * sizeof(glui32);
237 a->Colors = MemAlloc(datasize);
238 memcpy(a->Colors, b->Colors, datasize);
239 a->Rate = b->Rate;
240 a->NumPix = b->NumPix;
241 datasize = b->NumPix * sizeof(Pixel);
242 a->Pixels = MemAlloc(datasize);
243 memcpy(a->Pixels, b->Pixels, datasize);
244 }
245
246 void AddPixels(AnimationColor *animcol, Pixel *pixels, int numpixels)
247 {
248 int pixidx = 0;
249 Pixel newpix[4000];
250 for (int i = 0; i < animcol->NumPix; i++) {
251 CopyPixel(&newpix[pixidx++], &(animcol->Pixels[i]));
252 }
253
254 for (int i = 0; i < numpixels; i++) {
255 CopyPixel(&newpix[pixidx++], &pixels[i]);
256 }
257 int datasize = sizeof(Pixel) * pixidx;
258 free(animcol->Pixels);
259 animcol->Pixels = MemAlloc(datasize);
260 memcpy(animcol->Pixels, newpix, datasize);
261 animcol->NumPix = pixidx;
262 }
263
264 int AddNonHiddenColAnim(AnimationColor *old, AnimationColor *new, int numnew, int xpos, int ypos, int width, int height)
265 {
266 int goodpixels = 0;
267 Pixel pixels[3000];
268
269 for (int i = 0; i < old->NumPix; i++) {
270 if (!Intersects(old->Pixels[i], xpos, ypos, width, height)) {
271 CopyPixel(&pixels[goodpixels++], &(old->Pixels[i]));
272 }
273 }
274
275 if (goodpixels == 0) {
276 return 0;
277 }
278
279 for (int i = 0; i < numnew; i++) {
280 if (new[i].ColIdx == old->ColIdx &&new[i].NumPix) {
281 AddPixels(&new[i], pixels, goodpixels);
282 return 0;
283 }
284 }
285
286 CopyAnimCol(&new[numnew], old);
287 return 1;
288 }
289
290 int DrawSTImageFromData(uint8_t *imgdata, size_t total_size)
291 {
292
293 uint8_t *ptr = &imgdata[4];
294
295 ImgAddrOffs = *ptr++ * 256;
296 ImgAddrOffs += *ptr++;
297 ImgAddrOffs *= 2;
298
299 yoff = ImgAddrOffs / 160;
300 xoff = 2 * (ImgAddrOffs % 160) + 6 * (ImgAddrOffs % 2);
301
302 NibblesWide = *ptr++;
303
304 /* Make sure to clear the last image and any color cycling for all Claymorgue room images */
305 /* The small ones have to be special-cased */
306 if (NibblesWide > 70 || (CurrentGame == CLAYMORGUE && LastImgType == IMG_ROOM && (LastImgIndex == 0 || LastImgIndex == 5 || LastImgIndex == 11 || LastImgIndex == 16 || LastImgIndex == 18 || LastImgIndex == 31))) {
307 glk_window_clear(Graphics);
308 FreeAnimCols();
309 }
310
311 /* Draw a white background behind some Spider-Man images */
312 if (CurrentGame == SPIDERMAN && LastImgType == IMG_SPECIAL && (LastImgIndex == 6 || LastImgIndex == 7 || LastImgIndex == 8 || LastImgIndex == 12 || LastImgIndex == 18)) {
313 glui32 winwidth, winheight;
314 glk_window_get_size(Graphics, &winwidth, &winheight);
315 int width_in_pixels = NibblesWide * 4 * pixel_size;
316 glk_window_fill_rect(Graphics, 0xf9f8f9, (winwidth - width_in_pixels) / 2, 0, width_in_pixels, winheight);
317 }
318
319 NumOldAnimCols = NumAnimCols;
320 NumAnimCols = *ptr++;
321
322 for (int i = 0; i <= 15; i++) {
323 int blue = *(ptr + 1) & 0xf;
324 int green = (*(ptr + 1) >> 4) & 0xf;
325 int red = *ptr & 0xf;
326
327 SetRGB(i, red, green, blue);
328
329 ptr += 2;
330 }
331
332 AnimationColor *oldColAnim = NULL;
333
334 if (CurrentGame != CLAYMORGUE && IsSet(DARKBIT)) {
335 FreeAnimCols();
336 NumOldAnimCols = 0;
337 }
338
339 if (AnimColors != NULL) {
340 oldColAnim = AnimColors;
341 AnimColors = NULL;
342 }
343
344 ptr = SetPaletteAnimation(ptr);
345
346 if (CurrentGame != CLAYMORGUE && IsSet(DARKBIT)) {
347 FreeAnimCols();
348 }
349
350 Pixel **Pixels = MemAlloc(sizeof(Pixel *) * NumAnimCols);
351 for (int i = 0; i < NumAnimCols; i++)
352 Pixels[i] = MemAlloc(sizeof(Pixel) * 3000);
353
354 uint16_t imgdatasize = imgdata[2] * 256 + imgdata[3];
355 uint8_t *endOfData = ptr + imgdatasize;
356 if (endOfData > imgdata + total_size)
357 endOfData = imgdata + total_size;
358
359 if (PatternLookup == NULL)
360 GeneratePatternLookup();
361
362 CurAddr = ImgAddrOffs;
363
364 // CurAddr == screen memory where we're currently writing
365
366 InitialMask = 0xf0;
367 LastMask = 0x0f;
368 NxtLineStart = CurAddr;
369 LineNibblesLeft = NibblesWide;
370
371 uint8_t c;
372
373 do {
374 c = *ptr++;
375 if ((c & 0x80) == 0) {
376 for (int i = c; i >= 0; i--) {
377 c = *ptr++;
378 DrawPattern(c, Pixels);
379 }
380 } else {
381 // Repeat next byte (c & 0x7f) times
382 c = c & 0x7f;
383 uint8_t val = *ptr++;
384 for (int i = c; i >= 0; i--) {
385 DrawPattern(val, Pixels);
386 }
387 }
388 } while (ptr <= endOfData);
389
390 int ImgHeight = NxtLineStart / 160 - yoff;
391
392 for (int i = 0; i < NumAnimCols; i++) {
393 if (AnimColors[i].NumPix) {
394 AnimColors[i].Pixels = MemAlloc(AnimColors[i].NumPix * sizeof(Pixel));
395 memcpy(AnimColors[i].Pixels, Pixels[i], AnimColors[i].NumPix * sizeof(Pixel));
396 free(Pixels[i]);
397 }
398 }
399 free(Pixels);
400
401 if (AnimColors == NULL && NumOldAnimCols) {
402 AnimColors = MemAlloc(sizeof(AnimationColor) * NumOldAnimCols);
403 for (int i = 0; i < NumOldAnimCols; i++) {
404 AnimColors[i].Colors = NULL;
405 AnimColors[i].Pixels = NULL;
406 }
407 }
408
409 for (int i = 0; i < NumOldAnimCols; i++) {
410 NumAnimCols += AddNonHiddenColAnim(&oldColAnim[i], AnimColors, NumAnimCols, xoff, yoff, NibblesWide * 4, ImgHeight);
411 }
412
413 if (NumAnimCols) {
414 ColorCyclingRunning = 1;
415 ColorCycle = 0;
416 SetTimer(25);
417 } else {
418 ColorCyclingRunning = 0;
419 if (!AnimationRunning)
420 SetTimer(0);
421 }
422
423 return 1;
424 }
425
426 void ColorCyclingPixel(Pixel p, glui32 glk_color)
427 {
428 glsi32 xpos = p.x * pixel_size;
429
430 if (upside_down)
431 xpos = ImageWidth * pixel_size - xpos - (pixel_size * (p.width - 2));
432 xpos += x_offset;
433
434 if (xpos < x_offset || xpos >= right_margin) {
435 return;
436 }
437
438 int ypos = p.y * pixel_size;
439 if (upside_down)
440 ypos = ImageHeight * pixel_size - ypos - 3 * pixel_size;
441 ypos += y_offset;
442
443 glk_window_fill_rect(Graphics, glk_color, xpos,
444 ypos, pixel_size * p.width, pixel_size);
445 }
446
447 extern int STWebAnimation;
448 extern int STWebAnimationFinished;
449
450 void UpdateColorCycling(void)
451 {
452 for (int i = 0; i < NumAnimCols; i++) {
453 if ((ColorCycle + AnimColors[i].StartOffset) % AnimColors[i].Rate == 0) {
454 glui32 color = AnimColors[i].Colors[AnimColors[i].CurCol];
455
456 AnimColors[i].CurCol++;
457 if (AnimColors[i].CurCol >= AnimColors[i].NumCol) {
458 AnimColors[i].CurCol = 0;
459 if (CurrentGame == SPIDERMAN && STWebAnimation) {
460 STWebAnimationFinished = 1;
461 }
462 }
463
464 for (int j = 0; j < AnimColors[i].NumPix; j++) {
465 ColorCyclingPixel(AnimColors[i].Pixels[j], color);
466 }
467 }
468 }
469 ColorCycle++;
470 }