From: Harishankar Date: Thu, 30 Apr 2020 09:46:20 +0000 (+0530) Subject: Added Puzzle export as PNG image X-Git-Tag: 0.1a~22 X-Git-Url: https://harishankar.org/repos/?a=commitdiff_plain;h=57adeb73762dac7590dcac1b2d1e462d50e8565d;p=wordblah.git Added Puzzle export as PNG image Added feature to export puzzle and solution as PNG image using the libgd library. Also added a Makefile to the project --- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c471226 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +wordblox: wordblox.c + clang wordblox.c -lgd -o wordblox + diff --git a/constantstrings.h b/constantstrings.h index eb4270f..050c890 100644 --- a/constantstrings.h +++ b/constantstrings.h @@ -14,6 +14,7 @@ #define PUZZLE_MENU_TITLE "Edit Puzzle" #define MAIN_MENU_TITLE "Main Menu" #define INPUT_GRID_SIZE "Number of rows/columns: " +#define INPUT_EXPORT_ANSWERS "Export as solution (y/N): " #define INPUT_CHOICE "Your Choice: " #define EXCEED_MAX_GRID_SIZE "Exceeds max puzzle size" #define ERROR_WRITING_FILE "Error writing file" @@ -24,6 +25,11 @@ #define INPUT_CONFIRM_EXIT "Are you sure you wish to exit? \ Unsaved changes will be lost [y/N]" +#define USAGE_LINE_1 "Usage: %s [ [new ]]\n" +#define USAGE_LINE_2 " - puzzle file name\n" +#define USAGE_LINE_3 "new - create new puzzle with grid \ +columns (warning: existing file name may be overwritten)\n" + char *MAIN_MENU[] = {"1. New puzzle", "2. Open existing puzzle", @@ -39,6 +45,7 @@ char *PUZZLE_EDIT_MENU[] = "7. Set Clue - Across Word", "8. Set Clue - Down Word", "9. Save puzzle", - "10.Return to main menu" }; + "10.Export puzzle as PNG image", + "11.Return to main menu" }; #endif diff --git a/wordblox.c b/wordblox.c index ffbf471..223dde7 100644 --- a/wordblox.c +++ b/wordblox.c @@ -8,6 +8,32 @@ #include "wordblox.h" #include "constantstrings.h" +/* export the puzzle to a png file */ +void export_puzzle (Puzzle *p) +{ + if (p->grid_frozen == false) + { + printf (UNFROZEN_GRID); + char ch = getchar (); + return; + } + char fname[256]; + printf (INPUT_FILE); + fgets (fname, 256, stdin); + char* filename = strtok (fname, "\n"); + + printf (INPUT_EXPORT_ANSWERS); + char ans[3]; + fgets (ans, 3, stdin); + bool solution; + solution = (toupper (ans[0]) == 'Y') ? true : false; + + export_grid_image (p, filename, solution); + printf (FILE_SAVED); + char ch = getchar (); +} + +/* set clue for a word - only for frozen grid */ void set_clue_word (Puzzle *p, enum ORIENTATION orient) { print_puzzle (p); @@ -29,7 +55,10 @@ void set_clue_word (Puzzle *p, enum ORIENTATION orient) res = set_clue (p, cl, index, orient); if (res == false) + { printf (NO_WORD_INDEX); + char ch = getchar (); + } } /* clear a cell in the grid */ @@ -117,7 +146,7 @@ void add_across_word (Puzzle *p) char wd[MAX_PUZZLE_SIZE]; int row, col; printf (INPUT_WORD); - fgets (wd, p->grid_size, stdin); + fgets (wd, MAX_PUZZLE_SIZE, stdin); char *word = strtok (wd, "\n"); if (! is_valid_word (word)) { @@ -153,8 +182,8 @@ void add_across_word (Puzzle *p) bool confirm_exit () { printf (INPUT_CONFIRM_EXIT); - char res[2]; - fgets (res, 2, stdin); + char res[3]; + fgets (res, 3, stdin); if (toupper(res[0]) == 'Y') return true; else @@ -167,7 +196,7 @@ void puzzle_editor_loop (Puzzle *p, const char *filename) bool loop = true; while (loop) { - print_menu (WHITE, RED, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 10, 50); + print_menu (WHITE, RED, PUZZLE_MENU_TITLE, PUZZLE_EDIT_MENU, 11, 50); printf (INPUT_CHOICE); int ch = get_num (); switch (ch) @@ -190,19 +219,17 @@ void puzzle_editor_loop (Puzzle *p, const char *filename) ch = getchar (); break; case 7: set_clue_word (p, ACROSS); - print_puzzle (p); - ch = getchar (); break; case 8: set_clue_word (p, DOWN); - print_puzzle (p); - ch = getchar (); break; case 9: save_puzzle (p, filename); printf ("%s\n",FILE_SAVED); ch = getchar (); break; - case 10: loop = !confirm_exit (); - break; + case 10: export_puzzle (p); + break; + case 11: loop = !confirm_exit (); + break; } } } @@ -263,6 +290,27 @@ int main_loop () } int main (int argc, char* argv[]) -{ +{ + if (argc >= 2) + { + Puzzle p; + switch (argc) + { + case 2 : p = load_puzzle (argv[1]); + puzzle_editor_loop (&p, argv[1]); + break; + case 4 : if (strcmp (argv[2], "new") == 0) + { + int grid_size = atoi (argv[3]); + init_puzzle (&p, grid_size); + puzzle_editor_loop (&p, argv[1]); + break; + } + default: fprintf (stderr, USAGE_LINE_1, argv[0]); + fprintf (stderr, USAGE_LINE_2); + fprintf (stderr, USAGE_LINE_3); + exit (3); + } + } return (main_loop ()); } diff --git a/wordblox.h b/wordblox.h index f7cd6d2..53c522b 100644 --- a/wordblox.h +++ b/wordblox.h @@ -1,6 +1,9 @@ #ifndef __WORDBLOX_H #define __WORDBLOX_H +#include +#include +#include #include "constantstrings.h" #define MAX_PUZZLE_SIZE 20 @@ -41,6 +44,7 @@ typedef struct { bool grid_frozen; } Puzzle; +/* get a number from the user */ int get_num () { char s[5]; @@ -49,6 +53,73 @@ int get_num () return n; } +/* Output the grid to image - if answerkey is true export filled grid */ +void export_grid_image (Puzzle *p, const char *filename, bool answerkey) +{ + int img_size = p->grid_size * 40; + FILE * outfile = fopen (filename, "wb"); + if (outfile == NULL) + { + fprintf (stderr, "%s\n", ERROR_WRITING_FILE); + exit (1); + } + + gdImagePtr img = gdImageCreate (img_size, img_size); + int white = gdImageColorAllocate (img, 255,255,255); + int black = gdImageColorAllocate (img, 0, 0, 0); + int blue = gdImageColorAllocate (img, 0, 0, 216); + gdFontPtr sm_fnt = gdFontGetMediumBold (); + gdFontPtr lg_fnt = gdFontGetGiant (); + + for (int i = 0; i < p->grid_size; i ++) + { + for (int j = 0; j < p->grid_size; j++) + { + /* if it is a block, draw the black square */ + if (p->chars[i][j] == '#') + gdImageFilledRectangle (img, j*40, i*40, j*40+40, + i*40+40,black); + else + { + /* draw a regular square */ + gdImageRectangle (img, j*40, i*40, j*40+40, + i*40+40, black); + + /* print the numers, if it is either start across word or + a down word */ + if (p->start_across_word[i][j] != -1 || + p->start_down_word[i][j] != -1) + { + if (p->start_across_word[i][j] != -1) + { + char str[5]; + sprintf (str, "%d", p->start_across_word[i][j]); + gdImageString (img, sm_fnt, j*40+2, i*40+2, + (unsigned char *)str, blue); + } + else + { + char str[5]; + sprintf (str, "%d", p->start_down_word[i][j]); + gdImageString (img, sm_fnt, j*40+2, i*40+2, + (unsigned char *)str, blue); + } + } + /* if answerkey is true, draw the character in the cell */ + if (answerkey) + { + gdImageChar (img, lg_fnt, j*40+15, i*40+15, + p->chars[i][j], black); + } + } + } + } + + gdImagePng (img, outfile); + gdImageDestroy (img); + fclose (outfile); +} + /* Set the terminal colour */ void set_color (enum COLOR fg, enum COLOR bg, enum ATTR at) { printf ("\x1B[%d;%d;%dm", fg+30, bg+40, at);