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

Author Topic: sf::View Scrolling Tiles  (Read 1299 times)

0 Members and 1 Guest are viewing this topic.

Kalipto

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::View Scrolling Tiles
« on: March 25, 2017, 02:08:42 am »
Hey folks,

I'm new round these parts (SFML and C++) and am working on a little RPG type project. Taking it one step at a time. Right now I'm looking for a little more information about the way to use sf::View to get sprites to scroll.

So I've got a map array of around 500 x 500. Each item in the array tells whether the player can move through that point (like a path) or if that particular point should be impassible (like a wall).

I read the map array and draw sprites to an active sf:View. I can then use playerSprite.move(moveSpeed * clock.getElapsedTime().asSeconds(), 0); to slowly scroll around when a key is pressed. Keeping the view.setCenter on the playerSprite position.

What I'm struggling with is how to line-up the players position visually with the map array. The tiles move pixel by pixel and I'm not sure how to tell when the players position should be moved.

The map has a starting X/Y position. I'm not sure how to put the player sprite in the right location to even get it started.

I don't know if that makes sense.

Here's some code:

      //define the players locaiton on the map array. These values are passed to us from the map itself
      int charX, charY

        // This begins the game loop itself
        while (Window.isOpen())
        {
                // Event tells it to process any events in the "event loop"
                sf::Event Event;

                // this loop goes through all the events before moving on
                while (Window.pollEvent(Event))
                {                      
                        // Process events (keyboard ect)
                        switch (Event.type)
                        {

                        // stops the program if you click close
                        case sf::Event::Closed:
                                Window.close();
                                break;
                        }
                }

                if (Window.hasFocus())
                {
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                playerSpr.move(moveSpeed * clock.getElapsedTime().asSeconds(), 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                playerSpr.move(moveSpeed * clock.getElapsedTime().asSeconds()*-1, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                playerSpr.move(0, moveSpeed * clock.getElapsedTime().asSeconds()*-1);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                playerSpr.move(0, moveSpeed * clock.getElapsedTime().asSeconds());
                        }
                        view.setCenter(playerSpr.getPosition());
                }

                // Draw stuff to the screen
                Window.setView(view);          
                drawMap();
                Window.draw(playerSpr);
                Window.display();
                Window.clear();

void drawMap()
{
       // how much of the map around the charX/Y to draw
        short viewDistance = 15;
       // calculate the vertical view distance for wide screen
        short vertViewDistance = (viewDistance / 4) * 3;

        int sprX = 0, sprY = 0;

        for (int y = (charY - vertViewDistance); y <= charY + vertViewDistance; y++)
        {
                //std::cout << "\t";
                for (int x = (charX - viewDistance); x <= charX + viewDistance; x++)
                {
                        if (x >= 0 && y >= 0 && x < mapWidth && y < mapHeight)
                        {
                                if (map[x][y] == " ")
                                {
                                        // Draw Path
                                        pathSpr.setPosition(sprX*96, sprY*96);
                                        Window.draw(pathSpr);
                                }
                                else if (map[x][y] == "X" || map[x][y] == "0")
                                {
                                        // Draw Wall
                                        wallSpr.setPosition(sprX*96, sprY*96);
                                        Window.draw(wallSpr);                          
                                }
                        }
                        sprX++;
                }              
                sprY++;
                sprX = 0;
        }
}

 
]

Right now this code will move the player around the map. But it doesn't update the players location within the map array. So I can't tell if I'm walking into a wall or not.

Any thoughts on an approach?
« Last Edit: March 25, 2017, 04:38:20 am by Kalipto »

Kalipto

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::View Scrolling Tiles
« Reply #1 on: March 25, 2017, 06:17:00 am »
It's amazing what you can figure out when you work on an issue for 7 hours.

The long and short of the solution was to basically use the player sprites position divided by the tile size. That gives me the position within my map array.

Pretty simple when you finally figure it out. Now the map loads as I move around and its super awesome!

 

anything