summaryrefslogtreecommitdiffstats
path: root/esp32/main/scrn.c
blob: d455e5bf0b0786ebe775d612b2f3172fadd1ff3b (plain)
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
#include <esp_log.h>

#include "scrn.h"

#define MASK_ON(x) (1ULL << (7 - (x % 8)))
#define MASK_OFF(x) ~(1ULL << (7 - (x % 8)))

const static char *TAG = "scrn";

void scrn_clear(struct scrn *sc)
{
	int n = sc->width * sc->height / 8;

	for (int i = 0; i < n; i++)
		sc->fb[i] = 0;
}

void scrn_draw(struct scrn *sc, struct sprite *s)
{
	int s_n = s->width * s->height;
	int buf_n = sc->width * sc->height;
	int buf_i = s->offset_y * sc->width + s->offset_x;

	int s_i = 0, s_col = 0;

	while (s_i < s_n) {
		if (s_col == s->width) {
			s_col = 0;
			buf_i += sc->width - s->width;
		}

		if (s->bmp[s_i / 8] & MASK_ON(s_i)) {
			if (buf_i < buf_n)
				sc->fb[buf_i / 8] |= MASK_ON(buf_i);
			else
				ESP_LOGE(TAG, "draw(): pixel out of range");
		}

		s_i++;
		buf_i++;
		s_col++;
	}
}