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

Author Topic: [SOLVED]Text printing across the screen ?  (Read 3246 times)

0 Members and 1 Guest are viewing this topic.

gaz99

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED]Text printing across the screen ?
« on: February 08, 2012, 06:40:53 pm »
Hi i'm making a breakout clone and i'm trying to implement the score. I have it all working, however when i draw the text it doesn't draw it in the same place but instead draws it as if i was holding a number key down i.e
"000000000000000000000000000000000000" across the screen. I don't know why it's doing this any help would be apreciated Thanks.

heres the .cpp
Code: [Select]

#include "scoreboard.h"

scoreboard::scoreboard(){}

void scoreboard::init()
{
scoreboardIMG.LoadFromFile("Maze/assets/scoreboard.png");
scoreB = sf::Sprite (scoreboardIMG);
scoreB.SetPosition(800,0);

playerScore = 0;
    Cheeseburger.LoadFromFile("Maze/assets/andyb.ttf");
scoreText.SetFont(Cheeseburger);
scoreText.SetSize(15.f);
    scoreText.SetColor(sf::Color(0,0,255));
}

void scoreboard::draw(sf::RenderWindow& win)
{
win.Draw(scoreB);
win.Draw(scoreText);
}

void scoreboard::getScore(ball& ball)
{
playerScore = ball.getDestroyed() * 50;
pScore << playerScore;
scoreText.SetText(pScore.str());
scoreText.SetPosition(0.f, 320.f);
scoreText.Move(0.f,0.f);



}


and the
header
Code: [Select]

#ifndef SCOREBOARD_H
#define SCOREBOARD_H
#include<SFML\Graphics.hpp>
#include<sstream>
#include "ball.h"

class scoreboard
{
public:
scoreboard();
void init();
void draw(sf::RenderWindow&);
void getScore(ball&);

protected:
sf::Image scoreboardIMG;
sf::Sprite scoreB;
int playerScore;

sf::String scoreText;
sf::Font Cheeseburger;
std::ostringstream pScore;
};



#endif

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
[SOLVED]Text printing across the screen ?
« Reply #1 on: February 08, 2012, 07:44:49 pm »
I don't see anything wrong with that code, but are u calling getScore before draw?
I would call the SetPosition function inside draw so that the position is set to the correct place before drawing. If getScore isn't called before draw then the position will be wrong.

Also this line doesn't do much:
Code: [Select]
scoreText.Move(0.f,0.f);

I don't know what could be causing your problem, but try debugging (maybe you already tried those things):
- You should only see one 0 instead of a whole list of them, so is pScore still correct in the getScore function or does it already contain all those 0's?
- You are drawing scoreText together with scoreB, so if the text is drawn multiple times then the background would be drawn multiple times too. (I guess you would have noticed this so the problem does not lie with calling the draw function multiple times)
- You should try calling GetPosition inside the draw function and make sure that it is still (0, 320).


This has nothing to do with your problem, but you can replace the first line (which you use in your code) with the second one.
Code: [Select]
scoreB = sf::Sprite(scoreboardIMG);
scoreB.SetImage(scoreboardIMG);
.
TGUI: C++ SFML GUI

gaz99

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED]Text printing across the screen ?
« Reply #2 on: February 08, 2012, 08:35:06 pm »
Hi thanks for the quick reply so i put the position in the draw function like you suggested and it does the same thing the first 0 is drawn at the position specified but then draws across the screen.

my game loop is this
Code: [Select]

void game::play()
{
// cout<<"playing\n"<<endl;

sf::Event currentEvent;
win.GetEvent(currentEvent);

switch(gameState)
{
case game::Splashing:
{
ShowSplash();
break;
}
case game::Playing:
{

win.Clear(sf::Color::Black);//clear screen
draw();//draw
updateAll();//update
//check for close
if(currentEvent.Type == sf::Event::Closed)
{
gameState = Exiting;
}
if((currentEvent.Type == sf::Event::KeyPressed) && (currentEvent.Key.Code == sf::Key::Escape))
{
gameState = Exiting;
}
break;
}

case game::Exiting:
{
win.Close();
break;
}
}
}

And the draw and update functions called

Code: [Select]

void game::draw()
{
//cout<<"Drawing \n"<<endl;
PATH.draw(win);//blocks
BALL1.draw(win);//ball
PLAYER1.draw(win);//player
SCOREB.draw(win);




}
void game::updateAll()
{

//cout<<"updating\n"<<endl;
PLAYER1.update(win);//player
BALL1.update(win, getPlayer(), getBlock());
SCOREB.getScore(BALL1);
cout<<SCOREB.playerScore<<endl;


}


i printed the player score to the console and it is showing correct and updating correctly so it's something to do with my string but i have no idea why this is happening.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
[SOLVED]Text printing across the screen ?
« Reply #3 on: February 08, 2012, 08:52:27 pm »
The call pScore.str() does not clear the stream buffer. So either reset it manually (e.g. pScore = std::ostringstream()) or use the ostringstream variable locally, i.e. don't declare it as a member variable.

gaz99

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED]Text printing across the screen ?
« Reply #4 on: February 08, 2012, 09:11:55 pm »
Ahhhh problem solved thank you very much.