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:
#include "Files.h"
#include "EntityManager.h"
#include "Tile.h"
#include "Degree.h"
#include "Entity.h"
#include "Button.h"
int main()
{
sf::Font GameFont;
GameFont.loadFromFile("C:/Users/Luke/Desktop/Projects/Project2/Arial.ttf");
sf::Text GameText("Text", GameFont);
GameText.setCharacterSize(25);
GameText.setStyle(sf::Text::Regular);
GameText.setColor(sf::Color::White);
sf::RenderWindow MainWindow;
enum State{STARTING, MAINMENU, LOADING, INGAME, EXITING};
sf::Vector2i IPosition;
sf::Vector2f MPosition;
std::string InputText;
Button TestButton("C:/Users/Luke/Desktop/Projects/Project2/Arial.ttf", 30, "Test Button");
TestButton.SetButtonTextStyle(sf::Text::Style::Regular);
TestButton.SetButtonTextColor(sf::Color::White);
TestButton.SetButtonRectangleFillColor(sf::Color::Blue);
TestButton.SetButtonOrigin(200.0f, 400.0f);
MainWindow.create(sf::VideoMode(800, 600), "Project 2");
while(MainWindow.isOpen())
{
sf::Event GameEvent;
while(MainWindow.pollEvent(GameEvent))
{
switch(GameEvent.type)
{
case sf::Event::Closed:
MainWindow.close();
break;
case sf::Event::MouseMoved:
break;
default:
break;
}
MainWindow.draw(GameText);
MainWindow.draw(TestButton);
MainWindow.display();
MainWindow.clear();
}
}
return 0;
}
GameText, a separate text from the button class, draws to the screen without any issues.
Button.h:
#ifndef BUTTON_H
#define BUTTON_H
#include "Files.h"
class Button : public sf::Drawable
{
public:
Button();
Button(std::string FontAddress, int FontSize, std::string ButtonString);
~Button();
sf::RectangleShape ButtonRectangle;
sf::Font ButtonFont;
sf::Text ButtonText;
sf::Vector2f ButtonScale;
sf::Vector2f ButtonOrigin;
//void DefineButtonSprite(sf::Sprite& BSprite , sf::Texture& BTexture); //Currently Unused.
void SetButtonTextStyle(sf::Text::Style TextStyle);
void SetButtonTextColor(sf::Color TextColor);
void SetButtonRectangleFillColor(sf::Color RectangleColor);
void UpdateTextScale(int TextSize, std::string TextString);
void SetButtonOrigin(float OriginX, float OriginY);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
protected:
};
#endif // BUTTON_H
Button.cpp:
#include "Button.h"
Button::Button()
{
}
Button::Button(std::string FontAddress, int FontSize, std::string ButtonString)
{
sf::Vector2f rect;
rect.x = 100.0f;
rect.y = 100.0f;
ButtonRectangle.setSize(rect);
ButtonFont.loadFromFile(FontAddress);
if(ButtonFont.loadFromFile(FontAddress) == 0)
{
std::cout << "\nFailed to load.";
}
else if(ButtonFont.loadFromFile(FontAddress) == 1)
{
std::cout << "\nLoaded Font.";
}
UpdateTextScale(FontSize, ButtonString);
}
Button::~Button()
{
}
void Button::SetButtonTextStyle(sf::Text::Style TextStyle)
{
ButtonText.setStyle(TextStyle);
}
void Button::SetButtonTextColor(sf::Color TextColor)
{
ButtonText.setColor(TextColor);
}
void Button::SetButtonRectangleFillColor(sf::Color RectangleColor)
{
ButtonRectangle.setFillColor(RectangleColor);
}
void Button::UpdateTextScale(int TextSize, std::string TextString)
{
ButtonText.setCharacterSize(TextSize);
ButtonText.setString(TextString);
ButtonScale = ButtonText.getScale();
ButtonRectangle.setScale(ButtonScale);
}
void Button::SetButtonOrigin(float OriginX, float OriginY)
{
ButtonOrigin.x = OriginX;
ButtonOrigin.y = OriginY;
ButtonText.setOrigin(ButtonOrigin);
ButtonRectangle.setOrigin(ButtonOrigin);
}
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(ButtonRectangle, states);
target.draw(ButtonText, states);
std::cout << "\nButton::draw()";
}
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.
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.