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

Author Topic: Trouble changing Text and RectangleShape color with SetColor/SetFillColor  (Read 4210 times)

0 Members and 1 Guest are viewing this topic.

JGrieco42

  • Newbie
  • *
  • Posts: 6
    • View Profile
I have two classes which, for the purpose of this exercise, are defined as follows:

class Editor {
public:
struct editorItem_t {
        sf::RectangleShape                                      rect;
        sf::Text                                                        text;
        editorState_t                                           action;
};
std::list<editorItem_t>                                 editorItems;
}

class Game {
public:
        void Game::EditorMode( const sf::Event& currentEvent );
private:
        Editor editor;
}

Please assume that there is a font loaded and that the sf::Text component is currently writing correctly.  EditorMode is defined as follows, and this is where my problem lies.  I'm using C++11 auto notation, but even without that the color-change statements are not working as intended:

void Game::EditorMode( const sf::Event& currentEvent ) {
        //check to see if any editorItem_t rects were clicked
        if ( currentEvent.type == sf::Event::MouseButtonPressed ) {
                if ( sf::Mouse::isButtonPressed( sf::Mouse::Button::Left ) ) {
                        for ( auto itr = editor.editorItems.begin( ); itr != editor.editorItems.end( ); ++itr ) {
                                if ( itr->rect.getGlobalBounds( ).contains( currentEvent.mouseButton.x, currentEvent.mouseButton.y ) ) {
                                        itr->rect.setFillColor( sf::Color::Blue );      //this isn't working at all
                                        itr->text.setColor( sf::Color::White );     //this isn't working at all
                                        std::cout << "But this statement is working fine!" << std::endl;
                                }
                        }
                }
        }
}

The compiler basically seems to be treating the rect.setFillColor and text.SetColor statements as no-ops and I have no idea how to fix this, any help would be much appreciated.

Mark

  • Guest
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #1 on: December 24, 2013, 06:36:38 pm »
Lulz. The cycle of displaying stuff in SFML:
1. First of all, you have to clear all the screen, using function sf::RenderWindow::clear()
2. Next, you change the variables like color, size, rotation and stuff of transformable drawable objects just like you did in your code.
3. Then, you draw your objects, using function sf::RenderWindow::draw(const Drawable &Object)
I assume that your sf::RenderWindow variable is called window. You should write these two lines just after you change the variables:
window.draw(rect);
window.draw(text);
4. Finally, you display the stuff using sf::RenderWindow::display()

I see that you did only the second step...
« Last Edit: December 24, 2013, 06:43:47 pm by Mark »

JGrieco42

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #2 on: December 24, 2013, 06:43:38 pm »
Hello,

The EditorMode() function is the end of my "Process Input" phase, and immediately after finishing with that function the main loop moves to the update phase:

void Game::MainLoop( ) {
        while ( window.isOpen( ) ) {
                ProcessInput( );
                Update( );
                window.clear();
                entityManager.DrawAll( window );
                window.display( );
        }
}

I don't have trouble clearing or drawing any objects, just changing the color.  I tried to leave out some steps which I thought were not relevant to this problem.  Adding the window.draw as suggested doesn't seem to change anything, since the rect/text is still getting drawn during the normal draw phase of the loop:

void Game::EditorMode( const sf::Event& currentEvent ) {
    //check to see if any editorItem_t rects were clicked
    if ( currentEvent.type == sf::Event::MouseButtonPressed ) {
        if ( sf::Mouse::isButtonPressed( sf::Mouse::Button::Left ) ) {
            for ( auto itr = editor.editorItems.begin( ); itr != editor.editorItems.end( ); ++itr ) {
                if ( itr->rect.getGlobalBounds( ).contains( currentEvent.mouseButton.x, currentEvent.mouseButton.y ) ) {
                    itr->rect.setFillColor( sf::Color::Blue );  //this isn't working at all
                    itr->text.setColor( sf::Color::White );     //this isn't working at all
                    window.draw( itr->rect );
                    window.draw( itr->text );
                    std::cout << "But this statement is working fine!" << std::endl;
                }
            }
        }
    }
}
« Last Edit: December 24, 2013, 06:46:25 pm by JGrieco42 »

