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

Author Topic: [SOLVED] sf::Text not appearing on the screen v2  (Read 2100 times)

0 Members and 1 Guest are viewing this topic.

Munchor

  • Newbie
  • *
  • Posts: 16
    • View Profile
[SOLVED] sf::Text not appearing on the screen v2
« on: January 06, 2013, 08:43:08 pm »
#include "Player.hpp"

using namespace std;

Player::Player(Game *_game, Tilemap *_map)
{
  game = _game;
  map = _map;

  /* Prepare debug label */
  debugText.setFont(game->arialFont);
  debugText.setCharacterSize(80);
  debugText.setPosition(16, 16);
  debugText.setColor(sf::Color::Red);

  /* Load image */
  sf::Texture texture;
  if (!texture.loadFromFile("res/player.png")) {
    printf("Error loading player image.\n");
  }

  width = 32;
  height = 32;

  x = 20;
  y = 20;

  vx = 0.f;
  vy = 0.f;

  setTexture(texture);
}

void Player::update()
{
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    vx = 2;
  }

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    vx = -2;
  }

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
    vy = -3;
  }

  if (!map->raw[(int) floor((y + height) / 8)][(int) floor(x / 8)]) {
    vy += 0.4;
  }

  if (vx > 0) {
    for (int i = 0; i < floor(vx); i++) {
      if (!map->raw[(int) floor(y / 8)][(int) floor((x + width) / 8)])
        x++;
    }
  } else if (vx < 0) {
    for (int i = 0; i > ceil(vx); i--) {
      if (!map->raw[(int) floor(y / 8)][(int) (floor((x + width) / 8) - 4)])
        x--;
    }
  }

  if (vy > 0) {
    for (int i = 0; i < floor(vy); i++) {
      if (!map->raw[(int) floor((y + height) / 8)][(int) floor(x / 8)])
        y++;
    }
  } else if (vy < 0) {
    y += vy;
  }

  /* Apply friction */
  if (vx > 0) vx -= 0.1;
  if (vx < 0) vx += 0.1;
  if (abs(vx) < 0.15) vx = 0;

  if (vy > 0) vy -= 0.1;
  if (vy < 0) vy += 0.1;
  if (abs(vy) < 0.15) vy = 0;

  setPosition(x, y);

  debugText.setString("ss");
  game->window.draw(debugText);
}
 

That is my Player.cpp file.

I'm drawing that string, but it's nowhere to be seen on the screen.

On another class:

#include "PlayState.hpp"

using namespace std;

PlayState::PlayState(Game *_game)
{
  game = _game;

  /* Set up FPS string */
  fps.setFont(game->arialFont);
  fps.setCharacterSize(12);
  fps.setPosition(2, 2);
  fps.setColor(sf::Color::Red);

  currentMap = new Tilemap("res/map1.txt", &(game->window));
  player = new Player(_game, currentMap);
}

void PlayState::setup() { }

void PlayState::update()
{
  sf::Event event;
  while (game->window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) {
      game->window.close();
    } else if (event.type == sf::Event::KeyPressed) {
      if (event.key.code == sf::Keyboard::Escape) {
        game->window.close();
      }
    }
  }

  player->update();
}

void PlayState::draw()
{
  /* Clear the screen */
  game->window.clear();

  /* Draw the player */
  game->window.draw(*player);
 
  /* Draw the map */
  currentMap->draw(400, 320);

  /* Draw FPS */
  stringstream fpsStream;
  fpsStream << ceil(game->fps);
  fps.setString(fpsStream.str());
  game->window.draw(fps);

  /* Display */
  game->window.display();
}
 

I have some very similar code for drawing sf::Text fps. I'm using the same font (game->arialFont) and giving it the same color, but I can't see sf::Text debugText (I can see sf::Text fps).

Any ideas?
« Last Edit: January 06, 2013, 10:40:37 pm by Munchor »

kimci86

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Re: sf::Text not appearing on the screen v2
« Reply #1 on: January 06, 2013, 09:19:09 pm »
I suggest that you add a draw method to the Player class and draw the debugText there.
The problem here is that you draw the debugText in Player::upate and then clear the screen in PlayState::draw so the debugText is cleared.

Munchor

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: sf::Text not appearing on the screen v2
« Reply #2 on: January 06, 2013, 10:40:25 pm »
I suggest that you add a draw method to the Player class and draw the debugText there.
The problem here is that you draw the debugText in Player::upate and then clear the screen in PlayState::draw so the debugText is cleared.

That was dumb of me. Thanks, it worked!