SFML community forums

Help => Graphics => Topic started by: b0rn2c0de on April 29, 2013, 06:36:39 pm

Title: ship sprite not displayed
Post by: b0rn2c0de on April 29, 2013, 06:36:39 pm
hey guyz i started creating a space shooter type game and ran into an error
i currently have a ship class and a main file cpp .. the problem is that my ship wont draw on the screen plzz take a look at the code
here are the files

ship.h
#pragma once
#include<iostream>
#include<SFML\graphics.hpp>

class ship{
   private:
           sf::Texture st;
           sf::Sprite ss;
           int frame ;
   public:
           int xvel;
       int yvel;
           int life;
           ship(float x,float y);
           void move(sf::RenderWindow &name);
           };
ship::ship(float x ,float y)
{
        life=100;
        if(!st.loadFromFile("bul1.png")){std::cout<<"error";}
        ss.setTexture(st);
        ss.setPosition(x,y);
        frame = 2;
}
void ship::move(sf::RenderWindow &name)
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ yvel-=0.8;}    
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){ yvel+=0.8;}
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
         { xvel-=0.8;
          --frame;}
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
         { xvel+=0.8;
          ++frame;}
        else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Left)&& !sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){frame=2;}

        if(frame>4){frame=4;}
         else if(frame<0){frame=0;}

        if(frame==0){ss.setTextureRect(sf::IntRect(0,0,22,43));}
        else if(frame==1){std::cout<<"f2";
                ss.setTextureRect(sf::IntRect(42,0,33,43));}
        else if(frame==2){ss.setTextureRect(sf::IntRect(86,0,43,43));}
        else if(frame==3){ss.setTextureRect(sf::IntRect(140,0,33,43));}
        else if(frame==4){ss.setTextureRect(sf::IntRect(194,0,22,43));}
        ss.move(xvel,yvel);
        name.draw(ss);
        std::cout<<"draw";
}
 
 

and main file

#include "stdafx.h"
#include <SFML\graphics.hpp>
#include "ship.h"


int main()
{sf::RenderWindow app(sf::VideoMode(800,600),"test");
sf::Texture bt;
bt.loadFromFile("spw.jpg");
sf::Sprite background(bt);
ship player(400,300);
while(app.isOpen())
{       app.clear();
    app.draw(background);
        sf::Event x;
        while(app.pollEvent(x))
                {if(x.type==sf::Event::Closed){app.close();}}
        player.move(app);
        app.display();
}
        return 0;
}
 

please help guyz
Title: Re: ship sprite not displayed
Post by: Nexus on April 29, 2013, 10:50:45 pm
First, please express yourself meaningfully if you want to be taken seriously. This is not the hacker forum for "b0rn2c0de", "plz" and "guyz" ;)

Second, format your code in a readable way, i.e. indent it, use whitespaces and break lines where appropriate. Also, come up with a complete and minimal example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) that reproduces your problem. Ideally, this is only a single main() function. While you reduce your code, chances are high that you already see the problem.

Third, you should definitely read the SFML tutorials. You seem not to understand the separations between sprites and textures, also the event and draw loop are usually not mixed.
Title: Re: ship sprite not displayed
Post by: Raphman on April 29, 2013, 11:06:09 pm
You seem not to understand the separations between sprites and textures.

Could you explain that to me? I've read the documentation pretty extensively and I handle my sprites textures in a same way (loading a texture, then setting the sprite to use said texture).

Also OP, you shouldn't draw the ship in that move function, break it out into a separate one.
Title: Re: ship sprite not displayed
Post by: Nexus on April 29, 2013, 11:18:55 pm
Could you explain that to me? I've read the documentation pretty extensively and I handle my sprites textures in a same way (loading a texture, then setting the sprite to use said texture).
Textures are heavyweight resources, unlike sprites you should not duplicate them.

In the above code, not every ship instance should have its own texture.
Title: Re: ship sprite not displayed
Post by: Raphman on April 29, 2013, 11:28:36 pm
Oh yes, I see what you are saying.  I am doing what you said, good. Thanks.