Mark

  • Guest
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #3 on: December 24, 2013, 06:47:50 pm »
Try this:
void Game::EditorMode( const sf::Event& currentEvent ) {
        //check to see if any editorItem_t rects were clicked
        if ( currentEvent.type == sf::Event::MouseButtonPressed &&
         currentEvent.mouseButton.button == sf::Mouse::Left ) {
                        for ( auto itr = editor.editorItems.begin( ); itr != editor.editorItems.end( ); ++itr ) {
                                if ( itr->rect.getGlobalBounds( ).contains( currentEvent.mouseButton.x, currentEvent.mouseButton.y ) ) {
                                        itr->rect.setFillColor( sf::Color::Blue );      //this isn't working at all
                                        itr->text.setColor( sf::Color::White );     //this isn't working at all
                                        std::cout << "But this statement is working fine!" << std::endl;
                        }
                }
        }
}

This is how you check if something was pressed:
if(currentEvent.type == sf::Event::MouseButtonPressed &&  
   currentEvent.mouseButton.button == sf::Mouse::Left ) // Do stuff
« Last Edit: December 24, 2013, 06:52:58 pm by Mark »

JGrieco42

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #4 on: December 24, 2013, 07:17:09 pm »
Thanks Mark, but unfortunately that doesn't affect anything.  The reason I separated the ifs is so that I can check for right click and mouse wheel later using else statements rather than having to check for mouse event multiple times (the event is in scope the entire time so the event information will be stored in event.mouseButton).

the rect and text in question are created during the construction of the editor in the Game class (assignment in a constructor is bad, yes I know, but I'm only creating like 5 rectangleShapes and 5 texts at the beginning of the game so this is tolerable for my purposes).  During the construction I simply write:

editorItem_t button;
        button.rect = sf::RectangleShape( sf::Vector2f( editor.shell.getLocalBounds( ).width, EDITOR_BUTTON_HEIGHT ) );
        button.rect.setOutlineColor( sf::Color::White );
        button.rect.setOutlineThickness( EDITOR_OUTLINE_THICKNESS );
        button.rect.setFillColor( sf::Color( ) );  //black
        button.rect.setPosition( sf::Vector2f( 100, 100 ) );  //dummy location
       
        //center the text
        button.text = sf::Text( buttonText, editorFont, 20 );  //white text by default
        sf::FloatRect textRect = button.text.getLocalBounds( );
        button.text.setOrigin( textRect.left + textRect.width / 2.0f, textRect.top + textRect.height / 2.0f );

and this works fine.  In particular, I really don't understand how the button.rect.setFillColor() function works during construction of the editor but is unresponsive during the EditorMode setFillColor() statement.  Even writing a function in the Editor class called, say, ChangeColor, and calling it instead doesn't work for me:

void Editor::ChangeColor( sf::RectangleShape& rectangle ) {
        rectangle.setFillColor( sf::Color:Blue );
}

Mark

  • Guest
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #5 on: December 24, 2013, 07:29:47 pm »
Well, then we need more experienced people here... I am newbie to sfml too. By the way, if the window is black and the initial color of rectangle is black too then you wouldn't spot a difference if the rectangle wouldn't be drawn at all. But that doesn't explain why text color isn't changing, if you see it correctly at first.

randy808

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Trouble changing Text and RectangleShape color with SetColor/SetFillColor
« Reply #6 on: December 31, 2013, 04:48:21 pm »
Try posting both window.draw () and window.display in the scope of the editor mode function after you redefine the colors of the rect and test as opposed to just in the game loop and report what happens.Also try it again and comment out the clear button in your game loop and also report what happens. Can you also post the initialization of your list?  I dont yet have a solution for the problem,  so im just trying to get a better feel of what the problem might be.