lispgames / cl-sdl2

Common Lisp bindings for SDL2 using C2FFI.
MIT License
304 stars 82 forks source link

CL-SDL2. Game Loop Issue ! Is there solution of his problem or i must try another game library? #169

Open huseyindenli opened 3 months ago

huseyindenli commented 3 months ago

https://github.com/huseyindenli/UDEMY-opengl-python-to-CL

I have the main function which includes the game loop:

(defun main ()
  (sdl2:with-init (:everything)
    (sdl2:gl-set-attr :doublebuffer 1)
    (sdl2:with-window (screen :w *screen-width* :h *screen-height*
      :flags '(:opengl)
      :title "OpenGL in Common Lisp")
      (sdl2:with-gl-context (gl-context screen)
(progn

  (initialize)

  (sdl2:with-event-loop (:method :poll)
     (:keydown (:keysym keysym)
       (let ((scancode (sdl2:scancode-value keysym))
     (sym (sdl2:sym-value keysym))
     (mod-value (sdl2:mod-value keysym)))
 (declare (ignore sym mod-value))

 (cond
   ((sdl2:scancode= scancode :scancode-escape) (sdl2:push-event :quit))
   ((sdl2:scancode= scancode :scancode-up) (progn (update-data *camera* :up)))
   ((sdl2:scancode= scancode :scancode-down) (progn (update-data *camera* :down)))
   ((sdl2:scancode= scancode :scancode-left) (progn (update-data *camera* :left)))
   ((sdl2:scancode= scancode :scancode-right) (progn (update-data *camera* :right))))))
     (:idle ()
    (display)
    (sdl2:gl-swap-window screen)
    ;; (sleep 0.100)
    )
     (:quit () t)))))))

with initialization and display functions.

(```
defun initialize ()
  (gl:clear-color (first *background-color*)
  (second *background-color*)
  (third *background-color*)
  (fourth *background-color*))
  (gl:color (first *drawing-color*)
    (second *drawing-color*)
    (third *drawing-color*)
    (fourth *drawing-color*))
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:perspective 60 (/ *screen-width* *screen-height*) 0.1 1000.0)
  (gl:matrix-mode :modelview)
  (gl:load-identity)
  (gl:viewport 0 0 *screen-width* *screen-height*)
  (gl:enable :depth-test)
  )
(defun display ()
  (gl:clear :color-buffer-bit :depth-buffer-bit)
  (gl:push-matrix)
  (update-camera *camera*)
  (gl:translate 0 0 5)
  (draw *mesh*)
  (gl:pop-matrix))

But the :keydown event loop is not working properly. Here is the issue

Fist input is working properly if i press "up" or "down" the camera works properly, if i press the same again button it works properly, but if i press another button first i does not respond then if a press the same button again it is moving opposite direction.

    "up" => works properly (camera moves up).

    "down" => does not respond.

    "down" => does not work properly (camera moves up not down).

same for the opposite:

    "down" => works properly (camera moves down).

    "up" => does not respond.

    "up" => does not work properly (camera moves down not up).

I have done many variants, but i could not correct this issue. If i replace

(progn (update-data camera :up)) with (print "up") (progn (update-data camera :down)) with (print "down")



i get a slightly different behavior but again not the correct one. I get:

    "up" or "down" => white space ; not correct

    "up" => up ; correct

    "down" => up ; not correct

    "down" => down ; correct

    "up" => down ; not correct

    "up" => up ; correct

I can not solve this issue. What is the issue? How can i solve it? Is there a solution?