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

Author Topic: Problem with game loop  (Read 976 times)

0 Members and 1 Guest are viewing this topic.

Chivos

  • Newbie
  • *
  • Posts: 19
    • View Profile
Problem with game loop
« on: January 09, 2013, 06:14:42 am »
I am relatively new to OOP. I have been working on this problem for awhile and cannot find a good way to do it.

In my class I have:

Code: [Select]
int GetNextCard(int &ReturnCard)                               //Idea is to set ReturnCard to the last element, then delete the element
    {                                                                                     
        ReturnCard = cards.back();
        cards.pop_back();
        return ReturnCard;
    }

void SetNextCard(std::vector<int> &NextCard, int  &ReturnCard)     //Idea is to add to a new vector what the value of returncard
{                                                                                                                       //This is done so I can use something like sprite[NextCard]
  NextCard.push_back(ReturnCard);                                                       //instead of sprite[0] or sprite[1]
}                                                                                                                       //I want to use this method because for htis and splits
                                                                                                                        //the sprite[0] method seems difficult to use as there is no
void dealcardone(sf::RenderTarget& App, int x, int y)                         //way to determine how many cards have gone out from
{                                                                                                                      //player to play.
    if (fStop == true)
    {
        GetNextCard(ReturnCard);                                      //This is my draw function, uses bool to ensure it only runs once
        SetNextCard(NextCard, ReturnCard);
        fStop = false;
    }
        SpriteCard[ReturnCard].SetPosition(x,y);
        App.Draw(SpriteCard[NextCard[0]]);
}

void dealcardtwo(sf::RenderTarget& App, int x, int y)
{
    if (fStop2 == true)
    {
        GetNextCard(ReturnCard);
        SetNextCard(NextCard, ReturnCard);
        fStop2 = false;
    }
        SpriteCard[ReturnCard].SetPosition(x,y);
        App.Draw(SpriteCard[NextCard[1]]);
}


The problem is that the second time the main game loop runs, the first card gets changed to the second same card. I know why it is happening. It is happening because the second time through when it redraws the value of NextCard has grown, so it changes. I do not see a good way around this well keeping the cards in the main game loop. The cards must stay in the main game loop though, as I will have to change the position of a card if the player splits. Can anyone point me in the right direction as to what I need to do here? Any help would be greatly appreciated.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10927
    • View Profile
    • development blog
    • Email
Re: Problem with game loop
« Reply #1 on: January 09, 2013, 06:26:23 am »
As mentioned in the other topic of yours:
You should really express yourself more clearly, or even better, illustrate with a minimal complete example what you want to achieve and where your problem is. Please read this thread.
Pleeeeease, use the code=cpp tag.
You still didn't use the right tag.

Write your code [code=cpp]like this[/code] (3rd entry of the rightmost dropdown field).

Is it really so hard to follow those advises? >:(
Besides that, I don't know what crazy thing you've done to your space/tab formatting...

As for the code, if you want to a clean OOP way, then you should start by strictly separating the logic (what cards, which actions, etc.) from the rendering (sprites, draw() calls, etc).
Having two functions that only differ in one used variable isn't a good design at all.

For your described problem, I can't really help you since I've no idea what you're talking about and you've only provided some snippets.
« Last Edit: January 09, 2013, 07:26:10 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/