saikyun / freja

Self-modifiable editor for coding graphical things
67 stars 3 forks source link

Absolute coordinates affect the "code part" of the screen #45

Closed greenfork closed 3 years ago

greenfork commented 3 years ago

I'm not really sure what's wrong here. Probably mouse position is not captured properly?

https://user-images.githubusercontent.com/20167110/137634340-56c672b1-8a9d-4616-b6fe-1e1e8936e66a.mp4

This is an adapted example from the jaylib repo:

#(import freja-jaylib :as jl)
(import freja/flow :as jl)

#(defn init []
#  (jl/init-window 800 600 "Test Game")
#  (jl/set-target-fps 60)
#  (jl/hide-cursor))

#(defn deinit []
#  (jl/close-window))

(defn render [el]
  (when (el :focused?)
    (def {:width w :height h} el)
    (jl/begin-drawing)

    (jl/clear-background [0 0 0])
    (let [[x y] (jl/get-mouse-position)]
      (jl/draw-circle-gradient x y 31.4 :lime :red)
      (jl/draw-poly [200 100] 5 40 0 :magenta)
      (jl/draw-line-bezier
        [(- x 100) y]
        [(+ x 100) (+ y 50)]
        4 :pink)
      (jl/draw-line-ex
        [x (- y 10)]
        [x (+ y 10)]
        4 :sky-blue)
      (jl/draw-line-strip
        [[x 0] [x 100] [50 y] [10 180]]
        :ray-white))
    (jl/end-drawing)))

#(defn main-loop []
#  (while (not (jl/window-should-close))
#    (render 1)))

(jl/start-game {:render render})

#(defn main [& args]
#  (init)
#  (main-loop)
#  (deinit))
saikyun commented 3 years ago

Okay, so multiple things happening here:

  1. begin-drawing / end-drawing is not needed when running in freja, freja runs those :) This is what causes the flickering. Put them in the main-loop-function.
  2. clear-background is not needed. If you want a background, you'll have to use (draw-rectangle 0 0 w h :white)
  3. get-mouse-position returns the "global position". To get the relative position, you can do this:
    (def {:width w :height h :render-x rx :render-y ry} el)
    # ...
        (let [[x y] (jl/get-mouse-position)
               x (- x rx)
               y (- y ry)]
saikyun commented 3 years ago

There is one way to get relative mouse positions, and not have to care about focused?, and that is by utilizing :on-event in the struct passed to start-game. I have considered also "wrapping" get-mouse-position, but the problem then is that it can become harder to get the absolute mouse position.

I think I will want to make it easier to just build binaries without having to juggle running in freja and running only jaylib, and then make it clearer how to do this in a freja-friendly way. :) Perhaps I should also create some examples for freja, dealing with these issues.

greenfork commented 3 years ago

I'm not totally sure about the mouse position but the basic problems with flickering are solved, thanks a lot!

saikyun commented 3 years ago

Hmm, okay, you can verify that you're getting the local mouse position by eg rendering a circle using the calculated x and y. :)

Den mån 18 okt. 2021 15:31Dmitry Matveyev @.***> skrev:

Closed #45 https://github.com/saikyun/freja/issues/45.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/saikyun/freja/issues/45#event-5478441978, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAS46Z5AXEEQ6ZSLOSAFKOTUHQOUDANCNFSM5GE6L3ZA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

greenfork commented 3 years ago

Basically yes, I have a "cursor" for my game, so this works for me :D

saikyun commented 3 years ago

Okay, nice. :)