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

Author Topic: chess piece representation using fonts  (Read 2024 times)

0 Members and 1 Guest are viewing this topic.

nmb

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
chess piece representation using fonts
« on: January 26, 2017, 03:44:37 pm »
Hi,

I want to draw chess pieces using text font. How do that in SFML?

Thanks in advance.

Norm

nmb

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: chess piece representation using fonts
« Reply #1 on: January 26, 2017, 06:53:11 pm »
OK. I got something working.

#include <TGUI/TGUI.hpp>

#include <iostream>

const int board[24] = {
-1,-2,-1,-1,
-1,-1,-1,-1,
 0, 0, 0, 0,
 0, 0, 0, 0,
 1, 1, 1, 1,
 1, 1, 2, 1
};

const int fsize = 80;

int main()
{
        sf::RenderWindow window{ sf::VideoMode{ 800, 600 }, "Test Chess Font" };

        sf::Font font;

        if (!font.loadFromFile("data/Avenfont.ttf"))
        {
                // error...
                std::cout << "error loading font" << std::endl;
        }

        sf::Text text[16];

        int cnt = 0;

        for (int i = 0; i < 24; i++)
        {
                if (board[i] != 0)
                {
                        int r = int(i / 4);
                        int c = int(i % 4);

                        text[cnt].setFont(font);
                        text[cnt].setCharacterSize(80);

                        switch (board[i])
                        {
                        case -1:
                                text[cnt].setString("p");
                                text[cnt].setFillColor(sf::Color::Black);
                                text[cnt].setPosition(sf::Vector2f(c * 80, r * 80));
                                //std::cout << r << " " << c << std::endl;
                                break;
                        case 1:
                                text[cnt].setString("p");
                                text[cnt].setFillColor(sf::Color::Red);
                                text[cnt].setPosition(sf::Vector2f(c * 80, r * 80));
                                //std::cout << r << " " << c << std::endl;
                                break;
                        case -2:
                                text[cnt].setString("q");
                                text[cnt].setFillColor(sf::Color::Black);
                                text[cnt].setPosition(sf::Vector2f(c * 80, r * 80));
                                //std::cout << r << " " << c << std::endl;
                                break;
                        case 2:
                                text[cnt].setString("q");
                                text[cnt].setFillColor(sf::Color::Red);
                                text[cnt].setPosition(sf::Vector2f(c * 80, r * 80));
                                //std::cout << r << " " << c << std::endl;
                                break;
                        }
                        cnt++;
                }
        }

        std::cout << cnt << std::endl;

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

                }

                window.clear(sf::Color::White);

                for (int i = 0; i < 16; i++)
                {
                        window.draw(text[i]);
                }

                window.display();
        }

        return EXIT_SUCCESS;
}
 

« Last Edit: January 26, 2017, 08:18:26 pm by Laurent »