XXIIVV
Nasu on Plan 9 picture
14P10 — Nasu on Plan 9

Character Memory format of Famicom roms spritesheets.

The chr file format contains a series of bits equivalent to pixels for each tile in the spreadsheet of a rom. A chr tile is 8x8 pixels, the data for each tile is made up of 128 bits, where the first 64 bits are the first color, the next 64 bits the second color, and where the colors overlap result in the third color, for a total of 4 colors including the background.

0000 1e3f 3f3f 3f1e 78fc fcfc fc78 0000

For a simple editor and ANSI C implementation, see Nasu.

C Integration

typedef unsigned char Uint8;
Uint8 chrbuf[HOR * VER * 16];
fread(chrbuf, sizeof(chrbuf), 1, f)
fwrite(chrbuf, sizeof(chrbuf), 1, f)

Read/Write

void
putchr(Uint8 *chrbuf, int row, int col, int color)
{
	if(row < 0 || row > SZ - 8)
		return;
	if(color == 0 || color == 2)
		chrbuf[row] &= ~(1UL << (7 - col));
	else
		chrbuf[row] |= 1UL << (7 - col);
	if(color == 0 || color == 1)
		chrbuf[row + 8] &= ~(1UL << (7 - col));
	else
		chrbuf[row + 8] |= 1UL << (7 - col);
}

void
exportchr(void)
{
	int h, v, x, y;
	Uint8 chrbuf[SZ];
	FILE *f = fopen("export.chr", "wb");
	for(v = 0; v < VER; ++v)
		for(h = 0; h < HOR; ++h)
			for(y = 0; y < 8; ++y)
				for(x = 0; x < 8; ++x)
					putchr(chrbuf,
						y + (h + v * HOR) * 16,
						x,
						colortheme(getpixel(pixels, h * 8 + x, v * 8 + y)));
	if(!fwrite(chrbuf, sizeof(chrbuf), 1, f))
		error("Save", "Export failure");
	fclose(f);
	puts("Export complete");
}

Writing an icon

Icons are 8x8 monochrome bit maps, or half of a chr.

Uint8 icon[8] = {0x38, 0x7c, 0xfe, 0xfe, 0xfe, 0x7c, 0x38, 0x00};

Icons can be drawn like:

void
drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int fg, int bg)
{
	int v, h;
	for(v = 0; v < 8; v++)
		for(h = 0; h < 8; h++) {
			int clr = (icon[v] >> (7 - h)) & 0x1;
			putpixel(dst, x + h, y + v, clr == 1 ? fg : bg);
		}
}
— Submit an edit to chr_format.htm(69 lines)

incoming(6): tools nasu dotgrid moogle left meta

Last update on 14P10, edited 5 times. +24/32fh ----|-