~rabbits/nasu

3fd89bd0029bb309a4e8ffc7cc860a9a1cf899fa — neauoire 2 years ago 3f7c9f4
Updated format
2 files changed, 64 insertions(+), 89 deletions(-)

M README.md
M nasu.c
M README.md => README.md +1 -1
@@ 7,7 7,7 @@ A minimal chr editor, written in ANSI C.
To build nasu, you must have [SDL2](https://wiki.libsdl.org/).

```
sudo apt-get install libsdl2-dev
cc nasu.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -o nasu
```

## I/O

M nasu.c => nasu.c +63 -88
@@ 24,15 24,15 @@ typedef struct Brush {
	int erase;
} Brush;

char* modes[] = {
    "line",
    "tone",
    "bold",
    "full",
    "hori",
    "veri",
    "exes",
    "fixe"};
char *modes[] = {
	"line",
	"tone",
	"bold",
	"full",
	"hori",
	"veri",
	"exes",
	"fixe"};

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


@@ 40,10 40,10 @@ int WIDTH = 8 * HOR + PAD * 2;
int HEIGHT = 8 * VER + PAD * 2;
int FPS = 30;
int GUIDES = 0;
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
SDL_Texture* gTexture = NULL;
uint32_t* pixels;
SDL_Window *gWindow = NULL;
SDL_Renderer *gRenderer = NULL;
SDL_Texture *gTexture = NULL;
uint32_t *pixels;

/* helpers */



@@ 54,7 54,7 @@ distance(int ax, int ay, int bx, int by)
}

int
spos(char* s, char* ss)
spos(char *s, char *ss)
{
	int a = 0, b = 0;
	while(s[a] != '\0') {


@@ 200,7 200,7 @@ line(int ax, int ay, int bx, int by, int color)
/* draw */

void
draw(uint32_t* dst)
draw(uint32_t *dst)
{
	int b, i, j, id = 0;
	for(b = 0; b < SZ; b += 16)


@@ 223,34 223,34 @@ draw(uint32_t* dst)
}

void
update(Brush* b)
update(Brush *b)
{
	if(b->edit)
		SDL_SetWindowTitle(gWindow, "Nasu*");
	else
		SDL_SetWindowTitle(gWindow, "Nasu");
	printf("%s %d:%d [%d:%dx%d]\n",
	       modes[b->mode],
	       b->size,
	       b->color,
	       HOR,
	       VER,
	       ZOOM);
		modes[b->mode],
		b->size,
		b->color,
		HOR,
		VER,
		ZOOM);
}

/* options */

int
error(char* msg, const char* err)
error(char *msg, const char *err)
{
	printf("Error %s: %s\n", msg, err);
	return 0;
}

void
loadchr(char* path)
loadchr(char *path)
{
	FILE* f = fopen(path, "rb");
	FILE *f = fopen(path, "rb");
	if(f == NULL)
		error("Load", "Invalid input file");
	if(!fread(chrbuf, sizeof(chrbuf), 1, f))


@@ 259,9 259,9 @@ loadchr(char* path)
}

void
loadbmp(char* path)
loadbmp(char *path)
{
	FILE* f = fopen(path, "rb");
	FILE *f = fopen(path, "rb");
	int i, width = HOR * 8, height = VER * 8, size = 3 * width * height;
	unsigned char header[54];
	unsigned char data[4096 * 256];


@@ 270,15 270,14 @@ loadbmp(char* path)
	if(!fread(data, sizeof(unsigned char), size, f))
		error("Load", "Invalid bmp body");
	for(i = 0; i < size; i += 3)
		putchr((i / 3) % width, height - (i / 3) / width - 1,
		       getclr(data[i + 2], data[i + 1], data[i]));
		putchr((i / 3) % width, height - (i / 3) / width - 1, getclr(data[i + 2], data[i + 1], data[i]));
	fclose(f);
}

void
tochr(Brush* b)
tochr(Brush *b)
{
	FILE* f = fopen("nasu-export.chr", "wb");
	FILE *f = fopen("nasu-export.chr", "wb");
	if(!fwrite(chrbuf, sizeof(chrbuf), 1, f))
		error("Save", "Invalid output file");
	fclose(f);


@@ 288,14 287,14 @@ tochr(Brush* b)
void
tobmp(void)
{
	SDL_Surface* surface = SDL_GetWindowSurface(gWindow);
	SDL_Surface *surface = SDL_GetWindowSurface(gWindow);
	GUIDES = 0;
	draw(pixels);
	SDL_RenderReadPixels(gRenderer,
	                     NULL,
	                     SDL_PIXELFORMAT_ARGB8888,
	                     surface->pixels,
	                     surface->pitch);
		NULL,
		SDL_PIXELFORMAT_ARGB8888,
		surface->pixels,
		surface->pitch);
	SDL_SaveBMP(surface, "nasu-render.bmp");
	SDL_FreeSurface(surface);
}


@@ 315,7 314,7 @@ quit(void)
}

