Hello.
I'm using .Net version of SFML, but it doesnt matter, right?)
So, here is what i did:
public class Layer
: Drawable
{ private string name
; private List
<Drawable
> list
; private View view
; public Layer
(string name, View copyView
) { this.name = name
; list
= new List
<Drawable
>(); view
= new View
(copyView
); } public void Add(Drawable drawable
) { list
.Add(drawable
); } public void Remove(Drawable drawable
) { list
.Remove(drawable
); } public void Draw
(RenderTarget target, RenderStates states
) { target
.SetView(view
); for (int i
= 0; i
< list
.Count; i
++) target
.Draw(list
[i
], states
); } public void SetPosition
(float x,
float y
) { var offset
= new Vector2f
(view
.Center.X - x, view
.Center.Y - y
); view
.Move(offset
); } public void SetPosition
(Vector2f position
) { var offset
= view
.Center - position
; view
.Move(offset
); } public void Move
(Vector2f offset
) { SetPosition
(view
.Center + offset
); } public View GetView
() { return view
; } public string Name
{ get { return name
; } set { name
= value; } } } public class LayersHandler
: Drawable
{ private Dictionary
<string, Layer
> layers
; private View view
; public LayersHandler
(View view
) { layers
= new Dictionary
<string, Layer
>(); this.view = view
; } public void AddLayers
(params string[] names
) { for (int i
= 0; i
< names
.Length; i
++) AddLayer
(names
[i
]); } public void RemoveLayers
(params string[] names
) { for (int i
= 0; i
< names
.Length; i
++) RemoveLayer
(names
[i
]); } public void AddLayer
(string name
) { layers
.Add(name,
new Layer
(name, view
)); } public void RemoveLayer
(string name
) { layers
.Remove(name
); } public void AddToLayer
(string name, Drawable drawable
) { layers
[name
].Add(drawable
); } public void RemoveFromLayer
(string name, Drawable drawable
) { layers
[name
].Remove(drawable
); } public void Draw
(RenderTarget target, RenderStates states
) { foreach (var el
in layers
) target
.Draw(el
.Value); } public Layer GetLayer
(string name
) { return layers
[name
]; } } class Camera
{ private FloatRect bounds
; private Vector2f size
; private Vector2f center
; private float x
; private float y
; private List
<View
> views
; private Transformable followingObject
; public Camera
() { views
= new List
<View
>(); } public void Bind
(Layer layer
) { views
.Add(layer
.GetView()); size
= views
[0].Size; center
= views
[0].Center; x
= views
[0].Center.X; y
= views
[0].Center.Y; } public void MoveViews
(Vector2f offset
) { for (int i
= 0; i
< views
.Count; i
++) views
[i
].Move(offset
); } public void SetViewsCenter
(Vector2f center
) { for (int i
= 0; i
< views
.Count; i
++) views
[i
].Center = center
; } public void ScrollToXY
(float x,
float y
) { float xx
= x
; float yy
= y
; if (bounds
!= null) { xx
= eMath
.clamp(xx, bounds
.Left + size
.X / 2, bounds
.Left - size
.X / 2 + bounds
.Width); yy
= eMath
.clamp(yy, bounds
.Top + size
.Y / 2, bounds
.Top - size
.Y / 2 + bounds
.Height); } xx
= eMath
.lerp(this.x, xx, 0
.002f
); yy
= eMath
.lerp(this.y, yy, 0
.002f
); SetViewsCenter
(new Vector2f
(xx, yy
)); this.x = xx
; this.y = yy
; } public void Update
() { if (followingObject
!= null) ScrollToXY
(followingObject
.Position.X, followingObject
.Position.Y); } public void Follow
(Transformable obj
) { followingObject
= obj
; } public void SetBounds
(FloatRect bounds
) { this.bounds = bounds
; } } And usage:
//creating window, sprites and etc.layers
= new LayersHandler
(window
.GetView());// creating 2 layerslayers
.AddLayers("bottom",
"mid",
"UI");// adding drawable objectslayers
.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 loopwhile (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.