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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - salem987

Pages: [1]
1
General / Re: Modify textures stored in vector
« on: June 25, 2018, 04:14:10 pm »
I finally made it work with this:

Code: [Select]

#include <vector>
#include "Test.h"

int main()
{

    sf::VideoMode DesktopMode = sf::VideoMode::getDesktopMode();
    sf::RenderWindow window;
    window.create(DesktopMode,"The Game", sf::Style::Fullscreen);
    Test fleetButton(sf::Vector2f(500,500),"TextA");
    std::vector<Test*> listOfButtons;
    listOfButtons.push_back(&fleetButton);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // on regarde le type de l'évènement...
            switch (event.type)
            {
            // fenêtre fermée
            case sf::Event::Closed:
                window.close();
                break;

            // touche pressée
            case sf::Event::KeyPressed:
                return 0;
                break;

            // on ne traite pas les autres types d'évènements
            default:
                break;
            }
            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {

                    listOfButtons[0]->setText("Heyy");

                }
            }

        }
        window.clear(sf::Color::Black);
        window.draw(*listOfButtons[0]->getSprite());
        window.draw(*listOfButtons[0]->getText());
        window.display();

    }
}


I shoud just have initialized the vector like this:

Code: [Select]
std::vector<Test*> listOfButtons;

and not like this:

Code: [Select]
std::vector<Test> listOfButtons;

even if I don't really know why ^^

Thank you!

2
General / Re: Modify textures stored in vector
« on: June 25, 2018, 02:39:01 pm »
I have made now a Test class, which is a minimalist Button class with just a texture and a text on it.

Test.h :

Code: [Select]

#ifndef TEST_H
#define TEST_H
#include <string>
#include <SFML/Graphics.hpp>


class Test
{
public:
    Test(sf::Vector2f location,std::string tstring);
    sf::Sprite* getSprite();
    sf::Text * getText();
    void setText(std::string newText);

private:
    sf::Font font;
    sf::Sprite normal;
    sf::Sprite clicked;
    sf::Sprite* currentSpr;
    sf::Text textOnButton;
    sf::Text* normalText;
    int middleX;
    int middleY;
    int characterSize;
    sf::Texture simplePic;
    sf::Texture simplePicClicked;

};

#endif // TEST_H


Test.cpp :

Code: [Select]

#include "Test.h"


Test::Test(sf::Vector2f location,std::string tstring)
{
    simplePic.loadFromFile("button.png");
    this->font.loadFromFile("arial.ttf");
    simplePicClicked.loadFromFile("buttonClicked.png");
    normal.setTexture(simplePic);
    clicked.setTexture(simplePicClicked);
    this->currentSpr=&this->normal;
    this->normal.setPosition(location);
    this->clicked.setPosition(location);
    if((25*tstring.length()) > simplePic.getSize().x)
    {
        characterSize = ((simplePic.getSize().x) / tstring.length());
    }
    else
    {
        characterSize = 25;
    }
    textOnButton.setFont(font);
    textOnButton.setCharacterSize(characterSize);
    textOnButton.setStyle(sf::Text::Bold);
    textOnButton.setString(tstring);
    textOnButton.setString(tstring);
    middleX = (currentSpr->getPosition().x  + (simplePic.getSize().x / (characterSize*tstring.length()))) + (simplePic.getSize().x - textOnButton.getGlobalBounds().width)/2;
    middleY = (currentSpr->getPosition().y  + (simplePic.getSize().y / 3));
    textOnButton.setPosition(middleX,middleY);
    this->normalText=&this->textOnButton;

}

sf::Sprite* Test::getSprite()
{
    return currentSpr;
}

sf::Text* Test::getText()
{

    return normalText;
}

void Test::setText(std::string newText)
{
    textOnButton.setString(newText);
}


And finally the main :

Code: [Select]

#include <vector>
#include "Test.h"

int main()
{

    sf::VideoMode DesktopMode = sf::VideoMode::getDesktopMode();
    sf::RenderWindow window;
    window.create(DesktopMode,"The Game", sf::Style::Fullscreen);
    Test fleetButton(sf::Vector2f(500,500),"TextA");
    std::vector<Test> listOfButtons;
    listOfButtons.push_back(fleetButton);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // on regarde le type de l'évènement...
            switch (event.type)
            {
            // fenêtre fermée
            case sf::Event::Closed:
                window.close();
                break;

            // touche pressée
            case sf::Event::KeyPressed:
                return 0;
                break;

            // on ne traite pas les autres types d'évènements
            default:
                break;
            }
            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {

                    fleetButton.setText("Heyy");

                }
            }

        }
        window.clear(sf::Color::Black);
        window.draw(*fleetButton.getSprite());
        window.draw(*fleetButton.getText());
        window.display();

    }
}


