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

Author Topic: Drawing different image than the one I have  (Read 1140 times)

0 Members and 1 Guest are viewing this topic.

Munchor

  • Newbie
  • *
  • Posts: 16
    • View Profile
Drawing different image than the one I have
« on: January 08, 2013, 10:30:31 pm »
I have a certain image on res/player.png. I open the image with an Image Viewer, and I get the very image.

Now, I have this class, which in the end, with a ::draw() method draws it. (note that Player is an sf::Sprite).

Code: [Select]
#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(12);
  debugText.setPosition(2, 14);
  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()
{
  debugText.setString("");

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    vx = 3;
  }

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

  /* Jumpity jumpy jump */
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
    if (map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor(x / 8)]) {
      vy = -6;
    }
  }

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

  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
        break;
    }
  } else if (vx < 0) {
    for (int i = 0; i < floor(-vx); i++) {
      if (!map->raw[(int) floor(y / 8)][(int) (floor((x - 1) / 8))])
        x--;
      else
        break;
    }
  }

  if (vy > 0) {
    for (int i = 0; i < floor(vy); i++) {
      if (y + height - floor(y + height) == 0.5) {
        system("clear");
        printf("\n\n\n\n*******\n****************************\n*************** HERE\n\n");
        y + 0.001;
      }
     
      if (!map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor(x / 8)] &&
          !map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor((x + width - 1) / 8)])
        y++;
      else
        break;
    }
  } else if (vy < 0) {
    y += vy;
  }

  //printf("%lf, %lf\n", y + height, floor(y + height + 0.5));

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

  setPosition(x, y);
}

void Player::draw() {
  game->window.draw(*this);
  game->window.draw(debugText);
}

And the image I get on the screen when playing the Player is a white square (32 * 32).

Any ideas on why this might be happening? Thanks. I tried renaming the image, making the project countless times.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Drawing different image than the one I have
« Reply #1 on: January 08, 2013, 10:36:21 pm »
The white square indicates that your sprite doesn't have a texture. This is because the texture is only created locally in your constructor (and destroyed at its end) . The sf::Sprite class only take a reference to a sprite, so you have to keep the texture alive as long as you use the sprite. In your case make the texture a member of the class.

By the way a simple search with you favourite search engine would have brought up tons of posts with similar questions ;)

 

anything