Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some cl-sdl2 functions cause render-text-blended to render erratic, overlapping characters #27

Open
engeljohnb opened this issue May 1, 2022 · 0 comments

Comments

@engeljohnb
Copy link

The following code is the minimum recreateable example I could come up with from my bigger project. It's supposed to display the name of the keys the user types in, spaced out appropriately. Instead, after one or two keys it starts to display erratic characters overlapping each other.

Compiled with trivial-dump-core
With SBCL 2.0.1.debian
On Linux Mint 20.3

(require 'sdl2)
(require 'sdl2-ttf)

(defparameter *screen-width* 640)
(defparameter *screen-height* 480)
(defparameter *current-x* 0)

(defun get-text-size (font text)
  (list :w (first (multiple-value-list (sdl2-ttf:size-text font text)))
        :h (second (multiple-value-list (sdl2-ttf:size-text font text)))))

(defun create-text-surface (renderer font text)
   (let* ((sdl-surface (sdl2-ttf:render-text-blended 
                              font
                              text
                              #xff
                              #xff
                              #xff
                              #xff))
          (sdl-texture (sdl2:create-texture-from-surface renderer sdl-surface))
          (final-texture (sdl2:create-texture renderer
                                              :argb8888
                                              :target
                                              (getf (get-text-size font text) :w)
                                              (getf (get-text-size font text) :h))))
     (sdl2:set-texture-blend-mode sdl-texture :blend)
     (sdl2:set-texture-blend-mode final-texture :blend)
     (sdl2:set-render-target renderer final-texture)
     (sdl2:render-copy renderer sdl-texture)
     (sdl2:set-render-target renderer nil)
     (sdl2:free-surface sdl-surface)
     (sdl2:destroy-texture sdl-texture)
     final-texture))

(defun display-text (texture renderer font text)
  (let ((text-texture (create-text-surface renderer font text))
        (dest-rect (sdl2:make-rect *current-x* 
                                   0 
                                   (getf (get-text-size font text) :w)
                                   (getf (get-text-size font text) :h))))
    (sdl2:render-copy renderer texture :dest-rect dest-rect)
    (incf *current-x* (sdl2:rect-width dest-rect))
    (sdl2:set-render-target renderer texture)
    (sdl2:render-copy renderer text-texture :dest-rect dest-rect)
    (sdl2:set-render-target renderer nil)
    (sdl2:destroy-texture text-texture)))
                                   

(defun run()
  (sdl2-ttf:init)
  (sdl2:with-init (:everything)
    (sdl2:with-window (window :title "SDL2 Tutorial 01"
                              :w *screen-width*
                              :h *screen-height*
                              :flags '(:shown))
      (sdl2:with-renderer (renderer
                            window
                            :index -1
                            :flags '(:accelerated))
      (sdl2:set-render-draw-blend-mode renderer :blend)
      (sdl2:set-render-draw-color renderer #x22 #x22 #x22 #xff)
      (let ((key nil)
            (font (sdl2-ttf:open-font "fonts/LiberationMono-Regular.ttf" 13))
            (main-texture (sdl2:create-texture renderer
                                               :argb8888
                                               :target
                                               640
                                               480)))
        (sdl2:set-texture-blend-mode main-texture :blend)
        (sdl2:with-event-loop (:method :poll)
          (:quit () t)
          (:keydown (:keysym keysym)
            (setf key (string-downcase (sdl2:get-key-name (sdl2:get-key-from-scancode (sdl2:scancode keysym)))))
            (display-text main-texture renderer font key))
          (:idle ()
           (sdl2:render-clear renderer)
           (sdl2:render-copy renderer main-texture)
           (sdl2:render-present renderer)))
        (sdl2:destroy-texture main-texture)
        (sdl2-ttf:close-font font)))))
  (sdl2-ttf:quit))

A few things I've discovered:

The same executable works fine on my laptop running Manjaro.

The bug doesn't happen when I comment out the call to sdl2:set-render-draw-color in run.

The bug also doesn't happen when I comment out the calls to sdl2:free-surface and sdl2:destroy-texture in create-text-surface.

The bug doesn't happen when I change from an accelerated renderer to a software renderer.

I'm new to submitting github issues, so I apologize if I've gotten etiquette wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant