Useful Snippets #11
Replies: 8 comments
-
Basic window loopA very basic window loop that exits when the Escape key is pressed: while window:loop() and not window.keys[27] do
-- Do stuff here
end |
Beta Was this translation helpful? Give feedback.
-
Key code mapHere is a very basic key code map: local keys = {
backspace = 8,
tab = 9,
enter = 10,
arrow_up = 17,
arrow_down = 18,
arrow_right = 19,
arrow_left = 20,
escape = 27,
space = 32,
["'"] = 39,
[','] = 44,
['-'] = 45,
['.'] = 46,
['/'] = 47,
['0'] = 48,
['1'] = 49,
['2'] = 50,
['3'] = 51,
['4'] = 52,
['5'] = 53,
['6'] = 54,
['7'] = 55,
['8'] = 56,
['9'] = 57,
a = 65,
b = 66,
c = 67,
d = 68,
e = 69,
f = 70,
g = 71,
h = 72,
i = 73,
j = 74,
k = 75,
l = 76,
m = 77,
n = 78,
o = 79,
p = 80,
q = 81,
r = 82,
s = 83,
t = 84,
u = 85,
v = 86,
w = 87,
x = 88,
y = 89,
z = 90,
['['] = 91,
['\\'] = 92,
[']'] = 93,
['`'] = 96,
delete = 127,
}
-- Usage
if window.keys[keys.enter] then
-- Enter key is pressed
end There might be some keys missing. Let me know if you find one! Tip Some key codes can also be retrieved using the local f_key = string.byte('F') -- returns 70 |
Beta Was this translation helpful? Give feedback.
-
Calculate FPSCalculating the FPS to measure performance is super easy with the window delta: local fps = 1 / window.delta |
Beta Was this translation helpful? Give feedback.
-
Drawing rectanglesHere is a simple function to draw rectangles: local function draw_rectangle(window, x, y, width, height, color)
local dx_end = x + width - 1
for dy = y, y + height - 1 do
for dx = x, dx_end do
window:set(dx, dy, color)
end
end
end |
Beta Was this translation helpful? Give feedback.
-
Drawing circlesHere is a simple function to draw circles: local function draw_circle(window, x, y, radius, color)
local radius_neg = -radius
local radius_pow2 = radius * radius
for dy = radius_neg, radius do
local dy_pow2 = dy * dy
local sy = y + dy
for dx = radius_neg, radius do
if dx * dx + dy_pow2 < radius_pow2 then
window:set(x + dx, sy, color)
end
end
end
end |
Beta Was this translation helpful? Give feedback.
-
Drawing linesHere is a simple function to draw lines: local function draw_line(window, x0, y0, x1, y1, color)
local dx = math.abs(x1 - x0)
local dy = math.abs(y1 - y0)
local sx = x0 < x1 and 1 or -1
local sy = y0 < y1 and 1 or -1
local err = (dx > dy and dx or -dy) / 2
local e2
while true do
window:set(x0, y0, color)
if x0 == x1 and y0 == y1 then
break
end
e2 = err
if e2 > -dx then
err = err - dy
x0 = x0 + sx
end
if e2 < dy then
err = err + dx
y0 = y0 + sy
end
end
end |
Beta Was this translation helpful? Give feedback.
-
Loading and drawing imagesImages are a bit more complex to load and draw. I recommend using a very simple image format like Netpbm PPM (either the ASCII format, Here's the command for ImageMagick to convert a PNG to PPM: magick image.png -depth 8 image.ppm You could use GIMP as well, but keep in mind that GIMP adds a comment to the header of the PPM file, which makes it a bit more complicated to parse compared to the ImageMagick output. Although, the PPM loading function I wrote can handle comments. You can find my own PPM loading function in the Here is a function to draw the loaded data returned from my PPM loading function: local function draw_image(window, x, y, image_pixels, image_width, image_height)
for iy = 1, image_height do
local dy = y + (iy - 1)
for ix = 1, image_width do
window:set(x + (ix - 1), dy, image_pixels[iy][ix])
end
end
end |
Beta Was this translation helpful? Give feedback.
-
Drawing textSince most text drawing functions require large tables of font data I will not include one here for now. Check out the text-editor demo for an example on drawing text. It includes the microknight font and a function to draw text. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In this discussion I will collect many useful snippets for the lua-fenster library that I have come across. These snippets should be relatively short and general. Feel free to post your own snippets!
Beta Was this translation helpful? Give feedback.
All reactions