Lispbuilder-SDL:转动表面并保留颜色键(透明度)
Lispbuilder-SDL: turn surface and preserve color key (transparency)
解释
我正在使用 SDL 在 CL 中编写海龟图形应用程序。我创建了一张乌龟图并将其保存为 PNG 格式。问题 1:SDL:LOAD-IMAGE
不能很好地处理图像中的实际透明度。所以,我用白色给透明部分上色并做了
(sdl:load-image
(merge-pathnames #P"resources/turtle.png"
(asdf:system-source-directory :cl-turtle))
:image-type :png
:color-key sdl:*white*)
因此,:COLOR-KEY
选项表示所有 SDL:*WHITE*
像素都是透明的。这有效:在黄色背景上绘制乌龟时没有显示白色像素。
但是,我希望我的乌龟指向一个特定的方向并在对 TURN
命令的响应中改变它。我发现的一个建议是将精灵用于所有可能的角度,但这对海龟来说效果不佳:方向太多。
所以,我已经加载 LISPBUILDER-SDL-GFX
,以使用 SDL:ROTATE-SURFACE-XY
。它确实可以很好地旋转海龟。我可以看到,SDL:*WHITE*
设置为颜色键,并且在旋转表面中启用了颜色键控。但是,当我绘制它时,仍然呈现白色。有办法解决这个问题吗?
完整代码
(ql:quickload '(:lispbuilder-sdl :lispbuilder-sdl-image :lispbuilder-sdl-gfx :vom))
(defstruct turtle
direction
x
y
scale)
(defun draw-turtle (turtle image)
;; (format t "~&GFX? ~A~%" lispbuilder-sdl-cffi::*gfx-loaded-p*)
(let ((img (sdl:rotate-surface-xy (mod (turtle-direction turtle) 360)
:surface image)))
(setf (sdl::color-key img) sdl:*white*)
(setf (sdl:color-key-enabled-p img) t)
(vom:info "Color key enabled (img)?: ~A" (sdl:color-key-enabled-p img))
(vom:info "Color key is: ~A" (slot-value (sdl:color-key img) 'sdl::color-vector))
(sdl:draw-surface-at-* img
(turtle-x turtle)
(turtle-y turtle))))
(defun turtle ()
(sdl:with-init()
(sdl:window 500 500
:title-caption "Turtle")
(setf (sdl:frame-rate) 1)
(let ((turtle-image (sdl:load-image
(merge-pathnames #P"resources/turtle.png"
(asdf:system-source-directory :cl-turtle))
:image-type :png
:color-key sdl:*white*))
(turtle (make-turtle :direction 30 :x 200 :y 200 :scale 1)))
(sdl:with-events ()
(:quit-event () t)
(:key-down-event () (sdl:push-quit-event))
(:idle ()
(sdl:clear-display sdl:*yellow*)
(draw-turtle turtle turtle-image)
(sdl:update-display))))))
(turtle)
日志(VOM)输出
<INFO> [14:24:35] cl-turtle - Color key enabled (img)?: T
<INFO> [14:24:35] cl-turtle - Color key is: #(255 255 255)
截图
系统信息
Ubuntu 16.04 与 libSDL-1.2.so.0.11.4、libSDL_gfx.so.15.9.1 和 libSDL_image-1.2.so.0.8。 4
SBCL 1.3.7(来自 roswell sbcl-bin)
显然,调用 sdl:load-and-convert-image
而不是 sdl:load-image
可以解决问题。
Loads an image from the filename SOURCE as per LOAD-IMAGE-*, converts
this image to the current display format using SDL:CONVERT-SURFACE.
Parameters supported are the same as those for LOAD-IMAGE and
SDL:CONVERT-IMAGE.
解释
我正在使用 SDL 在 CL 中编写海龟图形应用程序。我创建了一张乌龟图并将其保存为 PNG 格式。问题 1:SDL:LOAD-IMAGE
不能很好地处理图像中的实际透明度。所以,我用白色给透明部分上色并做了
(sdl:load-image
(merge-pathnames #P"resources/turtle.png"
(asdf:system-source-directory :cl-turtle))
:image-type :png
:color-key sdl:*white*)
因此,:COLOR-KEY
选项表示所有 SDL:*WHITE*
像素都是透明的。这有效:在黄色背景上绘制乌龟时没有显示白色像素。
但是,我希望我的乌龟指向一个特定的方向并在对 TURN
命令的响应中改变它。我发现的一个建议是将精灵用于所有可能的角度,但这对海龟来说效果不佳:方向太多。
所以,我已经加载 LISPBUILDER-SDL-GFX
,以使用 SDL:ROTATE-SURFACE-XY
。它确实可以很好地旋转海龟。我可以看到,SDL:*WHITE*
设置为颜色键,并且在旋转表面中启用了颜色键控。但是,当我绘制它时,仍然呈现白色。有办法解决这个问题吗?
完整代码
(ql:quickload '(:lispbuilder-sdl :lispbuilder-sdl-image :lispbuilder-sdl-gfx :vom))
(defstruct turtle
direction
x
y
scale)
(defun draw-turtle (turtle image)
;; (format t "~&GFX? ~A~%" lispbuilder-sdl-cffi::*gfx-loaded-p*)
(let ((img (sdl:rotate-surface-xy (mod (turtle-direction turtle) 360)
:surface image)))
(setf (sdl::color-key img) sdl:*white*)
(setf (sdl:color-key-enabled-p img) t)
(vom:info "Color key enabled (img)?: ~A" (sdl:color-key-enabled-p img))
(vom:info "Color key is: ~A" (slot-value (sdl:color-key img) 'sdl::color-vector))
(sdl:draw-surface-at-* img
(turtle-x turtle)
(turtle-y turtle))))
(defun turtle ()
(sdl:with-init()
(sdl:window 500 500
:title-caption "Turtle")
(setf (sdl:frame-rate) 1)
(let ((turtle-image (sdl:load-image
(merge-pathnames #P"resources/turtle.png"
(asdf:system-source-directory :cl-turtle))
:image-type :png
:color-key sdl:*white*))
(turtle (make-turtle :direction 30 :x 200 :y 200 :scale 1)))
(sdl:with-events ()
(:quit-event () t)
(:key-down-event () (sdl:push-quit-event))
(:idle ()
(sdl:clear-display sdl:*yellow*)
(draw-turtle turtle turtle-image)
(sdl:update-display))))))
(turtle)
日志(VOM)输出
<INFO> [14:24:35] cl-turtle - Color key enabled (img)?: T
<INFO> [14:24:35] cl-turtle - Color key is: #(255 255 255)
截图
系统信息
Ubuntu 16.04 与 libSDL-1.2.so.0.11.4、libSDL_gfx.so.15.9.1 和 libSDL_image-1.2.so.0.8。 4
SBCL 1.3.7(来自 roswell sbcl-bin)
显然,调用 sdl:load-and-convert-image
而不是 sdl:load-image
可以解决问题。
Loads an image from the filename SOURCE as per LOAD-IMAGE-*, converts this image to the current display format using SDL:CONVERT-SURFACE.
Parameters supported are the same as those for LOAD-IMAGE and SDL:CONVERT-IMAGE.