Hello. I was working on my project and when i made a thread that would clean the window from all the objects (buttons and background) the c++ part was working, but sfml part wasn't. After i pressed "Chess" button it gave me this text:
Chess button pressed!
smth
Failed to activate OpenGL context: .
Failed to activate the window's context
Here's my code:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
RenderWindow window(VideoMode::getFullscreenModes()[0], "Chess Fantasy", Style::Fullscreen);
void thread1()
{
cout << "smth" << endl;
window.clear();
}
Thread chessThread(&thread1);
int main()
{
Texture texture1, texturebg;
if (!texture1.loadFromFile("texture1.png") ||
!texturebg.loadFromFile("texturebg.png"))
{
cerr << "Failed to load textures." << endl;
return 1;
}
RectangleShape chessButton(Vector2f(800, 200)); // Прямоугольник для кнопки Chess
chessButton.setTexture(&texture1);
chessButton.setOutlineThickness(5);
chessButton.setOutlineColor(Color(255, 255, 255));
RectangleShape mazeButton(Vector2f(800, 200)); // Прямоугольник для кнопки Maze
mazeButton.setTexture(&texture1);
mazeButton.setOutlineThickness(5);
mazeButton.setOutlineColor(Color(255, 255, 255));
RectangleShape quitButton(Vector2f(800, 200)); // Прямоугольник для кнопки Quit
quitButton.setTexture(&texture1);
quitButton.setOutlineThickness(5);
quitButton.setOutlineColor(Color(255, 255, 255));
RectangleShape background(Vector2f(window.getSize().x, window.getSize().y)); // Прямоугольник для фона
background.setTexture(&texturebg);
Font font;
if (!font.loadFromFile("arial.ttf"))
{
cerr << "Failed to load font." << endl;
return 1;
}
Text chessText("Chess", font, 72);
Text mazeText("Maze", font, 72);
// Уточненные позиции текстов и кнопок для точного выравнивания
float centerX = window.getSize().x / 2.0f;
float centerY = window.getSize().y / 2.0f;
chessText.setPosition(centerX - chessText.getLocalBounds().width / 2, centerY - 300);
chessButton.setPosition(centerX - chessButton.getLocalBounds().width / 2, centerY - 350);
mazeText.setPosition(centerX - mazeText.getLocalBounds().width / 2, centerY - 50);
mazeButton.setPosition(centerX - mazeButton.getLocalBounds().width / 2, centerY - 100);
Text quitText("Quit", font, 72);
quitText.setPosition(centerX - quitText.getLocalBounds().width / 2, centerY + 200);
quitButton.setPosition(centerX - quitButton.getLocalBounds().width / 2, centerY + 150);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
if (event.type == Event::MouseButtonPressed)
{
if (event.mouseButton.button == Mouse::Left)
{
if (chessButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
{
cout << "Chess button pressed!" << endl;
chessThread.launch();
}
else if (mazeButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
{
cout << "Maze button pressed!" << endl;
}
else if (quitButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
{
window.close();
}
}
}
}
window.draw(background);
window.draw(chessButton);
window.draw(chessText);
window.draw(mazeButton);
window.draw(mazeText);
window.draw(quitButton);
window.draw(quitText);
window.display();
}
return 0;
}