1. SDL TTF 3 Issue
- Posted by Icy_Viking 2 weeks ago
- 608 views
Hi all,
SDL recently released SDL_TTF 3 for SDL 3. However I am having some trouble getting a basic example up and running.
https://github.com/gAndy50/EuSDL3/blob/main/include/SDL_ttf.e - SDL_TTF 3 wrapper
https://wiki.libsdl.org/SDL3_ttf/QuickReference
https://github.com/libsdl-org/SDL_ttf/blob/main/docs/hello.c
This is what I'm trying to convert into Eu code, the program runs fine, but no text is displayed on the screen.
Maybe I'm overlooking something or missing some steps that might be required to display text on the screen?
include std/ffi.e include sdl.e include sdl_ttf.e atom width = 800 atom height = 600 atom flags = NULL if SDL_Init(SDL_INIT_VIDEO+SDL_INIT_EVENTS) = -1 then puts(1,"Failed to init SDL!\n") abort(0) end if sequence title = "Font Example" atom win = SDL_CreateWindow(title,width,height,flags) atom render = SDL_CreateRenderer(win,NULL) atom running = 1 atom event = 0 atom event_type = 0 event = allocate_struct(SDL_Event) sequence color = {255,255,255,SDL_ALPHA_OPAQUE} --SDL_Color if TTF_Init() = -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 text = TTF_RenderText_Blended(font,"Hello World",0,color) atom texture = SDL_CreateTextureFromSurface(render,text) atom w = 0 atom h = 0 atom scale = 4.0 atom text_rect = allocate_struct(SDL_FRect,{w/scale,h/scale,w,h}) while running = 1 do SDL_GetRenderOutputSize(render,w,h) SDL_SetRenderScale(render,scale,scale) SDL_GetTextureSize(texture,w,h) while SDL_PollEvent(event) != 0 do event_type = peek_type(event,C_UINT32) if event_type = SDL_EVENT_QUIT then running = 0 end if end while SDL_RenderClear(render) SDL_RenderTexture(render,texture,NULL,text_rect) SDL_RenderPresent(render) end while SDL_DestroyTexture(texture) SDL_DestroyRenderer(render) SDL_DestroyWindow(win) SDL_Quit()
2. Re: SDL TTF 3 Issue
- Posted by andreasWagner 2 weeks ago
- 544 views
Hallo
I tried but that's as far as I got.
D:\testpool\EuSDL3-main\include\SDL_surface.e:159 <0074>:: Errors resolving the following references: 'SDL_PEN_NUM_AXES' (SDL_events.e:404) has not been declared. 'SDL_PEN_NUM_AXES' (SDL_events.e:417) has not been declared. 'SDL_PEN_NUM_AXES' (SDL_events.e:430) has not been declared. 'SDL_IOFromFile' (SDL_surface.e:147) has been declared more than once. in SDL_iostream.e in SDL_rwops.e 'SDL_IOFromFile' (SDL_surface.e:159) has been declared more than once. in SDL_iostream.e in SDL_rwops.e return SDL_SaveBMP_IO(surface,SDL_IOFromFile(file,"wb"),1) ^ Press Enter
Andreas
3. Re: SDL TTF 3 Issue
- Posted by Icy_Viking 2 weeks ago
- 533 views
Hallo
I tried but that's as far as I got.
D:\testpool\EuSDL3-main\include\SDL_surface.e:159 <0074>:: Errors resolving the following references: 'SDL_PEN_NUM_AXES' (SDL_events.e:404) has not been declared. 'SDL_PEN_NUM_AXES' (SDL_events.e:417) has not been declared. 'SDL_PEN_NUM_AXES' (SDL_events.e:430) has not been declared. 'SDL_IOFromFile' (SDL_surface.e:147) has been declared more than once. in SDL_iostream.e in SDL_rwops.e 'SDL_IOFromFile' (SDL_surface.e:159) has been declared more than once. in SDL_iostream.e in SDL_rwops.e return SDL_SaveBMP_IO(surface,SDL_IOFromFile(file,"wb"),1) ^ Press Enter
Andreas
Oh yeah I need to fix the directory issues. I have all the files in one folder. You can do that to make it work, currently.
EDIT: I have now fixed the directory issue.
4. Re: SDL TTF 3 Issue
- Posted by andreasWagner 2 weeks ago
- 490 views
Hi
I have this running. Note the comments.
include std/ffi.e include std/os.e include std/machine.e include sdl.e include sdl_ttf.e atom width = 800 atom height = 600 atom flags = NULL if SDL_Init(SDL_INIT_VIDEO+SDL_INIT_EVENTS) = -1 then puts(1,"Failed to init SDL!\n") abort(0) end if sequence title = "Font Example" atom win = SDL_CreateWindow(title,width,height,flags) atom render = SDL_CreateRenderer(win,NULL) atom running = 1 atom event = 0 atom event_type = 0 event = allocate_struct(SDL_Event) sequence color = {0,0,255,SDL_ALPHA_OPAQUE} --SDL_Color i prefer blue over whiteif TTF_Init() = 0 then puts(1,"Failed to init TTF!\n") abort(0) end if atom font = TTF_OpenFont("arial1.ttf",20) -- this way it needs to be a file in the current directory -- returns zero not -1 on error ?font if font = 0 then -- returns zero not -1 on error puts(1,"Failed to open font!\n") abort(0) end if atom text = TTF_RenderText_Blended(font,"Hello World",0,color) atom texture = SDL_CreateTextureFromSurface(render,text) atom w = 50 -- width and height should be > 0 atom h = 50 atom scale = 4.0 atom pw=allocate(4) -- it seems it want pointer atom ph=allocate(4) atom key = 0 atom text_rect = allocate_struct(SDL_FRect,{w/scale,h/scale,w,h}) while running = 1 do SDL_GetRenderOutputSize(render,pw,ph) -- it seems it want pointer SDL_SetRenderScale(render,scale,scale) SDL_GetTextureSize(texture,pw,ph) -- it seems it want pointer while SDL_PollEvent(event) != 0 do event_type = peek_type(event,C_UINT32) if event_type = SDL_EVENT_QUIT then running = 0 end if end while SDL_RenderClear(render) SDL_RenderTexture(render,texture,NULL,text_rect) SDL_RenderPresent(render) end while SDL_DestroyTexture(texture) SDL_DestroyRenderer(render) SDL_DestroyWindow(win) SDL_Quit()
Andreas
5. Re: SDL TTF 3 Issue
- Posted by Icy_Viking 2 weeks ago
- 479 views
Thank you, it works! I think it was the pointers that were throwing me off.
6. Re: SDL TTF 3 Issue
- Posted by ghaberek (admin) 2 weeks ago
- 475 views
Thank you, it works! I think it was the pointers that were throwing me off.
I typically wrap "get" functions so the user doesn't have to deal with passing pointers. They can just call the function and get the results in a sequence.
public function SDL_GetTextureSize(atom texture) atom x = 0, y = 0 atom px = allocate_data(sizeof(C_FLOAT)) atom py = allocate_data(sizeof(C_FLOAT)) if c_func(xSDL_GetTextureSize, {texture,px,py}) then x = peek_type(px, C_FLOAT) y = peek_type(py, C_FLOAT) end if free(py) free(px) return {x,y} end function
-Greg
7. Re: SDL TTF 3 Issue
- Posted by Icy_Viking 2 weeks ago
- 477 views
Thank you, it works! I think it was the pointers that were throwing me off.
I typically wrap "get" functions so the user doesn't have to deal with passing pointers. They can just call the function and get the results in a sequence.
public function SDL_GetTextureSize(atom texture) atom x = 0, y = 0 atom px = allocate_data(sizeof(C_FLOAT)) atom py = allocate_data(sizeof(C_FLOAT)) if c_func(xSDL_GetTextureSize, {texture,px,py}) then x = peek_type(px, C_FLOAT) y = peek_type(py, C_FLOAT) end if free(py) free(px) return {x,y} end function
-Greg
Thanks Greg, I will keep this in mind going forward.