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.


Topics - SqrtPapere

Pages: [1]
1
Graphics / Why my sf::Text isn't updating every time?
« on: May 13, 2016, 12:13:06 pm »
Hello,
I'm developing a simple game that should works like a simpler pokemòn and obviously I'm using SFML.
The problem is that I've got the classic label of text that shows hp of the user and  enemy but the updating of it (graphically talking) isn't updating every time, I mean the logic of the game works properly cause if i'm attacking with 60 and the enemy has got 240 hp it took 4 hit to be killed but the sf::Text I'm trying to draw does not update at every "run" of the program.
Here it is the code of the BattleLoop using it:(player and enemy are the pointer to the user and the enemy)
void BattleLoop(std::vector<Character*> *chrt, int ID){
   
    Character *player = chrt->at(0);
    Character *enemy =  chrt->at(ID);
   
    sf::Texture playerTexture, enemyTexture, battleTexture;
    sf::Sprite playerSprite, enemySprite, battleSprite;
     
    sf::Font font;
    if (!font.loadFromFile("Lobster_1.3.otf"))
    {
        std::cout<< "failed to open font"<<std::endl;
    }
   
    sf::Text playerlife, enemylife;  //<---this is the no updating thing!!
 
    playerlife.setColor(sf::Color::Black);
    enemylife.setColor(sf::Color::Black);

    playerlife.setFont(font);
    enemylife.setFont(font);
   
    playerlife.setCharacterSize(20);
    enemylife.setCharacterSize(20);
   
    playerlife.setPosition(402, 200);
    enemylife.setPosition(22, 10);
   
    std::string playerpng = player->getType() + "battle.png";
    if(!playerTexture.loadFromFile(playerpng))
        std::cout<<"Error, could not load "<<playerpng<<std::endl;
    playerSprite.setTexture(playerTexture);
    playerSprite.setTextureRect(sf::IntRect(0,0,110,128));
    playerSprite.setPosition(32,128);
     
    std::string enemypng = std::to_string(enemy->getId()) + ".png";
    if(!enemyTexture.loadFromFile(enemypng))
        std::cout<<"Error, could not load  "<<enemypng<<std::endl;
    enemySprite.setTexture(enemyTexture);
    enemySprite.setPosition(32*11,50);

    bool enemyatk = false;
   
    sf::RenderWindow window2(sf::VideoMode(32*16, 32*8), "Battle!!");
   
    std::ostringstream playerlifeConv;   //I use this to convert the int (life) to string and then in Text
    playerlifeConv << "hp: " << player->getLife();
    playerlife.setString(playerlifeConv.str());
   
    std::ostringstream enemylifeConv;
    enemylifeConv << "hp: " << enemy->getLife();
    enemylife.setString(enemylifeConv.str());
     
    sf::Event events;
   
    while (window2.isOpen()){
       
        updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
//putting a lot of those method updateLife to prove that's not the problem

        window2.clear();
        window2.draw(playerSprite);
        window2.draw(enemySprite);
        window2.draw(playerlife);
        updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
        window2.draw(enemylife);
        window2.display();
        updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);

        while (window2.pollEvent(events)){
            updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
           
            if(events.type == sf::Event::Closed){
                window2.close();
            }
            else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
                updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
                window2.close();
            }
            if (events.type == sf::Event::KeyReleased) {//this is how I attack the enemy
                updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
                if (events.key.code==sf::Keyboard::Space) {
                    updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
                    enemy->reduceLife(player->getAttack());  //this is working!!
                    updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
                    enemyatk=true;

                    if (enemy->getLife()<=0) {
                        updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);

                        chrt->erase(chrt->begin()+ID);
                        enemyatk=false;
                        window2.close();
                    }
                    updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
             
                    if (enemyatk) {
                        player->reduceLife(enemy->getAttack());
                        enemyatk = false;
                        updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);

                    }
                    if (player->getLife()<=0) {
                        chrt->erase(chrt->begin());
                        window2.close();
                    }
                    updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);              
                }
            }
                 
            updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);

            window2.clear();
            window2.draw(playerSprite);
            updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
            window2.draw(enemySprite);
            window2.draw(playerlife);
            updateLife(playerlifeConv, enemylifeConv, player, enemy, &playerlife, &enemylife);
            window2.draw(enemylife);
            window2.display();
   
        }//eventloop

    }//gameloop
}

bool collision (sf::Vector2f vp1, sf::Vector2f vp2){  //metodo per collisioni con nemici, se ci troviamo in un raggio di 4px
    float a=fabs(vp1.x-vp2.x);  //fabs fa il valore assoluto
    float b=fabs(vp1.y-vp2.y);
    if (a<4 && b<4) {
        return true;
    }
    return false;
}

void updateLife(std::ostringstream &pl,std::ostringstream &el,  Character *p, Character *e, sf::Text *et, sf::Text *ee){
    pl.clear();
    pl.str("");
    pl << "hp: " << p->getLife();
    et->setString(pl.str());
    el.clear();
    el.str("");
    el << "hp: " << e->getLife();
    ee->setString(el.str());  
}
 

I hope to be clear.
Thankyou(sorry for bad english)

Pages: [1]
anything