Debian Codegraph @

terps/scott/ai_uk/line_drawing.c

raw symbols SCIP
1 // line_drawing.c
2 // part of ScottFree, an interpreter for adventures in Scott Adams format
3 //
4 // This file is based on code from the Level9 interpreter by Simon Baldwin
5
6 #include <stdlib.h>
7 #include <string.h>
8
9 #include "line_drawing.h"
10 #include "ringbuffer.h"
11 #include "sagadraw.h"
12 #include "sagagraphics.h"
13 #include "scott.h"
14
15 struct line_image *LineImages;
16
17 struct pixel_to_draw {
18 uint8_t x;
19 uint8_t y;
20 uint8_t colour;
21 };
22
23 VectorStateType VectorState = NO_VECTOR_IMAGE;
24
25 struct pixel_to_draw **pixels_to_draw = NULL;
26
27 int total_draw_instructions = 0;
28 int current_draw_instruction = 0;
29 int vector_image_shown = -1;
30
31 uint8_t *picture_bitmap = NULL;
32
33 int line_colour = 15;
34 int bg_colour = 0;
35
36 int scott_graphics_width = 255;
37 int scott_graphics_height = 94;
38
39 /*
40 * scott_linegraphics_plot_clip()
41 * scott_linegraphics_draw_line()
42 *
43 * Draw a line from x1,y1 to x2,y2 in colour line_colour.
44 * The function uses Bresenham's algorithm.
45 * The second function, scott_graphics_plot_clip, is a line drawing helper;
46 * it handles clipping.
47 */
48 static void
49 scott_linegraphics_plot_clip(int x, int y, int colour)
50 {
51 /*
52 * Clip the plot if the value is outside the context. Otherwise, plot the
53 * pixel as colour1 if it is currently colour2.
54 */
55 if (x >= 0 && x <= scott_graphics_width && y >= 0 && y < scott_graphics_height) {
56 picture_bitmap[y * 255 + x] = colour;
57 struct pixel_to_draw *todraw = MemAlloc(sizeof(struct pixel_to_draw));
58 todraw->x = x;
59 todraw->y = y;
60 todraw->colour = colour;
61 pixels_to_draw[total_draw_instructions++] = todraw;
62 }
63 }
64
65 int DrawingVector(void)
66 {
67 return (total_draw_instructions > current_draw_instruction);
68 }
69
70 void FreePixels(void)
71 {
72 for (int i = 0; i < total_draw_instructions; i++)
73 if (pixels_to_draw[i] != NULL)
74 free(pixels_to_draw[i]);
75 free(pixels_to_draw);
76 pixels_to_draw = NULL;
77 }
78
79 #ifdef SPATTERLIGHT
80 extern int gli_slowdraw;
81 #else
82 static int gli_slowdraw = 0;
83 #endif
84
85 void DrawSomeVectorPixels(int from_start)
86 {
87 VectorState = DRAWING_VECTOR_IMAGE;
88 int i = current_draw_instruction;
89 if (from_start)
90 i = 0;
91 if (i == 0)
92 RectFill(0, 0, scott_graphics_width, scott_graphics_height, Remap(bg_colour));
93 for (; i < total_draw_instructions && (!gli_slowdraw || i < current_draw_instruction + 50); i++) {
94 struct pixel_to_draw todraw = *pixels_to_draw[i];
95 PutPixel(todraw.x, todraw.y, Remap(todraw.colour));
96 }
97 current_draw_instruction = i;
98 if (current_draw_instruction >= total_draw_instructions) {
99 glk_request_timer_events(0);
100 VectorState = SHOWING_VECTOR_IMAGE;
101 FreePixels();
102 }
103 }
104
105 static void
106 scott_linegraphics_draw_line(int x1, int y1, int x2, int y2,
107 int colour)
108 {
109 int x, y, dx, dy, incx, incy, balance;
110
111 /* Normalize the line into deltas and increments. */
112 if (x2 >= x1) {
113 dx = x2 - x1;
114 incx = 1;
115 } else {
116 dx = x1 - x2;
117 incx = -1;
118 }
119
120 if (y2 >= y1) {
121 dy = y2 - y1;
122 incy = 1;
123 } else {
124 dy = y1 - y2;
125 incy = -1;
126 }
127
128 /* Start at x1,y1. */
129 x = x1;
130 y = y1;
131
132 /* Decide on a direction to progress in. */
133 if (dx >= dy) {
134 dy <<= 1;
135 balance = dy - dx;
136 dx <<= 1;
137
138 /* Loop until we reach the end point of the line. */
139 while (x != x2) {
140 scott_linegraphics_plot_clip(x, y, colour);
141 if (balance >= 0) {
142 y += incy;
143 balance -= dx;
144 }
145 balance += dy;
146 x += incx;
147 }
148 scott_linegraphics_plot_clip(x, y, colour);
149 } else {
150 dx <<= 1;
151 balance = dx - dy;
152 dy <<= 1;
153
154 /* Loop until we reach the end point of the line. */
155 while (y != y2) {
156 scott_linegraphics_plot_clip(x, y, colour);
157 if (balance >= 0) {
158 x += incx;
159 balance -= dy;
160 }
161 balance += dx;
162 y += incy;
163 }
164 scott_linegraphics_plot_clip(x, y, colour);
165 }
166 }
167
168 static int linegraphics_get_pixel(int x, int y)
169 {
170 return picture_bitmap[y * 255 + x];
171 }
172
173 static void diamond_fill(uint8_t x, uint8_t y, int colour)
174 {
175 uint8_t buffer[2048];
176 cbuf_handle_t ringbuf = circular_buf_init(buffer, 2048);
177 circular_buf_putXY(ringbuf, x, y);
178 while (!circular_buf_empty(ringbuf)) {
179 circular_buf_getXY(ringbuf, &x, &y);
180 if (x >= 0 && x < scott_graphics_width && y >= 0 && y < scott_graphics_height && linegraphics_get_pixel(x, y) == bg_colour) {
181 scott_linegraphics_plot_clip(x, y, colour);
182 circular_buf_putXY(ringbuf, x, y + 1);
183 circular_buf_putXY(ringbuf, x, y - 1);
184 circular_buf_putXY(ringbuf, x + 1, y);
185 circular_buf_putXY(ringbuf, x - 1, y);
186 }
187 }
188 circular_buf_free(ringbuf);
189 }
190
191 void DrawVectorPicture(int image)
192 {
193 if (image < 0) {
194 return;
195 }
196
197 if (vector_image_shown == image && pixels_to_draw) {
198 if (VectorState == SHOWING_VECTOR_IMAGE) {
199 return;
200 } else {
201 if (gli_slowdraw)
202 glk_request_timer_events(20);
203 DrawSomeVectorPixels(1);
204 return;
205 }
206 }
207
208 glk_request_timer_events(0);
209 vector_image_shown = image;
210 if (pixels_to_draw != NULL)
211 FreePixels();
212 pixels_to_draw = MemAlloc(255 * 97 * sizeof(struct pixel_to_draw *));
213 memset(pixels_to_draw, 0, 255 * 97 * sizeof(struct pixel_to_draw *));
214 total_draw_instructions = 0;
215 current_draw_instruction = 0;
216
217 if (palchosen == NO_PALETTE) {
218 palchosen = Game->palette;
219 DefinePalette();
220 }
221 picture_bitmap = MemAlloc(255 * 97);
222 bg_colour = LineImages[image].bgcolour;
223 memset(picture_bitmap, bg_colour, 255 * 97);
224 if (bg_colour == 0)
225 line_colour = 7;
226 else
227 line_colour = 0;
228 int x = 0, y = 0, y2 = 0;
229 uint8_t arg1, arg2, arg3;
230 uint8_t *p = LineImages[image].data;
231 uint8_t opcode = 0;
232 while (p - LineImages[image].data < LineImages[image].size && opcode != 0xff) {
233 if (p > entire_file + file_length) {
234 fprintf(stderr, "Out of range! Opcode: %x. Image: %d. LineImages[%d].size: %zu\n", opcode, image,
235 image, LineImages[image].size);
236 break;
237 }
238 opcode = *(p++);
239 switch (opcode) {
240 case 0xc0:
241 y = 190 - *(p++);
242 x = *(p++);
243 break;
244 case 0xc1:
245 arg1 = *(p++);
246 arg2 = *(p++);
247 arg3 = *(p++);
248 diamond_fill(arg3, 190 - arg2, arg1);
249 break;
250 case 0xff:
251 break;
252 default:
253 arg1 = *(p++);
254 y2 = 190 - opcode;
255 scott_linegraphics_draw_line(x, y, arg1, y2, line_colour);
256 x = arg1;
257 y = y2;
258 break;
259 }
260 }
261 if (picture_bitmap != NULL) {
262 free(picture_bitmap);
263 picture_bitmap = NULL;
264 }
265 if (gli_slowdraw)
266 glk_request_timer_events(20);
267 else
268 DrawSomeVectorPixels(1);
269 }