1. FAO Andy P, SDL2 library.
- Posted by ChrisB (moderator) Mar 27, 2021
- 909 views
Hi Andy.
Playing with your SDL2 library now, got it working with Phix, apart from font colouring which I am tearing my hair out at.
See the FontTest.exw, I have fiddled, so the line numbers may not be exact, lines 39 40
atom h = TTF_RenderText_Solid(f,"Hello World",255,0,0,100) --<--colour set here to red, alpha 100 atom ht = SDL_CreateTextureFromSurface(ren,h)
atom h = TTF_RenderText_Solid(f,"Hello World",0,255,0,100) --<--colour set here to green, alpha 100 --but this doesn't work, text now black. --in fact setting the red channel to anything, only shows the --strength of that red atom ht = SDL_CreateTextureFromSurface(ren,h)
I've done lots of reading of the SDL2 docs, the SDL2TTF docs, and I can't see what I'm missing. I've also tried setTextureColorMod to change the color chanels of ht, but again only the red channel was modified.
Any ideas?
Cheers
Chris
2. Re: FAO Andy P, SDL2 library.
- Posted by ghaberek (admin) Mar 28, 2021
- 912 views
Hi Andy.
Playing with your SDL2 library now, got it working with Phix, apart from font colouring which I am tearing my hair out at.
See the FontTest.exw, I have fiddled, so the line numbers may not be exact, lines 39 40
atom h = TTF_RenderText_Solid(f,"Hello World",255,0,0,100) --<--colour set here to red, alpha 100 atom ht = SDL_CreateTextureFromSurface(ren,h)
atom h = TTF_RenderText_Solid(f,"Hello World",0,255,0,100) --<--colour set here to green, alpha 100 --but this doesn't work, text now black. --in fact setting the red channel to anything, only shows the --strength of that red atom ht = SDL_CreateTextureFromSurface(ren,h)
I've done lots of reading of the SDL2 docs, the SDL2TTF docs, and I can't see what I'm missing. I've also tried setTextureColorMod to change the color chanels of ht, but again only the red channel was modified.
Any ideas?
I think the root problem here may be that SDL2 passes structs by value and not by pointer. TTF_RenderText_Solid expects an SDL_Color struct which is four UInt8 (or C_UCHAR in std/dll.e) values but Andy's wrapper is just passing them as four separate C_UINT values: https://github.com/gAndy50/EuSDL2/blob/master/EuSDLTTF.ew#L270. But we should be able to pack the four values into a single unsigned int and pass it that way instead. (Also the fourth value should be "a" for alpha, not "u"). I haven't tested this but I can at least confirm that sizeof(SDL_Color) is 4 which means the values are indeed each a single byte. Give this a shot and see how it goes:
public constant xTTF_RenderText_Solid = define_c_func(ttf,"+TTF_RenderText_Solid",{C_POINTER,C_POINTER,C_UINT},C_POINTER) --SDLColor is a struct of four UInt8 values, so pack them into a single unsigned int. public function TTF_RenderText_Solid(atom font,sequence text,atom r,atom g,atom b,atom a) atom str = allocate_string(text,1) atom color = bytes_to_int({r,g,b,a}) return c_func(xTTF_RenderText_Solid,{font,str,color}) end function
-Greg
3. Re: FAO Andy P, SDL2 library.
- Posted by ChrisB (moderator) Mar 28, 2021
- 884 views
Hi
Yes, that's it,perfect, thanks. I'll fix the othe similar functions as well.
Cheers
Chris
4. Re: FAO Andy P, SDL2 library.
- Posted by ChrisB (moderator) Mar 28, 2021
- 864 views
Hi Greg
What was the process that led you to that conclusion? I can vaguely follow it, but how on earth did you actually get there, where was the information that led you to that. Can I be your disciple?
Cheers
Chris
5. Re: FAO Andy P, SDL2 library.
- Posted by Icy_Viking Mar 28, 2021
- 872 views
Hi Andy.
Playing with your SDL2 library now, got it working with Phix, apart from font colouring which I am tearing my hair out at.
See the FontTest.exw, I have fiddled, so the line numbers may not be exact, lines 39 40
atom h = TTF_RenderText_Solid(f,"Hello World",255,0,0,100) --<--colour set here to red, alpha 100 atom ht = SDL_CreateTextureFromSurface(ren,h)
atom h = TTF_RenderText_Solid(f,"Hello World",0,255,0,100) --<--colour set here to green, alpha 100 --but this doesn't work, text now black. --in fact setting the red channel to anything, only shows the --strength of that red atom ht = SDL_CreateTextureFromSurface(ren,h)
I've done lots of reading of the SDL2 docs, the SDL2TTF docs, and I can't see what I'm missing. I've also tried setTextureColorMod to change the color chanels of ht, but again only the red channel was modified.
Any ideas?
I think the root problem here may be that SDL2 passes structs by value and not by pointer. TTF_RenderText_Solid expects an SDL_Color struct which is four UInt8 (or C_UCHAR in std/dll.e) values but Andy's wrapper is just passing them as four separate C_UINT values: https://github.com/gAndy50/EuSDL2/blob/master/EuSDLTTF.ew#L270. But we should be able to pack the four values into a single unsigned int and pass it that way instead. (Also the fourth value should be "a" for alpha, not "u"). I haven't tested this but I can at least confirm that sizeof(SDL_Color) is 4 which means the values are indeed each a single byte. Give this a shot and see how it goes:
public constant xTTF_RenderText_Solid = define_c_func(ttf,"+TTF_RenderText_Solid",{C_POINTER,C_POINTER,C_UINT},C_POINTER) --SDLColor is a struct of four UInt8 values, so pack them into a single unsigned int. public function TTF_RenderText_Solid(atom font,sequence text,atom r,atom g,atom b,atom a) atom str = allocate_string(text,1) atom color = bytes_to_int({r,g,b,a}) return c_func(xTTF_RenderText_Solid,{font,str,color}) end function
-Greg
I hadn't even noticed this before. Thanks Greg. I wrote this wrapper a long time ago, so its probably sloppy compared to my newer wrappers. the SDL libraries were my first big wrapper projects, probably bit off more than I could chew, but I did get them to work for the most part with some help here and there. The Mixer and Image libraries though, those will need some work. I will apply the changes made by Greg to my current wrapper and upload it to Github shortly. After that I'm still trying to figure out why my TileEngine wrapper is not working correctly.
Also, I'm not sure where u came from instead of a, probably was thinking of unsigned or something at the time.
EDIT: I've uploaded the updated files to my Github.