1
General / checking collisions with array of objects takes too much
« Last post by Ogn1k on Today at 09:28:52 am »Hi everyone. I have this button script:
Button.cpp
in short it just creates rectangleshape and text, chekcs its collision with mouse and returns it in "isPressed". My problem here is that when i try to create a vector with buttons it starts to freeze in some moments also taking up a lot of memory at these, i do it like this:
in game start function:
in game update function:
help me please to fix these freezes
Button.cpp
(click to show/hide)
#include "Button.h"
Button::Button(float x, float y, float width, float height,
String text, Texture* image,
Color idleColor, Color hoverColor, Color activeColor)
{
ButtonS.setTexture(*image);
ButtonS.setPosition(x, y);
texture = *image;
//ButtonT.setString(text);
ButtonSetup(x, y, width, height, text, idleColor, hoverColor, activeColor);
}
Button::~Button()
{
}
void Button::setFont(Font* b_font)
{
ButtonT.setFont(*b_font);
}
void Button::ButtonSetup(float x, float y, float width, float height, String text, Color idleColor, Color hoverColor, Color activeColor)
{
ButtonState = BTN_IDLE;
ButtonS.setPosition(x, y);
ButtonS.setScale(width / ButtonS.getGlobalBounds().width, height / ButtonS.getGlobalBounds().height);
font->loadFromFile("Resources/Edwardian.ttf");
ButtonT.setFont(*font);
ButtonT.setString(text);
ButtonT.setFillColor(Color::Black);
ButtonT.setPosition(x, y - ;
ButtonT.setCharacterSize(28);
ButtonBox = RectangleShape(Vector2f(width, height));
ButtonBox.setPosition(x, y);
this->activeColor = activeColor;
this->hoverColor = hoverColor;
this->idleColor = idleColor;
}
void Button::unPress()
{
ButtonState = BTN_IDLE;
}
bool Button::isHover()
{
if (ButtonState == BTN_HOVER)
return true;
return false;
}
const bool Button::isPressed() const
{
if (ButtonState == BTN_ACTIVE)
return true;
return false;
}
void Button::ButtonBoxfunc(bool act, Vector2f mousePos, int thick, Color color)
{
ButtonBoxAct = act;
if (ButtonBoxAct)
{
//std::cout << "asd" << std::endl;
ButtonBox.setOutlineColor(color);
ButtonBox.setOutlineThickness(thick);
ButtonState = BTN_IDLE;
if (ButtonBox.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (Mouse::isButtonPressed(Mouse::Left))
{
ButtonState = BTN_ACTIVE;
}
}
if (ButtonState == BTN_IDLE)
ButtonBox.setFillColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonBox.setFillColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonBox.setFillColor(activeColor);
}
}
void Button::updateEvented(Vector2f mousePos, Event event, bool fullColor)
{
ButtonState = BTN_IDLE;
if (ButtonS.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (event.mouseButton.button == sf::Mouse::Left)
{
ButtonState = BTN_ACTIVE;
}
}
if (fullColor)
{
if (ButtonState == BTN_IDLE)
setColorB(idleColor);
else if (ButtonState == BTN_HOVER)
setColorB(hoverColor);
else if (ButtonState == BTN_ACTIVE)
setColorB(activeColor);
}
else
{
if (ButtonState == BTN_IDLE)
ButtonS.setColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonS.setColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonS.setColor(activeColor);
}
}
void Button::render(RenderTarget* target)
{
if (ButtonBoxAct)
target->draw(ButtonBox);
//target->draw(ButtonS);
target->draw(ButtonT);
}
Button::Button(float x, float y, float width, float height,
String text, Texture* image,
Color idleColor, Color hoverColor, Color activeColor)
{
ButtonS.setTexture(*image);
ButtonS.setPosition(x, y);
texture = *image;
//ButtonT.setString(text);
ButtonSetup(x, y, width, height, text, idleColor, hoverColor, activeColor);
}
Button::~Button()
{
}
void Button::setFont(Font* b_font)
{
ButtonT.setFont(*b_font);
}
void Button::ButtonSetup(float x, float y, float width, float height, String text, Color idleColor, Color hoverColor, Color activeColor)
{
ButtonState = BTN_IDLE;
ButtonS.setPosition(x, y);
ButtonS.setScale(width / ButtonS.getGlobalBounds().width, height / ButtonS.getGlobalBounds().height);
font->loadFromFile("Resources/Edwardian.ttf");
ButtonT.setFont(*font);
ButtonT.setString(text);
ButtonT.setFillColor(Color::Black);
ButtonT.setPosition(x, y - ;
ButtonT.setCharacterSize(28);
ButtonBox = RectangleShape(Vector2f(width, height));
ButtonBox.setPosition(x, y);
this->activeColor = activeColor;
this->hoverColor = hoverColor;
this->idleColor = idleColor;
}
void Button::unPress()
{
ButtonState = BTN_IDLE;
}
bool Button::isHover()
{
if (ButtonState == BTN_HOVER)
return true;
return false;
}
const bool Button::isPressed() const
{
if (ButtonState == BTN_ACTIVE)
return true;
return false;
}
void Button::ButtonBoxfunc(bool act, Vector2f mousePos, int thick, Color color)
{
ButtonBoxAct = act;
if (ButtonBoxAct)
{
//std::cout << "asd" << std::endl;
ButtonBox.setOutlineColor(color);
ButtonBox.setOutlineThickness(thick);
ButtonState = BTN_IDLE;
if (ButtonBox.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (Mouse::isButtonPressed(Mouse::Left))
{
ButtonState = BTN_ACTIVE;
}
}
if (ButtonState == BTN_IDLE)
ButtonBox.setFillColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonBox.setFillColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonBox.setFillColor(activeColor);
}
}
void Button::updateEvented(Vector2f mousePos, Event event, bool fullColor)
{
ButtonState = BTN_IDLE;
if (ButtonS.getGlobalBounds().contains(mousePos))
{
ButtonState = BTN_HOVER;
if (event.mouseButton.button == sf::Mouse::Left)
{
ButtonState = BTN_ACTIVE;
}
}
if (fullColor)
{
if (ButtonState == BTN_IDLE)
setColorB(idleColor);
else if (ButtonState == BTN_HOVER)
setColorB(hoverColor);
else if (ButtonState == BTN_ACTIVE)
setColorB(activeColor);
}
else
{
if (ButtonState == BTN_IDLE)
ButtonS.setColor(idleColor);
else if (ButtonState == BTN_HOVER)
ButtonS.setColor(hoverColor);
else if (ButtonState == BTN_ACTIVE)
ButtonS.setColor(activeColor);
}
}
void Button::render(RenderTarget* target)
{
if (ButtonBoxAct)
target->draw(ButtonBox);
//target->draw(ButtonS);
target->draw(ButtonT);
}
in game start function:
(click to show/hide)
for (int i = 0; i < 32; i++)
{
int x = windowSize.width / 10 + i * 60;
int y = windowSize.height / 1.7;
if (i > 10)
{
y = windowSize.height / 1.5;
x = windowSize.width / 10 + i * 60 - 10 * 60;
}
if (i > 19)
{
y = windowSize.height / 1.35;
x = windowSize.width / 10 + i * 60 - 18 * 60;
}
if (i > 26)
{
y = windowSize.height / 1.23;
x = windowSize.width / 10 + i * 60 - 24 * 60;
}
keyboardButtons.push_back(Button(x, y, 40, 40, alphabet, "", Color::White, Color::Yellow, Color::Blue));
}
{
int x = windowSize.width / 10 + i * 60;
int y = windowSize.height / 1.7;
if (i > 10)
{
y = windowSize.height / 1.5;
x = windowSize.width / 10 + i * 60 - 10 * 60;
}
if (i > 19)
{
y = windowSize.height / 1.35;
x = windowSize.width / 10 + i * 60 - 18 * 60;
}
if (i > 26)
{
y = windowSize.height / 1.23;
x = windowSize.width / 10 + i * 60 - 24 * 60;
}
keyboardButtons.push_back(Button(x, y, 40, 40, alphabet, "", Color::White, Color::Yellow, Color::Blue));
}
(click to show/hide)
for (int i = 0; i < keyboardButtons.size(); i++)
{
keyboardButtons.setFont(&font);
keyboardButtons.updateEvented(mousePosView);
keyboardButtons.ButtonBoxfunc(true, mousePosView, 5, Color::Black);
if (keyboardButtons.isPressed())
{
//something
}
}
{
keyboardButtons.setFont(&font);
keyboardButtons.updateEvented(mousePosView);
keyboardButtons.ButtonBoxfunc(true, mousePosView, 5, Color::Black);
if (keyboardButtons.isPressed())
{
//something
}
}