Debian Codegraph @

terps/plus/graphics.c

raw symbols SCIP
1 //
2 // graphics.c
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 //
5 // Created by Petter Sjölund on 2022-06-04.
6 //
7
8 #include <string.h>
9
10 #include "glk.h"
11 #ifdef SPATTERLIGHT
12 #include "glkimp.h"
13 #endif
14
15 #include "animations.h"
16 #include "common.h"
17 #include "graphics.h"
18 #include "loaddatabase.h"
19
20 ImgType LastImgType;
21 int LastImgIndex;
22
23 int upside_down = 0;
24
25 int x = 0, y = 0, at_last_line = 0;
26
27 int xlen = 280, ylen = 158;
28 int xoff = 0, yoff = 0;
29
30 int pixel_size;
31 int x_offset, y_offset, right_margin;
32
33 PALETTE pal;
34
35 struct imgrec *Images;
36
37 int DrawDOSImageFromData(uint8_t *ptr, size_t datasize);
38 int DrawAtariC64ImageFromData(uint8_t *ptr, size_t datasize);
39 int DrawSTImageFromData(uint8_t *ptr, size_t datasize);
40 int DrawApple2ImageFromData(uint8_t *ptr, size_t datasize);
41 void DrawApple2ImageFromVideoMem(void);
42 void ClearApple2ScreenMem(void);
43
44 void DrawBlack(void)
45 {
46 glk_window_fill_rect(Graphics, 0, x_offset, y_offset, (glui32)(ImageWidth * pixel_size),
47 (glui32)(ImageHeight * pixel_size));
48 LastImgType = NO_IMG;
49 }
50
51 char *ShortNameFromType(char type, int index)
52 {
53 char buf[5];
54 int n = snprintf(buf, sizeof buf, "%c0%02d", type, index);
55 if (n < 0)
56 return NULL;
57 size_t len = strlen(buf) + 1;
58 char *result = MemAlloc(len);
59 memcpy(result, buf, len);
60 return result;
61 }
62
63 void PutPixel(glsi32 xpos, glsi32 ypos, int32_t color)
64 {
65 glui32 glk_color = ((pal[color][0] << 16)) | ((pal[color][1] << 8)) | (pal[color][2]);
66
67 xpos = xpos * pixel_size;
68
69 if (upside_down)
70 xpos = ImageWidth * pixel_size - xpos;
71
72 xpos += x_offset;
73
74 if (xpos < x_offset || xpos >= right_margin) {
75 return;
76 }
77
78 ypos = ypos * pixel_size;
79 if (upside_down)
80 ypos = (ImageHeight - 1) * pixel_size - ypos;
81 ypos += y_offset;
82
83 glk_window_fill_rect(Graphics, glk_color, xpos,
84 ypos, pixel_size, pixel_size);
85 }
86
87 void PutDoublePixel(glsi32 xpos, glsi32 ypos, int32_t color)
88 {
89 glui32 glk_color = ((pal[color][0] << 16)) | ((pal[color][1] << 8)) | (pal[color][2]);
90
91 xpos = xpos * pixel_size;
92
93 if (upside_down)
94 xpos = ImageWidth * pixel_size - xpos;
95 xpos += x_offset;
96
97 if (xpos < x_offset || xpos >= right_margin) {
98 return;
99 }
100
101 ypos = ypos * pixel_size;
102 if (upside_down) {
103 ypos = ImageHeight * pixel_size - ypos;
104 if (CurrentSys == SYS_ST)
105 ypos -= 3 * pixel_size;
106 else if (CurrentSys == SYS_ATARI8 || CurrentSys == SYS_MSDOS)
107 ypos -= pixel_size;
108 }
109 ypos += y_offset;
110
111 glk_window_fill_rect(Graphics, glk_color, xpos,
112 ypos, pixel_size * 2, pixel_size);
113 }
114
115 void SetColor(int32_t index, const RGB *color)
116 {
117 pal[index][0] = (*color)[0];
118 pal[index][1] = (*color)[1];
119 pal[index][2] = (*color)[2];
120 }
121
122 void SetRGB(int32_t index, int red, int green, int blue)
123 {
124 red = red * 35.7;
125 green = green * 35.7;
126 blue = blue * 35.7;
127
128 pal[index][0] = red;
129 pal[index][1] = green;
130 pal[index][2] = blue;
131 }
132
133 int DrawImageWithName(char *filename)
134 {
135 debug_print("DrawImageWithName %s\n", filename);
136
137 int i;
138 for (i = 0; Images[i].Filename != NULL; i++)
139 if (strcmp(filename, Images[i].Filename) == 0)
140 break;
141
142 if (Images[i].Filename == NULL) {
143 if (CurrentSys == SYS_MSDOS) {
144 if (FindAndAddImageFile(filename, &Images[i])) {
145 Images[i + 1].Filename = NULL;
146 } else {
147 return 0;
148 }
149 } else {
150 return 0;
151 }
152 }
153
154 if (!IsSet(GRAPHICSBIT))
155 return 0;
156
157 switch (Images[i].Filename[0]) {
158 case 'B':
159 LastImgType = IMG_OBJECT;
160 break;
161 case 'R':
162 LastImgType = IMG_ROOM;
163 break;
164 case 'S':
165 LastImgType = IMG_SPECIAL;
166 break;
167 default:
168 debug_print("DrawImageWithName: Unknown image type!\n");
169 }
170
171 LastImgIndex = Images[i].Filename[3] - '0' + 10 * (Images[i].Filename[2] - '0');
172
173 if (CurrentSys == SYS_C64 || CurrentSys == SYS_ATARI8) {
174 return DrawAtariC64ImageFromData(Images[i].Data, Images[i].Size);
175 } else if (CurrentSys == SYS_ST) {
176 return DrawSTImageFromData(Images[i].Data, Images[i].Size);
177 } else if (CurrentSys == SYS_APPLE2) {
178 return DrawApple2ImageFromData(Images[i].Data, Images[i].Size);
179 } else
180 return DrawDOSImageFromData(Images[i].Data, Images[i].Size);
181 }
182
183 void DrawItemImage(int item)
184 {
185 LastImgType = IMG_OBJECT;
186 LastImgIndex = item;
187 char buf[5];
188
189 int n = snprintf(buf, sizeof buf, "B0%02d", item);
190 if (n < 0)
191 return;
192
193 upside_down = (CurrentGame == SPIDERMAN && Items[0].Location == MyLoc);
194
195 DrawImageWithName(buf);
196 }
197
198 int DrawCloseup(int img)
199 {
200 LastImgType = IMG_SPECIAL;
201 LastImgIndex = img;
202 char buf[5];
203
204 int n = snprintf(buf, sizeof buf, "S0%02d", img);
205 if (n < 0)
206 return 0;
207
208 upside_down = 0;
209
210 if (CurrentSys == SYS_APPLE2)
211 ClearApple2ScreenMem();
212
213 int result = DrawImageWithName(buf);
214
215 if (result && CurrentSys == SYS_APPLE2) {
216 glk_window_clear(Graphics);
217 DrawApple2ImageFromVideoMem();
218 }
219
220 return result;
221 }
222
223 void ClearApple2ScreenMem(void);
224
225 int DrawRoomImage(int roomimg)
226 {
227 LastImgType = IMG_ROOM;
228 LastImgIndex = roomimg;
229
230 char buf[5];
231 int n = snprintf(buf, sizeof buf, "R0%02d", roomimg);
232 if (n < 0)
233 return 0;
234 buf[4] = 0;
235
236 if (Graphics)
237 glk_window_clear(Graphics);
238
239 if (CurrentSys == SYS_ST && CurrentGame != CLAYMORGUE)
240 DrawImageWithName("S999");
241 else if (CurrentSys == SYS_APPLE2)
242 ClearApple2ScreenMem();
243
244 upside_down = (CurrentGame == SPIDERMAN && Items[0].Location == MyLoc);
245
246 return DrawImageWithName(buf);
247 }
248
249 extern int AnimationRoom;
250
251 void DrawCurrentRoom(void)
252 {
253 OpenGraphicsWindow();
254
255 showing_inventory = (CurrentGame == CLAYMORGUE && MyLoc == 33);
256
257 if (!IsSet(GRAPHICSBIT)) {
258 CloseGraphicsWindow();
259 return;
260 }
261
262 int dark = IsSet(DARKBIT);
263 for (int i = 0; i <= GameHeader.NumItems; i++) {
264 if (Items[i].Flag & 2) {
265 if (Items[i].Location == MyLoc || Items[i].Location == CARRIED)
266 dark = 0;
267 }
268 }
269
270 if (Rooms[MyLoc].Image == 255 || Images[0].Filename == NULL) {
271 CloseGraphicsWindow();
272 return;
273 }
274
275 if (dark && Graphics != NULL) {
276 if (CurrentGame != CLAYMORGUE) {
277 if (CurrentSys == SYS_ST && !AnimationRunning)
278 DrawImageWithName("S999");
279 else
280 DrawBlack();
281 } else {
282 DrawImageWithName("R000");
283 }
284 return;
285 }
286
287 if (Graphics == NULL || AnimationRoom == MyLoc)
288 return;
289
290 if (!DrawRoomImage(Rooms[MyLoc].Image)) {
291 CloseGraphicsWindow();
292 return;
293 }
294
295 for (int ct = 0; ct <= GameHeader.NumObjImg; ct++)
296 if (ObjectImages[ct].Room == MyLoc && Items[ObjectImages[ct].Object].Location == MyLoc) {
297 DrawItemImage(ObjectImages[ct].Image);
298 }
299
300 if (CurrentSys == SYS_APPLE2) {
301 DrawApple2ImageFromVideoMem();
302 }
303 }