The picture not showing and when I close, error appear. I can't figure it out, maybe a problem with pointers.
stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry. stackoverflow is asking for details, but I have no more details - sorry.
Menu.h
#include "Picture.h"
class menu
{
const int menuPictCount;
picture* menuPicture[];
public:
menu(sf::RenderWindow& usableArea);
~menu();
void draw();
void posСorrection();
void shineButton(sf::Color color);
};
Menu.cpp <--------- the problem is here
menu::menu(sf::RenderWindow& usableArea) : menuPictCount(4)
{
this->menuPicture[this->menuPictCount];
//background
this->menuPicture[0] = new picture(usableArea, "image/background/0.png");
//buttons
this->menuPicture[1] = new picture(usableArea, "image/menu/newgame.png");
this->menuPicture[1] = new picture(usableArea, "image/menu/continue.png");
this->menuPicture[3] = new picture(usableArea, "image/menu/exit.png");
}
menu::~menu()
{
for (size_t i = 0; i < this->menuPictCount; i++) delete this->menuPicture[i];
delete[] menuPicture;
}
void menu::draw()
{
for (size_t i = 0; i < this->menuPictCount; i++) this->menuPicture[i]->draw();
}
void menu::posСorrection()
{
const int buttonsCount = this->menuPictCount - 1;
if (buttonsCount % 2 == 0)
{
int indexCenterOver = buttonsCount / 2;
int indexCenterUnder = indexCenterOver + 1;
this->menuPicture[indexCenterOver]->putCenterOver();
this->menuPicture[indexCenterUnder]->putCenterUnder();
for (size_t i = indexCenterOver - 1; i > 0; i--)
{
this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
}
for (size_t i = indexCenterUnder + 1; i <= buttonsCount; i++)
{
this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
}
}
else
{
const int indexCenter = buttonsCount / 2 + 1;
this->menuPicture[indexCenter]->putCenter();
for (size_t i = indexCenter - 1; i > 0; i--)
{
this->menuPicture[i]->putOverObj(*this->menuPicture[i + 1]);
}
for (size_t i = indexCenter + 1; i <= buttonsCount; i++)
{
this->menuPicture[i]->putUnderObj(*this->menuPicture[i - 1]);
}
}
}
void menu::shineButton(sf::Color color)
{
/*for (size_t i = 0; i <= this->buttons.GetCount(); i++)
{
if (this->buttons.GetElement(i)->data.trackContainsCursor())
if (this->buttons.GetElement(i)->data.getColor() != color)
{
this->buttons.GetElement(i)->data.setColor(color);
}
if (!this->buttons.GetElement(i)->data.trackContainsCursor())
if (this->buttons.GetElement(i)->data.getColor() == color)
{
this->buttons.GetElement(i)->data.setColor(color);
}
}*/
} ```
[1]: https://i.stack.imgur.com/uNY8B.png
Instead of
picture* menuPicture[];
use
std::vector<std::unique_ptr<picture>> menuPicture;
And in menu
constructor instead of doing this:
this->menuPicture[this->menuPictCount];
do this:
this->menuPicture.resize(this->menuPictCount);
Edit: Of course remove also delete
s from destructor. std::vector
and std::unique_ptr
will clean up after themselves.
If number of pictures in menu is constant (like in your constructor) you can change menuPicture
declaration to:
std::array<std::unique_ptr<picture>, 4> menuPicture;
but then you can't call resize on it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.