Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing text to a Image object  (Read 27551 times)

0 Members and 4 Guests are viewing this topic.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Drawing text to a Image object
« 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.

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: Drawing text to a Image object
« Reply #1 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, draw your image as Sprite, draw text over the sprite. Then get the texture out of rendertexture with getTexture and copyToImage

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #2 on: September 09, 2015, 10:15:30 am »
I'm having trouble trying to work this out.  ??? Could I have an example?

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #3 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.

 
DSFML - SFML for the D Programming Language.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
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.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #6 on: September 10, 2015, 09:51:51 am »
Thanks Jesper Juhl. But still don't know how to save RenderTexture objects to disc.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing text to a Image object
« Reply #7 on: September 10, 2015, 11:22:42 am »
Extract the texture from the RenderTexture, call copyToImage and then saveToFile().
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
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?
DSFML - SFML for the D Programming Language.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #9 on: September 11, 2015, 12:51:51 am »
I print out the calenders, so I want hi res pictures.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #10 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.
DSFML - SFML for the D Programming Language.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #11 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.

joelcnz

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #12 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).
« Last Edit: November 19, 2015, 07:44:31 am by joelcnz »

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Drawing text to a Image object
« Reply #13 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. :(
DSFML - SFML for the D Programming Language.