-
Notifications
You must be signed in to change notification settings - Fork 0
/
Canvas.cs
114 lines (95 loc) · 2.99 KB
/
Canvas.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Windows.Forms;
// TODO: refactor those controls as on "drawable layer" with the nice stuff from
// game design. E.g. Tick.
namespace diese
{
public class Canvas : Control
{
protected readonly List<Actor> actors;
protected readonly List<Actor> futureActors;
public Canvas()
{
SetStyle(
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer,
true);
BackColor = Color.Transparent;
actors = new List<Actor>();
futureActors = new List<Actor>();
}
public void AddActor(Actor actor)
{
futureActors.Add(actor);
}
public void RemoveActor(Actor actor)
{
actor.Dead = true;
}
public void Tick()
{
foreach (var actor in actors)
{
actor.Tick();
}
foreach (var actor in futureActors)
{
actors.Add(actor);
}
futureActors.Clear();
actors.RemoveAll(a => a.Dead);
}
protected override void OnPaint(PaintEventArgs e)
{
var rect = e.ClipRectangle;
var state = e.Graphics.Save();
e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
// Centre l'affichage.
e.Graphics.TranslateTransform(rect.Right / 2f, rect.Bottom / 2f);
foreach (var actor in actors)
{
actor.Draw(e.Graphics);
}
e.Graphics.Restore(state);
}
}
public class Background : Canvas
{
public Image Image;
private TextureBrush brush;
public Background()
: base()
{
Disposed += onDispose;
}
private void onDispose(object sender, EventArgs e)
{
// Free the memory of the brush before leaving.
brush.Dispose();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (brush == null)
{
brush = new TextureBrush(Image, WrapMode.Tile);
}
var rect = e.ClipRectangle;
e.Graphics.FillRectangle(brush, 0, 0, rect.Right, rect.Bottom);
#if DEBUG
e.Graphics.DrawLine(
Pens.Blue,
new PointF(rect.Left, rect.Top),
new PointF(rect.Right, rect.Bottom));
e.Graphics.DrawLine(
Pens.Blue,
new PointF(rect.Right, rect.Top),
new PointF(rect.Left, rect.Bottom));
#endif
}
}
}