1
SFML projects / Re: chess piece representation using fonts
« 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;
}
#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;
}