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

Author Topic: Making SFML smoother and more accurate  (Read 8712 times)

0 Members and 1 Guest are viewing this topic.

stoney153

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #15 on: June 17, 2013, 08:07:38 pm »
Am I thinking about the layout of this the right way?

Quote
playersprite.move ( playersprite.getPosition( window ).x +=0, playersprite.getPosition( window ).y -=10 );

Trying to combine my code with what stoney153 posted, I am just messing around with it right now, codeblocks is complaining about the way I've written it.

You don't need to put:  playersprite.getPosition( window ).x +=0, it's redundant. Just put 0 :P Anywhoo well if you want something simple just make a function for your user input, then add something like: if(sf::keyboard::iskeypressed(sf::keyboard::w)){x-=4} and when drawing your're sprite just use something like: sprite.setposition(x, y); :)

---modify---
P.S you're using 'getPosition' instead of 'setPositon' which would also be an issue. getPosition only finds where the x or y is. setPosition is the modifyer.
« Last Edit: June 17, 2013, 08:18:26 pm by stoney153 »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #16 on: June 17, 2013, 09:09:13 pm »
I get what you're saying about the differences between setPosition and getPosition but you know I'm using the move command for changing the sprite position right? I'm trying to put your stuff in but it tends to either complain about the x and y and of course if I put setPosition in that works with just coordinates but when I try to put it anywhere else it seems to want me to use that as the main command instead. I found that when I use setposition with the keys that tends to screw things up a bit but it works pretty well with the mouse, maybe I should look at the coordinates system I used on that.

I'm a bit confused about this now >_< I think I need some full example code to look at so I can see where it all needs to go.
« Last Edit: June 17, 2013, 09:15:40 pm by Lethn »

stoney153

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #17 on: June 17, 2013, 09:28:12 pm »
Oh sorry buddy sure, I've not had enough red bull yet :P

have something like:
Quote
void setpositions(){
   Myx = 100, Myy = 100; //making these global in your header. btw. global stuff - baaaaaaaad idea. but it's an example
        sf::texture txt;
        txt.loadfromfile("w/e");
   sprite.setTexture(texture, true); //assume you've created a sprite in a header or something, otherwise just sf::sprite sprite;
}

void UserInput(){
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
      Myy-=4;
   }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
      Myx-=4;
   }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
      Myy+=4;
   }
   if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
      Myx+=4;
   }
}

void drawwindow(){
    window->clear()
    window->draw(sprite)
    window->display();
}

Or something. Try fiddling and get back to me :D ...giggidy? -.-

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #18 on: June 17, 2013, 09:44:19 pm »
ooo thanks! I'll give that a shot, that's a lot clearer now, I always find seeing the whole thing helps me out with where it all goes and what to use it with.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #19 on: June 17, 2013, 10:07:32 pm »
Quote
int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "Lethn 2D Engine");

sf::Texture texturebackground;
if (!texturebackground.loadFromFile( "background.png" ) )
return EXIT_FAILURE;
sf::Sprite spritebackground(texturebackground);

sf::Texture player;
if ( !player.loadFromFile( "player.png" ) )
return EXIT_FAILURE;
sf::Sprite playersprite ( player );
playersprite.setPosition ( 100, 400 );


sf::View TopDownCamera;
TopDownCamera.reset ( sf::FloatRect ( 0, 0, 800, 600 ) );



    while (window.isOpen())
    {


        sf::Event event;
        while (window.pollEvent(event))

        {
            if (event.type == sf::Event::Closed)
                window.close();

                if ( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
                        Myy-=10;

                {
                playersprite.move ( x, y )
                }

                }


        }

        window.clear();
        window.setView( TopDownCamera );
        window.draw ( spritebackground );
        window.draw( playersprite );
        window.display();
    }

    return 0;
}



I'm pretty sure I have the right code but it's complaining about the use of letters and the 'Myy' thing you showed, I'll keep messing around and see if I can change anything.

stoney153

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Making SFML smoother and more accurate
« Reply #20 on: June 18, 2013, 04:06:06 pm »
Don't forget to initialize variables and don't put your movement inside the event while loop :)

int Myx;
int Myy;
etc.

You know there's a great little tutorial that might give you a huge insight into this sort of thing. It's outdated and you could code a much cleaner version of it, but try it out:
http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/

It'll give you more of a general insight into the use of sfml for tiled games (but you could modify it to suit your needs) but remember it's outdated. 1.6 instead of 2.0 (I've posted fixed version of some of his work in the comments - for part 2 and 3 I think.