~rabbits/nasu

54b4e3b9c5517de0abc008a363f20d03d2f51af0 — Devine Lu Linvega 2 years ago 351d127
Added bmp import
7 files changed, 59 insertions(+), 9 deletions(-)

M README.md
M build.sh
A example.bmp
R sprite.chr => example.chr
D nasu-export.chr
D nasu-render.bmp
M nasu.c
M README.md => README.md +9 -3
@@ 15,15 15,21 @@ sudo apt-get install libsdl2-dev
To resume working on a tileset:

```
./nasu export.chr
./nasu example.chr
```

Import a bmp:

```
./nasu example.bmp
```

## Controls

### IO

- `E` Export(export.chr)
- `R` Render(render.bmp)
- `E` Export(nasu-export.chr)
- `R` Render(nasu-render.bmp)

### General


M build.sh => build.sh +4 -3
@@ 5,10 5,11 @@ clang-format -i nasu.c
rm ./nasu

# debug(slow)
# 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 nasu.c -L/usr/local/lib -lSDL2 -o nasu
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 nasu.c -L/usr/local/lib -lSDL2 -o nasu

# build(fast)
cc nasu.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -o nasu
# cc nasu.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -L/usr/local/lib -lSDL2 -o nasu

# run
./nasu sprite.chr
./nasu example.chr
# ./nasu example.bmp

A example.bmp => example.bmp +0 -0
R sprite.chr => example.chr +0 -0
D nasu-export.chr => nasu-export.chr +0 -0
D nasu-render.bmp => nasu-render.bmp +0 -0
M nasu.c => nasu.c +46 -3
@@ 53,6 53,28 @@ distance(int ax, int ay, int bx, int by)
	return (bx - ax) * (bx - ax) + (by - ay) * (by - ay);
}

int
spos(char* s, char* ss)
{
	int a = 0, b = 0;
	while(s[a] != '\0') {
		if(s[a] == ss[b]) {
			if(ss[b + 1] == '\0')
				return a - b;
			b++;
		} else
			b = 0;
		a++;
	}
	return -1;
}

int
getclr(int r, int g, int b)
{
	return r && g && b && r == g && g == b ? 1 : r > g && r > b ? 2 : g > r && g > b ? 3 : 0;
}

/* chr */

int


@@ 226,7 248,7 @@ error(char* msg, const char* err)
}

void
load(char* path)
loadchr(char* path)
{
	FILE* f = fopen(path, "rb");
	if(f == NULL)


@@ 237,9 259,27 @@ load(char* path)
}

void
loadbmp(char* path)
{
	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];
	if(!fread(header, sizeof(unsigned char), 54, f))
		error("Load", "Invalid bmp header");
	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]));
	fclose(f);
}

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


@@ 430,10 470,13 @@ main(int argc, char** argv)
	if(!init())
		return error("Init", "Failure");

	if(argc > 1)
		load(argv[1]);
	if(argc > 1 && spos(argv[1], ".bmp") > -1)
		loadbmp(argv[1]);
	else if(argc > 1 && spos(argv[1], ".chr") > -1)
		loadchr(argv[1]);
	else
		newchr();

	draw(pixels);
	update(&brush);