aboutsummaryrefslogtreecommitdiff
path: root/src/brailleboi.c
blob: fce293a860d944cb5f6fe183096dbf936f97fe7f (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
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
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
#include <string.h>

#include "brailleboi.h"

int brailleInit(int width, int height, image_buffer* buf)
{
	setlocale(LC_ALL, "");
	wprintf(L"\033[?25l");
	buf->width = width;
	buf->height = height;
	buf->char_width = (width+1)/2;
	buf->char_height = (height+1)/4;
	buf->contents = malloc((buf->char_width * buf->char_height) * sizeof(int));
	return 0;
}

int brailleStop(image_buffer* buf)
{
	wprintf(L"\033[?25h");
	free(buf->contents);
	return 0;
}

int brailleReorganizeBits(int old)
{
	//Reorganizes bits because the unicode standard for 8 dotted braille is weird
	int new =	(old&0x87) |	// 0b10000111
				(old&0x70)>>1 |	// 0b01110000
				(old&0x08)<<3;	// 0b00001000
	return new;
}

void braillePrint(int data)
{
	wprintf(L"%lc", (0x2800|data));
}

void braillePrintCoords(wchar_t *wcs, int len, int data, int x, int y)
{
	swprintf(wcs, len, L"\033[%d;%dH%lc", x+1, y+1, (0x2800|data));
}

int braillePlot(int x, int y, image_buffer* buf)
{
	//Check if plot is within bounds
	if ((x>buf->width) | (y>buf->height))
		return 1;
	buf->contents[(y-1)/4 * buf->char_width + (x-1)/2] |= 1<<((y-1)%4+(1-x%2)*4);
	return 0;
}

void brailleClearBuffer(image_buffer* buf)
{
	memset(buf->contents, 0, (buf->char_width * buf->char_height) * sizeof(int));
}

void brailleUpdateScreen(image_buffer* buf)
{
	wprintf(L"\033[2J");
	int len = (buf->char_width * buf->char_height);
	wchar_t* frame = malloc(len * sizeof(wchar_t));
	for (int x=0;x<buf->char_width;x++) {
		for (int y=0;y<buf->char_height;y++) {
			braillePrintCoords(frame, len, brailleReorganizeBits(buf->contents[y * buf->char_width + x]), x, y);
		}
	}
	wprintf(frame);
	free(frame);
}