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

Author Topic: how can I find out if a RenderTexture is created or not?  (Read 2366 times)

0 Members and 2 Guests are viewing this topic.

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
how can I find out if a RenderTexture is created or not?
« on: September 15, 2012, 10:20:52 am »
Assume I receive a RenderTexture reference as Function parameter. or there is a RenderTexture member in my class, which is not created together with the instance of class.

if it's a sprite, I can do sth like this:

    if (sprite.getTexture())

but a RenderTexture's getTexture() returned a reference but not a pointer, so is there a simple way to know this ?
« Last Edit: September 15, 2012, 10:24:22 am by Hythloday »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: how can I find out if a RenderTexture is created or not?
« Reply #1 on: September 15, 2012, 10:33:42 am »
No, there's no way to know that. You have to manage it yourself.

But wouldn't it be a better design if the function that receives a RenderTexture required it to be created, leaving this "problem" to the calling code (which is probably the one that calls create)?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: how can I find out if a RenderTexture is created or not?
« Reply #2 on: September 15, 2012, 10:57:55 am »
As Laurent said it should be managed by the calling code, for example:

if(!renderTex.create(200, 400))
    // Handle error
    std::cout << "There was an error when creating a texture!" << std::endl;
else
    // Make use of the texture
    sprite.setTexture(renderTex.getTexture());
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: how can I find out if a RenderTexture is created or not?
« Reply #3 on: September 15, 2012, 11:27:07 am »
Actually, you guys missed my point。。。

I am currently working on a GUI.

and I try to avoid re-render the texture of a GUI in one game frame for multiple times.

For example, when I try to drag the border of a window to resize it, if the onDragTo() function re-create the Texture of the Window on each time of MouseMotionEvent. it would become extremely slow and unnecessary
, what I'm doing is to push a callback of these "slow" function to a list(and do a check to see if a similar callback is already pushed, that even if you call create multiple time, eventually there is only one onCreate() called in a frame), and do these callbacks(create, render, render contains etc.) on each frame of the main loop.

And this bring the problem that, during one frame, the acrually create callback is maybe just pending and have not actually done yet.

for example:

        Button* btn = Button::factory.newInstance2(gui);
        btn->create(80,25);
        btn->backColor.set(Color::Blue);
        btn->text.set(L"按钮");

the btn->create(80,25) line did not actually create the texture of the button, so the following code btn->backColor.set(Color::Blue); need not to trigger the render function to render the texture(it would be automatically done when onCreate() called).
« Last Edit: September 15, 2012, 11:49:28 am by Hythloday »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: how can I find out if a RenderTexture is created or not?
« Reply #4 on: September 15, 2012, 12:43:59 pm »
Can't you just create it by default?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: how can I find out if a RenderTexture is created or not?
« Reply #5 on: September 15, 2012, 12:54:16 pm »
Actually, you guys missed my point。。。
Well you didn't explain it good enough then... ;)

Please make use of the code=cpp tags when presenting code. ;)

I don't fully know your code, etc. but from what you wrote you probably want to refractor your code, because to me it sounds like you'd leave behind some unitialized variables, which is a bad thing. Also you might want to handle the drag event better. For instance you check when the dragging is done and then call a function that (re)creates the texture and not every time the MouseMoveEvent is triggered. ;)

I'd also highly suggest to use smart pointers instead of raw pointers.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: how can I find out if a RenderTexture is created or not?
« Reply #6 on: September 15, 2012, 01:33:49 pm »
Actually, you guys missed my point。。。
Well you didn't explain it good enough then... ;)

Please make use of the code=cpp tags when presenting code. ;)

I don't fully know your code, etc. but from what you wrote you probably want to refractor your code, because to me it sounds like you'd leave behind some unitialized variables, which is a bad thing. Also you might want to handle the drag event better. For instance you check when the dragging is done and then call a function that (re)creates the texture and not every time the MouseMoveEvent is triggered. ;)

I'd also highly suggest to use smart pointers instead of raw pointers.

well... I didn't find the "code" style combobox in the post editor before you told me... sorry...

as for the raw pointer...dont't worry....I just trying to do it in a QT-like way...internally I did use shared_ptr to manage new and delete....

as for the dragging... it just a style.... in Windows, you can choose two different style when resizing, the first way is like your description... do not repaint the whole window, but show a plain box indicating the window size, and do the repaint when drag released... the second way is to constantly repaint the window while dragging... apparently the latter is more "comfortable to the eyes.."

anyway... thank you(and Laurent) for help.
« Last Edit: September 15, 2012, 01:48:16 pm by Hythloday »