Hi, I started my first year of C++ at university and I have been messing around with the SFML library. I am trying to create a Tic Tac Toe game but my text isn't being drawn to screen but the board is. The text has been set and the position is correct but no text appears. Well the text appears for a fraction of a second then disappears
Please bare in mind I only started to learn C++ in October so forgive me if I am not doing things the best way.
Here's my code:
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
RectangleShape GameTile[3][3];
Text GameText[12];
void createTextProperties(string sPlayer1, string sPlayer2)
{
int X = 300; // Start X,Y of the Text
int Y = 10;
// Loops through the Text array setting properties
for(int i = 0; i < 12; i++)
{
GameText[i].setCharacterSize(24);
GameText[i].setColor(Color::Red);
}
// Set position of all text except the Tile counter text
for(int i = 0; i < 3; i++)
{
GameText[i].setPosition(Vector2f(X,Y));
// Seperate position of Player's Name and Turn Name
if(i == 1)
{
Y = Y + 100;
}
else
{
Y = Y + 25;
}
}
// Set String values
GameText[0].setString("X Player 1: " + sPlayer1); // Player's name and their counter
GameText[1].setString("O Player 2: " + sPlayer2);
GameText[2].setString(sPlayer1 + "'s turn"); // Always Player 1's turn first
}
void createTileProperties()
{
// Start Tile Position from Window Start
int X = 10;
int Y = 10;
// Loop through each tile and set properties
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
GameTile[i][j].setSize(Vector2f(50,50));
GameTile[i][j].setPosition(Vector2f(X,Y));
GameTile[i][j].setOutlineThickness(1);
GameTile[i][j].setOutlineColor(Color::Black);
X = X + 50;
}
Y = Y + 50;
X = 10;
}
}
void loadFont(Font &font)
{
if (!font.loadFromFile("arial.ttf"))
{
cout << "Error loading the font. Make sure the font files are in the project directory." << endl;
}
}
void drawGameObjects(RenderWindow &GameWindow, string sPlayer1, string sPlayer2)
{
Font font;
createTextProperties(sPlayer1, sPlayer2);
GameWindow.clear(Color::White);
// Draw Game Tiles -- Make sure this code is below the Draw Game Text for the z-order
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
GameWindow.draw(GameTile[i][j]);
}
}
// Draw Game Text
for(int i = 0; i < 12; i++)
{
loadFont(font);
GameText[i].setFont(font);
GameWindow.draw(GameText[i]);
}
}
void createGame(string sPlayer1, string sPlayer2)
{
RenderWindow GameWindow(VideoMode(600,400), "Tic Tac Toe");
bool isGameRunning = true; // The Game Loop condition - Is a game in session?
Font font;
// Functions to happen once
// Set Text & Tile Properties
createTileProperties();
createTextProperties(sPlayer1, sPlayer2);
while(isGameRunning == true)
{
// Loop through events
#pragma region Window Events
Event event;
while (GameWindow.pollEvent(event))
{
if (event.type == Event::Closed)
{
GameWindow.setVisible(false);
}
}
#pragma endregion
drawGameObjects(GameWindow, sPlayer1, sPlayer2);
string s = GameText[0].getString();
cout << s << " " << GameText[0].getPosition().x << " " << GameText[0].getPosition().y << endl;
GameWindow.display(); // Draw everything to screen
}
}
#pragma region Main Function
int main() {
string sPlayer1; // Variable for holding Player 1's name
string sPlayer2; // Variable for holding Player 2's name
// Welcome Message -- Get their names via User input
cout << "Welcome to Tic Tac Toe!" << endl << endl << "Please enter Player 1's (X) name: ";
cin >> sPlayer1;
cout << "Please enter Player 2's (O) name: ";
cin >> sPlayer2;
// Start the Game
createGame(sPlayer1, sPlayer2);
system("pause");
return 0;
}
#pragma endregion
Note: the string s in the game loop was just me troubleshooting the problem.