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 8411213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:22:57+00:00 2026-06-10T00:22:57+00:00

Basically i’m trying to make a tic-tac-toe game using C++ and SFML2 API. I

  • 0

Basically i’m trying to make a tic-tac-toe game using C++ and SFML2 API. I loaded the background and the spritesheet in my main function and got the background to show just fine. Afterwards i went on to create a Tile class which looks like this.

Tile.h

#ifndef TILE_H
#define TILE_H

#include <SFML\Graphics.hpp>

enum TileStatus
{
    FREE    = 0,
    CROSS   = 1,
    CIRCLE  = 2
};

class Tile
{

private:

    float   xPos,
            yPos;

    sf::Sprite tileSprite;


    int     height,
            width;

    TileStatus tileStatus;

public:

    //Constructors
    Tile();
    Tile( sf::Texture tileTexture , TileStatus tileStatus , float xPos , float yPos );

    //Methods
    sf::Sprite getTileSprite();
    void setTileStatus( TileStatus tileStatus );
    TileStatus getTileStatus();
};

#endif

and Tile.cpp

#include "tile.h"

Tile::Tile()
{
    xPos = 0;
    yPos = 0;

    height  = 0;
    width   = 0;

    tileStatus = FREE;

}

Tile::Tile( sf::Texture tileTexture , TileStatus tileStatus , float xPos , float yPos )
{
    this->tileStatus = tileStatus;

    height  = 126;
    width   = 126;

    this->xPos = xPos;
    this->yPos = yPos;

    this->tileSprite.setTexture( tileTexture );
    this->tileSprite.setTextureRect( sf::IntRect( 0,0,126,126 ) );
    this->tileSprite.setPosition( this->xPos , this->yPos );

    if( this->tileStatus == FREE)
    {
        tileSprite.setTextureRect( sf::IntRect( 0,0,126,126 ) );
    }
    else if( this->tileStatus == CROSS )
    {
        tileSprite.setTextureRect( sf::IntRect( 126,0,126,126 ) );
    }
    else if( this->tileStatus == CIRCLE )
    {
        tileSprite.setTextureRect( sf::IntRect( 252,0,126,126 ) );
    }
}

void Tile::setTileStatus( TileStatus tileStatus )
{
    this->tileStatus = tileStatus;

    if( this->tileStatus == FREE)
    {
        tileSprite.setTextureRect( sf::IntRect( 0,0,width,height ) );
    }
    else if( this->tileStatus == CROSS )
    {
        tileSprite.setTextureRect( sf::IntRect( 126,0,width,height ) );
    }
    else if( this->tileStatus == CIRCLE )
    {
        tileSprite.setTextureRect( sf::IntRect( 252,0,width,height ) );
    }
}

sf::Sprite Tile::getTileSprite()
{
    return tileSprite;
}

TileStatus Tile::getTileStatus()
{
    return this->tileStatus;
}

Basically i loaded this spritesheet as a texture in my main function and then i pass it to the Tile functions constructor and then i can set and change it’s sub-rectangles and do other stuff.

Now the issue here is that even though it compiles without any errors the sprite is not showing. However when i write the code ,that is in the Tile class, into the main class instead the image shows. Can anyone tell me what i’m doing wrong?

Here’s the main.cpp

#include <SFML\System.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>

#include "tile.h"

int main()
{
    sf::RenderWindow window( sf::VideoMode( 800,600,32 ) , "Tic - Tac - Toe" );

    // Load background image and display it
    sf::Texture BackgroundTexture;
    if(!BackgroundTexture.loadFromFile( "background.jpeg" ))
    {
        return EXIT_FAILURE;
    }
    sf::Sprite Background(BackgroundTexture);
    Background.setTextureRect( sf::IntRect( 0,0,800,600 ) );
    Background.setPosition( 0,0 );

    // Load the spritesheet
    sf::Texture Spritesheet;
    if(!Spritesheet.loadFromFile( "sprites.png" ))
    {
        return EXIT_FAILURE;
    }


    // Load font and create graphical text
    sf::Font font;
    if (!font.loadFromFile( "arial.ttf" ))
    {
        return EXIT_FAILURE;
    }
    sf::Text TurnNotification;

    //limit the framerate to 30FPS
    window.setFramerateLimit(30);

    /*********************************************/
    //THIS WORKS BUT IT'S NOT WHAT I NEED
    //sf::Sprite tileSprite;
    //tileSprite.setTexture( Spritesheet );
    //tileSprite.setTextureRect( sf::IntRect(126,0,126,126) );
    //tileSprite.setPosition(126,126);


    //This doesn't work
    Tile cTile(Spritesheet, CROSS, 0 , 0 );

    while(window.isOpen())
    {

        window.draw(Background);
        window.draw(cTile.getTileSprite());
        //window.draw(tileSprite);

        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }
            if((event.type == sf::Event::KeyPressed) && (sf::Keyboard::Escape))
            {
                window.close();
            }
        }
        window.display();
    }

    return EXIT_SUCCESS;
}

I hope my code is not too much of a mess. I’m still a newbie

  • 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-10T00:22:58+00:00Added an answer on June 10, 2026 at 12:22 am

    It seems that if you change your Tile constructor to take a sf::Texture& or a const sf::Texture& it works, maybe sf::Texture has special copy semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I'm trying to do is make a very simple vertical bullet projectile
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making.
Basically I have a main screen that comes up with a background image and
Basically, what I'm trying to create is a page of div tags, each has
Basically I have an iframe loaded that is accessed from the parent whenever it
Basically, I have a main page (parent page) with a link that opens up
Basically my task is to make the counter on a micro controller board count
Basically i'm trying to build a comment system. The user looks at a photo
Basically, what I am trying to do is to find last section of PE
Basically, I'm using python to print out some information in the terminal and some

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.