How would you implement layers?

Monkey Forums/Monkey Beginners/How would you implement layers?

Gianmichele(Posted 2013) [#1]
Hi everyone,

I'm really new to Monkey, and going through the invaderjim's videos which I find fantastic! I'm an artist(animator) with a passion for coding, so I always try to organize my code with the visuals in mind.

I'd love to create a few helper classes to create a sort of layering system, where I can add objects that would react to these layers.

From what I got so far using the push/pop matrix is the way to go, so I can have a master layer that controls the "stage" with resolution indipendence, etc and then a list of layers that gets rendered within this master layer to each of which I associate all the objects that I want.

Am I on the right track with this, or is there a different/simpler method I could use?

Thanks a lot.

Gianmichele


ziggy(Posted 2013) [#2]
If by layers you mean the Z-Order of rendering (place some images on top of others) I think your best option is to organize rendering order. You could create a class "stage" that has a collection of "layers" and each layer has a "render" method. Then call the "render" of each member of the "stage" class in the list order. That would be like rendering from back to front of a Z-Order.


Gianmichele(Posted 2013) [#3]
Thanks, that's exactly what I'm trying to achieve as well as some helper functions to create layers and set them up.


ziggy(Posted 2013) [#4]
This is a very very basic example on how this could be implemented. You can take it as a starting point to develop your own, if you like it:

Notice that the 2 sample layers are just a square and a moving circle, but a layer could be whatever. I mean, a single layer could be used to draw lots of elements, no need to create a layer per element.


Gianmichele(Posted 2014) [#5]
Thanks a lot. It is what I'm doing at the moment (more or less). It's fun getting into these things :)


Gianmichele(Posted 2014) [#6]
On a side note, what if I want to transform these layers for parallax effects for example?Is this where the pop/push matrix come into play?


Jesse(Posted 2014) [#7]
push/pop matrix is used for graphics manipulation such as scale, rotate and translate. It may be useful for what you are doing but not essential and not necessarily helpful.
if you want to scale an image use the push matrix and pop matrix on that specific image so the rest of the graphics do not get affected by the scaling. scaling and rotation work the same way.