SFML community forums

Bindings - other languages => D => Topic started by: joelcnz on September 09, 2015, 09:51:46 am

Title: Drawing text to a Image object
Post by: joelcnz on September 09, 2015, 09:51:46 am
Is it possible to draw text onto a image object? I've made a DSFML version of a chart from an Allegro 5.0 library.
Title: Re: Drawing text to a Image object
Post by: Ztormi on September 09, 2015, 10:05:16 am
So basically you want to draw an image, text on top of that and then get an image out of it? You could use a RenderTexture (http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTexture.php), draw your image as Sprite, draw text (http://www.sfml-dev.org/documentation/2.3.1/classsf_1_1Text.php) over the sprite. Then get the texture out of rendertexture with getTexture (http://www.sfml-dev.org/documentation/2.0/classsf_1_1RenderTexture.php#a95bc5152c497066d31fdc57da8e17678) and copyToImage (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Texture.php#aefc19bcd95565dd2348fd4cec0facddc)
Title: Re: Drawing text to a Image object
Post by: joelcnz on September 09, 2015, 10:15:30 am
I'm having trouble trying to work this out.  ??? Could I have an example?
Title: Re: Drawing text to a Image object
Post by: Jebbs on September 09, 2015, 07:00:03 pm
Should be something like this:


renderTexture.draw(base);
renderTexture.draw(text);

renderTexture.display();

auto capturedTexture = renderTexture.getTexture()

//do stuff with the captured texture, like save it to an image.

 
Title: Re: Drawing text to a Image object, and image onto RenderTexture, & save to disc
Post by: joelcnz on September 10, 2015, 09:13:00 am
I want to do a calander. It takes a loaded picture for each day. And text like the days of the week and day numbers etc.

I can’t see how I can get a loaded image file to go onto a RenderTexture.

   auto renderTexture = new RenderTexture;
   renderTexture.create(4010, 2835);

   auto g_font = new Font;
   g_font.loadFromFile("DejaVuSans.ttf");

   auto txt = new Text("Test 1.2.3.", g_font, 30);
   txt.setColor( Color.Green );
   txt.position(Vector2f(10, 60));
   
   renderTexture.draw(txt);
   
   auto capturedTexture = renderTexture.getTexture;
   auto toSave = capturedTexture.copyToImage;
   
   if (!toSave.saveToFile("output.png"))
       return -1;

source/app.d(14): Error: mutable method dsfml.graphics.texture.Texture.copyToImage is not callable using a const object
FAIL .dub/build/application-debug-posix.osx-x86_64-dmd_2068-CD798876B80E564B1F2D02B479176F6C/ template-dfsml executable
Error executing command run: dmd failed with exit code 1.
Title: Re: Drawing text to a Image object, and image onto RenderTexture, & save to disc
Post by: Jesper Juhl on September 10, 2015, 09:21:23 am
I can’t see how I can get a loaded image file to go onto a RenderTexture.
Load the image to a texture. Create a sprite with that texture. Draw the sprite to the rendertexture.
Title: Re: Drawing text to a Image object
Post by: joelcnz on September 10, 2015, 09:51:51 am
Thanks Jesper Juhl. But still don't know how to save RenderTexture objects to disc.
Title: Re: Drawing text to a Image object
Post by: eXpl0it3r on September 10, 2015, 11:22:42 am
Extract the texture from the RenderTexture, call copyToImage and then saveToFile().
Title: Re: Drawing text to a Image object, and image onto RenderTexture, & save to disc
Post by: Jebbs on September 10, 2015, 05:12:30 pm
source/app.d(14): Error: mutable method dsfml.graphics.texture.Texture.copyToImage is not callable using a const object
FAIL .dub/build/application-debug-posix.osx-x86_64-dmd_2068-CD798876B80E564B1F2D02B479176F6C/ template-dfsml executable
Error executing command run: dmd failed with exit code 1.

That's a bug in DSFML. It's fixed in the 2.1-rc branch which should be released in the next few days.

Out of curiosity, why do you need to make a new image for your calendar? Is just drawing the base image and the text on top not enough?
Title: Re: Drawing text to a Image object
Post by: joelcnz on September 11, 2015, 12:51:51 am
I print out the calenders, so I want hi res pictures.
Title: Re: Drawing text to a Image object
Post by: Jebbs on September 11, 2015, 01:55:42 am
 Ah, that makes sense. Well, unless you want to build the library yourself you'll have to wait a couple of days till I get the last of everything put together for the release.

Sorry for the inconvenience.
Title: Re: Drawing text to a Image object
Post by: joelcnz on September 11, 2015, 07:03:29 am
I did a print out with the day images only. I then got a pen out and wrote in the missing text.
Title: Re: Drawing text to a Image object
Post by: joelcnz on November 19, 2015, 03:09:34 am
I still haven't gotten my calendar to save as a picture file.  :( I've got bitmaps working.

http://dpaste.dzfl.pl/a1d1c54f019c (I cleaned out the code comments).
Title: Re: Drawing text to a Image object
Post by: Jebbs on November 21, 2015, 03:20:45 am
I still haven't gotten my calendar to save as a picture file.  :( I've got bitmaps working.

http://dpaste.dzfl.pl/a1d1c54f019c (I cleaned out the code comments).

Would it be possible to open up an issue on github so that this doesn't get forgotten? I'm pretty overloaded at the moment and will be for a couple of weeks yet. Sorry for the delay. :(