Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Layers and camera  (Read 2823 times)

0 Members and 1 Guest are viewing this topic.

rokstars

  • Newbie
  • *
  • Posts: 1
    • View Profile
Layers and camera
« on: January 19, 2016, 10:29:16 am »
Hello.
I'm using .Net version of SFML, but it doesnt matter, right?)
So, here is what i did:
(click to show/hide)

(click to show/hide)

(click to show/hide)

And usage:

//creating window, sprites and etc.
layers = new LayersHandler(window.GetView());
// creating 2 layers
layers.AddLayers("bottom", "mid", "UI");
// adding drawable objects
layers.AddToLayer("bottom", sprite);
layers.AddToLayer("bottom", shape);
layers.AddToLayer("mid", shape2);
layers.AddToLayer("UI", TextField);

cam = new Camera();
// Camera will scroll layer "bottom" and "mid"
cam.Bind(layers.GetLayer("bottom"));
cam.Bind(layers.GetLayer("mid"));
cam.Follow(shape);
cam.SetBounds(new FloatRect(0, 0, 1920, 1080));

//Game loop
while (window.IsOpen)
{
   cam.Update();
   window.Clear(Color.White);
   window.Draw(layers);
   window.Display();
}
 

Sorry for lot of code, but its not hard to understand whats going on here.
I want to scroll a specific layers, but others should stay at (0, 0). I also want to emlement paralax effect to some layers, but first of all i need to know: is this method OK or is it realy bad and there is a better way to do all that stuff?

P.S. Sorry for bad english.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Layers and camera
« Reply #1 on: January 20, 2016, 10:47:38 pm »
I think it will be better to incapsulate view inside layer implementation. It's too hard to read the code. I used layered rendering with sfml in the following way:

foreach(var layer in _layers)
   layer.Draw(renderTarget);

each layer know how to render itself. For example, here is how Layer.Draw is implemented:
public void Draw(RenderTarget target)
{
   target.SetView(_view);
   foreach(var item in _items)
      DrawItem(target, item);
...

 

anything