diff --git a/README.md b/README.md index 833599f..059735d 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,4 @@ sudo apt-get install libsdl2-dev ### Paint - `mouse1` Paint -- `mouse1+mouse2` Erase(Color1) +- `mouse2` Erase diff --git a/chr6.c b/chr6.c index 4154d70..5bb460e 100644 --- a/chr6.c +++ b/chr6.c @@ -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: @@ -219,27 +244,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) { @@ -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();