Advertisement
Guest User

sfml-rendertexture-minimal-segfault

a guest
May 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. class AnimatedSprite : public sf::Transformable, public sf::Drawable
  6. {
  7. public:
  8.     AnimatedSprite()
  9.     {
  10.         vertices.setPrimitiveType(sf::Quads);
  11.         vertices.resize(4);
  12.  
  13.         texture = new sf::Texture();
  14.         if (!texture->loadFromFile("assets/pokemon.png"))
  15.         {
  16.             std::cout << "ASDF\n";
  17.         }
  18.        
  19.         auto frameBounds = sf::IntRect(0,0,16,16);
  20.  
  21.         vertices[0].position = sf::Vector2f(0,0);
  22.         vertices[1].position = sf::Vector2f(0,frameBounds.height);
  23.         vertices[2].position = sf::Vector2f(frameBounds.width, frameBounds.height);
  24.         vertices[3].position = sf::Vector2f(frameBounds.width,0);
  25.  
  26.         float left = frameBounds.left;
  27.         float right = frameBounds.left + frameBounds.width;
  28.         float top = frameBounds.top;
  29.         float bottom = frameBounds.top + frameBounds.height;
  30.  
  31.         vertices[0].texCoords = sf::Vector2f(left, bottom);
  32.         vertices[1].texCoords = sf::Vector2f(left, top);
  33.         vertices[2].texCoords = sf::Vector2f(right, top);
  34.         vertices[3].texCoords = sf::Vector2f(right, bottom);
  35.     }
  36.  
  37. protected:
  38.     virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
  39.     {
  40.         states.transform *= getTransform();
  41.         states.texture = texture;
  42.         target.draw(vertices, states);
  43.     }
  44.  
  45.     sf::VertexArray vertices;
  46.     sf::Texture *texture;
  47. };
  48.  
  49. int main()
  50. {
  51.     sf::RenderTexture renderTexture;
  52.     renderTexture.create(160, 144);
  53.  
  54.     AnimatedSprite player;
  55.     player.setPosition(0,0);
  56.  
  57.     renderTexture.draw(player);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement