From cd1664f33b8a6c455c0ca220bd3875df99846ed4 Mon Sep 17 00:00:00 2001 From: Astoria Date: Sat, 31 Dec 2022 19:58:06 -0600 Subject: [PATCH] add drawtext --- draw/characters | 35 +++++++++++++++++++++++++++++++++++ draw/drawtext | 40 ++++++++++++++++++++++++++++++++++++++++ drawtest | 6 ++++++ 3 files changed, 81 insertions(+) create mode 100644 draw/characters create mode 100644 draw/drawtext create mode 100644 drawtest diff --git a/draw/characters b/draw/characters new file mode 100644 index 0000000..4a1c312 --- /dev/null +++ b/draw/characters @@ -0,0 +1,35 @@ +--Tom Thumb font https://robey.lag.net/2010/01/23/tiny-monospace-font.html +characters = {} +h = 3 +v = 5 +characters["a"] = {{false, true, false}, {true, false, true}, {true, true, true}, {true, false, true}, {true, false, true}} +characters["b"] = {{true, true, false}, {true, false, true}, {true, true, false}, {true, false, true}, {true, true, false}} +characters["c"] = {{false, true, true}, {true, false, false}, {true, false, false}, {true, false, false}, {false, true, true}} +characters["d"] = {{true, true, false}, {true, false, true}, {true, false, true}, {true, false, true}, {true, true, false}} +characters["e"] = {{true, true, true}, {true, false, false}, {true, true, true}, {true, false, false}, {true, true, true}} +characters["f"] = {{true, true, true}, {true, false, false}, {true, true, true}, {true, false, false}, {true, false, false}} +characters["g"] = {{false, true, true}, {true, false, false}, {true, true, true}, {true, false, true}, {false, true, true}} +characters["h"] = {{true, false, true}, {true, false, true}, {true, true, true}, {true, false, true}, {true, false, true}} +characters["i"] = {{true, true, true}, {false, true, false}, {false, true, false}, {false, true, false}, {true, true, true}} +characters["j"] = {{false, false, true}, {false, false, true}, {false, false, true}, {true, false, true}, {false, true, false}} +characters["k"] = {{true, false, true}, {true, false, true}, {true, true, false}, {true, false, true}, {true, false, true}} +characters["l"] = {{true, false, false}, {true, false, false}, {true, false, false}, {true, false, false}, {true, true, true}} +characters["m"] = {{true, false, true}, {true, true, true}, {true, true, true}, {true, false, true}, {true, false, true}} +characters["n"] = {{true, false, true}, {true, true, true}, {true, true, true}, {true, true, true}, {true, false, true}} +characters["o"] = {{false, true, false}, {true, false, true}, {true, false, true}, {true, false, true}, {false, true, false}} +characters["p"] = {{true, true, false}, {true, false, true}, {true, true, false}, {true, false, false}, {true, false, false}} +characters["q"] = {{false, true, false}, {true, false, true}, {true, false, true}, {true, true, true}, {false, true, true}} +characters["r"] = {{true, true, false}, {true, false, true}, {true, true, true}, {true, true, false}, {true, false, true}} +characters["s"] = {{false, true, true}, {true, false, false}, {false, true, false}, {false, false, true}, {true, true, false}} +characters["t"] = {{true, true, true}, {false, true, false}, {false, true, false}, {false, true, false}, {false, true, false}} +characters["u"] = {{true, false, true}, {true, false, true}, {true, false, true}, {true, false, true}, {false, true, true}} +characters["v"] = {{true, false, true}, {true, false, true}, {true, false, true}, {true, false, true}, {false, true, false}} +characters["w"] = {{true, false, true}, {true, false, true}, {true, true, true}, {true, true, true}, {true, false, true}} +characters["x"] = {{true, false, true}, {true, false, true}, {false, true, false}, {true, false, true}, {true, false, true}} +characters["y"] = {{true, false, true}, {true, false, true}, {false, true, false}, {false, true, false}, {false, true, false}} +characters["z"] = {{true, true, true}, {false, false, true}, {false, true, false}, {true, false, false}, {true, true, true}} +characters[" "] = {{}, {}, {}, {}, {}} +characters["."] = {{}, {}, {}, {}, {false, true, false}} +characters["!"] = {{false, true, false}, {false, true, false}, {false, true, false}, {}, {false, true, false}} +characters["?"] = {{true, true, true}, {false, false, true}, {false, true, false}, {}, {false, true, false}} +characters["NOSYMBOL"] = {{false, false, false}, {true, true, false}, {true, false, true}, {true,true,true}, {true, false, true}} \ No newline at end of file diff --git a/draw/drawtext b/draw/drawtext new file mode 100644 index 0000000..d21208b --- /dev/null +++ b/draw/drawtext @@ -0,0 +1,40 @@ +--Get Font +dofile("draw/characters") + +--For each letter draw a character. +function drawText(gpu, cstring, x, y, red, green, blue, alpha) + for pos=1,string.len(cstring),1 do + drawChar(gpu, string.sub(string.lower(cstring), pos, pos), x + ((pos - 1) * 4), y, red, green, blue, alpha) + end +end +--Draw a single character. +function drawChar(gpu, character, x, y, red, green, blue, alpha) + gpured, gpublue, gpugreen, gpualpha = gpu.getColor() + red = red or gpured + blue = blue or gpublue + green = green or gpugreen + alpha = alpha or gpualpha + if (type(alpha) == "nil") then + alpha = 255 + end + gpu.setColor(red, green, blue, alpha) + for ix=1,h,1 do + for iy=1,v,1 do + if (type(characters[character]) == "table") then + if(characters[character][iy][ix] == true) then + gpu.plot(ix+x,iy+y) + end + else + if(characters["?"][iy][ix] == true) then + gpu.setColor(255, 0, 0, 255) + gpu.plot(ix+x,iy+y) + else + gpu.setColor(0, 0, 0, 255) + gpu.plot(ix+x,iy+y) + end + gpu.setColor(red, green, blue, alpha) + end + end + end +end + diff --git a/drawtest b/drawtest new file mode 100644 index 0000000..ee46982 --- /dev/null +++ b/drawtest @@ -0,0 +1,6 @@ +os.loadAPI("drawtext") +gpu = peripheral.wrap("right") +gpu.setColor(0,0,0) +gpu.fill() +gpu.setColor(255,255,255) +drawtext.drawText(gpu, "test#", 1, 3)