void
domouse(SDL_Event* event, Brush* b)
domouse(SDL_Event *event, Brush *b)
{
	switch(event->type) {
	case SDL_MOUSEBUTTONUP:


@@ 362,21 361,21 @@ domouse(SDL_Event* event, Brush* b)
}

void
dokey(SDL_Event* event, Brush* b)
dokey(SDL_Event *event, Brush *b)
{
	switch(event->key.keysym.sym) {
	case SDLK_ESCAPE:
		quit();
		break;
	case SDLK_e:
		tochr(b);
		break;
	case SDLK_r:
		tobmp();
		break;
	case SDLK_TAB:
		b->color = b->color > 2 ? 0 : b->color + 1;
		break;
	case SDLK_ESCAPE: quit(); break;
	case SDLK_e: tochr(b); break;
	case SDLK_r: tobmp(); break;
	case SDLK_TAB: b->color = b->color > 2 ? 0 : b->color + 1; break;
	case SDLK_BACKQUOTE: b->mode = 7; break;
	case SDLK_1: b->mode = 0; break;
	case SDLK_2: b->mode = 1; break;
	case SDLK_3: b->mode = 2; break;
	case SDLK_4: b->mode = 3; break;
	case SDLK_5: b->mode = 4; break;
	case SDLK_6: b->mode = 5; break;
	case SDLK_7: b->mode = 6; break;
	case SDLK_h:
		GUIDES = !GUIDES;
		draw(pixels);


@@ 385,30 384,6 @@ dokey(SDL_Event* event, Brush* b)
		newchr();
		draw(pixels);
		break;
	case SDLK_BACKQUOTE:
		b->mode = 7;
		break;
	case SDLK_1:
		b->mode = 0;
		break;
	case SDLK_2:
		b->mode = 1;
		break;
	case SDLK_3:
		b->mode = 2;
		break;
	case SDLK_4:
		b->mode = 3;
		break;
	case SDLK_5:
		b->mode = 4;
		break;
	case SDLK_6:
		b->mode = 5;
		break;
	case SDLK_7:
		b->mode = 6;
		break;
	case SDLK_z:
		if(b->size > 1)
			b->size -= 1;


@@ 428,24 403,24 @@ init(void)
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
		return error("Init", SDL_GetError());
	gWindow = SDL_CreateWindow("Nasu",
	                           SDL_WINDOWPOS_UNDEFINED,
	                           SDL_WINDOWPOS_UNDEFINED,
	                           WIDTH * ZOOM,
	                           HEIGHT * ZOOM,
	                           SDL_WINDOW_SHOWN);
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		WIDTH * ZOOM,
		HEIGHT * ZOOM,
		SDL_WINDOW_SHOWN);
	if(gWindow == NULL)
		return error("Window", SDL_GetError());
	gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
	if(gRenderer == NULL)
		return error("Renderer", SDL_GetError());
	gTexture = SDL_CreateTexture(gRenderer,
	                             SDL_PIXELFORMAT_ARGB8888,
	                             SDL_TEXTUREACCESS_STATIC,
	                             WIDTH,
	                             HEIGHT);
		SDL_PIXELFORMAT_ARGB8888,
		SDL_TEXTUREACCESS_STATIC,
		WIDTH,
		HEIGHT);
	if(gTexture == NULL)
		return error("Texture", SDL_GetError());
	pixels = (uint32_t*)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
	pixels = (uint32_t *)malloc(WIDTH * HEIGHT * sizeof(uint32_t));
	if(pixels == NULL)
		return error("Pixels", "Failed to allocate memory");
	for(i = 0; i < HEIGHT; i++)


@@ 455,7 430,7 @@ init(void)
}

int
main(int argc, char** argv)
main(int argc, char **argv)
{
	int ticknext = 0;
	Brush brush;


@@ 463,7 438,7 @@ main(int argc, char** argv)
	brush.color = 1;
	brush.edit = 0;
	brush.size = 10;
	brush.mode = 2;
	brush.mode = 0;

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


@@ 488,8 463,8 @@ main(int argc, char** argv)
			if(event.type == SDL_QUIT)
				quit();
			else if(event.type == SDL_MOUSEBUTTONUP ||
			        event.type == SDL_MOUSEBUTTONDOWN ||
			        event.type == SDL_MOUSEMOTION) {
					event.type == SDL_MOUSEBUTTONDOWN ||
					event.type == SDL_MOUSEMOTION) {
				domouse(&event, &brush);
			} else if(event.type == SDL_KEYDOWN)
				dokey(&event, &brush);