1. Weird Error
- Posted by Icy_Viking Apr 04, 2023
- 572 views
Hello all,
So while I was trying to make a simple example using my TTF_Font wrapper with my SDL wrapper. I keep getting bad routine number (1).
Error from err file C:\Euphoria\include\std\ffi.e:1011 in function c_func() c_proc/c_func: bad routine number (1) rid = 1 args = {} fn = <no value> cif = <no value> name = <no value> nargs = <no value> parg_types = <no value> rtype = <no value> pargs = <no value> pfree = <no value> arg_types = <no value> i = 3 parg = <no value> rtype_size = <no value> prvalue = <no value> rvalue = <no value> i = 5 ... called from C:\Documents\Eu\SDL2\SDL_ttf.e:60 in function TTF_Init() ^Path modified to protect my security
https://github.com/gAndy50/SDL2Wrapper/blob/main/AddOns/SDL_ttf.e - Font Wrapper
include std/ffi.e include sdl.e include SDL_ttf.e if SDL_Init(SDL_INIT_VIDEO) = -1 then puts(1,"Failed to init SDL!\n") abort(0) end if atom win = SDL_CreateWindow("Font Ex",10,10,800,600,SDL_WINDOW_SHOWN) atom ren = SDL_CreateRenderer(win,-1,0) atom x = TTF_Init() --error from this function if x = -1 then puts(1,"Failed to init TTF!\n") abort(0) end if atom font = TTF_OpenFont("arial.ttf",20) atom r = SDL_SetRenderDrawColor(ren,0,0,0,0) atom run = 1 sequence col = {255,0,0,0} atom h = TTF_RenderText_Solid(font,"Hello World",col) atom ht = SDL_CreateTextureFromSurface(ren,h) atom evt = 0 atom evt_type = 0 evt = allocate_struct(SDL_Event) while run = 1 do while SDL_PollEvent(evt) != 0 do evt_type = peek_type(evt,C_UINT32) if evt_type = SDL_QUIT then run = 0 end if end while SDL_RenderClear(ren) SDL_RenderCopy(ren,ht,0,r) SDL_RenderPresent(ren) end while SDL_DestroyRenderer(ren) SDL_DestroyWindow(win) TTF_CloseFont(font) TTF_Quit() SDL_Quit()
2. Re: Weird Error
- Posted by ChrisB (moderator) Apr 05, 2023
- 551 views
Might be totally off the ball here, but try specifying the full path name to the font, I've had this issue before or similar, and the full path name sorted it.
Cheers
Chris
3. Re: Weird Error
- Posted by ghaberek (admin) Apr 05, 2023
- 528 views
So while I was trying to make a simple example using my TTF_Font wrapper with my SDL wrapper. I keep getting bad routine number (1).
Error from err file C:\Euphoria\include\std\ffi.e:1011 in function c_func() c_proc/c_func: bad routine number (1)
Same issue you've encountered before: the call to define_c_func() is returning -1 meaning the function TTF_OpenFont was not found.
It looks like you're trying to open the wrong library name in SDL_ttf.e. The correct library name is SDL2_ttf.dll (note the "2").
You should really be doing some kind of unit testing or sanity checking when building these wrappers.
I might do basic sanity-checking like this, which just prints a big sequence of all the values:
-- SDL_ttf.e ? ttf & xTTF_Init & -- etc.
And then later I would do unit testing like this:
-- tests/t_sdl_ttf.e include std/unittest.e include AddOns/SDL_ttf.e set_test_abort( 1 ) -- stop testing on failure test_not_equal( "SDL2_ttf", 0, sdl2_ttf ) -- library handle test_not_equal( "TTF_Init", -1, xTTF_Init ) -- function id -- etc.
On a side note, I should probably make std/ffi.e (optionally) crash when functions or libraries are not found, or at least make it optional.
Another note: I'm sure you're using Windows, but please be mindful of file name casing for Linux compatibility.
Best practice would be to reflect the existing names. So if the file is "SDL.h" in then you should use "SDL.e" and make sure to use "include SDL.e" not "include sdl.e"
-Greg
4. Re: Weird Error
- Posted by Icy_Viking Apr 05, 2023
- 523 views
- Last edited Apr 06, 2023
Thanks Greg. I had the naming thing wrong. I fixed it. The example works now. I'll keep your tips in mind.
I've also changed the the naming to libSDL2 and such for Linux/FreeBSD platforms. Yes I usually do my development under Windows, but I do have WSL installed, so I can test for Linux.
Updated example
include std/ffi.e include SDL.e include SDL_ttf.e if SDL_Init(SDL_INIT_VIDEO) = -1 then puts(1,"Failed to init SDL!\n") abort(0) end if atom win = SDL_CreateWindow("Font Ex",10,10,800,600,SDL_WINDOW_SHOWN) if win = -1 then puts(1,"Failed to create window!\n") abort(0) end if atom ren = SDL_CreateRenderer(win,-1,0) if ren = -1 then puts(1,"Failed to create renderer!\n") abort(0) end if atom x = TTF_Init() if x = -1 then puts(1,"Failed to init TTF!\n") abort(0) end if atom font = TTF_OpenFont("arial.ttf",20) if font = -1 then puts(1,"Failed to open font!\n") abort(0) end if atom r = SDL_SetRenderDrawColor(ren,0,0,0,0) atom run = 1 --Color of text (default red) --change values to experiment with different colors sequence col = {255,0,0,0} atom h = TTF_RenderText_Solid(font,"Hello World",col) atom ht = SDL_CreateTextureFromSurface(ren,h) atom evt = 0 atom evt_type = 0 atom key = SDL_GetKeyboardState(NULL) --allocate struct for position and size of text "Hello World" atom rect = allocate_struct(SDL_Rect,{800/4,600/4,200,200}) evt = allocate_struct(SDL_Event) while run = 1 do while SDL_PollEvent(evt) != 0 do evt_type = peek_type(evt,C_UINT32) if evt_type = SDL_QUIT then run = 0 end if end while --quick and dirty code to close program with ESC key SDL_PumpEvents() if peek(key+SDL_SCANCODE_ESCAPE) > 0 then run = 0 end if -- SDL_RenderClear(ren) SDL_RenderCopy(ren,ht,0,rect) SDL_RenderPresent(ren) end while SDL_DestroyRenderer(ren) SDL_DestroyWindow(win) TTF_CloseFont(font) TTF_Quit() SDL_Quit()