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

Author Topic: Why sprite not showing up after i use sf::setOrigin  (Read 405 times)

0 Members and 1 Guest are viewing this topic.

guuest

  • Newbie
  • *
  • Posts: 4
    • View Profile
Why sprite not showing up after i use sf::setOrigin
« on: September 25, 2022, 09:28:17 am »
why

here code:
#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>
#include "Animation.hpp"

namespace p_1{

    class Player{
        public:
            Player(sf::Texture &b, sf::Texture &p_t2_, sf::Vector2u img_count,
            float switch_time, float speed) : animation(&p_t2_, img_count, switch_time){
                sprite.setTexture(p_t2_);
                sprite.setScale(sf::Vector2f(2.f, 2.f));
                sprite.setPosition(640/2, 480/2);
                this->speed = speed;
                this->face_r = true;
                const sf::Vector2f spriteSize(sprite.getGlobalBounds().width/2.f, sprite.getGlobalBounds().height/2.f);
                sf::Vector2u size = b.getSize();
                //sprite.setOrigin(size.x / 2, size.y / 2);
               
            }
            virtual ~Player() = default;
        private:
            sf::Sprite sprite;
            unsigned int row;
            int speed=0.0f;
            bool face_r=false;
            Animation animation;
        public:
            void draw(sf::RenderWindow& window);
            void update(float dt);
            sf::Vector2f getPosition(){
                return sprite.getPosition();
            }
    };
}

 

guuest

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Why sprite not showing up after i use sf::setOrigin
« Reply #1 on: September 25, 2022, 09:29:10 am »
here main.cpp:

#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.hpp"

struct Texture{
   sf::Texture texture;
   sf::Texture background_t_0;
   sf::Texture background_t_1;
   sf::Texture background_t_2;
   sf::Texture background_t_3;
   sf::Texture background_t_4;
   sf::Texture background_t_5;
   sf::Texture background_light_1;
   sf::Texture background_t_6;
   sf::Texture background_light_0;
   sf::Texture background_t_7;
   sf::Texture background_t_8;
   sf::Texture background_t_9;
}t;

int main(){
   sf::RenderWindow window(sf::VideoMode(640, 480), "Pixel game", sf::Style::Default);
   sf::Event e;
   sf::Sprite background_0;
   sf::Sprite background_1;
   sf::Sprite background_2;
   sf::Sprite background_3;
   sf::Sprite background_4;
   sf::Sprite background_5;
   sf::Sprite light_1;
   sf::Sprite background_6;
   sf::Sprite light_0;
   sf::Sprite background_7;
   sf::Sprite background_8;
   sf::Sprite background_9;
   sf::View view(sf::Vector2f(0.f, 0.f), sf::Vector2f(640.0f, 480.0f));
   t.texture.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/pixil-frame-0(1).png");
   t.background_t_0.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0011_0.png");
   t.background_t_1.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0010_1.png");
   t.background_t_2.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0009_2.png");
   t.background_t_3.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0008_3.png");
   t.background_t_4.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0006_4.png");
   t.background_t_5.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0005_5.png");
   t.background_light_1.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0004_Lights.png");
   t.background_t_6.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0003_6.png");
   t.background_light_0.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0007_Lights.png");
   t.background_t_7.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0002_7.png");
   t.background_t_8.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0001_8.png");
   t.background_t_9.loadFromFile("/home/webmaster/Documents/vscode-sfml/src/Assets/Free Pixel Art Forest/PNG/Background layers/Layer_0000_9.png");

   background_0.setTexture(t.background_t_0);
   background_1.setTexture(t.background_t_1);
   background_2.setTexture(t.background_t_2);
   background_3.setTexture(t.background_t_3);
   background_4.setTexture(t.background_t_4);
   background_5.setTexture(t.background_t_5);
   light_1.setTexture(t.background_light_1);
   background_6.setTexture(t.background_t_6);
   light_0.setTexture(t.background_light_0);
   background_7.setTexture(t.background_t_7);
   background_8.setTexture(t.background_t_8);
   background_9.setTexture(t.background_t_9);
   p_1::Player player_1(t.background_t_0, t.texture, sf::Vector2u(8, 8), 0.1f, 150.f);
   float dt = 0.f;
   sf::Clock clock;
   while(window.isOpen()){
      dt = clock.restart().asSeconds();
      while(window.pollEvent(e)){
         switch (e.type)
         {
         case sf::Event::Closed:
            window.close();
            break;
         case sf::Event::KeyPressed:
            switch (e.key.code)
            {
            case sf::Keyboard::Escape:
               window.close();
               break;
           
            default:
               break;
            }
            break;
         
         default:
            break;
         }
      }
      player_1.update(dt);
      view.setCenter(player_1.getPosition());
      window.clear(sf::Color::White);
      window.setView(view);
      window.draw(background_0);
      window.draw(background_1);
      window.draw(background_2);
      window.draw(background_3);
      window.draw(background_4);
      window.draw(background_5);
      window.draw(light_1);
      window.draw(background_6);
      window.draw(light_0);
      window.draw(background_7);
      window.draw(background_8);
      window.draw(background_9);
      player_1.draw(window);
      window.display();
   }
   return 0;
}

kojack

  • Full Member
  • ***
  • Posts: 203
    • View Profile
Re: Why sprite not showing up after i use sf::setOrigin
« Reply #2 on: September 25, 2022, 04:16:24 pm »
You are setting the origin to half of the background texture size instead of half of the sprite texture size.
That would make the sprite appear half a screen to the left and above where you wanted it.

guuest

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Why sprite not showing up after i use sf::setOrigin
« Reply #3 on: September 26, 2022, 10:18:24 am »
How do i get sprite size? like this:

sf::Vector2f s_size = sprite.getSize();

or how do i get the sprite texture size?
« Last Edit: September 26, 2022, 10:20:30 am by guuest »

guuest

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Why sprite not showing up after i use sf::setOrigin
« Reply #4 on: September 26, 2022, 10:30:22 am »
I already did as you said it still don't work:

sf::Vector2u size = p_t2_.getSize();
                sprite.setOrigin(size.x / 2, size.y / 2); //p_t2_ sprite texture

The sprite also scaled i don't know what is has to do with origin i just went through some post,
« Last Edit: September 26, 2022, 10:35:22 am by guuest »

Hapax

  • Hero Member
  • *****
  • Posts: 3071
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: Why sprite not showing up after i use sf::setOrigin
« Reply #5 on: October 16, 2022, 09:45:41 pm »
To set the origin to the centre of the sprite:
sf::Vector2f spriteSize{ sprite.getLocalBounds().width, sprite.getLocalBounds().height };
sprite.setOrigin(sprite.getSize() / 2.f);

This means that when you set the sprite's position, its centre will be placed at that position (instead of the default top-left corner).
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

 

anything