/* Bring in gd library functions */
#include "gd.h"

/* Bring in standard I/O so we can output the PNG to a file */
#include <stdio.h>

//string stuff
#include "gdfontl.h"
#include <string.h>

#define width  640
#define height 480

/* this cleans up things a little */

void inline rrect(gdImagePtr im, int x1, int y1, int x2, int y2, int color) ;
void battery (gdImagePtr im, float x, float y, float w, float h, int color) ;
void drawgrid (gdImagePtr im, int color) ;
void cell (gdImagePtr im, float x, float y, float w, float h, int color) ;


#define p2(A, B)  A, B

int main() {

  int n;
  
  char *s = "Battery";
  
  gdImagePtr im;
  
  int grid, background, object;
  
  int x, y;
  
  FILE *out;
  /* Create the image */
  im = gdImageCreate(width, height);

  /* Allocate background */
  background = gdImageColorAllocate(im, 0, 64, 128);

  /* Allocate drawing color */
  grid = gdImageColorAllocate(im, 128, 128, 255);
  
  /* Allocate drawing color */
  object = gdImageColorAllocate(im, 200, 200, 200);
  
//-------------------------| DRAW |----------------------------- 
  

  drawgrid(im, grid);
  
  
  
  x = im->sx / 2 - (strlen(s) * gdFontGetLarge()->w / 2);
  y = im->sy / 2 - gdFontGetLarge()->h / 2;
  
  gdImageString(im, gdFontGetLarge(), x, y, s, object);
  
  gdImageArc( im, p2( im->sx/2, im->sy/2), p2(64, 64), 0, 360, object);
  
  gdImageArc( im, p2(128, 96), p2(5, 5), 0, 360, object);
  
  gdImageLine(im, p2(x-32, y+8), p2(128, 96), object);
  gdImageLine(im, p2(x-32, y+8), p2(x-4, y+8), object);
      
//  battery (im, p2(64, 64), p2(128, 64), object);


  cell (im, p2(64, 64), p2(64, 96), object);


  
 // rrect(im, p(640/2-250, 20), p(500, 89), object);
 // rrect(im, p(640/2-150/2, 150), p(150, 89), object);
 
// line3d( im, p3(-10, 1, 10), p3(10, 1, 10), object);
 
// line3d( im, p3(10, 10, -.10), p3(10, 10, .10), object);
// line3d( im, p3(10, 10, .10), p3(-10, 10, .10), object);
// line3d( im, p3(-10, 10, .10), p3(-10, 10, -.10), object);

// gdImageSetPixel(gdImagePtr im, int x, int y, int color)

// gdImageLine(im, 10, 0, 10, 99, black);

// gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

// gdImageFilledPolygon(gdImagePtr im, gdPointPtr points, int pointsTotal, int color)

// gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)

   

// gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color)




//-------------------------------------

  /* Open output file in binary mode */
  out = fopen("rect.png", "wb");

  /* Write PNG */
  gdImagePng(im, out);

  /* Close file */
  fclose(out);

  /* Destroy image */
  gdImageDestroy(im);

}


//-----------------------------------| Helper functions |-----------------------------------

void drawgrid (gdImagePtr im, int color) {
  int n;

  for (n = 0; n <= im->sx; n += 32) {
   gdImageLine(im, p2(n, 0), p2(n, im->sy), color);
  }
  
  for (n = 0; n <= im->sy; n += 32) {
   gdImageLine(im, p2(0, n), p2(im->sx, n), color);
  }
}


// D cell
void cell (gdImagePtr im, float x, float y, float w, float h, int color) {

  float factor;
  factor = 0.2;
    
  gdImageArc ( im, p2( x + (w/2), y+(h*factor)/2),   p2( w,   (h*factor)),    0, 360, color);
  gdImageLine( im, p2( x,         y+(h*factor)/2),   p2( x,   y+h-(h*factor)/2), color);
  gdImageLine( im, p2( x+w,       y+(h*factor)/2),   p2( x+w, y+h-(h*factor)/2), color);
  gdImageArc ( im, p2( x + (w/2), y+h-(h*factor)/2), p2( w,   (h*factor)),    0, 180, color);
    
  //---------------------
  
  gdImageArc ( im, p2( x + (w/2), y+(h*factor)/2), p2( w/4,   (h*factor)/4),    0, 180, color); 
  gdImageLine( im, p2( x + (w/2)+(w/8), y+(h*factor)/2),   p2( x+(w/2)+(w/8), y+(h*factor)/2-w/8), color);
  gdImageLine( im, p2( x + (w/2)-(w/8), y+(h*factor)/2),   p2( x+(w/2)-(w/8), y+(h*factor)/2-w/8), color);  
  gdImageArc ( im, p2( x + (w/2), y+(h*factor)/2-w/8),   p2( w/4,   (h*factor)/4),    0, 360, color);
  
  
}


// box 
//                                64       64      128       64
void battery (gdImagePtr im, float x, float y, float w, float h, int color) {

    float factor;
    factor = .8;

    gdImageRectangle(im, p2(x, y+h*(1-factor)), p2(x+w*factor, y+h),   color);
    
    gdImageLine(im, p2(x, y+h*(1-factor)),   p2(x+w*(1-factor), y),        color);
    gdImageLine(im, p2(x+w*(1-factor), y),   p2(x+w, y),                 color);
    gdImageLine(im, p2(x+w, y),              p2(x+w, y+(h*factor)),      color);
    gdImageLine(im, p2(x+w, y+(h*factor)),   p2(x+(w*factor), y+h),      color);    
    gdImageLine(im, p2(x+w, y),              p2(x+(w*factor), y+h*(1-factor)), color);
}


void inline rrect(gdImagePtr im, int x1, int y1, int x2, int y2, int color) {
  gdImageRectangle(im, x1, y1, x1+x2, y1+y2, color);
}












