-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcap.lua
706 lines (627 loc) · 19.8 KB
/
dcap.lua
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
-- Display capture
local component = require("component")
local gpu = component.gpu
local term = require("term")
local unicode = require("unicode")
local dcap = {}
--[[
local dlog = require("dlog")
dlog.fileOutput("/home/messages", "w")
dlog.mode("debug")
dlog.standardOutput(false)
--]]
--
function dcap.onComponentRemoved()
end
--
function dcap.onComponentAdded()
end
function dcap.getForeground()
local color, isPalette = gpu.getForeground()
return isPalette and -color - 1 or color
end
function dcap.getBackground()
local color, isPalette = gpu.getBackground()
return isPalette and -color - 1 or color
end
function dcap.setForeground(color)
local c, i = gpu.getForeground()
if (i and -c - 1 or c) ~= color then
gpu.setForeground(color < 0 and -color - 1 or color, color < 0)
end
end
function dcap.setBackground(color)
local c, i = gpu.getBackground()
if (i and -c - 1 or c) ~= color then
gpu.setBackground(color < 0 and -color - 1 or color, color < 0)
end
end
--[[
displayState = {
depth: <GPU color depth (number)>
res: {<GPU resolution w (number)>, <GPU resolution h (number)>}
view: {<GPU viewport w (number)>, <GPU viewport h (number)>}
palette: {
1: <GPU palette color 0 (number)>
2: <GPU palette color 1 (number)>
...
16: <GPU palette color 15 (number)>
}
textBuffer: { -- Sequence representing the text and colors on screen. The data is organized to reduce context switches (which reduces the length of this table).
1: <x (number)> -- Position of text on screen.
2: [<y (number)>]
3: [<fg (number)>] -- Color of text. Values in the range [-16, -1] correspond to a palette index.
4: [<bg (number)>]
5: <line (string)>
...
}
cursor: {<cursor x (number)>, <cursor y (number)>}
bg: <GPU current background (number)>
fg: <GPU current foreground (number)>
}
--]]
--
function dcap.captureDisplayState()
local displayState = {}
displayState.depth = gpu.getDepth()
displayState.res = {gpu.getResolution()}
displayState.view = {gpu.getViewport()}
local palette = {}
for i = 1, 16 do
palette[i] = gpu.getPaletteColor(i - 1)
end
displayState.palette = palette
-- alternative single array design for textBuffer:
-- {<entry>, ...}
-- entry types:
-- x: number[, y: number[, fg: number[, bg: number]]], str: string
-- ordered to minimize state changes of fg and bg
-- for fg and bg, negative values means palette index
--[[
textBufferInflated: {
<bg (number)>: {
<fg (number)>: {
1: <x (number)>
2: <y (number)>
3: <line (string)>
4: <x (number)>
5: <y (number)>
6: <line (string)>
...
}
...
}
...
}
--]]
local textBufferInflated = setmetatable({}, {
__index = function(t, k)
t[k] = setmetatable({}, {
__index = function(t2, k2)
t2[k2] = {}
return t2[k2]
end
})
return t[k]
end
})
-- Scan each character in the screen buffer row-by-row, and insert them into the textBufferInflated. Any white-on-black text that is a contiguous line of spaces is simply discarded to optimize.
local foundWBNonSpace = false
local width, height = gpu.getResolution()
for y = 1, height do
local x = 1
while x <= width do
local char, fg, bg, fgIndex, bgIndex = gpu.get(x, y)
fg = math.floor(fgIndex and -fgIndex - 1 or fg)
bg = math.floor(bgIndex and -bgIndex - 1 or bg)
local colorWB = (fg == 0xFFFFFF and bg == 0x0)
if not colorWB or char ~= " " or foundWBNonSpace then
local lineGroup = textBufferInflated[bg][fg]
local lineGroupSize = #lineGroup
-- Add the character to the last line if the y and x position line up (considering the true length in characters in case we have unicode ones).
if lineGroup[lineGroupSize - 1] == y and lineGroup[lineGroupSize - 2] + unicode.wlen(lineGroup[lineGroupSize]) == x then
if char ~= " " and colorWB then
foundWBNonSpace = true
end
lineGroup[lineGroupSize] = lineGroup[lineGroupSize] .. char
else
if foundWBNonSpace then
local lineGroupWB = textBufferInflated[0x0][0xFFFFFF]
lineGroupWB[#lineGroupWB] = string.match(lineGroupWB[#lineGroupWB], "(.-) *$")
foundWBNonSpace = false
end
if char ~= " " or not colorWB then
lineGroup[lineGroupSize + 1] = x
lineGroup[lineGroupSize + 2] = y
lineGroup[lineGroupSize + 3] = char
end
end
x = x + unicode.wlen(char)
else
x = x + 1
end
end
end
if foundWBNonSpace then
local lineGroupWB = textBufferInflated[0x0][0xFFFFFF]
lineGroupWB[#lineGroupWB] = string.match(lineGroupWB[#lineGroupWB], "(.-) *$")
end
local textBuffer = {}
local textBufferSize = 0
local lineOffset
for bg, fgGroup in pairs(textBufferInflated) do
-- Add bg color (color changed).
textBuffer[textBufferSize + 4] = bg
lineOffset = 5
for fg, lineGroup in pairs(fgGroup) do
-- Add fg color (color changed).
textBuffer[textBufferSize + 3] = fg
lineOffset = math.max(lineOffset, 4)
local lastY
for i = 1, #lineGroup, 3 do
-- Add x position.
textBuffer[textBufferSize + 1] = lineGroup[i]
-- Add y position if it changed.
if lineGroup[i + 1] ~= lastY then
lastY = lineGroup[i + 1]
textBuffer[textBufferSize + 2] = lastY
lineOffset = math.max(lineOffset, 3)
end
-- Add the line to the end.
textBufferSize = textBufferSize + lineOffset
textBuffer[textBufferSize] = lineGroup[i + 2]
lineOffset = 2
end
end
end
displayState.textBufferInflated = textBufferInflated
displayState.textBuffer = textBuffer
displayState.cursor = {term.getCursor()}
displayState.bg = dcap.getBackground()
displayState.fg = dcap.getForeground()
return displayState
end
--
function dcap.restoreDisplayState(displayState)
gpu.setDepth(displayState.depth)
for i = 1, 16 do
gpu.setPaletteColor(i - 1, displayState.palette[i])
end
-- Clear entire screen.
local width, height = gpu.getResolution()
dcap.setBackground(0x000000)
dcap.setForeground(0xFFFFFF)
gpu.fill(1, 1, width, height, " ")
gpu.setResolution(displayState.res[1], displayState.res[2])
gpu.setViewport(displayState.view[1], displayState.view[2])
-- Draw the new screen contents.
local i = 1
local textBuffer = displayState.textBuffer
local textBufferSize = #textBuffer
local x, y
while i < textBufferSize do
x = textBuffer[i]
i = i + 1
if type(textBuffer[i]) == "number" then
y = textBuffer[i]
i = i + 1
if type(textBuffer[i]) == "number" then
dcap.setForeground(textBuffer[i])
i = i + 1
if type(textBuffer[i]) == "number" then
dcap.setBackground(textBuffer[i])
i = i + 1
end
end
end
gpu.set(x, y, textBuffer[i])
i = i + 1
end
term.setCursor(displayState.cursor[1], displayState.cursor[2])
dcap.setBackground(displayState.bg)
dcap.setForeground(displayState.fg)
end
local resolutionX, resolutionY
local currentBg, currentFg
local lastFrameChars, lastFrameBgs, lastFrameFgs
local currentFrameChars, currentFrameBgs, currentFrameFgs
local minBoundsX, minBoundsY, maxBoundsX, maxBoundsY
local forceUpdated
function dcap.logState(outFile)
local function drawCharGrid(arr)
outFile:write(" ")
for x = 1, resolutionX do
outFile:write(x > 9 and tostring(math.floor(x / 10) % 10) or " ")
end
outFile:write("\n ")
for x = 1, resolutionX do
outFile:write(string.format("%d", x % 10))
end
outFile:write("\n")
local i = 1
for y = 1, resolutionY do
outFile:write(string.format("%3d ", y))
for x = 1, resolutionX do
if arr[i] == "" then
outFile:write("\\")
else
outFile:write(arr[i] or ".")
end
i = i + 1
end
outFile:write("\n")
end
end
local function drawColorGrid(arr)
outFile:write(" ")
for x = 1, resolutionX do
outFile:write(x > 9 and tostring(math.floor(x / 10) % 10) or " ")
end
outFile:write("\n ")
for x = 1, resolutionX do
outFile:write(string.format("%d", x % 10))
end
outFile:write("\n")
local i = 1
for y = 1, resolutionY do
outFile:write(string.format("%3d ", y))
for x = 1, resolutionX do
if arr[i] and arr[i] < 0 then
outFile:write(math.floor(-arr[i]) % 10)
elseif arr[i] == 0x000000 then
outFile:write("b")
elseif arr[i] == 0xFFFFFF then
outFile:write("w")
else
outFile:write(arr[i] and "x" or ".")
end
i = i + 1
end
outFile:write("\n")
end
end
outFile:write("lastFrameChars:\n")
local cachedMeta = getmetatable(lastFrameChars)
setmetatable(lastFrameChars, nil)
drawCharGrid(lastFrameChars)
drawColorGrid(lastFrameBgs)
drawColorGrid(lastFrameFgs)
outFile:write("\n\ncurrentFrameChars:\n")
drawCharGrid(currentFrameChars)
drawColorGrid(currentFrameBgs)
drawColorGrid(currentFrameFgs)
setmetatable(lastFrameChars, cachedMeta)
outFile:write("\nminBounds (", minBoundsX, ", ", minBoundsY, ")\n")
outFile:write("\nmaxBounds (", maxBoundsX, ", ", maxBoundsY, ")\n")
local x, y = dcap.checkFramebufferUpdate()
outFile:write("\ncheckFramebufferUpdate() = ", tostring(x), ", ", tostring(y), "\n")
end
local function forceFramebufferUpdate()
if next(lastFrameChars) ~= nil then
lastFrameChars = setmetatable({}, getmetatable(lastFrameChars))
lastFrameBgs = {}
lastFrameFgs = {}
end
if next(currentFrameChars) ~= nil then
currentFrameChars = {}
currentFrameBgs = {}
currentFrameFgs = {}
end
forceUpdated = true
end
local function onGpuSetBackground(color, isPaletteIndex)
currentBg = isPaletteIndex and -color - 1 or color
end
local function onGpuSetForeground(color, isPaletteIndex)
currentFg = isPaletteIndex and -color - 1 or color
end
local function onGpuSetResolution(width, height)
local maxWidth, maxHeight = gpu.maxResolution()
if width > 0 and width < maxWidth and height > 0 and height < maxHeight and (width ~= resolutionX or height ~= resolutionY) then
resolutionX = width
resolutionY = height
forceFramebufferUpdate()
end
end
local function onGpuSet(x, y, value, vertical)
--[[if
minBoundsX = math.min(minBoundsX, x)
minBoundsY = math.min(minBoundsY, y)
if #value == unicode.len(value) then
if not vertical then
local stopX = math.min(x + #value - 1, resolutionX)
local i = (y - 1) * resolutionX + x
for charIndex = 1, stopX - x + 1 do
lastFrameChars[i]
currentFrameChars[i] = value:sub(charIndex, charIndex)
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
end
maxBoundsX = math.max(maxBoundsX, stopX)
maxBoundsY = math.max(maxBoundsY, y)
else
local stopY = math.min(y + #value - 1, resolutionY)
local i = (y - 1) * resolutionX + x
for charIndex = 1, stopY - y + 1 do
lastFrameChars[i]
currentFrameChars[i] = value:sub(charIndex, charIndex)
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
end
maxBoundsX = math.max(maxBoundsX, x)
maxBoundsY = math.max(maxBoundsY, stopY)
end
elseif not vertical then
else
local stopY = math.min(y + unicode.len(value) - 1, resolutionY)
local i = (y - 1) * resolutionX + x
for charIndex = 1, stopY - y + 1 do
lastFrameChars[i]
currentFrameChars[i] = unicode.sub(value, charIndex, charIndex)
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
end
maxBoundsX = math.max(maxBoundsX, x)
maxBoundsY = math.max(maxBoundsY, stopY)
end--]]
-- skip perfect unicode handling for now, not really needed
if not vertical then
if y < 1 or y > resolutionY then
return
end
local startX = math.max(x, 1)
local stopX = math.min(x + unicode.wlen(value) - 1, resolutionX)
local i = (y - 1) * resolutionX + startX
local charIndex = startX - x + 1
local lastCharWidth = 1
for blockIndex = charIndex, stopX - x + 1 do
_ = lastFrameChars[i]
if lastCharWidth <= 1 then
local char = unicode.sub(value, charIndex, charIndex)
if char == "" then
char = " "
end
currentFrameChars[i] = char
charIndex = charIndex + 1
lastCharWidth = unicode.charWidth(char)
else
currentFrameChars[i] = " "
lastCharWidth = lastCharWidth - 1
end
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
i = i + 1
end
minBoundsX = math.min(minBoundsX, startX)
minBoundsY = math.min(minBoundsY, y)
maxBoundsX = math.max(maxBoundsX, stopX)
maxBoundsY = math.max(maxBoundsY, y)
else
if x < 1 or x > resolutionX then
return
end
local startY = math.max(y, 1)
local stopY = math.min(y + unicode.len(value) - 1, resolutionY)
local i = (startY - 1) * resolutionX + x
for charIndex = startY - y + 1, stopY - y + 1 do
_ = lastFrameChars[i]
currentFrameChars[i] = unicode.sub(value, charIndex, charIndex)
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
i = i + resolutionX
end
minBoundsX = math.min(minBoundsX, x)
minBoundsY = math.min(minBoundsY, startY)
maxBoundsX = math.max(maxBoundsX, x)
maxBoundsY = math.max(maxBoundsY, stopY)
end
end
local function onGpuCopy(x, y, width, height, tx, ty)
-- Find bounds of copy area and target area that both land on screen.
local startX = math.max(math.max(x, 1) + tx, 1) - tx
local startY = math.max(math.max(y, 1) + ty, 1) - ty
local stopX = math.min(math.min(x + width - 1, resolutionX) + tx, resolutionX) - tx
local stopY = math.min(math.min(y + height - 1, resolutionY) + ty, resolutionY) - ty
--tx = tx + x
--ty = ty + y
-- Trim bounds such that target area also lands on screen.
--startX = math.max(startX + tx, 1) - tx
--startY = math.max(startY + ty, 1) - ty
--stopX = math.min(stopX + tx, resolutionX) - tx
--stopY = math.min(stopY + ty, resolutionY) - ty
if startX <= stopX and startY <= stopY then
minBoundsX = math.min(minBoundsX, startX + tx)
minBoundsY = math.min(minBoundsY, startY + ty)
maxBoundsX = math.max(maxBoundsX, stopX + tx)
maxBoundsY = math.max(maxBoundsY, stopY + ty)
end
local deltaX = 1
if tx > 0 then
startX, stopX = stopX, startX
deltaX = -1
end
local deltaY = 1
if ty > 0 then
startY, stopY = stopY, startY
deltaY = -1
end
for y = startY, stopY, deltaY do
local i = (y - 1) * resolutionX + startX
local ti = (y + ty - 1) * resolutionX + startX + tx
for x = startX, stopX, deltaX do
_ = lastFrameChars[ti]
local char = currentFrameChars[i]
if char then
currentFrameChars[ti] = char
currentFrameBgs[ti] = currentFrameBgs[i]
currentFrameFgs[ti] = currentFrameFgs[i]
else
currentFrameChars[ti] = lastFrameChars[i]
currentFrameBgs[ti] = lastFrameBgs[i]
currentFrameFgs[ti] = lastFrameFgs[i]
end
i = i + deltaX
ti = ti + deltaX
end
end
end
local function onGpuFill(x, y, width, height, char)
if char ~= unicode.sub(char, 1, 1) then
return
end
local charWidth = unicode.charWidth(char)
-- Find bounds of fill area that land on screen.
local startX = math.max(x, 1)
local startY = math.max(y, 1)
local stopX = math.min(x + width * charWidth - 1, resolutionX)
local stopY = math.min(y + height - 1, resolutionY)
if startX <= stopX and startY <= stopY then
minBoundsX = math.min(minBoundsX, startX)
minBoundsY = math.min(minBoundsY, startY)
maxBoundsX = math.max(maxBoundsX, stopX)
maxBoundsY = math.max(maxBoundsY, stopY)
end
for y = startY, stopY do
local i = (y - 1) * resolutionX + startX
for x = startX, stopX do
_ = lastFrameChars[i]
if charWidth == 1 or (x - startX) % charWidth == 0 then
currentFrameChars[i] = char
else
currentFrameChars[i] = " "
end
currentFrameBgs[i] = currentBg
currentFrameFgs[i] = currentFg
i = i + 1
end
end
end
function dcap.setupFramebuffers()
resolutionX, resolutionY = gpu.getResolution()
currentBg = dcap.getBackground()
currentFg = dcap.getForeground()
lastFrameChars = setmetatable({}, {
__index = function(t, k)
local y = math.floor((k - 1) / resolutionX) + 1
local x = k - (y - 1) * resolutionX
local char, fg, bg, fgIndex, bgIndex = gpu.get(x, y)
t[k] = char
lastFrameBgs[k] = bgIndex and -bgIndex - 1 or bg
lastFrameFgs[k] = fgIndex and -fgIndex - 1 or fg
end
})
lastFrameBgs = {}
lastFrameFgs = {}
currentFrameChars = {}
currentFrameBgs = {}
currentFrameFgs = {}
minBoundsX = resolutionX + 1
minBoundsY = resolutionY + 1
maxBoundsX = 0
maxBoundsY = 0
forceUpdated = false
local gpuPreCallbacks = {
setBackground = onGpuSetBackground,
setForeground = onGpuSetForeground,
setPaletteColor = forceFramebufferUpdate,
setDepth = forceFramebufferUpdate,
setResolution = onGpuSetResolution,
setViewport = forceFramebufferUpdate,
set = onGpuSet,
copy = onGpuCopy,
fill = onGpuFill,
}
return gpuPreCallbacks
end
function dcap.checkFramebufferUpdate()
if forceUpdated then
return true
end
for y = minBoundsY, maxBoundsY do
local i = (y - 1) * resolutionX + minBoundsX
for x = minBoundsX, maxBoundsX do
if currentFrameChars[i] and (currentFrameChars[i] ~= lastFrameChars[i] or currentFrameBgs[i] ~= lastFrameBgs[i] or currentFrameFgs[i] ~= lastFrameFgs[i]) then
return x, y
end
i = i + 1
end
end
end
function dcap.swapFramebuffers()
if next(currentFrameChars) ~= nil or next(lastFrameChars) ~= nil then
lastFrameChars = setmetatable(currentFrameChars, getmetatable(lastFrameChars))
lastFrameBgs = currentFrameBgs
lastFrameFgs = currentFrameFgs
currentFrameChars = {}
currentFrameBgs = {}
currentFrameFgs = {}
end
minBoundsX = resolutionX + 1
minBoundsY = resolutionY + 1
maxBoundsX = 0
maxBoundsY = 0
forceUpdated = false
end
local gpuRealFuncs
function dcap.debugBind()
local gpuPreCallbacks = dcap.setupFramebuffers()
gpuRealFuncs = {}
for k, v in pairs(gpu) do
local gpuCallback = gpuPreCallbacks[k]
if gpuCallback then
gpuRealFuncs[k] = v
gpu[k] = function(...)
local status, ret = xpcall(gpuCallback, debug.traceback, ...)
dlog.out("gpu", k, " called with:", {...}, "status is ", status, " ", ret)
return v(...)
end
end
end
end
function dcap.debugUnbind()
for k, v in pairs(gpuRealFuncs) do
gpu[k] = v
end
gpuRealFuncs = nil
end
-- left to test:
-- swapping buffers - done
-- color change - done
-- depth/viewport change - done
--[[
local outFile = io.open("/home/dcap_state.txt", "w")
assert(outFile)
dlog.out("start")
dcap.debugBind()
--gpu.set(1, 1, "hello world!")
--gpu.set(1, 2, "★star★")
--gpu.set(158, 3, "abcdefgh")
gpu.fill(4, 3, 5, 6, "★")
gpu.set(4, 3, "abcdefghij")
gpu.set(4, 4, "klmnopqrst")
gpu.set(4, 5, "uvwxyz0123")
gpu.set(4, 6, "456789ABCD")
gpu.set(4, 7, "EFGHIJKLMN")
gpu.set(4, 8, "OPQRSTUVWX")
--gpu.copy(4, 3, 10, 6, 47, 155)
--gpu.copy(4, 3, 10, 6, 150, 45)
gpu.setForeground(0, true)
gpu.setBackground(7, true)
gpu.fill(159, 49, 3, 4, "x")
gpu.setBackground(0)
gpu.setForeground(0xFFFFFF)
dcap.logState(outFile)
dcap.swapFramebuffers()
gpu.copy(4, 3, 10, 6, 150, 45)
dcap.logState(outFile)
dcap.swapFramebuffers()
gpu.setForeground(0xFF0000)
gpu.set(4, 3, "ab")
--gpu.setViewport(160, 50)
dcap.logState(outFile)
dcap.swapFramebuffers()
dcap.debugUnbind()
dlog.out("end")
outFile:close()
--]]
return dcap