简体   繁体   中英

C++, Adding conditional statements in class vars

Sorry, but I have to repeat the same question as I asked before " C++, Adding conditions in class vars ".

I am using SDL2 here.

In obj.h: (excluding preprocessor commands)

class obj {
public:
        SDL_Rect clip;
        void addCollideWith( SDL_Rect rect );
        void hasCollide();
        void clearCollideWith();
private:
        std::list<bool *> collideWith;
};

In obj.cpp: (excluding preprocessor commands)

void obj::addCollideWith( SDL_Rect rect )
{
        collideWith.push_back(SDL_HasIntersection(obj.clip, rect));
}

void obj::hasCollide()
{
    bool retval = true;
    for (std::list<bool *>::iterator it = collideWith.begin(); it != collideWith.end(); it++) 
    {
        retval = retval && **it;
    }
    return retval;
}

void clearCollideWith()
{
    collideWith.clear();
}

Inside main function, I am saying that the object moves by one pixel and every time when it moves by one pixel, it checks for collision with other objects. I cleared the pointer thing '*' as I am not putting in variables as you can see: collideWith.push_back(SDL_HasIntersection(obj.clip, rect)); . What I do is to make it move a pixel, clear collideWith and add collideWith condition again for updating whether it is true or false.

Now, whats the problem?

Its making the program really really slow! If I remove collideWith thing and then, starts the program, it gets a lot more smoother. Now, what I want, is to store the statement rather than true or false. std::list takes:

collideWith.pushBack(true /*OR*/ false);

But what I want is:

collideWith.pushBack(/*statement determining whether it is true or false*/ var1 > var2);

Please do complain if context is missing or the question is somehow, not understandable! (NOTE: Context related to moving the object and declaring obj clip sub-vars is not mentioned as they are not a part of question.)

You could try to replace

    std::list<bool *> collideWith;

with

    std::list<SDL_Rect> collideWith;

in order to track of the rectangles that you want to considere.

The implementation could be :

void obj::addCollideWith( SDL_Rect rect )
{
        collideWith.push_back(rect);
}

// to test if it collides with at least one rectangle
bool obj::hasCollide()
{
    bool retval = false;
    for (std::list<SDL_Rect>::iterator it = collideWith.begin(); it != collideWith.end(); it++) 
    {
        retval = retval || SDL_HasIntersection(obj.clip, *it);
    }
    return retval;
}

// to test if it collides with all rectangles
/* bool obj::hasCollide()
{
    bool retval = true;
    for (std::list<SDL_Rect>::iterator it = collideWith.begin(); it != collideWith.end(); it++) 
    {
        retval = retval && SDL_HasIntersection(obj.clip, *it);
    }
    return retval;
} */

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM