Debian Codegraph @

terps/scott/saga/pcdraw.c

raw symbols SCIP
1 //
2 // pcdraw.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Routines to draw MS-DOS bitmap graphics
6 // Based on code by David Lodge 29/04/2005
7 //
8 // Original code at https://github.com/tautology0/textadventuregraphics
9 //
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "sagagraphics.h"
16 #include "scott.h"
17
18 int x = 0, y = 0, at_last_line = 0;
19
20 int xlen = 280, ylen = 158;
21 int xoff = 0, yoff = 0;
22 int size;
23 int ycount = 0;
24 int skipy = 1;
25
26 extern char *DirPath;
27
28 uint8_t *FindImageFile(const char *shortname, size_t *datasize)
29 {
30 *datasize = 0;
31 uint8_t *data = NULL;
32 size_t pathlen = strlen(DirPath) + strlen(shortname) + 5;
33 char *filename = MemAlloc(pathlen);
34 int n = snprintf(filename, pathlen, "%s%s.PAK", DirPath, shortname);
35 if (n > 0) {
36 FILE *infile = fopen(filename, "rb");
37 if (infile) {
38 fseek(infile, 0, SEEK_END);
39 size_t length = ftell(infile);
40 if (length > 0) {
41 debug_print("Found and read image file %s\n", filename);
42 fseek(infile, 0, SEEK_SET);
43 data = MemAlloc(length);
44 *datasize = fread(data, 1, length, infile);
45 }
46 fclose(infile);
47 } else {
48 fprintf(stderr, "Could not find or read image file %s\n", filename);
49 }
50 }
51 free(filename);
52 return data;
53 }
54
55 static void DrawDOSPixels(int pattern)
56 {
57 int pix1, pix2, pix3, pix4;
58 // Now get colors
59 pix1 = (pattern & 0xc0) >> 6;
60 pix2 = (pattern & 0x30) >> 4;
61 pix3 = (pattern & 0x0c) >> 2;
62 pix4 = (pattern & 0x03);
63
64 if (!skipy) {
65 PutDoublePixel(x, y, pix1);
66 x += 2;
67 PutDoublePixel(x, y, pix2);
68 x += 2;
69 PutDoublePixel(x, y, pix3);
70 x += 2;
71 PutDoublePixel(x, y, pix4);
72 x += 2;
73 } else {
74 PutPixel(x, y, pix1);
75 x++;
76 PutPixel(x, y, pix2);
77 x++;
78 PutPixel(x, y, pix3);
79 x++;
80 PutPixel(x, y, pix4);
81 x++;
82 }
83
84 if (x >= xlen + xoff) {
85 y += 2;
86 x = xoff;
87 ycount++;
88 }
89 if (ycount > ylen) {
90 y = yoff + 1;
91 at_last_line++;
92 ycount = 0;
93 }
94 }
95
96 int DrawDOSImage(USImage *image)
97 {
98 if (image->imagedata == NULL) {
99 fprintf(stderr, "DrawDOSImage: ptr == NULL\n");
100 return 0;
101 }
102
103 debug_print("DrawDOSImage: usage:%d index:%d\n", image->usage, image->index);
104
105 x = 0;
106 y = 0;
107 at_last_line = 0;
108
109 xlen = 0;
110 ylen = 0;
111 xoff = 0;
112 yoff = 0;
113 ycount = 0;
114 skipy = 1;
115
116 int work;
117 int c;
118 int i;
119 int rawoffset;
120
121 // clang-format off
122
123 RGB black = { 0,0,0 };
124 RGB magenta = { 255,0,255 };
125 RGB cyan = { 0,255,255 };
126 RGB white = { 255,255,255 };
127
128 // clang-format on
129
130 /* set up the palette */
131 SetColor(0, &black);
132 SetColor(1, &cyan);
133 SetColor(2, &magenta);
134 SetColor(3, &white);
135
136 uint8_t *ptr = image->imagedata;
137 uint8_t *origptr = ptr;
138
139 // Get the size of the graphics chunck
140 ptr = origptr + 0x05;
141 work = *ptr++;
142 size = work + (*ptr * 256);
143
144 // Get whether it is lined
145 ptr = origptr + 0x0d;
146 work = *ptr++;
147 if (work == 0xff)
148 skipy = 0;
149
150 // Get the offset
151 ptr = origptr + 0x0f;
152 work = *ptr++;
153 rawoffset = work + (*ptr * 256);
154 xoff = ((rawoffset % 80) * 4) - 24;
155 yoff = rawoffset / 40;
156 yoff -= (yoff & 1);
157 x = xoff;
158 y = yoff;
159
160 // Get the y length
161 ptr = origptr + 0x11;
162 work = *ptr++;
163 ylen = work + (*ptr * 256);
164 ylen -= rawoffset;
165 ylen /= 80;
166
167 // Get the x length
168 ptr = origptr + 0x13;
169 xlen = *ptr * 4;
170
171 ptr = origptr + 0x17;
172 while (ptr - origptr < size) {
173 // First get count
174 c = *ptr++;
175
176 if ((c & 0x80) == 0x80) { // is a counter
177 work = *ptr++;
178 c &= 0x7f;
179 for (i = 0; i < c + 1; i++) {
180 DrawDOSPixels(work);
181 }
182 } else {
183 // Don't count on the next j characters
184 for (i = 0; i < c + 1; i++) {
185 work = *ptr++;
186 DrawDOSPixels(work);
187 }
188 }
189 if (at_last_line > 1)
190 break;
191 }
192 return 1;
193 }