I can not understand the reason for the error.
Unhandled exception at 0x000007FED080264D (sfml-graphics-2.dll) in Test.exe: 0xC0000005: violation of access rights when reading at 0x0000000000000000.
Video:
Project:
https://drive.google.com/file/d/15NDtyPFyfFMrHVf64q3HNxGbd4psblGR/view?usp=sharingFor those who do not want to download the project.
main.cpp:
#include "SFML/Graphics.hpp"
#include <iostream>
#include <windows.h>
#include <list>
#include <thread>
#include "Button.h"
using namespace std;
using namespace sf;
Font font;
void m2()
{
RenderWindow MainWindow;
MainWindow.create(VideoMode(640,480), L"Test");
//window.setVerticalSyncEnabled(true);
MainWindow.setFramerateLimit(60);//колличество кадров в секунду
//Button button(L"Кнопка", font, 50, 50, 11);
Button *button = new Button(L"Кнопка", font, 5, 5, 11);
Button *button2 = new Button(L"LOL", font, 60, 5, 20);
while(MainWindow.isOpen())
{
Vector2i pixelPos = Mouse::getPosition(MainWindow);//забираем коорд курсора
Vector2f pos = MainWindow.mapPixelToCoords(pixelPos);//переводим их в игровые (уходим от коорд окна)
Event event;
while(MainWindow.pollEvent(event))
{
//если мы нажмем на кнопку закрыть
if(event.type == Event::Closed)
MainWindow.close();
//Нажатие
if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)//если нажата клавиша мыши
while(true)
{
if(button->pressed(event.mouseButton.x, event.mouseButton.y))
{break;}
if(button2->pressed(event.mouseButton.x, event.mouseButton.y))
{break;}
break;
}
//Отжатие
if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)//если нажата клавиша мыши
{
button->released();
button2->released();
}
//Клик мыши
if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Left)
do{
if(button->click(event.mouseButton.x, event.mouseButton.y))
{cout << "Кнопка 1" << endl; button2->setText(L"EEE!!");break;}
if(button2->click(event.mouseButton.x, event.mouseButton.y))
{cout << "Кнопка 2" << endl; button2->setText(L"Long long text on button2");break;}
}while(0);
//Правый клик
if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Right)
{
}
}
MainWindow.clear(Color(45, 45, 48));
bool tr = 1;
button->draw(MainWindow, pos.x, pos.y, &tr);
button2->draw(MainWindow, pos.x, pos.y, &tr);
MainWindow.display();
}
delete button;
delete button2;
return;
}
int main()
{
SetConsoleCP(1251);// установка кодовой страницы win-cp 1251 в поток ввода
SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода
font.loadFromFile("tahoma.ttf");
thread func_thread(m2);
//Sleep(5000);
RenderWindow MainWindow;
MainWindow.create(VideoMode(640,480), L"Test");
//window.setVerticalSyncEnabled(true);
MainWindow.setFramerateLimit(60);//колличество кадров в секунду
//Button button(L"Кнопка", font, 50, 50, 11);
Button *button = new Button(L"Кнопка", font, 5, 5, 11);
Button *button2 = new Button(L"EEE!!", font, 60, 5, 20);
while(MainWindow.isOpen())
{
Vector2i pixelPos = Mouse::getPosition(MainWindow);//забираем коорд курсора
Vector2f pos = MainWindow.mapPixelToCoords(pixelPos);//переводим их в игровые (уходим от коорд окна)
Event event;
while(MainWindow.pollEvent(event))
{
//если мы нажмем на кнопку закрыть
if(event.type == Event::Closed)
MainWindow.close();
//Нажатие
if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)//если нажата клавиша мыши
while(true)
{
if(button->pressed(event.mouseButton.x, event.mouseButton.y))
{break;}
if(button2->pressed(event.mouseButton.x, event.mouseButton.y))
{break;}
break;
}
//Отжатие
if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)//если нажата клавиша мыши
{
button->released();
button2->released();
}
//Клик мыши
if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Left)
do{
if(button->click(event.mouseButton.x, event.mouseButton.y))
{cout << "Кнопка 1" << endl; button2->setText(L"EEE!!");break;}
if(button2->click(event.mouseButton.x, event.mouseButton.y))
{cout << "Кнопка 2" << endl; button2->setText(L"Long long text on button2");break;}
}while(0);
//Правый клик
if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Right)
{
}
}
MainWindow.clear(Color(45, 45, 48));
bool tr = 1;
button->draw(MainWindow, pos.x, pos.y, &tr);
button2->draw(MainWindow, pos.x, pos.y, &tr);
MainWindow.display();
}
delete button;
delete button2;
return 0;
}
Button.h:
#pragma once
#include "SFML/Graphics.hpp"
#include <iostream>
using namespace sf;
using namespace std;
class Button
{
public:
Button(wchar_t *PText, Font &font, float x, float y, int CharacterSize);
~Button();
void draw(RenderWindow &window, int x, int y, bool *tr);
void setPosition(float x, float y);
void setText(wchar_t *PText);
void setTextColor(Color &color);
void setColor(Color &color);
void setGuidanceColor(Color &color);
void setPressColor(Color &color);
void setCharacterSize(int CharacterSize);
bool pressed(int x, int y);
void released();
bool released(int x, int y);
bool click(int x, int y);
private:
Text *text;
RectangleShape *Rectangle;
Color *CStandart;
Color *GuidanceColor;
Color *PressedColor;
int *positionX;
int *positionY;
float *width;
float *height;
bool *Pressed;
bool activityZoneCheck(int x, int y);
};
Button.cpp:
#include "Button.h"
Button::Button(wchar_t *PText, Font &font, float x = 0, float y = 0, int CharacterSize = 11)
{
Rectangle = new RectangleShape;
text = new Text;
CStandart = new Color(Color(45, 45, 48));
GuidanceColor = new Color(Color(62, 62, 64));
PressedColor = new Color(27, 27, 28);
positionX = new int(x);
positionY = new int(y);
width = new float(0);
height = new float(16);
Pressed = new bool(0);
//Rectangle->setPosition(x,y);
Rectangle->setFillColor(*CStandart);
text->setString(PText);
text->setFont(font);
text->setCharacterSize(CharacterSize);
text->setColor(Color(255, 255, 255));
setPosition(x, y);
}
Button::~Button()
{
delete text;
delete Rectangle;
delete CStandart;
delete GuidanceColor;
delete PressedColor;
delete positionX;
delete positionY;
delete width;
delete height;
delete Pressed;
}
void Button::draw(RenderWindow &window, int x, int y, bool *tr)
{
if(*tr && activityZoneCheck(x, y) && !*Pressed)
{
Rectangle->setFillColor(*GuidanceColor);
*tr = 0;
}
else if(!*Pressed)
Rectangle->setFillColor(*CStandart);
window.draw(*Rectangle);
window.draw(*text);
}
void Button::setPosition(float x, float y)
{
*positionX = x;
*positionY = y;
Rectangle->setPosition(x,y);
FloatRect Bounding_Rectangle(text->getLocalBounds());
text->setPosition(x+4,y);
*width = (Bounding_Rectangle.width + 8) * 1.02;
*height = (Bounding_Rectangle.height + 4)*1.35;
Rectangle->setSize(Vector2f(*width, *height));
}
void Button::setText(wchar_t *PText)
{
text->setString(PText);
setPosition(*positionX, *positionY);
}
void Button::setTextColor(Color &color)
{
text->setColor(color);
}
void Button::setColor(Color &color)
{
*CStandart = color;
}
void Button::setGuidanceColor(Color &color)
{
*GuidanceColor = color;
}
void Button::setPressColor(Color &color)
{
*PressedColor = color;
}
void Button::setCharacterSize(int CharacterSize)
{
text->setCharacterSize(CharacterSize);
setPosition(*positionX, *positionY);
}
bool Button::pressed(int x, int y)
{
if(activityZoneCheck(x, y))
{
Rectangle->setFillColor(*PressedColor);
*Pressed = 1;
return 1;
}
return 0;
}
void Button::released()
{
Rectangle->setFillColor(*CStandart);
*Pressed = 0;
}
bool Button::released(int x, int y)
{
if(activityZoneCheck(x, y))
{
Rectangle->setFillColor(*CStandart);
*Pressed = 0;
return 1;
}
return 0;
}
bool Button::click(int x, int y)
{
return activityZoneCheck(x, y);
}
//pivate:
bool Button::activityZoneCheck(int x, int y)
{
if(x >= *positionX && x <= *positionX+*width && y >= *positionY && y <= *positionY+*height)
return true;
else
return false;
}