-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderer.py
334 lines (176 loc) · 6.9 KB
/
Renderer.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from Platform_Windows import CAttribute, CreateCoord
from OSPlatform import Platform
from Config import Debug
from LAL import constant
from STL import Arrays
from STL import Copy
from STL import SYS
from STL import OS
import State
import Timing
CellAttribute = Platform.CAttribute
CBufferWidth = 80
if Debug.Enabled : CBufferHeight = 48
else : CBufferHeight = 24
CBorderLineRow = 24
CDebugStart = 25
CLogSize = 18
CPersistentStart = 44
CPersistentSize = 4
CGameEnd = 23
class Vec2D :
X = int
Y = int
class CellPacket :
def SetAttribute(self, _attribute) :
self._AttribBuf = []
for index in range(len(self.String)) :
self._AttribBuf.append(_attribute);
def WriteToBuffer(self) :
global _Buffer, _BufferSize
if self.String == '' : return
if _Buffer[0] == None :
_Buffer[0] = self
else :
_Buffer = Arrays.append(_Buffer, self)
def Draw(self) :
global _CSB;
_CSB.WriteConsoleOutputCharacter(Characters = self.String, WriteCoord = self.Position)
_CSB.WriteConsoleOutputAttribute(Attributes = self._AttribBuf, WriteCoord = self.Position)
String = ""
Position = Platform.CreateCoord(X = 0, Y = 0)
_AttribBuf = None
_GameBorder = CellPacket()
_PersistentBorder = CellPacket()
_GameScanline = CellPacket()
_GameScanline_Y = int()
_ScreenCenter = Vec2D
_ScreenCenter.X = int(Platform.MainScreenWidth / 2)
_ScreenCenter.Y = int(Platform.MainScreenHeight / 2)
_ScreenPosition = Vec2D
_ScreenPosition.X = (_ScreenCenter.X - (int(CBufferWidth / 2) * 8)) - 20
_ScreenPosition.Y = (_ScreenCenter.Y - (int(CBufferWidth / 2) * 8)) - 200
_ConsoleHandle = int()
_CSB_Front = None
_CSB_Back = None;
_CSB = None;
_CoordSize = Platform.CreateCoord( X = CBufferWidth, Y = CBufferHeight )
_BufferSize = CBufferWidth * CBufferHeight
_Console_Coord = Platform.CreateSmallRect(Left = 0, Top = 0, Right = CBufferWidth - 1, Bottom = CBufferHeight - 1)
_RefreshTimer = Timing.Timer(1.0 / 30.0)
_Buffer = Arrays.empty(1, dtype = CellPacket)
_DebugLog = Arrays.empty(1, dtype = str)
_PersistentSection = Arrays.array([CellPacket(), CellPacket(), CellPacket(), CellPacket()])
def SetupConsole() :
global _ConsoleHandle, _CSB_Front, _CSB_Back, _CSB, _CoordSize, _Console_Coord, _ScreenPosition
_ConsoleHandle = Platform.RequestConsole()
Platform.SetConsoleTitle("TBF Engine: Type Python")
if _ConsoleHandle == 0 :
raise RuntimeError("Renderer: RequestConsole failed.")
_CSB_Front = Platform.CreateScreenBuffer()
_CSB_Back = Platform.CreateScreenBuffer()
if _CSB_Front == 0 or _CSB_Back == 0:
raise RuntimeError("Renderer: Failed to create a console screen buffer.")
_CSB_Front.SetConsoleActiveScreenBuffer()
_CSB_Front.SetConsoleScreenBufferSize(Size = _CoordSize)
_CSB_Front.SetConsoleCursorInfo(Size = Platform.CConsole_CursorMinSize, Visible = False)
_CSB_Front.SetConsoleWindowInfo(Absolute = True, ConsoleWindow = _Console_Coord)
_CSB_Front.SetConsoleScreenBufferSize(Size = _CoordSize)
_CSB_Front.SetConsoleWindowInfo(Absolute = True, ConsoleWindow = _Console_Coord)
_CSB_Front.SetConsoleCursorInfo(Size = Platform.CConsole_CursorMinSize, Visible = False)
_CSB_Back.SetConsoleScreenBufferSize(Size = _CoordSize)
_CSB_Back.SetConsoleCursorInfo(Size = Platform.CConsole_CursorMinSize, Visible = False)
_CSB_Back.SetConsoleWindowInfo(Absolute = True, ConsoleWindow = _Console_Coord)
_CSB_Back.SetConsoleScreenBufferSize(Size = _CoordSize)
_CSB_Back.SetConsoleWindowInfo(Absolute = True, ConsoleWindow = _Console_Coord)
_CSB_Back.SetConsoleCursorInfo(Size = Platform.CConsole_CursorMinSize, Visible = False)
_CSB = _CSB_Front
Platform.SetWindowPosition(
_ConsoleHandle,
Platform.CHandle_Top,
_ScreenPosition.X,
_ScreenPosition.Y,
0,
0,
Platform.CWindowFlag_NoSize
)
def DrawGameScanline() :
global CGameEnd, _GameScanline, _GameScanline_Y
_GameScanline.Position = CreateCoord(X = 0, Y = _GameScanline_Y)
_GameScanline.WriteToBuffer()
_GameScanline_Y += 1
if _GameScanline_Y > CGameEnd :
_GameScanline_Y = 0
def WriteToLog(_string : str) :
global _DebugLog
if Debug.Enabled :
_DebugLog = Arrays.append(_DebugLog, _string)
def WriteLogToBuffer() :
global _DebugLog;
if Debug.Enabled :
logRange = CLogSize
if _DebugLog.size < CLogSize :
logRange = _DebugLog.size
for index in range(logRange - 1) :
LogPacket = CellPacket()
LogPacket.String = _DebugLog[index + 1]
LogPacket.SetAttribute(Platform.CConsole_WhiteCell)
LogPacket.Position = Platform.CreateCoord(X = 0, Y = index + CDebugStart)
LogPacket.WriteToBuffer()
def WriteToPersistentSection(_index : int, _string : str) :
global _PersistentSection
_PersistentSection[_index - 1].String = _string
_PersistentSection[_index - 1].SetAttribute(Platform.CConsole_WhiteCell)
def WritePersistentSectionToBuffer() :
global _PersistentSection;
_PersistentSection[0].WriteToBuffer()
_PersistentSection[1].WriteToBuffer()
_PersistentSection[2].WriteToBuffer()
_PersistentSection[3].WriteToBuffer()
def ResetDrawPosition() :
global _CSB; _CSB.SetConsoleCursorPosition(Platform.CreateCoord(X = 0, Y = 0))
_EmptyBuffer = CellPacket()
for index in range(_BufferSize) : _EmptyBuffer.String += " "
_EmptyBuffer.SetAttribute(0);
def Clear() :
global _Buffer, _EmptyBuffer; _Buffer = Arrays.empty(1, dtype = CellPacket)
_EmptyBuffer.WriteToBuffer()
_EmptyBuffer.Draw();
_Buffer = Arrays.empty(1, dtype = CellPacket)
def RenderFrame() :
global _Buffer
for index in range(_Buffer.size) : _Buffer[index].Draw()
def LoadModule() :
global _GameBorder, _PersistentBorder, _Buffer, _BufferSize, CBorderLineRow, CPersistentStart, CPersistentSize, _PersistentSection
_GameBorder.Position = Platform.CreateCoord(X = 0, Y = CBorderLineRow)
for index in range(CBufferWidth) :
_GameBorder.String = _GameBorder.String + "="
_GameBorder.SetAttribute(Platform.CConsole_WhiteCell)
_PersistentBorder = Copy.copy(_GameBorder)
_PersistentBorder.Position = Platform.CreateCoord(X = 0, Y = CPersistentStart - 1)
for index in range(CBufferWidth) :
_GameScanline.String = _GameScanline.String + "-"
_GameScanline.SetAttribute(Platform.CConsole_WhiteCell)
for index in range(CPersistentSize) :
_PersistentSection[index].Position = Platform.CreateCoord(X = 0, Y = CPersistentStart + index)
SetupConsole()
def UnloadModule() :
Platform.ReleaseConsole()
def ProcessTiming() :
_RefreshTimer.Tick()
test = False;
def Update() :
global _GameBorder, _PersistentBorder, _CSB, _CSB_Front, _CSB_Back
if _RefreshTimer.Ended():
if _CSB == _CSB_Front : _CSB = _CSB_Back
else : _CSB = _CSB_Front
Clear()
DrawGameScanline()
WriteLogToBuffer()
WritePersistentSectionToBuffer()
State.GetEngineState().Render()
_GameBorder.WriteToBuffer()
_PersistentBorder.WriteToBuffer()
RenderFrame()
_CSB.SetConsoleActiveScreenBuffer();
_RefreshTimer.Reset()