We can notice here that fleetButton sets the text to "heyy" correctly (like the 2 pictures I sent earlier).
However, if I replace fleetButton by listOfButtons[ 0 ], like here:

Code: [Select]

#include <vector>
#include "Test.h"

int main()
{

    sf::VideoMode DesktopMode = sf::VideoMode::getDesktopMode();
    sf::RenderWindow window;
    window.create(DesktopMode,"The Game", sf::Style::Fullscreen);
    Test fleetButton(sf::Vector2f(500,500),"TextA");
    std::vector<Test> listOfButtons;
    listOfButtons.push_back(fleetButton);

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // on regarde le type de l'évènement...
            switch (event.type)
            {
            // fenêtre fermée
            case sf::Event::Closed:
                window.close();
                break;

            // touche pressée
            case sf::Event::KeyPressed:
                return 0;
                break;

            // on ne traite pas les autres types d'évènements
            default:
                break;
            }
            if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {

                    listOfButtons[0].setText("Heyy");

                }
            }

        }
        window.clear(sf::Color::Black);
        window.draw(*listOfButtons[0].getSprite());
        window.draw(*listOfButtons[0].getText());
        window.display();

    }
}


When I click, the text stays the same :(


3
General / Re: Modify textures stored in vector
« on: June 25, 2018, 12:01:02 pm »
Thanks for your answer, in fact I have been drawing the fleetButton and not listOfButtons[0].

However, I have put

Code: [Select]
window.draw(*listOfButtons[0].getSprite());
window.draw(*listOfButtons[0].getText());

instead now, and still nothing :/ .

Does that mean that listOfButton[0] (modified after the loop) gets destroyed when the for loop ends?

4
General / Re: Modify textures stored in vector
« on: June 25, 2018, 11:03:05 am »
The problem is that there no error message at all from the compiler.

Here are some screenshots with this piece of code:

Code: [Select]
if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
                   
                   fleetButton.setText("Heyy");

                }
            }


Before clicking:



After clicking:



It works perfectly fine here ^^

However, if I put now the Button object in a vector and call setText from it,

Code: [Select]
std::vector<Button> listOfButtons;
listOfButtons.push_back(fleetButton);

and then

Code: [Select]
if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                {
     
                    for(auto &i : listOfButtons)
                    {

                          i.setText("heyy");
                       
                    }

                }
            }

When I click, the text on the button does not change anymore, while it should be "hey". I'm really stuck here since I don't know where the problem comes from ^^'.

Thanks for the help :P

5
General / Re: Modify textures stored in vector
« on: June 25, 2018, 10:40:48 am »
It is really confusing since fleetButton.setText("heyy"); works perfectly but once in a vector and called with listOfButtons [ i ].setText("heyy"); it doesn't work anymore.. Does it have something to do with pointers maybe?

I also have tried this way:

for(auto &i : listOfButtons)
                    {

                        if(i.checkClick(sf::Mouse::getPosition().x,sf::Mouse::getPosition().y))
                        {

                            i.setText("heyy");

                        }
                    }

But still nothing.

6
General / Re: Modify textures stored in vector
« on: June 25, 2018, 12:29:17 am »
listOfButtons.setText("Hey");
That's not how you access elements in a vector.

The [ i ] seemed to have disappeared in my message lol, I mean

listOfButtons [ i ].setText of course

7
General / Modify textures stored in vector
« on: June 24, 2018, 06:54:21 pm »
Hi everyone,

I made a kind of "Button" class that is composed of a sprite and a text on it.

One of the functions of the class is the setText function that just set a new text on the button:

void Button::setText(std::string newText){
    textOnButton.setString(newText);
}

It actually works in the main():

fleetButton.setText("Heyy") for example works and the new text of the button fleetButton becomes "Heyy".

However, I would like to store all my future button in a vector.

std::vector<Button> listOfButtons;
listOfButtons.push_back(fleetButton);

However, the setText doesn't work anymore if I use it like this :

for(int i =0; i<listOfButtons.size();i++){
        listOfButtons[i].setText("Hey");
 }

In fact, the text doesn't change.

It's the same for the setTexture function that I have.

Thanks for the help ^^

Pages: [1]