Page 1 of 1

A technique for managing namespace for C Libraries...

Posted: Thu Sep 13, 2007 7:53 pm
by oofoe
I've recently created NewLisp wrappers for SDL_image.dll and SDL_ttf.dll. I was not happy with the way the existing SDL.lsp wrapper was set up where you had to specify the namespace redundantly (e.g. "SDL:SDL_Init"), so I tried to come up with a slightly nicer representation as shown below. The benefit of this is that the naming is more natural ("TTF:Init" instead of "TTF:TTF_Init") and you're not doing much more work to specify it than you would otherwise. There is a slight startup penalty, but it doesn't seem to be significant so far.

In the example below, the original include file specs are shown for reference. Note that provide allows you to import multiple symbols at a time (although it doesn't deal with "cdecl"). Perhaps this macro is small and simple enough it could be customized per library as needed.

Does anyone have any opinions about this, or perhaps a better way to do it?


Code: Select all

; SDL_ttf.lsp
; jrlf 2007-09-13
; Library import for SDL_ttf 2.0.9
; The SDL_ttf library is distributed under the terms of the GNU LGPL license:
; The library source is available from the libraries page at the SDL website:
; To be used in conjunction with SDL.lsp.

(context 'TTF)

# Determine which library to use first.
(if (= (last (sys-info)) 6)
   (constant 'library (string MAIN:LIBPATH "SDL_ttf.dll"))
   (constant 'library "????.so.0") ; I don't know what the Linux one is.
(define-macro (provide)
  (dolist (_symbol (args))
    (constant _symbol (import library (string "TTF_" _symbol))))

; Constants.
(constant 'UNICODE_BOM_NATIVE 0xfeff
          'UNICODE_BOM_SWAPPED 0xfffe
          'STYLE_NORMAL 0x00
          'STYLE_BOLD 0x01
          'STYLE_ITALIC 0x02
          'STYLE_UNDERLINE 0x04

; Functions.

; extern DECLSPEC void SDLCALL TTF_ByteSwappedUNICODE(int swapped);
(provide ByteSwappedUNICODE)

; extern DECLSPEC int SDLCALL TTF_Init(void);
(provide Init)

; extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFont(const char *file, int ptsize);
; extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndex(const char *file, int ptsize, long index);
; extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize);
; extern DECLSPEC TTF_Font * SDLCALL TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index);
(provide OpenFont)
(provide OpenFontIndex)
(provide OpenFontRW)
(provide OpenFontIndexRW)

; extern DECLSPEC int SDLCALL TTF_GetFontStyle(const TTF_Font *font);
; extern DECLSPEC void SDLCALL TTF_SetFontStyle(TTF_Font *font, int style);
(provide GetFontStyle)
(provide SetFontStyle)

; extern DECLSPEC int SDLCALL TTF_FontHeight(const TTF_Font *font);
(provide FontHeight)

; extern DECLSPEC int SDLCALL TTF_FontAscent(const TTF_Font *font);
(provide FontAscent)

; extern DECLSPEC int SDLCALL TTF_FontLineSkip(const TTF_Font *font);
(provide FontLineSkip)

; extern DECLSPEC long SDLCALL TTF_FontFaces(const TTF_Font *font);
(provide FontFaces)

; extern DECLSPEC int SDLCALL TTF_FontFaceIsFixedWidth(const TTF_Font *font);
; extern DECLSPEC char * SDLCALL TTF_FontFaceFamilyName(const TTF_Font *font);
; extern DECLSPEC char * SDLCALL TTF_FontFaceStyleName(const TTF_Font *font);
(provide FontFaceIsFixedWidth

; See
; extern DECLSPEC int SDLCALL TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,
;				     int *minx, int *maxx, int *miny, int *maxy, int *advance);
(provide GlyphMetrics)

; extern DECLSPEC int SDLCALL TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h);
; extern DECLSPEC int SDLCALL TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h);
; extern DECLSPEC int SDLCALL TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h);
(provide SizeText

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Solid(TTF_Font *font,
; 				const char *text, SDL_Color fg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Solid(TTF_Font *font,
; 				const char *text, SDL_Color fg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Solid(TTF_Font *font,
; 				const Uint16 *text, SDL_Color fg);
(provide RenderText_Solid

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Solid(TTF_Font *font,
; 					Uint16 ch, SDL_Color fg);
(provide RenderGlyph_Solid)

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Shaded(TTF_Font *font,
; 				const char *text, SDL_Color fg, SDL_Color bg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Shaded(TTF_Font *font,
; 				const char *text, SDL_Color fg, SDL_Color bg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Shaded(TTF_Font *font,
; 				const Uint16 *text, SDL_Color fg, SDL_Color bg);
(provide RenderText_Shaded

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Shaded(TTF_Font *font,
; 				Uint16 ch, SDL_Color fg, SDL_Color bg);
(provide RenderGlyph_Shaded)

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderText_Blended(TTF_Font *font,
; 				const char *text, SDL_Color fg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUTF8_Blended(TTF_Font *font,
; 				const char *text, SDL_Color fg);
; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderUNICODE_Blended(TTF_Font *font,
; 				const Uint16 *text, SDL_Color fg);
(provide RenderText_Blended

; extern DECLSPEC SDL_Surface * SDLCALL TTF_RenderGlyph_Blended(TTF_Font *font,
; 						Uint16 ch, SDL_Color fg);
(provide RenderGlyph_Blended)

; #define TTF_RenderText(font, text, fg, bg)	\
; 	TTF_RenderText_Shaded(font, text, fg, bg)
; #define TTF_RenderUTF8(font, text, fg, bg)	\
; 	TTF_RenderUTF8_Shaded(font, text, fg, bg)
; #define TTF_RenderUNICODE(font, text, fg, bg)	\
; 	TTF_RenderUNICODE_Shaded(font, text, fg, bg)
(define (RenderText font text fg bg) (RenderText_Shaded font text fg bg))
(define (RenderUTF8 font text fg bg) (RenderUTF8_Shaded font text fg bg))
(define (RenderUNICODE font text fg bg) (RenderUNICODE_Shaded font text fg bg))

; extern DECLSPEC void SDLCALL TTF_CloseFont(TTF_Font *font);
(provide CloseFont)

; extern DECLSPEC void SDLCALL TTF_Quit(void);
(provide Quit)

; extern DECLSPEC int SDLCALL TTF_WasInit(void);
(provide WasInit)

; XXX It is possible that these won't work right. Need to check.
(constant 'SetError SDL:SDL_SetError)
(constant 'GetError SDL:SDL_GetError)