I'm trying binding libgd library with Nim, but I'm facing a problem with 
pointers.

My interface: 
    
    
    # https://libgd.github.io/manuals/2.2.5/files/gd-h.html#gdPoint
    
    type
      gdImagePtr* = ref object
    
    type
      gdPoint* = object
        x*: int
        y*: int
    
    proc gdImageCreate*(width: int, height: int): gdImagePtr {.importc, dynlib: 
"/usr/lib/libgd.so".}
    proc gdImageDestroy*(im: gdImagePtr): void {.importc, dynlib: 
"/usr/lib/libgd.so".}
    #
    # These functions mimic the original functions of the library and it is in 
these functions that I have problems.
    #
    proc gdImagePolygon*(im: gdImagePtr, p: gdPointPtr, n: int, c: int): void 
{.importc, dynlib: "/usr/lib/libgd.so".}
    proc gdImageOpenPolygon*(im: gdImagePtr, p: gdPointPtr, n: int, c: int): 
void {.importc, dynlib: "/usr/lib/libgd.so".}
    proc gdImageFilledPolygon*(im: gdImagePtr, p: gdPointPtr, n: int, c: int): 
void {.importc, dynlib: "/usr/lib/libgd.so".}
    
    # I skipped many functions
    
    template withGd*(img: untyped, width: int, height: int, body: untyped): 
typed =
      block gd:
        let img = gdImageCreate(width, height)
        body
        gdImageDestroy(img)
    
    
    Run
    
    
    # Polygon
    withGd im, 500, 500:
      var points: array[4, gdPoint]
      points[0].x = 50
      points[0].y = 50
      points[1].x = 250
      points[1].y = 50
      points[2].x = 250
      points[2].y = 200
      points[3].x = 50
      points[3].y = 200
      
      let white = im.color(255, 255, 255)
      let red = im.color(255, 0, 0)
      im.gdImagePolygon(addr points, points.len, red)
      let png_out = open("outputs/test_polygon.png", fmWrite)
      im.save(png_out, extension="png")
      png_out.close()
    
    
    Run

I need to pass a pointer to the array of points. So I replace gdImagePtr with 
pointer type. 
    
    
    # Original
    proc gdImagePolygon*(im: gdImagePtr, p: gdPointPtr, n: int, c: int): void 
{.importc, dynlib: "/usr/lib/libgd.so".}
    # I replace the gdPointPtr with pointer type
    proc gdImagePolygon*(im: gdImagePtr, p: pointer, n: int, c: int): void 
{.importc, dynlib: "/usr/lib/libgd.so".}
    
    
    Run

The code compiles, but does not draw the lines properly, only one line appears 
at the top. What is wrong?

Reply via email to