Source

watches / lunar / draw.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/** \file
 * Drawing functions.
 */
#include "draw.h"
#include "image.h"


void
draw_pixel(
	color24_t c,
	int32_t x,
	int32_t y
)
{
	//printf("pix %d %d @ %d %d %d\n", x, y, c.red, c.blue, c.green);
	if (x >= VSCREEN_WIDTH || y >= VSCREEN_HEIGHT)
		return;
	if (x < 0 || y < 0)
		return;

	x >>= VSCREEN_SHIFT;
	y >>= VSCREEN_SHIFT;
	pulse_set_draw_window(x, y, x, y);
	pulse_draw_point24(c);
}

static inline int32_t
abs(
	int32_t x
)
{
	if (x < 0)
		return -x;
	return x;
}

void
draw_line(
	color24_t c,
	int32_t x0,
	int32_t y0,
	int32_t x1,
	int32_t y1
)
{
	const int32_t dx = abs(x1 - x0);
	const int32_t dy = abs(y1 - y0);

	const int32_t sx = x0 < x1 ? 1 : -1;
	const int32_t sy = y0 < y1 ? 1 : -1;
	int32_t err = dx - dy;

#if VSCREEN_SHIFT != 0
	uint32_t last_px = -1;
	uint32_t last_py = -1;
#endif

	while(1)
	{
#if VSCREEN_SHIFT != 0
		uint32_t px = x0 >> VSCREEN_SHIFT;
		uint32_t py = y0 >> VSCREEN_SHIFT;
		if (px != last_px || py != last_py)
		{
			last_px = px;
			last_py = py;
			draw_pixel(c, x0, y0);
		}
#else
		draw_pixel(c, x0, y0);
#endif

		if (x0 == x1 && y0 == y1)
			break;
		int32_t e2 = 2 * err;
		if (e2 > -dy)
		{
			err -= dy;
			x0 += sx;
		}
		if (e2 < +dx)
		{
			err += dx;
			y0 += sy;
		}
	}
}


void
draw_line_bg(
	PulseResource image_id,
	int32_t px, // position of image on screen
	int32_t py,
	int32_t x0, // position of line on screen
	int32_t y0,
	int32_t x1,
	int32_t y1
)
{
	image_resource_t resource;
	image_resource(image_id, &resource);
	
	uint32_t offset = resource.offset;
	const uint32_t width = 3 * resource.width;

	const int32_t dx = abs(x1 - x0);
	const int32_t dy = abs(y1 - y0);

	const int32_t sx = x0 < x1 ? 1 : -1;
	const int32_t sy = y0 < y1 ? 1 : -1;
	int32_t err = dx - dy;

	while(1)
	{
		uint8_t lum;
		uint8_t img_x = x0 - px;
		uint8_t img_y = y0 - py;
		spiflash_read(
			offset + width * img_y + img_x,
			&lum,
			sizeof(lum)
		);

		color24_t c = { lum, lum, lum };

		draw_pixel(c, x0, y0);

		if (x0 == x1 && y0 == y1)
			break;
		int32_t e2 = 2 * err;
		if (e2 > -dy)
		{
			err -= dy;
			x0 += sx;
		}
		if (e2 < +dx)
		{
			err += dx;
			y0 += sy;
		}
	}
}


static inline int32_t
vscreen_ipart(
	int32_t x
)
{
	return x & ~(VSCREEN_PIXEL - 1);
}


static inline int32_t
vscreen_fpart(
	int32_t x
)
{
	return x & (VSCREEN_PIXEL - 1);
}


static inline int32_t
vscreen_rfpart(
	int32_t x
)
{
	return VSCREEN_PIXEL - vscreen_fpart(x);
}


static inline int32_t
vscreen_round(
	int32_t x
)
{
	return vscreen_ipart(x + (VSCREEN_PIXEL / 2));
}


