~rabbits/nasu

118672baa665457d07888aa6d888e09f667cc472 — Devine Lu Linvega 11 months ago a1cec23
Cleanup
1 files changed, 37 insertions(+), 58 deletions(-)

M chr6.c
M chr6.c => chr6.c +37 -58
@@ 31,12 31,12 @@ typedef struct Brush {

char* modes[] = {
    "line",
    "tone1",
    "tone2",
    "block",
    "hor",
    "ver",
    "cross"};
    "tone",
    "bold",
    "full",
    "hori",
    "veri",
    "exes"};

unsigned char buffer[SZ];
int colors[] = {color1, color2, color3, color4, color0};


@@ 70,28 70,22 @@ dispt(Point* a, Point* b)
}

void
pixel(uint32_t* dst, Point p, int c)
pixel(uint32_t* dst, int x, int y, int c)
{
	int x, y;
	p.x = (p.x * ZOOM) + PAD;
	p.y = (p.y * ZOOM) + PAD;
	for(x = 0; x < ZOOM; ++x)
		for(y = 0; y < ZOOM; ++y)
			dst[(p.y + y) * WIDTH + (p.x + x)] = c;
	int h, v;
	for(h = 0; h < ZOOM; ++h)
		for(v = 0; v < ZOOM; ++v)
			dst[((y * ZOOM) + PAD + v) * WIDTH + ((x * ZOOM) + PAD + h)] = c;
}

void
draw(uint32_t* dst, int id, int color)
{
	int ti = id / 64;
	int px = (ti / (HOR * VER)) * (8 * HOR);
	int tx = (ti % HOR) * 8;
	int ty = ((ti / HOR) * 8);
	int odd = (ti + (ti / HOR + 2)) % 2 == 0;
	Point p;
	p.x = px + tx + (id % 8);
	p.y = ty + ((id % 64) / 8);
	pixel(dst, p, colors[GUIDES && odd && color == 0 ? 4 : color]);
	int px = (ti / (HOR * VER)) * (8 * HOR) + (ti % HOR) * 8 + (id % 8);
	int py = ((ti / HOR) * 8) + ((id % 64) / 8);
	pixel(dst, px, py, colors[GUIDES && odd && color == 0 ? 4 : color]);
}

void


@@ 200,27 194,17 @@ line(Point* p0, Point* p1, int color)
}

void
erase(Brush* b)
{
	int i, id;
	Point p1;
	setpt(&p1, b->pos.x, b->pos.y);
	if(!inspt(&p1, 8 * HOR, 8 * VER))
		return;
	id = (p1.x / 8) + (p1.y / 8) * 16;
	for(i = 0; i < 8; ++i) {
		buffer[(id * 16) + i] = 0x00;
		buffer[(id * 16) + i + 8] = 0x00;
	}
	b->edit = 1;
	redraw(pixels);
}

void
update(Brush* b)
{
	char title[512];
	snprintf(title, 512, "chr6 %d c%d %d:%d^%d%c", b->size, b->color, HOR, VER, ZOOM, b->edit ? '*' : ' ');
	snprintf(title, 512, "%s%dc%d [%d:%dx%d]%c",
	         modes[b->mode],
	         b->size,
	         b->color,
	         HOR,
	         VER,
	         ZOOM,
	         b->edit ? '*' : ' ');
	SDL_SetWindowTitle(gWindow, title);
}



@@ 268,21 252,16 @@ render(void)
	SDL_Surface* surface = SDL_GetWindowSurface(gWindow);
	GUIDES = 0;
	redraw(pixels);
	SDL_RenderReadPixels(gRenderer, NULL, SDL_PIXELFORMAT_ARGB8888, surface->pixels, surface->pitch);
	SDL_RenderReadPixels(gRenderer,
	                     NULL,
	                     SDL_PIXELFORMAT_ARGB8888,
	                     surface->pixels,
	                     surface->pitch);
	SDL_SaveBMP(surface, "render.bmp");
	SDL_FreeSurface(surface);
}

void
clear(void)
{
	int i;
	for(i = 0; i < SZ; i++)
		buffer[i] = color1;
	redraw(pixels);
}

void
quit(void)
{
	free(pixels);


@@ 354,6 333,9 @@ dokey(SDL_Event* event, Brush* b)
		GUIDES = !GUIDES;
		redraw(pixels);
		break;
	case SDLK_n:
		create();
		break;
	case SDLK_1:
		b->mode = 0;
		break;


@@ 383,9 365,6 @@ dokey(SDL_Event* event, Brush* b)
		if(b->size < 30)
			b->size += 1;
		break;
	case SDLK_n:
		clear();
		break;
	}
	update(b);
}


@@ 395,28 374,28 @@ init(void)
{
	int i, j;
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		return error("init", SDL_GetError());
	gWindow = SDL_CreateWindow("chr66",
		return error("Init", SDL_GetError());
	gWindow = SDL_CreateWindow("nasu",
	                           SDL_WINDOWPOS_UNDEFINED,
	                           SDL_WINDOWPOS_UNDEFINED,
	                           WIDTH,
	                           HEIGHT,
	                           SDL_WINDOW_SHOWN);
	if(gWindow == NULL)
		return error("window", SDL_GetError());
		return error("Window", SDL_GetError());
	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
	if(gRenderer == NULL)
		return error("renderer", SDL_GetError());
		return error("Renderer", SDL_GetError());
	gTexture = SDL_CreateTexture(gRenderer,
	                             SDL_PIXELFORMAT_ARGB8888,
	                             SDL_TEXTUREACCESS_STATIC,
	                             WIDTH,
	                             HEIGHT);
	if(gTexture == NULL)
		return error("texture", SDL_GetError());
		return error("Texture", SDL_GetError());
	pixels = (uint32_t*)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
	if(pixels == NULL)
		return error("pixels", "failed to allocate memory");
		return error("Pixels", "Failed to allocate memory");
	for(i = 0; i < HEIGHT; i++)
		for(j = 0; j < WIDTH; j++)
			pixels[i * WIDTH + j] = color1;


@@ 435,7 414,7 @@ main(int argc, char** argv)
	brush.mode = 2;

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

	if(argc > 1)
		load(argv[1]);