Dear users,
It,s nice to meet all here. Sometimes I meet this forum to found some asnwers to my questions. But after this trouble I have no way, please help me!
Some words about code: I studing in the University and now I need to complete my project. I'm doing game on C++ with SFML. I done my class for buttons. All ok, besides one. In my game, I need to use array of buttons. Just in my game are many buttons. There is beginning my trouble. I use dynamic memory. In start I allocale memory (dynamic array of buttons), after I sey my buttons, and after I need to draw it. But when I try to draw button I get warning about access to memory. I can't resolve this bug.
When I try to drawButton I get warning:
— Exception thrown at 0x00007FF7FFD7B766 in dab_project.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
ribs_button[0][0] = ButtonRectangle(Vector2f(171+25,21), Vector2f(100, 25), window);
ribs_button[0][0].setButtonTexture("recourses/rib.png", Vector2i(100, 75));
ribs_button[0][0].drawButton();
BUT, when I get button from array and write it to separated variable all are going without expections! See example, this code done successfully.
ribs_button[0][0] = ButtonRectangle(Vector2f(171+25,21), Vector2f(100, 25), window);
ribs_button[0][0].setButtonTexture("recourses/rib.png", Vector2i(100, 75));
ButtonRectangle button1 = ribs_button[0][0];
button1.drawButton();
I already watched my memory via watch in Visual Studio, all variables is right in array and ribs_button[0][0] coincide with button1. Warning caused by (*window).draw() from my button class function drawButton. I find out it after debug. I was afraid what this expection cause by error while memory allocating. But all vars coincide and writes in ribs_button correctly. Now I afraid, what my expection cause by peculiarity of SFML.
I can solve it how I do it via button1, but it does not suit me!
Code from my class:
void ButtonRectangle::drawButton() {
MousePos = Mouse::getPosition(*window);
//входит ли точка в прямоугольник.
contain = ((MousePos.x >= position.x) && (MousePos.x <= position.x + size.x) && (MousePos.y >= position.y) && (MousePos.y <= position.y + size.y));
if (contain) {
if (Mouse::isButtonPressed(MouseClickButton)) {
if (active) {
(*window).draw(ButtonSpritePressed); //рисуем нажатую кнопку
}
else if (stopTimer() > 10) {
active = 1;
(*window).draw(ButtonSpritePressed);
}
else {
(*window).draw(ButtonSpriteSelected);
}
}
else {
(*window).draw(ButtonSpriteSelected); //рисуем активную кнопка
if (!Mouse::isButtonPressed(MouseClickButton))
setTimer();
std::cout << "active" << std::endl;
}
}
else {
(*window).draw(ButtonSprite); //рисуем обычную кнопку
/*active = 0;
stopTimer();*/
}
}
void Buttons::setButtonTexture(String name, Vector2i sizet) {
TextureSize = sizet;
if (!ButtonTexture.loadFromFile(name)) std::cout << "Текстура не найдена!" << std::endl;
ButtonSprite.setTexture(ButtonTexture);
ButtonSprite.setTextureRect(IntRect(0, 0, TextureSize.x, TextureSize.y));
ButtonSprite.setPosition(position);
ButtonSpriteSelected.setTexture(ButtonTexture);
ButtonSpriteSelected.setTextureRect(IntRect(0, TextureSize.y, TextureSize.x, TextureSize.y));
ButtonSpriteSelected.setPosition(position);
ButtonSpritePressed.setTexture(ButtonTexture);
ButtonSpritePressed.setTextureRect(IntRect(0, TextureSize.y * 2, TextureSize.x, TextureSize.y));
ButtonSpritePressed.setPosition(position);
}
Constructor:
ButtonRectangle::ButtonRectangle(Vector2f pos, Vector2f sizes, RenderWindow * wind) {
position = pos;
size = sizes;
window = wind;
MouseClickButton = Mouse::Button::Left;
}
Memory allocale:
ButtonRectangle **ribs_button = (ButtonRectangle**)calloc(2 * newGame.m + 1, sizeof(ButtonRectangle*));
for (size_t i = 0; i < m + 1; i += 2) {
ribs_button[i] = (ButtonRectangle*)calloc(newGame.n, sizeof(ButtonRectangle));
}
for (size_t i = 1; i < 2 * m + 1; i += 2) {
ribs_button[i] = (ButtonRectangle*)calloc(newGame.n + 1, sizeof(ButtonRectangle));
}
I have matrix with n elements in even rows and n + 1 elements in odd rows.
Sorry for my english, I am russian boy.
— Victor