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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - xx_Dan_xx

Pages: [1]
1
General / Re: Storing the array of RectangleShapes in an array
« on: January 20, 2015, 11:30:03 pm »
Hapax, thanks so much for you reply. It helped a great deal and performed exactly for my needs.

In regards to the amount of rectangles to be drawn, they are very basic and small and, as of right now, they are drawn instantly and see no block ahead. But I'll look into those links.

As for not using multi-dimensional arrays or vectors, I believe its necessary as they form a squared tiled board and as far as I am aware, it requires less lines of code to then drawn each board from a multi-d array than working out the single indexes belonging to each board.


2
General / Storing the array of RectangleShapes in an array
« on: January 20, 2015, 05:28:28 pm »
Hi, I started learning C++ in September and I am beginning to get to grips with SFML.

I have created four arrays of RectangleShapes as so:

RectangleShape Player1Board[10][10];
RectangleShape Player2Board[10][10];
RectangleShape Player3Board[10][10];
RectangleShape Player4Board[10][10];

I want to store these arrays in another array but I am not sure how to do that without referring to a particular element in one of the arrays.

Basically here is the non-code version of what I want;

Array Boards[4] = {Player1Board, Player2Board, Player3Board, Player4Board}

I want to do this so in another function I can manipulate one of the boards by passing one integer rather than all four boards and then determining which one I want to mess with.




3
Graphics / Re: Text not being drawn to screen
« on: December 03, 2014, 07:51:09 pm »
This
 loadFont(font);
 GameText[i].setFont(font);
must be out of any loop, in "createTextProperties". But GameText must be drawed in every render loop: GameWindow.draw(GameText[ i ]);

Hm ok but dont I need the setFont inside a loop so every text element has its font set to Arial?

Edit: Ok I seem have to fixed the problem by merging the loadFont function into the drawGameObjects function? Which I don't understand as the code only throws an error if it doesnt load it right?

4
Graphics / Text not being drawn to screen
« on: December 03, 2014, 07:38:46 pm »
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.

Pages: [1]
anything