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

Author Topic: Really stuck, GUI project (sf::Drawable problem)  (Read 3542 times)

0 Members and 2 Guests are viewing this topic.

Tenchi

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - tenchi@hikago-fr.com
    • View Profile
    • http://www.soundclick.com/tenchi
Really stuck, GUI project (sf::Drawable problem)
« on: May 07, 2010, 02:24:02 am »
Hi everyone !

I've been beginning a GUI recently for a game project with a friend. I've already coded a kind of rich-text:



I'm now trying to implement several GUI classes, all inheriting from sf::Drawable :
    Every part of a window will be inheriting of Widget
    For example in the source I added Button : Widget which draws a box and a text label
    Window, which contains a sf::RenderImage container where we will draw every Widget that has been added to the vector of widgets via void add(Widget widget)


My problem occurs when I try to draw all my Drawable Widgets in my sf::RenderImage named container. I really don't see what I do wrong and the compiler error message doesn't help me much :
Code: [Select]

void Window::Render(sf::RenderTarget &target, sf::Renderer &renderer) const
{
for (unsigned int i = 0; i < widgets.size(); i++)
{
container.Draw(widgets[i]);
}

sprite.SetImage(container.GetImage());
sprite.SetPosition(pos.x, pos.y);
target.Draw(sprite);

}

1>c:\users\tenchi\documents\visual studio 2008\projects\sfmlgui\sfmlgui\gui.cpp(133) : error C2663: 'sf::RenderTarget::Draw' : 2 overloads have no legal conversion for 'this' pointer
1>c:\users\tenchi\documents\visual studio 2008\projects\sfmlgui\sfmlgui\gui.cpp(136) : error C2662: 'sf::Sprite::SetImage' : cannot convert 'this' pointer from 'const sf::Sprite' to 'sf::Sprite &'
1>        Conversion loses qualifiers
1>c:\users\tenchi\documents\visual studio 2008\projects\sfmlgui\sfmlgui\gui.cpp(137) : error C2663: 'sf::Drawable::SetPosition' : 2 overloads have no legal conversion for 'this' pointer


I have uploaded the VC2008 project here. The preceding errors occur in gui.cpp at the very bottom (I commented the lines that don't compile so you can build the project normally to test it)

(If you build it, don't run it from visual studio but from either Debug/ or Release/ so that the program finds resources/ ... didn't find how to do this clean)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Really stuck, GUI project (sf::Drawable problem)
« Reply #1 on: May 07, 2010, 07:42:16 am »
The Render function is const, you can't modify the class members in it.

But:
- You don't need to call SetImage everytime the content of the image changes (it's still the same image object), just set it once after you create the render-image.
- SetPosition can probably be called somewhere else, when it actually changes for example?

And... what's the benefit of using a RenderImage if you update it every time the widget must be drawn? Why not drawing the widgets directly to the final render target?
Laurent Gomila - SFML developer

Tenchi

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - tenchi@hikago-fr.com
    • View Profile
    • http://www.soundclick.com/tenchi
Really stuck, GUI project (sf::Drawable problem)
« Reply #2 on: May 07, 2010, 12:00:40 pm »
Thanks, I didn't realize it was const !

Well. All my widgets have local window coordinates so I draw them all in the RenderImage and then I just have to correctly position it where the window actually is.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Really stuck, GUI project (sf::Drawable problem)
« Reply #3 on: May 07, 2010, 06:09:31 pm »
This would be the same without the render-image. A drawable rendered inside another drawable automatically inherits its parent position and color, and combines them with its own attributes.
Laurent Gomila - SFML developer

Tenchi

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - tenchi@hikago-fr.com
    • View Profile
    • http://www.soundclick.com/tenchi
Really stuck, GUI project (sf::Drawable problem)
« Reply #4 on: May 07, 2010, 06:25:40 pm »
In fact I already tried rendering directly to the target. But when I do
Code: [Select]
void Window::Render(sf::RenderTarget &target, sf::Renderer &renderer) const
{
target.Draw(main_box);
for (unsigned int i = 0; i < widgets.size(); i++)
{
target.Draw(*widgets[i]);
}
}

Runtime error R6025 "pure virtual function call" happens.
The only pure virtual function I have here is Widget::Render(...). In my widget pointer vector here, I only have a Button which inherits from Widget. So normally it should call Button::Render and not Widget::Render.. Sorry to bother, I'm really noob ^^

The same for loop worked perfectly in another function than Window::Render (I did it first in a function "Update()" which renders everything in a RenderImage and then updates a sprite which I drawed in Window::Render)

Hope it's not too confusing xD

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Really stuck, GUI project (sf::Drawable problem)
« Reply #5 on: May 07, 2010, 06:36:49 pm »
You should try to fix this pure virtual function call, rather than avoiding it with a code that is worse ;)
Laurent Gomila - SFML developer

Tenchi

  • Newbie
  • *
  • Posts: 7
    • MSN Messenger - tenchi@hikago-fr.com
    • View Profile
    • http://www.soundclick.com/tenchi
Really stuck, GUI project (sf::Drawable problem)
« Reply #6 on: May 07, 2010, 07:50:10 pm »
Finally got rid of this horrible runtime error O______O
Instead of doing this :
Code: [Select]
window.add(&Button(blah blah));
I had to do this :
Code: [Select]
Button button(blah blah);
Widget *ptr = &btn;
window.add(ptr);


Hiura also pointed that I could do :
Code: [Select]
Button* button = new Button(blah blah);
window.add(btn);


I just still don't understand what went wrong with the 1st one... Also with this method I must clean pointers afterwards :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Really stuck, GUI project (sf::Drawable problem)
« Reply #7 on: May 07, 2010, 07:56:23 pm »
The problem in the first example is that you store the address of an object that is immediately destroyed. The stored pointer is then invalid.

Same with the second one, the object will be destroyed at the end of the current scope, and the stored pointer will also become invalid.

You really have to manage the lifetime of the stored objects properly, which means allocating them with new so that you decide when they are destroyed, with delete.

To avoid doing that manually, you can use smart pointers from boost or std::tr1 (shared_ptr).
Laurent Gomila - SFML developer

 

anything