// -----------------------------------------------------------
// common.c - shared routines and tables
// -----------------------------------------------------------
#include "common.h"

char framectr;
char washphase;

// ---- clear screen RAM, fill color RAM with white ----
void clear_screen(void)
{
	for (unsigned i = 0; i < 1000; i++)
	{
		SCREEN[i] = ' ';
		COLRAM[i] = VCOL_WHITE;
	}
}

// ---- print a text line ----
void print_text(char row, char col, char color, const char *txt)
{
	char *sp = SCREEN + rowoff[row] + col;
	char *cp = COLRAM + rowoff[row] + col;
	for (char i = 0; txt[i]; i++)
	{
		sp[i] = txt[i];
		cp[i] = color;
	}
}

// 3x5 block font for the big slide titles (5 bytes per glyph,
// low 3 bits used, MSB side = left)
static const char bigglyphs[] = {
	0, 0, 0, 0, 0,          //  0 space
	2, 5, 7, 5, 5,          //  1 A
	3, 4, 4, 4, 3,          //  2 C
	6, 5, 5, 5, 6,          //  3 D
	7, 4, 6, 4, 7,          //  4 E
	7, 4, 6, 4, 4,          //  5 F
	7, 2, 2, 2, 7,          //  6 I
	4, 4, 4, 4, 7,          //  7 L
	5, 7, 7, 5, 5,          //  8 M
	6, 5, 5, 5, 5,          //  9 N
	7, 5, 5, 5, 7,          // 10 O
	6, 5, 6, 4, 4,          // 11 P
	6, 5, 6, 5, 5,          // 12 R
	3, 4, 2, 1, 6,          // 13 S
	7, 2, 2, 2, 2,          // 14 T
	5, 5, 2, 2, 2,          // 15 Y
	3, 4, 5, 5, 7           // 16 G
};

static const char bigmap[27] = {     // screen code (a=1..z=26) -> glyph index
	 0,                              // (unused)
	 1,  0,  2,  3,  4,  5, 16,  0,  6,  0,   // a-j
	 0,  7,  8,  9, 10, 11,  0, 12, 13, 14,   // k-t
	 0,  0,  0,  0, 15,  0                    // u-z
};

// ---- draw a text line with big 3x5 block letters ----
void draw_big(char row, char col, char color, const char *txt)
{
	for (char i = 0; txt[i]; i++)
	{
		char c = txt[i];
		if (c == ' ')                // space: leave the cell empty
			continue;

		const char *glyph = bigglyphs + 5 * bigmap[c];
		char x = col + 4 * i;        // cell x: 3 wide + 1 gap per letter
		for (char r = 0; r < 5; r++)
		{
			char *sp = SCREEN + rowoff[row + r] + x;
			char *cp = COLRAM + rowoff[row + r] + x;
			char bits = glyph[r];
			for (char b = 0; b < 3; b++)         // leftmost pixel first
			{
				if (bits & (4 >> b))
				{
					sp[b] = 0xa0;                // inverse space = solid block
					cp[b] = color;
				}
			}
		}
	}
}

// ---- shared tables ----

const unsigned rowoff[25] = {
	   0,  40,  80, 120, 160, 200, 240, 280, 320, 360,
	 400, 440, 480, 520, 560, 600, 640, 680, 720, 760,
	 800, 840, 880, 920, 960
};

const char washtbl[16] = {           // gold glow ramp (16 entries, cyclic)
	0x09, 0x02, 0x08, 0x0a, 0x07, 0x07, 0x01, 0x01,
	0x01, 0x01, 0x07, 0x07, 0x0a, 0x08, 0x02, 0x09
};

const char pulsetbl[8] = {           // grey-white pulse (8 entries, cyclic)
	0x0b, 0x0c, 0x0f, 0x01, 0x01, 0x0f, 0x0c, 0x0b
};

const char fadetbl[16] = {           // fade-to-black: each color -> one shade darker,
                                     //   every chain reaches black in at most 4 steps
                                     //   (e.g. white -> lgrey -> mgrey -> dgrey -> black)
	//   blk   wht   red   cyn   pur   grn   blu   yel
	0x00, 0x0f, 0x09, 0x0e, 0x06, 0x0b, 0x00, 0x08,
	//   org   brn   lrd   dgr   mgr   lgn   lbl   lgr
	0x02, 0x00, 0x02, 0x00, 0x0b, 0x05, 0x06, 0x0c
};
