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 (P3 or P6) to store the image in a file. A PPM image is basically just a raw list of RGB values with the file header at the start. 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 unfortunately 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. Here is a very simple code to load such a PPM (P6) image: local function load_image(path)
local image = assert(io.open(path, 'rb'))
local image_type = image:read(2)
assert(image_type == 'P6', 'Invalid image type: ' .. tostring(image_type))
assert(image:read(1), 'Invalid image header') -- Whitespace
local image_width = image:read('*number')
assert(image_width, 'Invalid image width: ' .. tostring(image_width))
assert(image:read(1), 'Invalid image header') -- Whitespace
local image_height = image:read('*number')
assert(image_height, 'Invalid image height: ' .. tostring(image_height))
assert(image:read(1), 'Invalid image header') -- Whitespace
local image_max_color = image:read('*number')
assert(
image_max_color == 255,
'Invalid image maximum color: ' .. tostring(image_max_color)
)
assert(image:read(1), 'Invalid image header') -- Whitespace
local image_buffer = {}
while true do
local r_raw = image:read(1)
local g_raw = image:read(1)
local b_raw = image:read(1)
if not r_raw or not g_raw or not b_raw then
break
end
local r = string.byte(r_raw)
local g = string.byte(g_raw)
local b = string.byte(b_raw)
table.insert(image_buffer, fenster.rgb(r, g, b))
end
image:close()
return image_buffer, image_width, image_height
end And here is a function to draw the loaded image: local function draw_image(window, x, y, image_buffer, image_width, image_height)
local ix_end = image_width - 1
for iy = 0, image_height - 1 do
local dy = y + iy
local iy_index = iy * image_width + 1
for ix = 0, ix_end do
window:set(x + ix, dy, image_buffer[iy_index + 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.
-
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