-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.d
139 lines (116 loc) · 2.37 KB
/
renderer.d
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
module renderer;
import consoled;
import std.stdio;
import std.format;
import core.thread;
enum RenderMode
{
raw, fancy
}
void delay(int seconds)
{
Thread.sleep(dur!"seconds"(seconds));
}
class ScreenCoordinate
{
public int x;
public int y;
this(int posX, int posY)
{
this.x = posX;
this.y = posY;
}
public override string toString()
{
return format("%s, %s", this.x, this.y);
}
}
class Tile
{
protected ScreenCoordinate pos;
protected string name;
this()
{
this.pos = new ScreenCoordinate(0, 0);
this.name = "";
}
public string getName()
{
return this.name;
}
public void render()
{
write(this.name);
}
}
class CTile : Tile
{
protected Color fg = Color.white;
protected Color bg = Color.initial;
this()
{
this.pos = new ScreenCoordinate(0, 0);
this.name = "";
}
public override void render()
{
foreground(this.fg);
background(this.bg);
writec(this.name);
resetColors();
}
}
class TileRenderer
{
private Tile[] tileRegistry;
private RenderMode rendermode = RenderMode.fancy;
public void registerTile(Tile tile)
{
this.tileRegistry ~= tile;
}
public void clearTileRegistry()
{
this.tileRegistry = [];
}
public void setRenderMode(RenderMode mode)
{
this.rendermode = mode;
}
//For debugging, so that you don't include the screen positions
private void updateRaw()
{
foreach(tile; this.tileRegistry)
{
tile.render();
write(" ", tile.pos.x, ", ", tile.pos.y);
writeln();
}
}
//Currently Redrawing all the tiles
//I don't like this, adding to todo
private void updateFancy()
{
clearScreen();
foreach(tile; this.tileRegistry)
{
setCursorPos(tile.pos.x, tile.pos.y);
tile.render();
}
stdout.flush();
}
public void update()
{
if(this.rendermode == RenderMode.fancy)
{
this.updateFancy();
}
else if(this.rendermode == RenderMode.raw)
{
this.updateRaw();
}
}
~this()
{
setCursorPos(0, size.y + 1);
}
}