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

Author Topic: Custom button class does not display. [SOLVED]  (Read 2665 times)

0 Members and 1 Guest are viewing this topic.

HavingPhun

  • Newbie
  • *
  • Posts: 7
    • View Profile
Custom button class does not display. [SOLVED]
« on: June 15, 2014, 10:31:45 pm »
Hello, I have been teaching myself how to use SFML 2.1 for a few days and am having trouble getting a button object I made draw itself to a window. My program runs without any errors.

Main.cpp:
(click to show/hide)

Button.h:
(click to show/hide)

Button.cpp:
(click to show/hide)

Files.h:
(click to show/hide)

I made button inherit sf::drawable and made sure I did it in the same way that sf::sprite did so I could draw buttons in the same way you can draw sprites by calling window.draw(sprite). Though there are no errors when compiling and running the program, neither the buttons rectangle or it's text are drawn. I cannot figure out why though.

Thank you ahead of time for any help, and any other general tips or criticism of my code is welcome, especially if I am doing something horribly wrong.

OS: Windows 7 64bit.
SFML Version: 2.1 Static.
GPU: EVGA gtx 670 ftw.
« Last Edit: June 18, 2014, 11:21:31 pm by HavingPhun »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Custom button class does not display.
« Reply #1 on: June 15, 2014, 10:44:39 pm »
For the rectangle: I don't see you setting the size anywhere, and the documentation says its default constructor uses a size of (0,0), so I'd try giving it a nonzero size.

For the text: You aren't checking the return value of loadFromFile(), so maybe it just can't find the font file.  If that isn't the issue, could you try drawing a simple sf::Text object (unrelated to your Button class) and make sure that shows up properly?

For style:
- Why give the Button all of its arguments in an initialization method rather than the constructor?
- In general, passing that many arguments in one function is not a good sign.  One alternative that might be more readable would be having the constructor take a size and position, then using setFont(), setColors() and setString() methods to neatly split up all the other parameters.
- MainWindow.clear(); should be underneath the event loop, next to draw() and display().  I actually didn't see it for a while, and nearly wrote a post explaining why you need to call clear().
- Your indentation is inconsistent in a few places.
- //dtor is a completely superfluous comment
« Last Edit: June 15, 2014, 10:59:16 pm by Ixrec »

HavingPhun

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Custom button class does not display.
« Reply #2 on: June 15, 2014, 11:34:29 pm »
For the rectangle: I don't see you setting the size anywhere, and the documentation says its default constructor uses a size of (0,0), so I'd try giving it a nonzero size.

For the text: You aren't checking the return value of loadFromFile(), so maybe it just can't find the font file.  If that isn't the issue, could you try drawing a simple sf::Text object (unrelated to your Button class) and make sure that shows up properly?


Thank you for the quick response, I have made the changes you suggested in my code. The button still does not display.

Main.cpp:
(click to show/hide)

GameText, a separate text from the button class, draws to the screen without any issues.

Button.h:
(click to show/hide)

Button.cpp:
(click to show/hide)

At the end of Button::draw I have a cout statement to show if the function runs correctly, my output shows the statement many times so the function is being run. I also set the rectangles initial size to 100 by 100 and it still does not get drawn. I checked the return statement of ButtonFont.loadFromFile and it returns as true. Though I think it already has a check built into itself as I have had it output that the file could not be loaded when I put in the wrong path.

For style:
- Why give the Button all of its arguments in an initialization method rather than the constructor?
- In general, passing that many arguments in one function is not a good sign.  One alternative that might be more readable would be having the constructor take a size and position, then using setFont(), setColors() and setString() methods to neatly split up all the other parameters.
- MainWindow.clear(); should be underneath the event loop, next to draw() and display().  I actually didn't see it for a while, and nearly wrote a post explaining why you need to call clear().
- Your indentation is inconsistent in a few places.
- //dtor is a completely superfluous comment

- I moved them to the constructor, that slipped my mind for some reason. I fixed it.
- I have made those changes in the code above.
- Placed it under MainWindow.display().
- When I have Code::Blocks make me a new empty class, it uses different indentation than myself and places //ctor and //dtor in the constructor and destructor. I was too lazy to clean that up, will have to at some point.  ;D

Thank you for your criticism. I will try to look for reasons for why this is still not working, but am stumped so far.

Edit: Added relevant information such as OS, SFML version, and GPU to initial post.
« Last Edit: June 16, 2014, 02:02:55 am by HavingPhun »

Jonki

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Custom button class does not display.
« Reply #3 on: June 16, 2014, 06:38:31 am »
You're not supposed to have your render code inside the event loop. If there are no events, those functions won't be called.
dafuq did I just write?

HavingPhun

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Custom button class does not display.
« Reply #4 on: June 16, 2014, 09:41:12 pm »
You're not supposed to have your render code inside the event loop. If there are no events, those functions won't be called.
I changed that, It doesn't really make much sense to have my display function in my event loop. I also noticed that I forgot to set ButtonText's font so I this to Buttons constructor.
ButtonText.setFont(ButtonFont);
 

I also changed:
MainWindow.draw(TestButton);
 

To:
MainWindow.draw(TestButton, sf::RenderStates::Default)
 

Since I was not sure if I needed to enter the second argument. Though the button still does not display.

HavingPhun

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Custom button class does not display. [SOLVED]
« Reply #5 on: June 18, 2014, 11:21:04 pm »
I fixed it. I set the buttons origin to a position outside of the windows viewing area.

TestButton.SetButtonOrigin(200.0f, 400.0f);
 
This was out of bounds, setting the origin to (0,0) sets it in the upper left corner. Thank you Jonki and Ixrec for your help.