Added Puzzle export as PNG image
[wordblah.git] / wordblox.h
index f7cd6d2..53c522b 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef __WORDBLOX_H
 #define __WORDBLOX_H
 
+#include <gd.h>
+#include <gdfontmb.h>
+#include <gdfontg.h>
 #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);