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

Author Topic: SFML PixelPerfectTest function  (Read 1078 times)

0 Members and 1 Guest are viewing this topic.

zpawlo00

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
SFML PixelPerfectTest function
« on: March 18, 2018, 10:05:45 pm »
I want to use this function:
 
Quote
bool TGame::PixelPerfectTest( )
{
   sf::FloatRect Intersection;
   if (Object1.getGlobalBounds().intersects(Object2.getGlobalBounds(), Intersection)) {
      sf::IntRect O1SubRect = Object1.getTextureRect();
      sf::IntRect O2SubRect = Object2.getTextureRect();

      sf::Uint8* mask1 = Bitmasks.GetMask(Object1.getTexture());
      sf::Uint8* mask2 = Bitmasks.GetMask(Object2.getTexture());

      for (int i = Intersection.left; i < Intersection.left + Intersection.width; i++) {
         for (int j = Intersection.top; j < Intersection.top + Intersection.height; j++) {

            sf::Vector2f o1v = Object1.getInverseTransform().transformPoint(i, j);
            sf::Vector2f o2v = Object2.getInverseTransform().transformPoint(i, j);

            if (o1v.x > 0 && o1v.y > 0 && o2v.x > 0 && o2v.y > 0 &&
               o1v.x < O1SubRect.width && o1v.y < O1SubRect.height &&
               o2v.x < O2SubRect.width && o2v.y < O2SubRect.height) {

               if (Bitmasks.GetPixel(mask1, Object1.getTexture(), (int)(o1v.x) + O1SubRect.left, (int)(o1v.y) + O1SubRect.top) > AlphaLimit &&
                  Bitmasks.GetPixel(mask2, Object2.getTexture(), (int)(o2v.x) + O2SubRect.left, (int)(o2v.y) + O2SubRect.top) > AlphaLimit)
                  return true;

            }
         }
      }
   }
   return false;
}
this function is from there https://github.com/SFML/SFML/wiki/Source:-Simple-Collision-Detection-for-SFML-2
I want to check collision beetwen:
Quote
#ifndef TPLAYER_H
#define TPLAYER_H
#include <SFML/Graphics.hpp>


class TPlayer : public sf::Drawable
{
public:

   TPlayer(float t_X, float t_Y);
   TPlayer() = delete;
   virtual ~TPlayer() = default;
   void update();

   float moveRight();
   float moveLeft();

   void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
   sf::Texture playerTexture;
   sf::Sprite playerSprite;
   sf::Texture *pTexture = &playerTexture;


private:
   
   


   const double playerVelocity{4};
   sf::Vector2f velocity{ playerVelocity, 0.f };




};

#endif // TPLAYER_H
and:
Quote
#pragma once
#include <SFML\Graphics.hpp>


class TBlocks:public sf::Drawable
{
public:
   TBlocks(float t_X, float t_Y);
   TBlocks()=delete;
   virtual ~TBlocks() = default;

   void update_block();
   void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
   sf::Texture block_texture;
   sf::Sprite blockSprite;
   sf::Texture *bTexture = &block_texture;

private:
   

   
   std::string blocks[3];


};

how to get sprites from this classes into function?

 

anything