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

Author Topic: button error  (Read 2437 times)

0 Members and 1 Guest are viewing this topic.

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
button error
« on: February 10, 2018, 08:26:50 pm »
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=sharing


For 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);//&#1082;&#1086;&#1083;&#1083;&#1080;&#1095;&#1077;&#1089;&#1090;&#1074;&#1086; &#1082;&#1072;&#1076;&#1088;&#1086;&#1074; &#1074; &#1089;&#1077;&#1082;&#1091;&#1085;&#1076;&#1091;

        //Button button(L"&#1050;&#1085;&#1086;&#1087;&#1082;&#1072;", font, 50, 50, 11);
        Button *button = new Button(L"&#1050;&#1085;&#1086;&#1087;&#1082;&#1072;", font, 5, 5, 11);
        Button *button2 = new Button(L"LOL", font, 60, 5, 20);

        while(MainWindow.isOpen())
        {
                Vector2i pixelPos = Mouse::getPosition(MainWindow);//&#1079;&#1072;&#1073;&#1080;&#1088;&#1072;&#1077;&#1084; &#1082;&#1086;&#1086;&#1088;&#1076; &#1082;&#1091;&#1088;&#1089;&#1086;&#1088;&#1072;
                Vector2f pos = MainWindow.mapPixelToCoords(pixelPos);//&#1087;&#1077;&#1088;&#1077;&#1074;&#1086;&#1076;&#1080;&#1084; &#1080;&#1093; &#1074; &#1080;&#1075;&#1088;&#1086;&#1074;&#1099;&#1077; (&#1091;&#1093;&#1086;&#1076;&#1080;&#1084; &#1086;&#1090; &#1082;&#1086;&#1086;&#1088;&#1076; &#1086;&#1082;&#1085;&#1072;)
               
                Event event;
                while(MainWindow.pollEvent(event))
                {
                        //&#1077;&#1089;&#1083;&#1080; &#1084;&#1099; &#1085;&#1072;&#1078;&#1084;&#1077;&#1084; &#1085;&#1072; &#1082;&#1085;&#1086;&#1087;&#1082;&#1091; &#1079;&#1072;&#1082;&#1088;&#1099;&#1090;&#1100;
                        if(event.type == Event::Closed)
                                MainWindow.close();

                        //&#1053;&#1072;&#1078;&#1072;&#1090;&#1080;&#1077;
                        if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)//&#1077;&#1089;&#1083;&#1080; &#1085;&#1072;&#1078;&#1072;&#1090;&#1072; &#1082;&#1083;&#1072;&#1074;&#1080;&#1096;&#1072; &#1084;&#1099;&#1096;&#1080;
                        while(true)
                        {
                                if(button->pressed(event.mouseButton.x, event.mouseButton.y))
                                {break;}
                                if(button2->pressed(event.mouseButton.x, event.mouseButton.y))
                                {break;}
                                break;
                        }

                        //&#1054;&#1090;&#1078;&#1072;&#1090;&#1080;&#1077;
                        if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)//&#1077;&#1089;&#1083;&#1080; &#1085;&#1072;&#1078;&#1072;&#1090;&#1072; &#1082;&#1083;&#1072;&#1074;&#1080;&#1096;&#1072; &#1084;&#1099;&#1096;&#1080;
                        {
                                button->released();
                                button2->released();
                        }

                        //&#1050;&#1083;&#1080;&#1082; &#1084;&#1099;&#1096;&#1080;
                        if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Left)
                        do{
                                if(button->click(event.mouseButton.x, event.mouseButton.y))
                                {cout << "&#1050;&#1085;&#1086;&#1087;&#1082;&#1072; 1" << endl; button2->setText(L"EEE!!");break;}
                                if(button2->click(event.mouseButton.x, event.mouseButton.y))
                                {cout << "&#1050;&#1085;&#1086;&#1087;&#1082;&#1072; 2" << endl; button2->setText(L"Long long text on button2");break;}
                        }while(0);

                        //&#1055;&#1088;&#1072;&#1074;&#1099;&#1081; &#1082;&#1083;&#1080;&#1082;
                        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);// &#1091;&#1089;&#1090;&#1072;&#1085;&#1086;&#1074;&#1082;&#1072; &#1082;&#1086;&#1076;&#1086;&#1074;&#1086;&#1081; &#1089;&#1090;&#1088;&#1072;&#1085;&#1080;&#1094;&#1099; win-cp 1251 &#1074; &#1087;&#1086;&#1090;&#1086;&#1082; &#1074;&#1074;&#1086;&#1076;&#1072;
    SetConsoleOutputCP(1251); // &#1091;&#1089;&#1090;&#1072;&#1085;&#1086;&#1074;&#1082;&#1072; &#1082;&#1086;&#1076;&#1086;&#1074;&#1086;&#1081; &#1089;&#1090;&#1088;&#1072;&#1085;&#1080;&#1094;&#1099; win-cp 1251 &#1074; &#1087;&#1086;&#1090;&#1086;&#1082; &#1074;&#1099;&#1074;&#1086;&#1076;&#1072;

        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);//&#1082;&#1086;&#1083;&#1083;&#1080;&#1095;&#1077;&#1089;&#1090;&#1074;&#1086; &#1082;&#1072;&#1076;&#1088;&#1086;&#1074; &#1074; &#1089;&#1077;&#1082;&#1091;&#1085;&#1076;&#1091;

        //Button button(L"&#1050;&#1085;&#1086;&#1087;&#1082;&#1072;", font, 50, 50, 11);
        Button *button = new Button(L"&#1050;&#1085;&#1086;&#1087;&#1082;&#1072;", font, 5, 5, 11);
        Button *button2 = new Button(L"EEE!!", font, 60, 5, 20);

        while(MainWindow.isOpen())
        {
                Vector2i pixelPos = Mouse::getPosition(MainWindow);//&#1079;&#1072;&#1073;&#1080;&#1088;&#1072;&#1077;&#1084; &#1082;&#1086;&#1086;&#1088;&#1076; &#1082;&#1091;&#1088;&#1089;&#1086;&#1088;&#1072;
                Vector2f pos = MainWindow.mapPixelToCoords(pixelPos);//&#1087;&#1077;&#1088;&#1077;&#1074;&#1086;&#1076;&#1080;&#1084; &#1080;&#1093; &#1074; &#1080;&#1075;&#1088;&#1086;&#1074;&#1099;&#1077; (&#1091;&#1093;&#1086;&#1076;&#1080;&#1084; &#1086;&#1090; &#1082;&#1086;&#1086;&#1088;&#1076; &#1086;&#1082;&#1085;&#1072;)
               
                Event event;
                while(MainWindow.pollEvent(event))
                {
                        //&#1077;&#1089;&#1083;&#1080; &#1084;&#1099; &#1085;&#1072;&#1078;&#1084;&#1077;&#1084; &#1085;&#1072; &#1082;&#1085;&#1086;&#1087;&#1082;&#1091; &#1079;&#1072;&#1082;&#1088;&#1099;&#1090;&#1100;
                        if(event.type == Event::Closed)
                                MainWindow.close();

                        //&#1053;&#1072;&#1078;&#1072;&#1090;&#1080;&#1077;
                        if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)//&#1077;&#1089;&#1083;&#1080; &#1085;&#1072;&#1078;&#1072;&#1090;&#1072; &#1082;&#1083;&#1072;&#1074;&#1080;&#1096;&#1072; &#1084;&#1099;&#1096;&#1080;
                        while(true)
                        {
                                if(button->pressed(event.mouseButton.x, event.mouseButton.y))
                                {break;}
                                if(button2->pressed(event.mouseButton.x, event.mouseButton.y))
                                {break;}
                                break;
                        }

                        //&#1054;&#1090;&#1078;&#1072;&#1090;&#1080;&#1077;
                        if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)//&#1077;&#1089;&#1083;&#1080; &#1085;&#1072;&#1078;&#1072;&#1090;&#1072; &#1082;&#1083;&#1072;&#1074;&#1080;&#1096;&#1072; &#1084;&#1099;&#1096;&#1080;
                        {
                                button->released();
                                button2->released();
                        }

                        //&#1050;&#1083;&#1080;&#1082; &#1084;&#1099;&#1096;&#1080;
                        if (event.type == event.MouseButtonReleased && event.mouseButton.button == Mouse::Left)
                        do{
                                if(button->click(event.mouseButton.x, event.mouseButton.y))
                                {cout << "&#1050;&#1085;&#1086;&#1087;&#1082;&#1072; 1" << endl; button2->setText(L"EEE!!");break;}
                                if(button2->click(event.mouseButton.x, event.mouseButton.y))
                                {cout << "&#1050;&#1085;&#1086;&#1087;&#1082;&#1072; 2" << endl; button2->setText(L"Long long text on button2");break;}
                        }while(0);

                        //&#1055;&#1088;&#1072;&#1074;&#1099;&#1081; &#1082;&#1083;&#1080;&#1082;
                        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;
}

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: button error
« Reply #1 on: February 10, 2018, 09:34:21 pm »
when reading at 0x0000000000000000.

As a developer this should already give you an explanation of your crash. Spoiler: you are accessing a null pointer (it points to 0 in memory). So start your application with a debugger and you will find the cause of the crash easily.

Also from just flying over your code, you are using the heap when there is no need for it (new/delete). You should really train your basic C++ before you write multimedia applications or games.


AlexAUT

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: button error
« Reply #2 on: February 10, 2018, 10:02:25 pm »
You should not use like in Java: http://www.stroustrup.com/bs_faq2.html#new-java
Using namespace is also usually discouraged.
Back to C++ gamedev with SFML in May 2023

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: button error
« Reply #3 on: February 11, 2018, 02:25:39 pm »
Removed (new \ delete). Looked in the debugger, but did not understand why the error occurs.

It is only known that an error occurs only when a class is used in multiple threads.

Tima32

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: button error
« Reply #4 on: February 11, 2018, 06:18:57 pm »
Solved the problem with the help of a std::mutex.
Why does FloatRect lack thread safety? :-\

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: button error
« Reply #5 on: February 11, 2018, 06:29:27 pm »
SFML isn't thread-safe overall, the responsibility is laid on the user. Plus in most cases multi-threading is a bad idea anyways, you really need to know what you're doing.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/