guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ffi for glutInit


From: Nala Ginrut
Subject: Re: ffi for glutInit
Date: Fri, 27 Jul 2012 02:07:02 +0800

Hmm...Mark you enlightened me how to deal with 2 dimension array with
pure Guile code. thanks!
But I'll suggest Aleix to handle this issue with C code which is
easier.  Anyway, we don't have to do all the things with Guile code
since Guile is designed to interact with C code. We just choose the
way we like.
I think the principle is the same:
---------------pseudo code-----------------
// ac is integer ,av is a list of strings
SCM  scm_glut_init (SCM ac ,SCM av)
{
  int *argc = scm_gc_malloc(sizeof(int) ,"glut-init-argc");
  char **argv = NULL;
  SCM p;
  int i = 0;

  *argc = scm_to_int(ac);
  for(p = scm_car(av) ;i < *argc ;i++ ,p = scm_cdr(p))
   {
      argv[i] = scm_to_locale_string(p);
   }

  argv[*argc] = NULL;
  glut_init(argc ,argv);

  return SCM_UNSPECIFIED;
}
---------------end------------------

PS: I didn't guarantee the code above can be used directly, just
describe the principle.

On Fri, Jul 27, 2012 at 12:39 AM, Mark H Weaver <address@hidden> wrote:
> Sorry, the indentation of my example code got messed up by tabs.
> Here it is again:
>
>
>   (use-modules (system foreign))
>
>   (define libglut-obj (dynamic-link "libglut"))
>
>   ;; (glut-init args), where args is the complete list of command
>   ;; arguments (starting with the program name), calls glutInit and
>   ;; returns the (possibly) modified list of arguments.
>   (define glut-init
>     (let ((foo-init-raw (pointer->procedure
>                          void
>                          (dynamic-func "glutInit" libglut-obj)
>                          (list '* '*)))
>           (saved-c-strings '()))
>       (lambda (args)
>         (let* ((num-args (length args))
>                (c-strings (map string->pointer args))
>                (argcp (make-c-struct (list int)
>                                      (list num-args)))
>                (argv (make-c-struct (make-list (+ 1 num-args) '*)
>                                     (append c-strings
>                                             (list %null-pointer)))))
>           (set! saved-c-strings (append c-strings saved-c-strings))
>           (foo-init-raw argcp argv)
>           (let ((argc (car (parse-c-struct argcp (list int)))))
>             (map pointer->string
>                  (parse-c-struct argv
>                                  (make-list argc '*))))))))
>
>   ;; Example usage
>   (set-program-arguments (glut-init (program-arguments)))
>
>      Mark
>
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]