Text drawn to sprite not working right.
Blitz3D Forums/Blitz3D Programming/Text drawn to sprite not working right.
| ||
Hi there this is very simple but for some reason will not work, heres a small section of code:p.part=New part title2$="Chris Crinkle Presents" xsz#=Len(title2)*12 ysz#=32 p\tex=CreateTexture(xsz,ysz) SetBuffer TextureBuffer(p\tex) t_font=LoadFont("courier new",ysz,1) SetFont t_font Color 255,255,255 Text xsz/2,ysz/2,title2$,1,1 SetBuffer BackBuffer() p\ent=CreateSprite() EntityTexture p\ent,p\tex p\x=GraphicsWidth()/2 p\y=GraphicsHeight()/2 ScaleSprite p\ent,xsz,ysz So what you can probably guess is happening a sprite is being made and an texture created for it, then the text title2$ should be drawn bang in the middle of the sprite texture. However the code, which looks right, doesn't work. "xsz" and "ysz" are the resolutions of the texture, so dividing them in half would be the centre of the texture. The text however is always off at one side and definately is NOT centered!!? |
| ||
And then i thought "bugger it, i'll draw from the corner and forget about centering":p.part=New part title2$="Chris Crinkle Presents" xsz#=Len(title2$)*11 ysz#=32 p\tex=CreateTexture(xsz,ysz) SetBuffer TextureBuffer(p\tex) t_font=LoadFont("courier new",ysz,1) SetFont t_font Color 255,255,255 Text 0,0,title2$ SetBuffer BackBuffer() p\ent=CreateSprite() EntityTexture p\ent,p\tex ScaleSprite p\ent,xsz*1,ysz*1 There is an error which can be achieved by changing the xsz multiplier from 11 to 12. With 11, it's working at the correct aspect ratio. But at 12 it suddenly adds much more to it and screws up the aspect ratio. Any help around this bug would be appriciated! |
| ||
Blitz3D textures can only exist in sizes of the next largest power-of-two. What happens when you create a texture of an arbitrary size can actually vary from machine to machine, so it's usually best not to do this. Your bug with the point size change is probably because you're tryign to create the texture with an X size of 242 instead of 264, which kicks its real size down a notch from 512 to 256 (and the correct stringwidth is 374). This is probably messing with the aspect ratio and stretching the image when this line is crossed, as well as throwing off the centring by being far larger than expected in the original size. Another point of note is that you should not use Len() to calculate string sizes. Using a monospaced font at least gives you some leeway, but it's easiest (and most accurate) to just use the StringWidth command, which returns the correct result for the currently loaded font (rather larger than your calculation result). Point sizes don't provide any meaningful information in pixels to the user, remember. |
| ||
I did the same mistake until I discovered "StringWidth " :) very useful command. It made me read the docs with more attention. One thing I'd like to mention: don't use Text directly on a texturebuffer, it will work on some machines, and not on others. Better Text to the backbuffer, then copyrect to the texturebuffer(). Last edited 2010 |