Find path to package resources?

Discussion of Common Lisp

Find path to package resources?

Postby jakykong » Sun Dec 09, 2018 1:14 pm

Good morning!

As in my last thread, I'm working on a small game using cl-sdl2. At this point I've got a playable demo and I'm working out a few kinks.

The biggest is loading images. I have a small routine that loads sprites from *.png files, and memoizes the resultant textures.
Code: Select all
(defvar *sprites* nil)
(defun load-sprite (sprite-type renderer)
  (let* ((filename (concatenate 'string (symbol-name sprite-type) ".png"))
         (sprite (getf *sprites* sprite-type)))
    (unless sprite
      (setf sprite (sdl2:create-texture-from-surface renderer
                                                     (sdl2-image:load-image filename))
            (getf *sprites* sprite-type) sprite))
    sprite))

This routine works fine as long as the images are stored in the current path, but if I just start SLIME and eval, I get a path error. To get around that problem, I added this to the top of my source file initially:
Code: Select all
(sb-posix:chdir #P"/home/jakykong/lisp/mouse")

This works, but it's obviously not going to allow the code to move around. If I'm using SLIME, it's doubly annoying since starting SLIME from the current directory is inconvenient.

As a first pass at figuring out a better way to locate resources, I looked in the ASDF documentation to see if there's a way to find the source path for the current package, and the closest I could get is:

Code: Select all
(defun get-system-path (system)
  (asdf:component-pathname (asdf:find-system system)))

Which returns a path like #P"/home/jakykong/quicklisp/local-projects/name/"; I can either change directory there as before or use #'merge-pathnames when loading the image.

This feels like a hack that probably has pitfalls I'm not considering. Is there a more conventional way to locate external files?
jakykong
 
Posts: 5
Joined: Sat Jul 08, 2017 10:19 pm

Re: Find path to package resources?

Postby pjstirling » Sun Dec 09, 2018 1:33 pm

I use ASDF:SYSTEM-RELATIVE-PATHNAME (usually with a per-package wrapper) to directly create PATHNAMES when I need to do this.
pjstirling
 
Posts: 164
Joined: Sun Nov 28, 2010 4:21 pm

Re: Find path to package resources?

Postby jakykong » Sun Dec 09, 2018 2:33 pm

pjstirling wrote:I use ASDF:SYSTEM-RELATIVE-PATHNAME (usually with a per-package wrapper) to directly create PATHNAMES when I need to do this.


I hadn't spotted that function earlier. That's much cleaner. Thank you! :)
jakykong
 
Posts: 5
Joined: Sat Jul 08, 2017 10:19 pm


Return to Common Lisp

Who is online

Users browsing this forum: Bing [Bot] and 9 guests

cron