~rabbits/nasu

274d25ebdd586c83b8c83f3213f738c5abce862d — Devine Lu Linvega 2 years ago 7207d01
Started paint tools
1 files changed, 80 insertions(+), 44 deletions(-)

M nasu6.c
M nasu6.c => nasu6.c +80 -44
@@ 23,9 23,13 @@ typedef struct Brush {
	Point prev;
} Brush;

/* TODO: Remove globals, set as arg */

static int SCREEN_WIDTH = 512 + PAD * 2;
static int SCREEN_HEIGHT = 512 + PAD * 2;
static int FPS = 30;

unsigned char buffer[1024 * 4];
int ZOOM = 4;
int clr = 0xFF0000;



@@ 84,6 88,73 @@ pixel(uint32_t* dst, Point p, int c)
	}
}

void
paint(uint32_t* dst, int id, int color)
{
	int ti = id / 64;
	int px = (ti / 256) * 128;
	int py = 0;
	int tx = (ti % 16) * 8;
	int ty = ((ti / 16) * 8) % 128;
	Point p;
	p.x = px + tx + (id % 8);
	p.y = py + ty + ((id % 64) / 8);
	if(color == 1)
		pixel(dst, p, 0x72DEC2);
	else if(color == 2)
		pixel(dst, p, 0xFFFFFF);
	else if(color == 3)
		pixel(dst, p, 0x333333);
	else
		pixel(dst, p, 0x000000);
}

void
update(void)
{
	int b, i, j, id = 0, ch1, ch2, color;
	for(b = 0; b < 1024 * 4; b += 16)
		for(i = 0; i < 8; i++)
			for(j = 7; j >= 0; j--) {
				ch1 = buffer[b + i];
				ch2 = buffer[b + i + 8];
				color = ((ch1 >> j) & 0x1) + (((ch2 >> j) & 0x1) << 1);
				paint(pixels, id, color);
				id++;
			}
}

void
write(int tx, int ty, int px, int py)
{
	int id = tx + ty * 16;
	int pid = px + py * 8;
	int pix = id * 16;
	pix += py;

	printf("#%d [%d:%d](%d,%d)\n", id, tx, ty, px, py);

	buffer[pix] = 0xFF;
	update();
}

void
edit(Point* p)
{
	int tx, ty, px, py;
	Point aligned;
	addpt(setpt(&aligned, p->x, p->y), -PAD);
	if(aligned.x < 0 || aligned.y < 0 || aligned.x > 8 * 16 * ZOOM || aligned.y > 8 * 16 * ZOOM)
		return;

	/* convert screen to texture pos */
	tx = aligned.x / (8 * ZOOM);
	ty = aligned.y / (8 * ZOOM);
	px = (aligned.x / ZOOM) % 8;
	py = (aligned.y / ZOOM) % 8;
	write(tx, ty, px, py);
}

void export(uint32_t* dst)
{
	/* TODO: chr file export */


@@ 178,10 249,16 @@ handle_mouse(SDL_Event* event, Brush* b)
	case SDL_MOUSEMOTION:
		if(b->down) {
			setpt(&b->pos, event->motion.x, event->motion.y);
			edit(&b->pos);
			setpt(&b->prev, b->pos.x, b->pos.y);

			/*
			
			addpt(&b->pos, -PAD);
			divpt(&b->pos, ZOOM);
			pixel(pixels, b->pos, 0xFFFFFF);
			setpt(&b->prev, b->pos.x, b->pos.y);
			*/
		}
		break;
	}


@@ 214,53 291,12 @@ handle_keypress(SDL_Event* event, Brush* b, Point* o)
	}
}

void
paint(uint32_t* dst, int id, int color)
{
	int ti = id / 64;
	int px = (ti / 256) * 128;
	int py = 0;
	int tx = (ti % 16) * 8;
	int ty = ((ti / 16) * 8) % 128;
	Point p;
	p.x = px + tx + (id % 8);
	p.y = py + ty + ((id % 64) / 8);
	if(color == 1)
		pixel(dst, p, 0x72DEC2);
	else if(color == 2)
		pixel(dst, p, 0xFFFFFF);
	else if(color == 3)
		pixel(dst, p, 0x333333);
	else
		pixel(dst, p, 0x000000);
}

int
load(FILE* f)
{
	int b, i, j, id;
	int ch1;
	int ch2;
	int color;
	unsigned char buffer[1024 * 4];
	if(!fread(buffer, sizeof(buffer), 1, f)) {
		printf("ERROR\n");
		return 0;
	}

	id = 0;
	for(b = 0; b < 1024 * 4; b += 16) {
		for(i = 0; i < 8; i++) {
			for(j = 7; j >= 0; j--) {
				ch1 = buffer[b + i];
				ch2 = buffer[b + i + 8];
				color = ((ch1 >> j) & 0x1) + (((ch2 >> j) & 0x1) << 1);
				paint(pixels, id, color);
				id++;
			}
		}
	}

	if(!fread(buffer, sizeof(buffer), 1, f))
		return error("Invalid size file", "");
	update();
	return 1;
}