~rabbits/nasu

5b067408cc2af336f8f512c8e3b7d6e18bf4aadf — Devine Lu Linvega 1 year, 2 months ago b4a02d9
Started implementing modes
2 files changed, 96 insertions(+), 16 deletions(-)

M build.sh
M chr6.c
M build.sh => build.sh +2 -2
@@ 5,10 5,10 @@ clang-format -i chr6.c
rm ./chr6

# debug
# cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined chr6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o chr6
cc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined chr6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o chr6

# build
cc -std=c89 -Wall chr6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o chr6
# cc -std=c89 -Wall chr6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o chr6

# run
./chr6 sprite.chr

M chr6.c => chr6.c +94 -14
@@ 20,9 20,22 @@ typedef struct Brush {
	int color;
	int down;
	int edit;
	int mode;
	int size;
	int erase;
	Point pos;
	Point prev;
} Brush;

char* modes[] = {
    "line",
    "tone1",
    "tone2",
    "block",
    "hor",
    "ver",
    "cross"};

unsigned char buffer[4096];
int colors[] = {color1, color2, color3, color4, color0};
int WIDTH = 8 * HOR * ZOOM + PAD * 2;


@@ 115,10 128,10 @@ write(int tx, int ty, int px, int py, int color)
}

void
edit(Brush* b)
edit(Point* p, int color)
{
	Point p1;
	setpt(&p1, b->pos.x - PAD, b->pos.y - PAD);
	setpt(&p1, p->x - PAD, p->y - PAD);
	if(!inspt(&p1, 8 * HOR * ZOOM, 8 * VER * ZOOM))
		return;
	write(


@@ 126,9 139,62 @@ edit(Brush* b)
	    p1.y / (8 * ZOOM),
	    (p1.x / ZOOM) % 8,
	    (p1.y / ZOOM) % 8,
	    b->color);
	    color);
}

int
patt(int mode, int size, Point p)
{
	if(mode == 1)
		return ((p.x + p.y) % 4) == 0 && ((p.y - p.x) % 4) == 0;
	if(mode == 2)
		return ((p.x + p.y) % 2) == 0 && ((p.y - p.x) % 2) == 0;
	if(mode == 3)
		return 1;
	if(mode == 4)
		return p.y % size == 0;
	if(mode == 5)
		return p.x % size == 0;
	if(mode == 6)
		return (p.x + p.y) % size == 0 || (p.x - p.y) % size == 0;
	return 0;
}

void
fill(Brush* b, int mode, int size, Point p0, int color)
{
	int x, y;
	Point p;
	for(x = -size / 2; x < size; ++x)
		for(y = -size / 2; y < size; ++y) {
			setpt(&p, p0.x + x, p0.y + y);
			if(patt(mode, size, p))
				edit(&p, color);
		}
	b->edit = 1;
	update(b);
	redraw();
}

void
line(Point* p0, Point* p1, int color)
{
	double dx = abs(p1->x - p0->x), sx = p0->x < p1->x ? 1 : -1;
	double dy = -abs(p1->y - p0->y), sy = p0->y < p1->y ? 1 : -1;
	double err = dx + dy, e2;
	for(;;) {
		edit(p0, color);
		if(p0->x == p1->x && p0->y == p1->y)
			break;
		e2 = 2 * err;
		if(e2 >= dy) {
			err += dy;
			p0->x += sx;
		}
		if(e2 <= dx) {
			err += dx;
			p0->y += sy;
		}
	}
	redraw();
}



@@ 146,7 212,6 @@ erase(Brush* b)
		buffer[(id * 16) + i + 8] = 0x00;
	}
	b->edit = 1;
	update(b);
	redraw();
}



@@ 227,18 292,24 @@ domouse(SDL_Event* event, Brush* b)
	case SDL_MOUSEBUTTONUP:
		if(event->button.button == SDL_BUTTON_LEFT)
			b->down = 0;
		if(event->button.button == SDL_BUTTON_RIGHT) {
			setpt(&b->pos, event->motion.x, event->motion.y);
			erase(b);
		}
		if(event->button.button == SDL_BUTTON_RIGHT)
			b->erase = 0;
		setpt(&b->prev, 0, 0);
		break;
	case SDL_MOUSEBUTTONDOWN:
		if(event->button.button == SDL_BUTTON_LEFT)
			b->down = 1;
		if(event->button.button == SDL_BUTTON_RIGHT)
			b->erase = 1;
		setpt(&b->prev, event->motion.x, event->motion.y);
	case SDL_MOUSEMOTION:
		if(b->down) {
			setpt(&b->pos, event->motion.x, event->motion.y);
			edit(b);
			if(b->mode == 0)
				line(&b->prev, &b->pos, b->erase ? 0 : b->color);
			else
				fill(b, b->mode, b->size, b->pos, b->erase ? 0 : b->color);
			setpt(&b->prev, b->pos.x, b->pos.y);
		}
		break;
	}


@@ 266,18 337,25 @@ dokey(SDL_Event* event, Brush* b)
		redraw();
		break;
	case SDLK_1:
		b->color = 0;
		b->mode = 0;
		break;
	case SDLK_2:
		b->color = 1;
		b->mode = 1;
		break;
	case SDLK_3:
		b->color = 2;
		b->mode = 2;
		break;
	case SDLK_4:
		b->color = 3;
		b->mode = 3;
		break;
	case SDLK_z:
		b->size -= 1;
		break;
	case SDLK_x:
		b->size += 1;
		break;
	}
	printf("%d\n", b->mode);
}

int


@@ 321,6 399,8 @@ main(int argc, char** argv)
	brush.down = 0;
	brush.color = 1;
	brush.edit = 0;
	brush.size = 10;
	brush.mode = 2;

	if(!init())
		return error("SDL", "failure");