-1

I'm currently using a LuaJIT wrapper for SDL2 (see "anima" on github) but, unfortunately, SDL_ttf was not included in the ffi and I'm having trouble using additional libraries on Windows. Furthermore, the requirements for this project already risk getting quite bloated.

Anyways, I just need a way to print text to the window surface. Is there a way to do so that is relatively simple? Before anyone asks, yes, I did try C, but trying to use SDL_ttf there has been its own nightmare and I would prefer to use Lua as much as possible other than compiling a very simple executable for a clean-looking frontend.

Edit: I suppose I should clarify that SDL2 simply has not been working when I attempt to use SDL2 in C, but considering that such isn't the scope of the question, I digress

  • A complete SDL2 troubleshooting guide for C (assuming you use MinGW): https://stackoverflow.com/a/64396980/2752075 – HolyBlackCat Jun 13 '21 at 21:26
  • Sorry, I don't know SDL2. `I just need a way to print text to the window surface` - Can you get WindowHandle or DeviceContext of the surface and use primitive WinAPI functions (via LuaJIT FFI) to write a text on that surface? – Egor Skriptunoff Jun 14 '21 at 06:44
  • @EgorSkriptunoff That has potential... There is a way to load windows dlls and (I guess) use their respective C calls from the LuaJIT interpreter. Thank you for the idea – Iona Erofeeff Jun 18 '21 at 03:53

1 Answers1

0

You could with bitmap fonts, but the anima repo that you mentioned doesn't appear to have SDL.ttf so truetype is likely out. 'Prolly have better results with https://github.com/Tangent128/luasdl2. Functionally, they're nearly identical, just a few differences in syntax. Also, you don't neccissarily have to litter your script with all the error tests, but they recommend them for dev feedback.

#! /usr/bin/env luajit
--  luarocks install lua-sdl2
--  apt install fonts-freefont-ttf

local SDL = require( 'SDL' )
local font = require( 'SDL.ttf' )

local function et( this, that )  --  error test
    if not this then error( that ) end
end

local ret, err = SDL .init { SDL .flags .Video } ; et( ret, err )
local ret, err = font .init() ; et( ret, err )

local win, err = SDL .createWindow { title='Font', width=200, height=100 } ; et( win, err )
local rdr, err = SDL .createRenderer( win, 0, 0 ) ; et( rdr, err )

local fonts = '/usr/share/fonts/truetype/freefont/'
local mono = fonts ..'FreeMono.ttf'
local sans = fonts ..'FreeSans.ttf'
local serif = fonts ..'FreeSerif.ttf'

local fon, home = font .open( serif, 40 ) ; et( fon, home )

local white = { r = 255,  g = 255,  b = 255 }
local surf, err = fon :renderUtf8( 'Gérard!', 'blended', white ) ; et( surf, err )

local texture, err = rdr :createTextureFromSurface( surf ) ; et( texture, err )

rdr :clear()
rdr :copy( texture )
rdr :present()

local seconds = 3
local miliseconds = seconds *1000
SDL .delay( miliseconds )
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
  • I appreciate that this would work if I had the library, but luarocks would not install on my system. Furthermore, I don't think the existence of luarocks in my system would be conducive to the accessibility of the current project. Thank you for the answer anyway... it just doesn't really answer the question – Iona Erofeeff Jun 18 '21 at 03:51