~rabbits/nasu

33b93714457151914150c59d955e98fac47e8b34 — Devine Lu Linvega 2 years ago f2723aa
close file after read/write
2 files changed, 37 insertions(+), 33 deletions(-)

M README.md
M chr6.c
M README.md => README.md +1 -1
@@ 27,4 27,4 @@ sudo apt-get install libsdl2-dev
### Paint

- `mouse1` Paint
- `mouse1+mouse2` Erase(Color1)
- `mouse2` Erase

M chr6.c => chr6.c +36 -32
@@ 139,19 139,42 @@ erase(Brush* b)
	redraw();
}

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

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

void
save(void)
{
	fwrite(buffer, sizeof(buffer), 1,
	       fopen("output.chr", "wb"));
	FILE* f = fopen("output.chr", "wb");
	if(!fwrite(buffer, sizeof(buffer), 1, f))
		error("Save", "Invalid output file");
	fclose(f);
	SDL_SetWindowTitle(gWindow, "chr6");
}

int
error(char* msg, const char* err)
void
load(char* path)
{
	printf("Error %s: %s\n", msg, err);
	return 0;
	FILE* f = fopen(path, "rb");
	if(f == NULL)
		error("Load", "Invalid input file");
	if(!fread(buffer, sizeof(buffer), 1, f))
		error("Load", "Invalid file size");
	fclose(f);
	redraw();
}

void


@@ 169,14 192,16 @@ quit(void)
}

void
mousehandler(SDL_Event* event, Brush* b)
domouse(SDL_Event* event, Brush* b)
{
	switch(event->type) {
	case SDL_MOUSEBUTTONUP:
		if(event->button.button == SDL_BUTTON_LEFT)
			b->down = 0;
		if(event->button.button == SDL_BUTTON_RIGHT)
		if(event->button.button == SDL_BUTTON_RIGHT) {
			setpt(&b->pos, event->motion.x, event->motion.y);
			erase(b);
		}
		break;
	case SDL_MOUSEBUTTONDOWN:
		if(event->button.button == SDL_BUTTON_LEFT)


@@ 191,7 216,7 @@ mousehandler(SDL_Event* event, Brush* b)
}

void
keyhandler(SDL_Event* event, Brush* b)
dokey(SDL_Event* event, Brush* b)
{
	switch(event->key.keysym.sym) {
	case SDLK_ESCAPE:


@@ 220,27 245,6 @@ keyhandler(SDL_Event* event, Brush* b)
}

int
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("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)
{
	int i, j;


@@ 304,9 308,9 @@ main(int argc, char** argv)
			if(event.type == SDL_MOUSEBUTTONUP ||
			   event.type == SDL_MOUSEBUTTONDOWN ||
			   event.type == SDL_MOUSEMOTION) {
				mousehandler(&event, &brush);
				domouse(&event, &brush);
			} else if(event.type == SDL_KEYDOWN)
				keyhandler(&event, &brush);
				dokey(&event, &brush);
		}
	}
	quit();