From: Jeff
Subject: Simple macro with lots of options
Date: 
Message-ID: <Hbwld.28975$V41.2086@attbi_s52>
I'm wondering if anyone has any suggestions as to how and go about
making the following macro act more like a DEFPACKAGE-style macro:

(defmacro with-allegro ((&key (width 640) 
                              (height 480)
                              (depth 32) 
                              (fullscreen) 
                              (keyboard t) 
                              (mouse t)
                              (title))
                        &body body)
  (let ((install-progn (append (when title `((set-window-title ,title)))
                               (when keyboard '((install-keyboard)))
                               (when mouse '((install-mouse))))))
    `(when (allegro-init) ,@install-progn
       (set-color-depth ,depth)
       (when (zerop (set-gfx-mode (if ,fullscreen 1 2) 
                                  ,width 
                                  ,height 
                                  0 
                                  0))
         ,@body)
       (allegro-exit))))

Right now, to use this function, I do something like this:

(with-allegro (:keyboard nil
               :width 800
               :height 600
               :fullscreen t
               :title "My simple app")
  (do-stuff-here))

This just feels unwieldy, and I was wondering how I could go about
doing two things:

1. Make it more DEFPACKAGE-like:

   (with-allegro (:width 800)
                 (:height 600)
     (do-stuff-here))

2. Fix up INSTALL-PROGN to be a lot more elegant. Right now it feels
very much like a hack. The basic premise is that if TITLE is NIL, then
don't call SET-WINDOW-TITLE, etc.

Jeff M.

-- 
(surf-to "http://www.retrobyte.org/")

From: Pascal Bourguignon
Subject: Re: Simple macro with lots of options
Date: 
Message-ID: <87mzxkpvj3.fsf@thalassa.informatimago.com>
"Jeff" <·······@gmail.com> writes:

> I'm wondering if anyone has any suggestions as to how and go about
> making the following macro act more like a DEFPACKAGE-style macro:
> 
> (defmacro with-allegro ((&key (width 640) 
>                               (height 480)
>                               (depth 32) 
>                               (fullscreen) 
>                               (keyboard t) 
>                               (mouse t)
>                               (title))
>                         &body body)
>   (let ((install-progn (append (when title `((set-window-title ,title)))
>                                (when keyboard '((install-keyboard)))
>                                (when mouse '((install-mouse))))))
>     `(when (allegro-init) ,@install-progn
>        (set-color-depth ,depth)
>        (when (zerop (set-gfx-mode (if ,fullscreen 1 2) 
>                                   ,width 
>                                   ,height 
>                                   0 
>                                   0))
>          ,@body)
>        (allegro-exit))))
> 
> Right now, to use this function, I do something like this:
> 
> (with-allegro (:keyboard nil
>                :width 800
>                :height 600
>                :fullscreen t
>                :title "My simple app")
>   (do-stuff-here))
> 
> This just feels unwieldy, and I was wondering how I could go about
> doing two things:
> 
> 1. Make it more DEFPACKAGE-like:
> 
>    (with-allegro (:width 800)
>                  (:height 600)
>      (do-stuff-here))

(defmacro with-allegro-like-defpackage ((&rest options) &body body) ...)
(defmacro with-allegro-like-defpackage (&rest options-and-body) ...)

You'll have to parse the options and body yourself...

Add more default options: (fullscreen t) (title "Untitled").
Your macro is like WITH-OPEN-FILE, which is nice. 


> 2. Fix up INSTALL-PROGN to be a lot more elegant. Right now it feels
> very much like a hack. The basic premise is that if TITLE is NIL, then
> don't call SET-WINDOW-TITLE, etc.
You can write it like that:

    `(when (allegro-init)
        ,@(when title    `((set-window-title ,title)))
        ,@(when keyboard '((install-keyboard)))
        ,@(when mouse    '((install-mouse))))
        (set-color-depth ,depth) 
        ...)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
From: Jeff
Subject: Re: Simple macro with lots of options
Date: 
Message-ID: <uYKld.336827$wV.267210@attbi_s54>
Pascal Bourguignon wrote:

> You can write it like that:
> 
>     `(when (allegro-init)
>         ,@(when title    `((set-window-title ,title)))
>         ,@(when keyboard '((install-keyboard)))
>         ,@(when mouse    '((install-mouse))))
>         (set-color-depth ,depth) 
>         ...)

Thanks, Pascal.

Jeff M.

-- 
(surf-to "http://www.retrobyte.org/")
From: Wade Humeniuk
Subject: Re: Simple macro with lots of options
Date: 
Message-ID: <epLld.85507$E93.20075@clgrps12>
Jeff wrote:
> I'm wondering if anyone has any suggestions as to how and go about
> making the following macro act more like a DEFPACKAGE-style macro:
> 

Not so much like defpackage, but as more functional style.  defpackage
does not have a body so it can get away with that style.  You are
better off grouping your args in a list.

(defun set-allegro-interface (&key (width 640) (height 480) (depth 32)
                                    fullscreen (keyboard t) (mouse t) title)
   (when title (set-window-title title))
   (when keyboard (install-keyboard))
   (when mouse (install-mouse))
   (set-color-depth depth)
   (set-gfx-mode (if fullscreen 1 2) width height 0 0))

(defmacro with-allegro (interface-args &body body)
     `(when (allegro-init)
        (when (zerop (set-allegro-interface ,@interface-args))
          ,@body)
        (allegro-exit)))

Wade
From: Wade Humeniuk
Subject: Re: Simple macro with lots of options
Date: 
Message-ID: <YALld.85508$E93.29408@clgrps12>
Change that sightly,

(defun set-allegro-interface (&key (width 640) (height 480) (depth 32)
                                    fullscreen (keyboard t) (mouse t) title)
   (when title (set-window-title title))
   (when keyboard (install-keyboard))
   (when mouse (install-mouse))
   (set-color-depth depth)
   (zerop (set-gfx-mode (if fullscreen 1 2) width height 0 0)))

(defmacro with-allegro (interface-args &body body)
     `(when (allegro-init)
        (when (set-allegro-interface ,@interface-args)
          ,@body)
        (allegro-exit)))

Wade