~rabbits/dito

53b07feaf905e4173bd4106069878c485159b762 — neauoire 2 years ago 4bd5005
Standardize draw functions
1 files changed, 49 insertions(+), 53 deletions(-)

M dito.c
M dito.c => dito.c +49 -53
@@ 164,49 164,45 @@ putpixel(Uint32 *dst, int x, int y, int color)
}

void
drawchr(Uint32 *dst, int x, int y, int id)
drawchr(Uint32 *dst, int x, int y, Uint8 *sprite)
{
	int v, h, offset = id * 16;
	int v, h;
	for(v = 0; v < 8; v++)
		for(h = 0; h < 8; h++) {
			int px = (x * 8) + (8 - h);
			int py = (y * 8) + v;
			int ch1 = doc.data[offset + v];
			int ch2 = doc.data[offset + v + 8];
			int clr = ((ch1 >> h) & 0x1) + (((ch2 >> h) & 0x1) << 1);
			putpixel(dst, px - 1, py, clr);
			int ch1 = ((sprite[v] >> h) & 0x1);
			int ch2 = (((sprite[v + 8] >> h) & 0x1) << 1);
			putpixel(dst, x + 7 - h, y + v, ch1 + ch2);
		}
}

void
drawiconalpha(Uint32 *dst, int x, int y, Uint8 *icon, int fg)
drawicn(Uint32 *dst, int x, int y, Uint8 *sprite, int fg, int bg)
{
	int v, h;
	for(v = 0; v < 8; v++)
		for(h = 0; h < 8; h++) {
			if((icon[v] >> (7 - h)) & 0x1)
				putpixel(dst, x + h, y + v, fg);
			int ch1 = (sprite[v] >> (7 - h)) & 0x1;
			putpixel(dst, x + h, y + v, ch1 ? fg : bg);
		}
}

void
drawicon(Uint32 *dst, int x, int y, Uint8 *icon, int fg, int bg)
drawicna(Uint32 *dst, int x, int y, Uint8 *icon, int fg)
{
	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);
		}
		for(h = 0; h < 8; h++)
			if((icon[v] >> (7 - h)) & 0x1)
				putpixel(dst, x + h, y + v, fg);
}

void
drawui(Uint32 *dst)
{
	int bottom = VER * 8 + 8;
	drawicon(dst, 0, bottom, icons[COLORS ? 3 : 2], 1, 0);
	drawicon(dst, 2 * 8, bottom, icons[GUIDES ? 10 : 9], GUIDES ? 1 : 2, 0);
	drawicon(dst, (HOR - 1) * 8, bottom, icons[11], doc.unsaved ? 2 : 3, 0); /* save state */
	drawicn(dst, 0, bottom, icons[COLORS ? 3 : 2], 1, 0);
	drawicn(dst, 2 * 8, bottom, icons[GUIDES ? 10 : 9], GUIDES ? 1 : 2, 0);
	drawicn(dst, (HOR - 1) * 8, bottom, icons[11], doc.unsaved ? 2 : 3, 0); /* save state */
}

Uint32


@@ 236,30 232,38 @@ getcolor(int x, int y)
}

int
getpalette(Uint32 clr)
getpattern(int palette, int x, int y)
{
	if(palette == 3)
		return 1;
	if(palette == 2)
		return ((x + y) % 2) == 0 && ((y - x) % 2) == 0;
	if(palette == 1)
		return ((x + y) % 4) == 0 && ((y - x) % 4) == 0;
	return 0;
}

