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

Messages - oofoe

#46
newLISP and the O.S. /
September 17, 2007, 10:19:37 AM
Hi!



Just to let you know, the (read-headers) seems to do the trick! I guess I had oversimplified...



Thanks very much for looking at it!
#47
Anything else we might add? /
September 17, 2007, 08:16:50 AM
I'm interested in automatic header interpretation myself. The SDL libraries aren't terribly complicated, but it's a pain to sift through all that manually, especially the event return structure...



Don't people write M4 macro processor replacements before breakfast in Lispland? ;-) Maybe I should try that, but after lunch.
#48
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)
#49
newLISP and the O.S. / Thanks!
September 13, 2007, 12:24:35 PM
Quote from: "Lutz"I tested your http-conf.lsp on Win32. To make it work you have to add a (read-headers) routine to clear out the headers coming from the client browser.


I'll try it as soon as I get home today.



Thanks for checking it out!
#50
newLISP and the O.S. / Another data point...
September 04, 2007, 12:33:19 PM
Hi!



Tried the program out from my machine at work (NewLisp 9.2.0, XP SP2, FireFox 1.5, running with "newlisp test.lsp -httpd -d 80").  Same results, with the occasional "The connection to the server was reset while the page was loading." error page instead of the blank page.



Thanks for looking at it!
#51
newLISP and the O.S. /
September 03, 2007, 03:10:08 PM
Hi,


QuoteI don't see how your (send text) function can return nil. It will return, what the (print text) statement evaluates too, which is the last thing printed, which is text not nil.


OK, I will admit freely and unreservedly, that I am an idiot. That's what I get for trying to cut and paste an example from a larger chunk of code without trying it first.



If you still have any patience left, please take a look at this instead (complete example this time, and tested too!):


(define (httpd-conf path query)
  (if (= 0 (length path))
    (begin
     (setq text "This is some text.")
     (print "HTTP/1.0 200 OKrn")
     (print (format "Server: newLISP v.%d (%s)rn" (sys-info -1) ostype))
     (print "Content-type: text/plainrnrn")
     (print text)
     nil)
    (begin
      (println "Path is " path)
      path)
    )
)


This does terminate properly. However, sometimes I get a completely blank page (which is the problem I was complaining about in the first place). Using NewLisp 9.2.0.



Thanks!
#52
Hi,



Sorry to leave the (app:home) in there.  That just prints the home page content using the send and template function. I modified your httpd-conf as so:


(define (httpd-conf path query)
  (println path ":" query) ; Delete this line to see anything on IE.
  (if (= 0 (length path))
    (begin
      (set 'page (send (template "This is a simple test.")))
      (println "Page is " page)
      page)
    (begin
      (println "Path is " path)
      path)
    )
)


When I run it, I usually get a result that looks like this:



:
HTTP/1.0 200 OK
Server: newLISP v.6 (Win32)
Content-length: 169
Content-type: text/html

<DOCTYPE>
<html>
  <head>
    <title>Test</title>
  </head>

  <body>
    This is a simple test.
  </body>
</html>
HTTP/1.0 200 OK
Server: newLISP v.6 (Win32)
Content-length: 0
Content-type: text/html

nilPage is nil


So it seems that NewLisp is ignoring the nil returned by send and running its page processing anyway. It's very wierd. If I telnet to port 80, I get the same thing. I have also tried it in IE 6.0 (don't have 7 installed) and get variations of the same thing. Normally, I use FireFox 1.5 or 2.0.



I'm running NewLisp on Windows XP SP2, serving locally on port 80.



Thanks!
#53
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!
#54
Anything else we might add? / More fun with SDL...
August 13, 2007, 04:20:51 AM
Hi,



If you do a search for SDL on this message board, there's a reference to someone's project using the SDL_mixer, including a music file player. Unfortunately, the site seems to be down, but you might be able to find it in the Google or Wayback Machine cache.



The following is a little load file for using the SDL_image functionality in your SDL program. SDL_image adds loading support for a number of major image formats (specifically png) to SDL. Just grab the SDL_image.dll and drop it in next to your SDL.dll and SDL.lsp.



The neatest thing about it is that instead of having to say something like "IMG:IMG_Load" in your program, you can just do an "IMG:Load". This makes the calls look a little different from their C usage, but (I feel) that it is much more elegant -- and easier to use.