static void
draw_pixel_aa(
	color24_t c,
	int32_t x,
	int32_t y,
	uint32_t frac
)
{
	if (c.red)
		c.red = (c.red * frac) >> VSCREEN_SHIFT;
	if (c.blue)
		c.blue = (c.blue * frac) >> VSCREEN_SHIFT;
	if (c.green)
		c.green = (c.green * frac) >> VSCREEN_SHIFT;

	draw_pixel(c, x, y);
}


/** Draw using Xiaolin Wu's line algorithm */
#define GRAD_SCALE 256

void
draw_line_horiz(
	color24_t c,
	int32_t x1,
	int32_t y1,
	int32_t x2,
	int32_t y2
)
{
	const int32_t dx = x2 - x1;
	const int32_t dy = y2 - y1;

	if (x2 < x1)
	{
		int32_t temp = x1;
		x1 = x2;
		x2 = temp;
		temp = y1;
		y1 = y2;
		y2 = temp;
	}

	// The gradient is scaled by another shift factor since
	// otherwise we won't hit the end points correctly.
	const int32_t grad = (dy * GRAD_SCALE * VSCREEN_PIXEL) / dx;
	const int32_t half_pixel = VSCREEN_PIXEL / 2;

	int32_t xend, yend, xgap;

	// Handle first endpoint
	xend = vscreen_round(x1);
	yend = y1 + (grad * (xend - x1)) / GRAD_SCALE / VSCREEN_PIXEL;
	xgap = vscreen_rfpart(x1 + half_pixel);
	const int32_t xpxl1 = xend;
	const int32_t ypxl1 = vscreen_ipart(yend);
	draw_pixel_aa(c, xpxl1, ypxl1, (vscreen_rfpart(yend) * xgap) / VSCREEN_PIXEL);
	draw_pixel_aa(c, xpxl1, ypxl1 + VSCREEN_PIXEL, (vscreen_fpart(yend) * xgap) / VSCREEN_PIXEL);
	int32_t intery = yend * GRAD_SCALE + grad; // first y-intersection

	//printf("%d %d %d => %d %d grad %d\n", x1, xend, y1, x2, y2, grad);


	// Handle second endpoint
	xend = vscreen_round(x2);
	yend = y2 + (grad * (xend - x2)) / GRAD_SCALE / VSCREEN_PIXEL;
	xgap = vscreen_fpart(x2 + half_pixel);
	const int32_t xpxl2 = xend;
	const int32_t ypxl2 = vscreen_ipart(yend);
	draw_pixel_aa(c, xpxl2, ypxl2, (vscreen_rfpart(yend) * xgap) / VSCREEN_PIXEL);
	draw_pixel_aa(c, xpxl2, ypxl2 + VSCREEN_PIXEL, (vscreen_fpart(yend) * xgap) / VSCREEN_PIXEL);

	//printf("x: %d %d\n", xpxl1 + 1, xpxl2);

	// Loop (this could make a single window draw)
	for (int32_t x = xpxl1 + VSCREEN_PIXEL ; x < xpxl2 ; x += VSCREEN_PIXEL)
	{
		int32_t y = intery / GRAD_SCALE;
		intery += grad;
		draw_pixel_aa(c, x, vscreen_ipart(y), vscreen_rfpart(y));
		draw_pixel_aa(c, x, vscreen_ipart(y)+VSCREEN_PIXEL, vscreen_fpart(y));
	}
}


