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

Author Topic: Text rendering issue [SFML 2.4.2] [screenshot included] [code added]  (Read 1431 times)

0 Members and 1 Guest are viewing this topic.

JamesYeoman

  • Newbie
  • *
  • Posts: 17
    • View Profile
This is a really weird issue I am having and have been stuck on for most of today.

What should be rendering is "0 | 0" but the pipe is the only part of the string that is being rendered.



Here is the code:

/*
*       Game.h
*       Written by James Yeoman
*/

#pragma once

#include "Paddle.h"
#include "Ball.h"

#include <SFML/Graphics.hpp>

class Game
{
private:
        sf::RenderWindow window;
        uint16_t WindowWidth;
        uint16_t WindowHeight;

        uint8_t score1;
        uint8_t score2;

        bool isRoundOver;
       
        void createEntities();

        Ball* puck;

        Paddle* paddles[2];

        sf::Font scoreFont;

        sf::Text* scoreSmall;
        sf::Text* scoreBig;

public:
        Game(uint16_t, uint16_t, std::string);
        void startLoop();
};

/*
*       Game.cpp
*       Written by James Yeoman
*/


#include "Game.h"
#include <string>

Game::Game(uint16_t windowWidth, uint16_t windowHeight, std::string title) :
        window(sf::VideoMode(windowWidth, windowHeight), title)
{
        this->WindowWidth = windowWidth;
        this->WindowHeight = windowHeight;

        this->window.setVerticalSyncEnabled(true);

        this->score1 = 0;
        this->score2 = 0;

        this->isRoundOver = false;

        this->scoreFont = sf::Font();
        if (!this->scoreFont.loadFromFile("fonts/scoreFont.ttf"))
        {
        }
       
        this->scoreSmall = new sf::Text("", this->scoreFont, 30U);
        this->scoreSmall->setString(std::to_string(score1) + " | " + std::to_string(score2));
        this->scoreSmall->setOrigin(this->scoreSmall->getLocalBounds().width / 2, this->scoreSmall->getLocalBounds().height / 2);
        this->scoreSmall->setPosition(this->WindowWidth / 2, this->scoreSmall->getGlobalBounds().height);
        this->scoreSmall->setFillColor(sf::Color::White);
        this->scoreSmall->setOutlineColor(sf::Color::White);
        this->scoreSmall->setOutlineThickness(1.0f);

        std::printf(scoreSmall->getString().toAnsiString().data());

        this->scoreBig = new sf::Text("0 | 0", this->scoreFont, 200U);
        this->scoreBig->setOrigin(this->scoreBig->getLocalBounds().width / 2, this->scoreBig->getLocalBounds().height / 2);
        this->scoreBig->setPosition(windowWidth / 2.0f, windowHeight / 2.0f);
        this->scoreBig->setFillColor(sf::Color::White);
}

void Game::createEntities()
{
        this->paddles[0] = new Paddle(sf::Vector2f(20, 100), 1, sf::Vector2f(this->window.getSize()));
        this->paddles[1] = new Paddle(sf::Vector2f(20, 100), 2, sf::Vector2f(this->window.getSize()));
        this->puck = new Ball(sf::Vector2f(20, 20), &(this->window), this->paddles);
}

void Game::startLoop()
{
        sf::Event event;
        while (window.isOpen())
        {
                // Initialises the paddles and the ball
                this->createEntities();

                // Round loop
                while (this->isRoundOver == false)
                {
                        while (this->window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                        this->window.close();
                        }

                        this->window.clear();

                        this->paddles[0]->Update();
                        this->window.draw(this->paddles[0]->getBody());

                        this->paddles[1]->Update();
                        this->window.draw(this->paddles[1]->getBody());

                        this->isRoundOver = this->puck->Update();
                        this->window.draw(this->puck->getBody());

                        this->window.draw(*this->scoreSmall);

                        this->window.display();
                       
                }

                /* Checks who won the round */
                if (this->puck->getBody().getPosition().x > this->WindowWidth / 2)
                        this->score1 += 1;
                else
                        this->score2 += 1;

                this->scoreSmall->setString(std::to_string(this->score1) + " | " + std::to_string(this->score2));
                this->scoreBig->setString(this->scoreSmall->getString());

                /* Positions the score in the middle of the screen */
                this->scoreBig->setPosition(WindowWidth / 2.0f, WindowHeight / 2.0f);

                /* Display the score in the middle of the screen */
                while (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Space) == false)
                {
                        while (this->window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                        this->window.close();
                        }

                        this->window.clear();

                        this->window.draw(this->paddles[0]->getBody());
                        this->window.draw(this->paddles[1]->getBody());

                        this->window.draw(*this->scoreSmall);
                        this->window.draw(*this->scoreBig);

                        this->window.display();
                }
                this->isRoundOver = false;
        }

}
 
« Last Edit: June 05, 2017, 11:39:29 am by JamesYeoman »

JamesYeoman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Text rendering issue [SFML 2.4.2] [screenshot included] [code added]
« Reply #1 on: June 05, 2017, 11:39:07 am »
I seriously have no idea what is going wrong here

JamesYeoman

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Text rendering issue [SFML 2.4.2] [screenshot included] [code added]
« Reply #2 on: June 05, 2017, 11:59:25 am »
I have found the issue... it was a problem with the font itself  ::)
The font must have omitted the numbers or something because it worked just fine with a different font

 

anything