Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8993797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:16:29+00:00 2026-06-15T23:16:29+00:00

I am learning C++/SFML and as a practice exercise I am making a small

  • 0

I am learning C++/SFML and as a practice exercise I am making a small program that dispalys a 10×10 grid of 64×64 px square brown sprites in a window. This program allows you to select green/yellow/blue/grey/brown square sprites using the keyboard and replace any tile on said grid with this selected sprite. Game loop below:

while (window.isOpen())
{
    window.clear(sf::Color(sf::Color::Black));
    sf::Event event;
    while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) s_paintBrush = s_sand;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) s_paintBrush = s_grass;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num3)) s_paintBrush = s_dirt;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num4)) s_paintBrush = s_water;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num5)) s_paintBrush = s_rock;

    if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        sf::Vector2i localPosition = sf::Mouse::getPosition(window);
        int i = localPosition.x / x;
        int j = localPosition.y / y;
        if (i < columns && j < rows && i >= 0 && j >=0) grid[j][i].m_terrain = s_paintBrush;;
    }

    for (int i = 0; i < rows; ++i)
    { 
        for (int j = 0; j < columns; ++j)
        {           
                window.draw(grid[i][j].m_terrain);
                grid[i][j].m_terrain.setPosition(x * j, y * i);
        }
    }
    window.display();
}   

“grid” is a 2D vector of the class “tile” that contains the sf::Sprite member variable “m_terrain” that stores the sprite to be displayed at the corresponding grid location. Integers x & y = 64 and are used to determine the coordinates of “grid” that correspond to the mouseclick location on the window. Anything with the prefix s_ is a sf::Sprite.

This program is working fine with one exception, when I press the left mouse button on a tile I want to change, the previous sprite disappears leaving a blank square area, and the replacement sprite displays in the top left corner of the window until the mouse button is released. The replacement sprite is then correctly displayed at the mouseclick location.

To me this indicates that the new sprite is being generated on mouseclick, but not receiving the setPosition coordinates until the mouse button is released. I am unsure how to fix this, and have not been able to find an answer (that is simple enough for me to understand at least, I am only learning!).

Thankyou for your attention.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:16:31+00:00Added an answer on June 15, 2026 at 11:16 pm

    The reason that happens is that you draw the sprite before you set the position. Then when next event occurs (the button up event) the sprite is drawn again but this time with the correct coordinates.

    Switching the lines where you draw and set position will make it work:

    grid[i][j].m_terrain.setPosition(x * j, y * i);
    window.draw(grid[i][j].m_terrain);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For learning purposes I'm building a simple messaging program that I think can work
Learning a lot in my few years of programming that the best projects are
Learning C and this is an exercise. The user is asked to input the
Learning haskell and want a function to generate a 2D grid similar to how
Learning java server technologies, trying to clarify some things. There are few technologies that
I'm learning SFML library and i picked a code from the tutorial. it opens
Learning ruby. I'm under the impression that boolean attributes should be named as follows:
Learning C++ with help of Bruce Eckel Thinking in C++. Stuck in exercise 05
I've recently began learning the SFML API for learning purposes but it seems to
learning Hibernate and its really making me crazy i am trying to persist an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.