oakes / play-clj

A Clojure game library
The Unlicense
939 stars 72 forks source link

Missing hook to manage fading out during render ? #47

Closed DjebbZ closed 10 years ago

DjebbZ commented 10 years ago

Hi,

I wanted to use Batch.setColor() with the alpha paramter to make an entity fade out. When reading the code in play-clj, I noticed that the draw method lacks a hook the manipulate the Batch. As I don't want to use this method for all entities but only some of them, I can't just retrieve the batch from the renderer then set its color. Am I right ?

oakes commented 10 years ago

Were you planning on calling setColor for one entity, and then immediately change it back before the next entity is drawn? If so, wouldn't that require two hooks (before and after the entity is drawn)?

DjebbZ commented 10 years ago

We're not sure how to use setColor anyway, never did this kind of programming with OpenGL before. But yeah, maybe 2 hooks would be necessary. Or the ability to pass a function that manipulate the Batch directly. Not sure how to architecture it to make it fit play-clj the best.

oakes commented 10 years ago

I'll try to think of a good way of doing this, but in the meantime you should be able to make your own multimethod like this:

(defmethod play-clj.core/draw! Stage
  [{:keys [^Stage renderer] :as screen} entities]
  (let [^Batch batch (.getBatch renderer)]
    (.begin batch)
    (doseq [entity entities]
      (some-> (:before-draw entity) (apply [batch]))
      (e/draw-entity! entity screen batch)
      (some-> (:after-draw entity) (apply [batch])))
    (.end batch))
  entities)

I haven't tried it, but I assume this would let you add the hooks to a given entity by assoc'ing functions to an entity, like this:

(assoc entity :before-draw #(.setColor % (color 1 1 1 0.5)))
DjebbZ commented 10 years ago

I didn't think of using multimethods, great idea ! Will try tomorrow and report.

DjebbZ commented 10 years ago

Didn't have time to try today, but if I understand the technique, it's basically reimplementing the draw! multimethod by overriding the original one, right ?

oakes commented 10 years ago

Correct; let me know if it doesn't work for you.