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

Author Topic: checking collisions with array of objects takes too much  (Read 2206 times)

0 Members and 2 Guests are viewing this topic.

Ogn1k

  • Newbie
  • *
  • Posts: 5
    • View Profile
checking collisions with array of objects takes too much
« on: May 28, 2024, 09:28:52 am »
Hi everyone. I have this button script:
Button.cpp
(click to show/hide)
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:
(click to show/hide)
in game update function:
(click to show/hide)
at start i initialize buttons right to vector and in update check if button is pressed, the updateEvented checks collision and changes button states and buttonBoxfunc changes button color also helping to check collision creating box to collide with mouse
help me please to fix these freezes
« Last Edit: May 29, 2024, 07:28:36 am by Ogn1k »

johnnywz00

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: checking collisions with array of objects takes too much
« Reply #1 on: June 26, 2024, 10:23:01 pm »
I confess I haven't thoroughly read through your code, but I note that in your initialization portion you may be incurring a lot of redundancy the way the `if` statements are written.
I would suggest the form:

if(i>26){}
else if(i>19) {}
else if(i>10 {}
else {}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: checking collisions with array of objects takes too much
« Reply #2 on: June 26, 2024, 11:08:11 pm »
I wouldn't actually say it's just redundant. Rather, it's likely incorrect.

For example, if "i" is 27, all of those ifs would be executed.

So, the if...else if...else if...else is likely the best choice here as Johnny mentioned.


As an added bonus, the expressions: (ButtonState == BTN_HOVER) and (ButtonState == BTN_ACTIVE) evaluate to booleans. Therefore, since you return true if the boolean is true, you can simply return the expression directly.
e.g.:
const bool Button::isPressed() const
{
   return (ButtonState == BTN_ACTIVE);
}



As for freezing, how and when are you executing those two loops?
I noticed that you are loading a font in with each creation of a Button so that's 32 fonts loaded with that first "i" loop. Loading fonts can be a slow process so it's my guess that it's that that's causing the slow-down.

The other "i" loop sets a font within the loop. It's possible that this may also cause a delay if the fonts are being rendered for the first time. This is not anywhere near as slow as loading though so fix that first ;)

One thing you should note is that the Button should not know or care what the font is (so should not be loading it itself), especially since you give the ability to set it externally.
Instead, load the font outside of the Button class and pass a pointer to it only. This way it will loaded only once and each button can use the same one(s).

You should likely pass the font to the Button class during construction along with all its other parameters.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*