int
getpalette(Uint32 clr, int x, int y)
{
	Uint32 res = 0, rec = 999999;
	Uint32 res = 0, dis = 999999;
	int i;
	for(i = 0; i < 4; ++i) {
		Uint32 d = coloroffset(clr, theme[i]);
		if(d < rec) {
			rec = d;
		if(d < dis) {
			dis = d;
			res = i;
		}
	}
	return res;
}

int
getpattern(int palette, int x, int y)
void
drawpicker(Uint32 *dst, int id)
{
	if(palette == 3)
		return 1;
	if(palette == 2)
		return ((x + y) % 2) == 0 && ((y - x) % 2) == 0;
	if(palette == 1)
		return ((x + y) % 4) == 0 && ((y - x) % 4) == 0;
	return 0;
	drawicna(dst, pickers[id].x - 3, pickers[id].y - 3, icons[0], 4);
	drawicna(dst, pickers[id].x + 6, pickers[id].y - 3, icons[4 + id], 4);
	drawicna(dst, pickers[id].x - 3, pickers[id].y - 3, icons[1], id);
}

void


@@ 272,34 276,25 @@ redraw(Uint32 *dst)
	theme[2] = getcolor(pickers[2].x, pickers[2].y);
	theme[3] = getcolor(pickers[3].x, pickers[3].y);
	/* Draw */
	clear(pixels);
	clear(dst);
	drawui(dst);
	/* Draw images */
	SDL_LockSurface(image);
	for(y = 0; y < image->h; ++y) {
		for(x = 0; x < image->w; ++x) {
			int palette = getpalette(getcolor(x, y));
			int palette = getpalette(getcolor(x, y), x, y);
			putchr(x, y, COLORS ? palette : getpattern(palette, x, y));
		}
	}
	SDL_UnlockSurface(image);
	for(y = 0; y < VER; ++y)
		for(x = 0; x < HOR; ++x)
			drawchr(dst, x, y, x + y * HOR);
	/* draw pickers */
	if(GUIDES) {
		drawiconalpha(pixels, pickers[0].x - 3, pickers[0].y - 3, icons[0], 4);
		drawiconalpha(pixels, pickers[1].x - 3, pickers[1].y - 3, icons[0], 4);
		drawiconalpha(pixels, pickers[2].x - 3, pickers[2].y - 3, icons[0], 4);
		drawiconalpha(pixels, pickers[3].x - 3, pickers[3].y - 3, icons[0], 4);
		drawiconalpha(pixels, pickers[0].x - 3, pickers[0].y - 3, icons[1], 0);
		drawiconalpha(pixels, pickers[1].x - 3, pickers[1].y - 3, icons[1], 1);
		drawiconalpha(pixels, pickers[2].x - 3, pickers[2].y - 3, icons[1], 2);
		drawiconalpha(pixels, pickers[3].x - 3, pickers[3].y - 3, icons[1], 3);
		drawiconalpha(pixels, pickers[0].x + 8, pickers[0].y - 3, icons[4], 4);
		drawiconalpha(pixels, pickers[1].x + 8, pickers[1].y - 3, icons[5], 4);
		drawiconalpha(pixels, pickers[2].x + 8, pickers[2].y - 3, icons[6], 4);
		drawiconalpha(pixels, pickers[3].x + 8, pickers[3].y - 3, icons[7], 4);
			drawchr(dst, x * 8, y * 8, &doc.data[(x + y * HOR) * 16]);
	if(GUIDES) { /* draw pickers */
		drawpicker(dst, 0);
		drawpicker(dst, 1);
		drawpicker(dst, 2);
		drawpicker(dst, 3);
	}
	SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
	SDL_RenderClear(gRenderer);


@@ 344,6 339,7 @@ savedoc(Document *d, char *name)
	d->unsaved = 0;
	scpy(name, d->name, 256);
	fclose(f);
	printf("0x%x 0x%x 0x%x 0x%x\n", theme[0], theme[1], theme[2], theme[3]);
	printf("Saved: %s\n", d->name);
	redraw(pixels);
	return 1;


@@ 491,10 487,10 @@ main(int argc, char **argv)
	image = SDL_LoadBMP(argv[1]);
	if(!image)
		return error("Load", "Invalid bmp image.");
	setpicker(&pickers[0], 35, 50);
	setpicker(&pickers[1], 50, 15);
	setpicker(&pickers[2], 90, 80);
	setpicker(&pickers[3], 65, 110);
	setpicker(&pickers[0], 66, 16);
	setpicker(&pickers[1], 46, 49);
	setpicker(&pickers[2], 140, 10);
	setpicker(&pickers[3], 120, 40);
	makedoc(&doc, "untitled.chr");
	while(1) {
		int tick = SDL_GetTicks();