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

Author Topic: Sprite tearing problem?  (Read 2477 times)

0 Members and 1 Guest are viewing this topic.

Geners

  • Newbie
  • *
  • Posts: 9
    • View Profile
Sprite tearing problem?
« on: November 30, 2013, 06:23:42 am »



This is what's happening when I load certain sprites. Is it just that they're too big? Is this a known problem with SFML?

Here is my code (It's long) Tell me if posting the sprite map we're using would help (I load the charizard sprite under the function EVOLVE(); )

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
 
using namespace sf;
using namespace std;
 
enum STATE {PLAY, PAUSED, STARTER, LOADING} GAME_STATE = LOADING;
enum POKEMON { BULB, CHAR, SQUIRT, IVY, CHARMIL, WART, NONE } POKEMON_STATE = NONE;
Clock TIMER;
Time t1 = TIMER.getElapsedTime();
Time t2 = milliseconds(333);
Texture pauseTexture;
Sprite pauseSprite;
Texture texture;
Sprite sprite;
Texture startMenu;
Sprite startMenuS;
Texture palletTexture;
Sprite palletSprite;
RenderWindow window(VideoMode(640, 480), "PokeMOBA!", Style::Titlebar);
Vector2i poke;
double speed = 15.1;
 
 
void LOAD()
{
    palletTexture.loadFromFile("palletTown.png");
    palletSprite.setTexture(palletTexture);
    texture.loadFromFile("PokeSprites.png");
    sprite.setTexture(texture);
    pauseTexture.loadFromFile("PAUSE.png");
    pauseSprite.setTexture(pauseTexture);
    GAME_STATE = STARTER;
}
void START()
{
 
    window.clear();
    startMenu.loadFromFile("choosepokemon.png");
    startMenuS.setTexture(startMenu);
    window.draw(startMenuS);
    window.display();
     
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::Vector2i localPosition = sf::Mouse::getPosition(window); // window is a sf::Window
         
        if (localPosition.y < 165  && localPosition.y > 40)
        { //Clicked near a pokeball
 
            if ( 150 > localPosition.x && localPosition.x > 20)
            { //chose Bulbasaur!
                poke.x = 0;
                poke.y = 0;
                GAME_STATE = PLAY;
                                POKEMON_STATE = BULB;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();

                               
            }
            if (380 > localPosition.x && localPosition.x > 250)
            {//Chose CHARMANDER
                poke.x = 256;
                poke.y = 0;
                GAME_STATE = PLAY;
                                POKEMON_STATE = CHAR;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
            }
            if (625 > localPosition.x && localPosition.x > 495)
            {//Chose Squirtle
                poke.x = 450;
                poke.y = 0;
                GAME_STATE = PLAY;
                                POKEMON_STATE = SQUIRT;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
            }
        }
    }
}
void EVOLVE() {
        if(POKEMON_STATE == BULB && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO IVYSAUR!
                poke.x = 64;
                poke.y = 0;
                POKEMON_STATE = IVY;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
        }

        if(POKEMON_STATE == IVY && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO Venusaur!
                poke.x = 128;
                poke.y = 0;
                POKEMON_STATE = IVY;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
        }
       

        if(POKEMON_STATE == CHAR && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO CHARMILLION!
                poke.x = 320;
                poke.y = 0;
                POKEMON_STATE = CHARMIL;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
        }
       
        if(POKEMON_STATE == CHARMIL && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO Charizard!!
                poke.x = 385;
                poke.y = 0;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
       

        }

                if(POKEMON_STATE == SQUIRT && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO Venusaur!
                poke.x = 512;
                poke.y = 0;
                POKEMON_STATE = WART;
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
       

        }

                        if(POKEMON_STATE == WART && Keyboard::isKeyPressed(Keyboard::M)) { //EVOLVED INTO Venusaur!
                poke.x = 576;
                poke.y = 0;
           
                texture.loadFromFile("PokeSprites.png", IntRect(poke.x,poke.y,32,32));
                sprite.setTexture(texture);
                window.clear();
                window.draw(sprite);
                window.display();
       

        }
}
void PAUSE()
{
    window.clear();
    pauseTexture.loadFromFile("PAUSE.png");
    pauseSprite.setTexture(pauseTexture);
    window.draw(pauseSprite);
    window.display();
     
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    {
        GAME_STATE = PLAY;
        window.clear();
    }
}
void playDraw()
{
    window.draw(palletSprite);
    window.draw(sprite);
    window.display();
}
void walkAnimation(int x1, int y1, double speedx, double speedy)
{
    //if(t1 < t2)
    if (true)
    {
        TIMER.restart();
        window.clear();
        sprite.move(speedx,speedy);
        texture.loadFromFile("PokeSprites.png", IntRect(x1,y1,32,32));
        sprite.setTexture(texture);
        //sprite.setTextureRect(IntRect(x1,y1,32,32));
        playDraw();;
        window.clear();
        sprite.move(speedx,speedy);
        texture.loadFromFile("PokeSprites.png", IntRect(x1,y1+32,32,32));
        sprite.setTexture(texture);
        //sprite.setTextureRect(IntRect(x1+32,y1,32,32));
        playDraw();
    }
}
void playControls()
{    
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    {
        PAUSE();
        GAME_STATE = PAUSED;
    }
 
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
    {
        START();
        GAME_STATE = STARTER;
    }
     
     
    //while (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
        walkAnimation(poke.x,poke.y,speed,-speed);
 
    //while (Keyboard::isKeyPressed(Keyboard::Up) && Keyboard::isKeyPressed(Keyboard::Left))
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))  
        walkAnimation(poke.x,poke.y,-speed,-speed);
 
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //poke.x//poke.y+32//-SPEED//0
        walkAnimation(poke.x+32,poke.y,-speed,0);
     
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //poke.x//poke.y+64//+SPEED//0
        walkAnimation(poke.x+32,poke.y+64,speed,0);
     
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //This is the basis//poke.x//poke.y//0//-SPEED
        walkAnimation(poke.x,poke.y,0,-speed);
     
    while (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //poke.x//poke.y+32//0//+SPEED
        walkAnimation(poke.x,poke.y+64,0,speed);
         
 
}
int main()
{
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
 
        // Initialize
        sprite.setScale(2,2); //Increases the size of the sprite (xratio,yratio)
 
        while (GAME_STATE == LOADING)
        {
            LOAD();
        }
 
        while (GAME_STATE == STARTER)
        {
            START();
        }
 
        while (GAME_STATE == PAUSED)
        {
            PAUSE();
                        if(Keyboard::isKeyPressed(Keyboard::Delete)) {
                               
                                window.close();
                                break;
                        }
        }
 
        while (GAME_STATE == PLAY)
        {    
                       
            playDraw();
                        EVOLVE();
            playControls();
               

                       

                       
        }
 
   } //Close of while (window.isOpen())
 
    return 0;
} //Close of int main()
« Last Edit: November 30, 2013, 08:31:19 am by Geners »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Sprite tearing problem?
« Reply #1 on: November 30, 2013, 11:39:20 am »
It's not something that should happen. Just to clarify, the big line from the character to the right is not intended, right?

You should create a minimal but complete example. At best you start a new project and simply draw the sprite that causes issues. Also make surd your graphics driver is up to date. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Sprite tearing problem?
« Reply #2 on: November 30, 2013, 01:18:50 pm »
Weird things will happen when you have drawing code setup the way you do. I highly recommend that you reread the official tutorials again and pay close attention to how you are supposed to handle textures and the drawing loop.

Mixing multiple load/clear/draw/display calls inside event handling is a terrible way to write code and is inefficient.
« Last Edit: November 30, 2013, 01:29:09 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite tearing problem?
« Reply #3 on: November 30, 2013, 02:16:17 pm »
Furthermore, you duplicate loads of code. If you have a look at the overall code structure, you will notice that many parts look very similar -- identical apart from some values (concretely, I'm referring to the various if and while blocks). These are typical examples to outsource into functions. It will make your code shorter, more readable and much easier to maintain.

A single main function and global variables are not scalable either. If you plan to make a bigger project, object-oriented programming is a must. In addition to the code-design problems, global SFML variables are very likely to provoke technical problems. Avoid global variables at all costs.
« Last Edit: November 30, 2013, 02:19:17 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything