c++spritegame-developmentsfmlpositioning

SFML C++ Game Development - Issue with Sprite Placement and Cropping During Shuffling


I'm developing a game using SFML in C++ where I encounter an issue related to sprite placement and cropping during shuffling. The sprites are not being centered within the game board cells, and there seems to be cropping on the sides. The problem occurs specifically during shuffling, and the initial placement works correctly. I've already tried adjusting the placement logic and scale calculations, but the issue persists.

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>
#include <iostream>
#include <iomanip>

static sf::RectangleShape board[8][8];
static sf::Texture textures[7];
static sf::Sprite sprites[8][8];
int highlightedRow = 0;
int highlightedCol = 0;
const float cellSize = 62.0f;


// Initialize random seed
void RandomSeed()
{
    std::srand(std::time(0));
}
// Center the sprite within the board block
void spriteScaleCenter(int row, int col, float scale)
{
    sprites[row][col].setScale(scale, scale);
    float xOffset = (cellSize - sprites[row][col].getGlobalBounds().width) / 2.0f;
    float yOffset = (cellSize - sprites[row][col].getGlobalBounds().height) / 2.0f;
    sprites[row][col].setPosition(board[row][col].getPosition().x + xOffset, board[row][col].getPosition().y + yOffset);
}

// Stores random images to textures and sets the sprite textures
void randTexturesSprites()
{
    // Stores images in textures array
    if (!textures[0].loadFromFile("image0.png"))
    {
        std::cout << "Failed to load texture: " << "image0.png" << std::endl;
        return;
    }
    if (!textures[1].loadFromFile("image1.png"))
    {
        std::cout << "Failed to load texture: " << "image1.png" << std::endl;
        return;
    }
    if (!textures[2].loadFromFile("image2.png"))
    {
        std::cout << "Failed to load texture: " << "image2.png" << std::endl;
        return;
    }
    if (!textures[3].loadFromFile("image3.png"))
    {
        std::cout << "Failed to load texture: " << "image3.png" << std::endl;
        return;
    }
    if (!textures[4].loadFromFile("image4.png"))
    {
        std::cout << "Failed to load texture: " << "image4.png" << std::endl;
        return;
    }
    if (!textures[5].loadFromFile("image5.png"))
    {
        std::cout << "Failed to load texture: " << "image5.png" << std::endl;
        return;
    }
    if (!textures[6].loadFromFile("image6.png"))
    {
        std::cout << "Failed to load texture: " << "image6.png" << std::endl;
        return;
    }


    // Assign random textures to each sprite and set scale
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            board[i][j].setSize(sf::Vector2f(cellSize, cellSize));
            board[i][j].setPosition((i + 7.9) * cellSize, (j + 0.2) * cellSize);
            if ((i + j) % 2 == 0)
            {
                board[i][j].setFillColor(sf::Color(43, 42, 42)); // Grey
            }
            else
            {
                board[i][j].setFillColor(sf::Color(0, 0, 0)); // Black
            }

            int randomTextureIndex = rand() % 7;
            sprites[i][j].setTexture(textures[randomTextureIndex]);

            // Set the scale of the sprites
            float scale = cellSize / static_cast<float>(textures[randomTextureIndex].getSize().x);
            scale -= 0.07f;
            sprites[i][j].setScale(scale, scale);

            spriteScaleCenter(i, j, scale);
        }
    }
}



int main()
{
    
    // Set up the window
    sf::RenderWindow window(sf::VideoMode(1000, 600), "Blitz", sf::Style::Close);

    RandomSeed();

    // Set up the board
    randTexturesSprites();


    // Game loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed)
            {
                randTexturesSprites();
            }
        }

        // Clear the window
        window.clear();

        // Set the background color
        window.clear(sf::Color(234, 182, 118)); // Change to your desired background color

        // Draw the board with highlight
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                window.draw(board[i][j]);
                window.draw(sprites[i][j]);
            }
        }

        // Display the content
        window.display();

    }

    return 0;
}


When the program is run for the first time: When the program is run for the first time

I expect the sprites to be accurately centered within each cell of the game board during shuffling, without any cropping on the sides.

When images are shuffled: When images are shuffled


Solution

  • I encountered a problem in my SFML C++ game development project, where the sprites on my game board were not being centered within the cells, and there was cropping on the sides, specifically during shuffling. The initial placement worked correctly, but shuffling caused issues.

    After some debugging and adjustments, I found a solution to the problem. The issue was related to the initialization of variables and arrays. When shuffling, it's essential to reset not only the sprite positions but also the texture, board, and sprite arrays to ensure a clean slate.