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

Author Topic: Creating a Text Wrapper  (Read 2506 times)

0 Members and 1 Guest are viewing this topic.

aanthonyz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Creating a Text Wrapper
« on: December 30, 2014, 08:13:14 am »
Im trying to create a function where I can input parameters for everything(size, text, color, pos, etc) rather than making individual functions but for some reason it doesnt appear on the screen when I implement it.

void Menu::SetText(sf::Text &Text,char* Output,int Size)
{
        Text.setColor(sf::Color::White);
        Text.setString(Output);
        Text.setCharacterSize(Size);
        Text.setPosition(0,0);
}
 

When I use this function it doesnt work but when I input each line manually then it displays perfectly fine. Any help would be appreciated.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Creating a Text Wrapper
« Reply #1 on: December 30, 2014, 08:53:58 am »
Are there any errors at all or does it simply not do what you expect?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Creating a Text Wrapper
« Reply #2 on: December 30, 2014, 09:13:28 am »
Provide a minimal and complete example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

aanthonyz

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Creating a Text Wrapper
« Reply #3 on: December 30, 2014, 05:11:55 pm »
No errors at all just doesnt display the text.

#include <SFML\Graphics.hpp>
#pragma once
class Menu
{
public:
        Menu(void);
        ~Menu(void);
        bool MainMenuLoad();
        void Display(sf::RenderWindow* Display);
        void SetText(sf::Text &Text,char* Output,int Size);
private:
        sf::Font MainMenuFont;
        sf::Text MainMenuText[5];
        sf::Texture MainMenuTexture[5];
        sf::Sprite MainMenuSprite[5];
};
#include "Menu.h"


Menu::Menu(void)
{
        MainMenuLoad();
}

bool Menu::MainMenuLoad()
{
        //Font
        MainMenuFont.loadFromFile("Textures\\ka1.ttf");
        MainMenuText[0].setFont(MainMenuFont);
        MainMenuText[0].setString("Version: 0.1a");
        MainMenuText[0].setCharacterSize(30);
        MainMenuText[0].setPosition(0,1080-MainMenuText[0].getLocalBounds().height);
        MainMenuText[1].setFont(MainMenuFont);
        MainMenuText[1].setString("Singleplayer");
        MainMenuText[1].setCharacterSize(75);
        MainMenuText[1].setPosition(1920-MainMenuText[1].getLocalBounds().width,330);
        MainMenuText[2].setFont(MainMenuFont);
        MainMenuText[2].setString("Multiplayer");
        MainMenuText[2].setCharacterSize(75);
        MainMenuText[2].setPosition(1920-MainMenuText[2].getLocalBounds().width,420);
        MainMenuText[3].setFont(MainMenuFont);
        MainMenuText[3].setString("Options");
        MainMenuText[3].setCharacterSize(75);
        MainMenuText[3].setPosition(1920-MainMenuText[3].getLocalBounds().width,510);
        MainMenuText[4].setFont(MainMenuFont);
        MainMenuText[4].setString("Exit");
        MainMenuText[4].setCharacterSize(75);
        MainMenuText[4].setPosition(1920-MainMenuText[4].getLocalBounds().width,600);
        //SetText(MainMenuText[0],"Version: 0.1a",100);
        //Background Image
        MainMenuTexture[0].loadFromFile("Textures\\Main_1.png");
        MainMenuSprite[0].setTexture(MainMenuTexture[0]);
        //Logo
        MainMenuTexture[1].loadFromFile("Textures\\Logo.png");
        MainMenuSprite[1].setTexture(MainMenuTexture[1]);
        MainMenuSprite[1].scale(sf::Vector2f(1.5,1.5));
        return true;
}

void Menu::SetText(sf::Text &Text,char* Output,int Size)
{
        Text.setColor(sf::Color::White);
        Text.setString(Output);
        Text.setCharacterSize(Size);
        Text.setPosition(0,0);
}
void Menu::Display(sf::RenderWindow* Display)
{
        Display->draw(MainMenuSprite[0]);
        Display->draw(MainMenuSprite[1]);
        Display->draw(MainMenuText[0]);
        Display->draw(MainMenuText[1]);
        Display->draw(MainMenuText[2]);
        Display->draw(MainMenuText[3]);
        Display->draw(MainMenuText[4]);
}

 

So this is what I have so far. If I write out every single line like I have on the top it works. but when I try to use the SetText function which I made it. It complies with no errors but nothing actually shows up on the screen, which should be white text that says "Version: 0.1a".

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Creating a Text Wrapper
« Reply #4 on: December 30, 2014, 05:23:31 pm »
You aren't checking the return value for any of the loadFromFile calls, so this could be as simple as a typo in the filepaths.

Also, it's not a complete and minimal example if we can't actually compile and run it.  The main() function needs to be in there, and everything but a single Text object should be removed unless some of the other drawables are actually needed to reproduce the problem.