1
Graphics / Re: Why my sf::Text isn't updating every time?
« on: May 13, 2016, 08:55:20 pm »Then show a complete and minimal code that reproduces the problem. Your original code is so cluttered with these duplicated calls everywhere, that it's hard to know what really happens.Then Laurent here it is a complete code, the problem is that it works in this very simple way. Hope you can understand it now.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
void updateLife(int *p, int *e, sf::Text *et, sf::Text *ee){
et->setString("hp: " + std::to_string(*p));
ee->setString("hp: " + std::to_string(*e));
}
void reduceLife( int attack, int &life){
life-=attack;
}
int main(int, char const**)
{
int playerhp = 60;
int playeratk = 60;
int enemyhp = 240;
int enemyatk = 10;
sf::Font font;
if (!font.loadFromFile("Lobster_1.3.otf"))
{
std::cout<< "failed to open font"<<std::endl;
}
sf::Text playerlife, enemylife;
playerlife.setColor(sf::Color::White);
enemylife.setColor(sf::Color::White);
playerlife.setFont(font);
enemylife.setFont(font);
playerlife.setCharacterSize(20);
enemylife.setCharacterSize(20);
playerlife.setPosition(402, 200);
enemylife.setPosition(22, 10);
bool attackable = false;
sf::RenderWindow window2(sf::VideoMode(32*16, 32*8), "Battle!!");
sf::Event events;
while (window2.isOpen()){
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
window2.clear();
window2.draw(playerlife);
window2.draw(enemylife);
window2.display();
while (window2.pollEvent(events)){
if(events.type == sf::Event::Closed){
window2.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window2.close();
}
if (events.type == sf::Event::KeyReleased) {
if (events.key.code==sf::Keyboard::Space) {
reduceLife(playeratk, enemyhp);
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
enemyatk=true;
if (enemyhp<=0) {
enemyatk=false;
window2.close();
}
if (enemyatk) {
reduceLife(enemyatk, playerhp);
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
enemyatk = false;
}
if (playerhp <= 0) {
window2.close();
}
}
}
window2.clear();
window2.draw(playerlife);
window2.draw(enemylife);
window2.display();
}//eventloop
}//gameloop
return EXIT_SUCCESS;
}
#include <SFML/Graphics.hpp>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
void updateLife(int *p, int *e, sf::Text *et, sf::Text *ee){
et->setString("hp: " + std::to_string(*p));
ee->setString("hp: " + std::to_string(*e));
}
void reduceLife( int attack, int &life){
life-=attack;
}
int main(int, char const**)
{
int playerhp = 60;
int playeratk = 60;
int enemyhp = 240;
int enemyatk = 10;
sf::Font font;
if (!font.loadFromFile("Lobster_1.3.otf"))
{
std::cout<< "failed to open font"<<std::endl;
}
sf::Text playerlife, enemylife;
playerlife.setColor(sf::Color::White);
enemylife.setColor(sf::Color::White);
playerlife.setFont(font);
enemylife.setFont(font);
playerlife.setCharacterSize(20);
enemylife.setCharacterSize(20);
playerlife.setPosition(402, 200);
enemylife.setPosition(22, 10);
bool attackable = false;
sf::RenderWindow window2(sf::VideoMode(32*16, 32*8), "Battle!!");
sf::Event events;
while (window2.isOpen()){
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
window2.clear();
window2.draw(playerlife);
window2.draw(enemylife);
window2.display();
while (window2.pollEvent(events)){
if(events.type == sf::Event::Closed){
window2.close();
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window2.close();
}
if (events.type == sf::Event::KeyReleased) {
if (events.key.code==sf::Keyboard::Space) {
reduceLife(playeratk, enemyhp);
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
enemyatk=true;
if (enemyhp<=0) {
enemyatk=false;
window2.close();
}
if (enemyatk) {
reduceLife(enemyatk, playerhp);
updateLife(&playerhp, &enemyhp, &playerlife, &enemylife);
enemyatk = false;
}
if (playerhp <= 0) {
window2.close();
}
}
}
window2.clear();
window2.draw(playerlife);
window2.draw(enemylife);
window2.display();
}//eventloop
}//gameloop
return EXIT_SUCCESS;
}
So I want this logic, and sometimes it works in my original code, but not every time how is supposed to happen..