~rabbits/nasu

cb5001ce771ecb2074516bf5297f6318b27537a3 — Devine Lu Linvega 2 years ago 2c86051
Added export
4 files changed, 44 insertions(+), 22 deletions(-)

M .gitignore
M build.sh
M nasu6.c
D sprite2.chr
M .gitignore => .gitignore +2 -1
@@ 2,4 2,5 @@
*jpg~
*png~
*gif~
nasu6
\ No newline at end of file
nasu6
output.chr
\ No newline at end of file

M build.sh => build.sh +8 -2
@@ 1,8 1,14 @@

# format code
clang-format -i nasu6.c

# remove old
rm ./nasu6

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 nasu6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o nasu6
# 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 nasu6.c -I/usr/local/include -L/usr/local/lib -lSDL2 -o nasu6

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

# run
./nasu6 sprite.chr

M nasu6.c => nasu6.c +34 -19
@@ 55,10 55,11 @@ draw(uint32_t* dst, int id, int color)
	int px = (ti / 256) * 128;
	int tx = (ti % 16) * 8;
	int ty = ((ti / 16) * 8) % 128;
	int odd = (ti + (ti / 16 + 2)) % 2 == 0;
	Point p;
	p.x = px + tx + (id % 8);
	p.y = ty + ((id % 64) / 8);
	pixel(dst, p, colors[color]);
	pixel(dst, p, colors[odd && color == 0 ? 3 : color]);
}

void


@@ 111,6 112,7 @@ edit(Brush* b)
	    (p1.x / ZOOM) % 8,
	    (p1.y / ZOOM) % 8,
	    b->color);
	SDL_SetWindowTitle(gWindow, "nasu*");
	redraw();
}



@@ 127,12 129,16 @@ erase(Brush* b)
		buffer[(id * 16) + i] = 0x00;
		buffer[(id * 16) + i + 8] = 0x00;
	}
	SDL_SetWindowTitle(gWindow, "nasu*");
	redraw();
}

void export(uint32_t* dst)
void
save(void)
{
	/* TODO: chr file export */
	fwrite(buffer, sizeof(buffer), 1,
	       fopen("output.chr", "wb"));
	SDL_SetWindowTitle(gWindow, "nasu");
}

int


@@ 157,7 163,7 @@ quit(void)
}

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


@@ 179,14 185,14 @@ handle_mouse(SDL_Event* event, Brush* b)
}

void
handle_keypress(SDL_Event* event, Brush* b)
keyhandler(SDL_Event* event, Brush* b)
{
	switch(event->key.keysym.sym) {
	case SDLK_ESCAPE:
		quit();
		break;
	case SDLK_e:
		export(pixels);
		save();
		break;
	case SDLK_1:
		b->color = 0;


@@ 204,14 210,26 @@ handle_keypress(SDL_Event* event, Brush* b)
}

int
load(FILE* f)
load(char* path)
{
	FILE* f = fopen(path, "rb");
	if(f == NULL)
		return error("Load", "Invalid input file");
	if(!fread(buffer, sizeof(buffer), 1, f))
		return error("Invalid size file", "");
		return error("Load", "Invalid file size");
	redraw();
	return 1;
}

void
create(void)
{
	int i;
	for(i = 0; i < 4096; ++i)
		buffer[i] = 0x00;
	redraw();
}

int
init(void)
{


@@ 250,20 268,17 @@ main(int argc, char** argv)
{
	int ticknext = 0;
	Brush brush;
	FILE* f;
	brush.color = 1;

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

	if(argc < 2)
		return error("Missing input file", "");

	f = fopen(argv[1], "rb");

	if(f == NULL)
		return error("Invalid input file.", "");
	/* IO */

	load(f);
	if(argc > 1)
		load(argv[1]);
	else
		create();

	/* main loop */



@@ 281,9 296,9 @@ main(int argc, char** argv)
			if(event.type == SDL_MOUSEBUTTONUP ||
			   event.type == SDL_MOUSEBUTTONDOWN ||
			   event.type == SDL_MOUSEMOTION) {
				handle_mouse(&event, &brush);
				mousehandler(&event, &brush);
			} else if(event.type == SDL_KEYDOWN)
				handle_keypress(&event, &brush);
				keyhandler(&event, &brush);
		}
		SDL_RenderClear(gRenderer);
		SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);

D sprite2.chr => sprite2.chr +0 -0