static void
draw_line_vert(
	color24_t c,
	int32_t x1,
	int32_t y1,
	int32_t x2,
	int32_t y2
)
{
	const int32_t dx = x2 - x1;
	const int32_t dy = y2 - y1;

	if (y2 < y1)
	{
		int32_t temp = x1;
		x1 = x2;
		x2 = temp;
		temp = y1;
		y1 = y2;
		y2 = temp;
	}

	const int32_t grad = (dx * VSCREEN_PIXEL * GRAD_SCALE) / dy;
	const int32_t half_pixel = VSCREEN_PIXEL / 2;

	int32_t xend, yend, ygap;

	// Handle first endpoint
	yend = vscreen_round(y1);
	xend = x1 + (grad * (yend - y1)) / GRAD_SCALE / VSCREEN_PIXEL;
	ygap = vscreen_rfpart(y1 + half_pixel);
	const int32_t ypxl1 = yend;
	const int32_t xpxl1 = vscreen_ipart(xend);
	draw_pixel_aa(c, xpxl1, ypxl1, (vscreen_rfpart(xend) * ygap) / VSCREEN_PIXEL);
	draw_pixel_aa(c, xpxl1 + VSCREEN_PIXEL, ypxl1, (vscreen_fpart(xend) * ygap) / VSCREEN_PIXEL);
	int32_t interx = xend * GRAD_SCALE + grad; // first x-intersection


	// Handle second endpoint
	yend = vscreen_round(y2);
	xend = x2 + (grad * (yend - y2)) / GRAD_SCALE / VSCREEN_PIXEL;
	ygap = vscreen_fpart(y2 + half_pixel);
	const int32_t ypxl2 = yend;
	const int32_t xpxl2 = vscreen_ipart(xend);
	draw_pixel_aa(c, xpxl2, ypxl2, (vscreen_rfpart(xend) * ygap) / VSCREEN_PIXEL);
	draw_pixel_aa(c, xpxl2 + VSCREEN_PIXEL, ypxl2, (vscreen_fpart(xend) * ygap) / VSCREEN_PIXEL);

	//printf("x: %d %d\n", xpxl1 + 1, xpxl2);

	// Loop (this could make a single window draw)
	for (int32_t y = ypxl1 + VSCREEN_PIXEL ; y < ypxl2 ; y += VSCREEN_PIXEL)
	{
		int32_t x = interx / GRAD_SCALE;
		interx += grad;

		draw_pixel_aa(c, vscreen_ipart(x), y, vscreen_rfpart(x));
		draw_pixel_aa(c, vscreen_ipart(x)+VSCREEN_PIXEL, y, vscreen_fpart(x));
	}
}


void
draw_line_aa(
	color24_t c,
	int32_t x0,
	int32_t y0,
	int32_t x1,
	int32_t y1
)
{
	const int32_t dx = x1 > x0 ? x1 - x0 : x0 - x1;
	const int32_t dy = y1 > y0 ? y1 - y0 : y0 - y1;
	if (dx == 0 && dy == 0)
		draw_pixel(c, x0, y0);
	else
	if (dx > dy)
		draw_line_horiz(c, x0, y0, x1, y1);
	else
		draw_line_vert(c, x0, y0, x1, y1);

/*
	// Mark where we should start and end
	draw_pixel(COLOR_WHITE, x0, y0);
	draw_pixel(COLOR_BLUE, x1, y1);
*/
}




static char buf[2];
struct PWidgetTextDynamic font;

void
draw_monostring(
	int32_t x,
	int32_t y,
	color24_t color,
	PulseResource font_name,
	int width,
	int height,
	const char * s
)
{
	pulse_init_dynamic_text_widget(
		&font,
		buf,
		font_name,
		color,
		PWTS_TRUNCATE
	);

	uint32_t orig_x = x >> VSCREEN_SHIFT;

	struct PWTextBox box = {
		.left		= orig_x,
		.top		= y >> VSCREEN_SHIFT,
		.right		= SCREEN_WIDTH - 1,
		.bottom		= SCREEN_HEIGHT - 1,
	};

	buf[1] = '\0';

	while ((buf[0] = *s++))
	{
		if (height != 0)
			fill(
				COLOR_BLACK,
				box.left,
				box.top,
				box.left + width - 1,
				box.top + height - 1
			);

		pulse_render_text(&box, &font);
		box.left += width;
	}
}