Changeset 33297 in project for wiki/Wrapping simple c structs


Ignore:
Timestamp:
04/18/16 01:16:12 (5 years ago)
Author:
John Foerch
Message:

Wrapping simple structs: how to do it with foreigners

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/Wrapping simple c structs

    r31663 r33297  
    3434
    3535</enscript>
     36
     37
     38; Using Foreigners
     39
     40Here is another way to wrap the same struct, this time using define-foreign-record-type from foreigners.
     41
     42<enscript highlight="scheme">
     43(use foreigners)
     44
     45(foreign-declare "#include <SDL/SDL.h>")
     46
     47(define-foreign-record-type (sdl-rect SDL_Rect)
     48  (short x sdl-rect-x sdl-rect-x-set!)
     49  (short y sdl-rect-y sdl-rect-y-set!)
     50  (unsigned-short w sdl-rect-w sdl-rect-w-set!)
     51  (unsigned-short h sdl-rect-h sdl-rect-h-set!))
     52
     53(define make-sdl-rect
     54  (foreign-lambda* sdl-rect
     55      ((short x) (short y) (unsigned-short w) (unsigned-short h))
     56    "SDL_Rect* r = (SDL_Rect*)malloc(sizeof(SDL_Rect));"
     57    "r->x = x;"
     58    "r->y = y;"
     59    "r->w = w;"
     60    "r->h = h;"
     61    "C_return(r);"))
     62
     63(define a (make-sdl-rect 10 20 30 40))
     64
     65(print a ": " (sdl-rect-x a) " " (sdl-rect-y a) " "
     66       (sdl-rect-w a) " " (sdl-rect-h a) " ")
     67
     68(sdl-rect-x-set! a 5)
     69(sdl-rect-y-set! a 10)
     70(sdl-rect-w-set! a 15)
     71(sdl-rect-h-set! a 20)
     72
     73(print a ": " (sdl-rect-x a) " " (sdl-rect-y a) " "
     74       (sdl-rect-w a) " " (sdl-rect-h a) " ")
     75</enscript>
Note: See TracChangeset for help on using the changeset viewer.