Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Topics - oofoe

#21
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?



Thanks!



; 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:
; http://www.gnu.org/copyleft/lesser.html
; The library source is available from the libraries page at the SDL website:
; http://www.libsdl.org/
; 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
         FontFaceFamilyName
         FontFaceStyleName)


; See http://freetype.sourceforge.net/freetype2/docs/tutorial/step2.html
; 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
         SizeUTF8
         SizeUNICODE)

; 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
         RenderUTF8_Solid
         RenderUNICODE_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
         RenderUTF8_Shaded
         RenderUNICODE_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
         RenderUTF8_Blended
         RenderUNICODE_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)
#22
Hi,



If I start newlisp in web server mode (with "-httpd -d 80") and use the following code, I occassionally get a blank page.  It's sort of like newlisp is ignoring the return value of httpd-conf and trying to send a page which doesn't exist. Here's what my httpd-conf looks like at the moment:



(define (send text)
  (print "HTTP/1.0 200 OKrn")
  (print (format "Server: newLISP v.%d (%s)rn" (sys-info -1) ostype))
  (print (format "Content-length: %drn" (length text)))
  (print "Content-type: text/htmlrnrn")
  (print text))

(define (template text)
  "( text -- nil) Print out filled in template."

  (send (replace "%content%" (read-file  "default.html") text))
  nil ; As specified in CodePatterns.html#distributed
  )

(define (httpd-conf path query)
  (if (= 0 (length path)) (app:home)
    path
    )
)


The idea is that a custom home page is displayed. Anything else (image requests, etc.) is sent as normal. The worst part about it is that it almost works... One of every four (or so) requests fails with a blank page. It may have something to do with request speed since I've tried manually accessing with telnet and it works every time.



Any ideas? Very frustrating.



Thanks!
#23
Anything else we might add? / Bitmaps in SDL...
August 10, 2007, 12:57:08 PM
Hi!



I'm transliterating the SDL code from this page into a NewLisp program:

http://lazyfoo.net/SDL_tutorials/lesson02/index.php">http://lazyfoo.net/SDL_tutorials/lesson02/index.php

I'm using NewLisp 9.1.1, SDL 1.2.8 and the SDL.lsp import file from http://www.turtle.dds.nl/newlisp/SDL.lsp">http://www.turtle.dds.nl/newlisp/SDL.lsp



However, for whatever reason, I can't get the bitmaps to load. Before, when I had a "home-grown" SDL/gl mix (ripped from the teapot example), I could see the top half of the "hello_world.bmp", but no background image.



Note that I supplied two functions which are defined with C macros in the SDL include files -- SDL_LoadBMP and SDL_BlitSurface.



Any thoughts?



Thanks!



Here's the code:


; l002.lsp
; jrlf 2007-08-10
; Surface loading and blitting.
; http://lazyfoo.net/SDL_tutorials/lesson02/index.php


; Load SDL library.
(load "SDL.lsp")

(context 'SDL)
(define (SDL_LoadBMP filename)
  (SDL:SDL_LoadBMP_RW (SDL:SDL_RWFromFile filename "rb") 1))
(define (SDL_BlitSurface src srcrect dst dstrect)
  (SDL_UpperBlit src srcrect dst dstrect))
(context 'MAIN)


; Screen attributes.
(constant 'screen_width 640)
(constant 'screen_height 480)
(constant 'screen_bpp 32)


(define (load_image filename)
  (let ((loaded (SDL:SDL_LoadBMP filename))
        (optimized nil))
    (if loaded (begin
                (setq optimized (SDL:SDL_DisplayFormat loaded))
                (SDL:SDL_FreeSurface loaded)
                ))
    optimized))


(define (apply_surface x y from to)
  (SDL:SDL_BlitSurface from "" to (pack "u u lu lu" x y 0 0))
)


(if (< (SDL:SDL_Init SDL:SDL_INIT_EVERYTHING) 0)
    (begin  (println "Could not initialize SDL!")  (exit)))
(if (< (setq screen (SDL:SDL_SetVideoMode screen_width screen_height
                                          screen_bpp SDL:SDL_SWSURFACE)) 0)
    (begin  (println "Couldn't initialize the screen!") (exit)))
(SDL:SDL_WM_SetCaption "Hello World" "")

(setq message (load_image "hello_world.bmp")
      background (load_image "background.bmp")
)
(apply_surface 0 0 background screen)
(apply_surface 180 140 message screen)

(if (< (SDL:SDL_Flip screen) 0)
    (begin
     (println "Couldn't flip screen!") (exit)))

(SDL:SDL_Delay 2000)

(SDL:SDL_FreeSurface message)
(SDL:SDL_FreeSurface background)
(SDL:SDL_Quit)

(exit)
#24
Anything else we might add? / Using objects in objects...
September 28, 2005, 12:26:13 PM
Hi,



I'm trying to use an object inside a context.  I can create objects in the MAIN context fine, but when I try to do the same thing inside another context, the compiler bails. Can anyone suggest what's going wrong?



Here's a snippet of source code](context 'sphere)

(setq centre '(0 0 0))
(setq radius 1)


(context 'render)

(define (go)
  (new 'sphere s)
  (setq s:radius 2)
  (print "Rendering a sphere of radius " s:radius ".n")
)


(context 'MAIN)

(new sphere 's)
(print "Radius of s is " s:radius ".n")

(new render 'r)
(r:go)

(exit)[/code]

Here's what a run looks like (Linux and Windows):


Radius of s is 1.

context expected in function new : nil
called from user defined function go


Thanks in advance!



Jos'h[/color]