; SDL_image.lsp
; jrlf 2007-08-12
; Library import for SDL_image 1.2.6.
; See http://www.libsdl.org/projects/SDL_image/.
; To be used in conjunction with SDL.lsp.

(context 'IMG)

# Determine which library to use first.
(if (= (last (sys-info)) 6)
(constant 'library "SDL_image.dll")
(constant 'library "????.so.0") ; I don't know what the Linux one is.
)
(define-macro (provide _symbol)
  (let ((truename (string "IMG_" _symbol)))
    (import library truename)
    (constant _symbol (eval (sym truename))))
  )

; Functions.
(provide LoadTyped_RW)
(provide Load)
(provide Load_RW)

(provide isBMP)
(provide isGIF)
(provide isJPG)
(provide isLBM)
(provide isPCX)
(provide isPNG)
(provide isPNM)
(provide isTIF)
(provide isXCF)
(provide isXPM)
(provide isXV)

; XXX Not including individual loading functions for simplicity.

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

(context 'MAIN)


Is there a better place to upload code like this? I don't have a personal website at the moment that allows for file uploads.
#55
Anything else we might add? / Solved! Sorta...
August 12, 2007, 05:45:31 AM
Hi,



This should fix the above problem code:



(define (apply_surface x y from to)
  (letn ((info (unpack "lu lu lu lu" from))
         (width (info 2))
         (height (info 3)))
        (println "Image size is " width "x" height)
        (SDL:SDL_BlitSurface
; from (pack "d d u u" 0 0 width height) ; This works, but too explicit.
         from 0 ; This works like the C++ examples.
to (pack "d d u u" x y width height))
        )
  )


According to the example code I was working from (see http://lazyfoo.net/SDL_tutorials/lesson02/index.php">http://lazyfoo.net/SDL_tutorials/lesson02/index.php), I should be able to leave the source rectangle specification blank (set to NULL). However, my attempt to set this parameter to NULL using either NIL or "" (as I've seen in other NewLisp code that uses external libraries) didn't work.



I got it to work finally by explicitly setting the rectangle (as you can see in the first commented-out line). Then I realized that perhaps a straight zero might get passed as a NULL properly.



I now have a lovely blitted image that looks like the final one in the tutorial.



If you are keeping track, here are some additions to the SDL library from http://www.turtle.dds.nl/newlisp/SDL.lsp">http://www.turtle.dds.nl/newlisp/SDL.lsp :



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


Having the SDL_SaveBMP is particularly nice, since you can save screenshots easily.



Jos'h
#56
Hi,



Been playing with it more. Revised version that shows chunks of the image at the bottom of this post



Seems that the way to get the SDL_Surface width and height is just using unpack, viz:

(setq info (unpack "lu lu lu lu" testi))
(println "Image size is " (info 2) "x" (info 3))


So, I modified the code to do the blit with the appropriate image size (see below), but you still just see pieces of the image. Is this perhaps some sort of memory problem?



Thanks!


; 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)
  (letn ((info (unpack "lu lu lu lu" from))
         (width (info 2))
         (height (info 3)))
        (println "Image size is " width "x" height)
        (SDL:SDL_BlitSurface from nil to (pack "u u lu lu" x y width height))
        )
  )


(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)
#57
Anything else we might add? / More information...
August 10, 2007, 03:25:22 PM
Hi,



On further investigation, it seems that the bmp files *are* loading, but either not completely, or not blitting completely. This change to the apply_surface function should show pieces of the bitmaps:


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


Next thing to try (I think) is to see if I can get the loaded image information. However, the image data is stored in a dynamically allocated struct of type SDL_Surface (see SDL_video.h). I'm not quite sure how to get the information... Perhaps some combination of address and unpack...



Thanks!
#58
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)
#59
Anything else we might add? / Thanks!
September 29, 2005, 01:06:00 PM
Whoa! I don't think would have *ever* figured that one out...  Thanks very much for the explanation.



However, while it works now, this is somewhat inconvenient. It is very useful to be able to break apart the problems using objects inside of objects. Is there any other sort of namespace partitioning thing I could use for this, or is there some "Lisp-y" way that I should program that avoids this situation?



Thanks again!



Jos'h
#60
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]