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

Author Topic: Showing an inventory.  (Read 3929 times)

0 Members and 2 Guests are viewing this topic.

molmasepic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Showing an inventory.
« on: February 03, 2011, 09:10:56 pm »
hey i got this funky little code:

Code: [Select]
       if(BagUpdate)
        {
        for(int a=((pPlayer->get_BagShow())*9);a<((pPlayer->get_BagShow()*9)+9);++a)//18-27=A
        {
            sf::Sprite bsItem;
            sf::Image ibsItem;
            ibsItem.LoadFromFile(pPlayer->Bag[a]->get_Sprite());
            bsItem.SetImage(ibsItem);
            bsItem.SetSubRect(sf::IntRect((pPlayer->Bag[a]->get_Index()*15),0,((pPlayer->Bag[a]->get_Index()*15)+15),15));
            bsItem.Resize(15.f/16.f,15.f/16.f);
            bsItem.SetPosition(((17.f+(BagSlotX[a]))/16.f),((60.f+(BagSlotY[a]))/16.f));
            GameWind.Draw(bsItem);
            cout << "Bag updated!" << endl;
        }
        BagUpdate = false;
        }



basically what is does is show the inventory when BagUpdate is true.

the only problem is that when doing so(and this is in the game loop) it only shows the inventory for one frame(and doing this every frame can slow things down alot)

so im wondering if maybe im doing this wrong? if so then could someone help me out?
(again) what im trying to do show all of the sprites of the bag items without slowing the computer down...

im also wondering if its possible to make a small function that maybe pauses this loop automatically(if this is possible then please give an example, it would be nice)

if anyone doesnt understand what im asking or how the function works then reply here to let me know

i will keep this thread as updated as i can with this current status.

thank you for reading, and even more foe helping out. :D

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
Showing an inventory.
« Reply #1 on: February 03, 2011, 09:14:41 pm »
Maybe, if BagUpdate is true, render the bag on a sf::Image and then draw the image every frame (so you will see the actual items).
Displaying the already rendered image of the bag is faster the redrawing the entire bag every frame, so I guess it's a good solution...

molmasepic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Showing an inventory.
« Reply #2 on: February 03, 2011, 09:21:38 pm »
yes i have tried that, but when doing so, i get an error saying that 'ibsItem is not declared in this scope'

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
Showing an inventory.
« Reply #3 on: February 03, 2011, 09:31:21 pm »
Pretty confused by that code.
Why are you going from x * 9 to x * 9 + 9?
Anyway.

If you have several items, lets say helm, arms, sword and legs.
Make a sf::Image as instance of the class in which you render the bag, big enough to fit all the items, then render to it (pseudo code):
Code: [Select]

void DrawInv(bool BagUpdate) {
    if ( BagUpdate ) {
        for ( int i = 0; i < BagCount; ++i ) {
            Image.RenderTo(Inventory[i]); // put the items image on this image
        }
    }
}
Window->Draw(Image);

Now it only redraws all the stuff if the bag has been updated, else it will only display the already drawn image of the items.

Don't forget the scope rules, Image must be accesible outsire the for loop. Best thing to do is to make a member of the class.

* I don't know the exact code, but you'll find it out, sorry for that.

molmasepic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Showing an inventory.
« Reply #4 on: February 04, 2011, 12:37:38 am »
Quote from: "vEjEsE"
Pretty confused by that code.
Why are you going from x * 9 to x * 9 + 9?
Anyway.

If you have several items, lets say helm, arms, sword and legs.
Make a sf::Image as instance of the class in which you render the bag, big enough to fit all the items, then render to it (pseudo code):
Code: [Select]

void DrawInv(bool BagUpdate) {
    if ( BagUpdate ) {
        for ( int i = 0; i < BagCount; ++i ) {
            Image.RenderTo(Inventory[i]); // put the items image on this image
        }
    }
}
Window->Draw(Image);

Now it only redraws all the stuff if the bag has been updated, else it will only display the already drawn image of the items.

Don't forget the scope rules, Image must be accesible outsire the for loop. Best thing to do is to make a member of the class.

* I don't know the exact code, but you'll find it out, sorry for that.



i really dont understand what youre saying :(


EDIT: what im trying to do is draw the image every frame but at the same time update it, but at different framerates(keep the image drawn while NOT reloading the image)

i probably did this all wrong.....


EDIT:--->how about if i try putting a function inside this function(that way nothing is undefined)


just found out that CANNOT happen...nevermind that xD

hmm...i just need a way to get it drawn every frame without slowing everything down...

how bout some "frameskip"?

DoctorJ

  • Newbie
  • *
  • Posts: 21
    • View Profile
Showing an inventory.
« Reply #5 on: February 04, 2011, 08:08:18 pm »
There is a "LoadFromFile" call within the loop. You should load all these graphic files into memory before starting the game loop. Then they can be accessed much faster.

molmasepic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Showing an inventory.
« Reply #6 on: February 05, 2011, 02:39:08 am »
yes i know, what im trying to do is update the sprites when needed...

and when i move anything out of that for loop then i get undefined errors thrown at my face......



this is what i got now:

Code: [Select]
       if(BagUpdate)//boolean
        {
            for(int a=0;a<9;++a)
            {
                sf::Sprite sbItem;//make a sprite for a
                sf::Image isbItem;//make an image for a
                isbItem.LoadFromFile("Graphics/Items.png");//load a
                sbItem.SetImage(isbItem);//set the sprite to image
                sbItem.Resize(15.f/16.f,15.f/16.f);
                sbItem.SetSubRect(sf::IntRect((pPlayer->Bag[a]->get_Index()*15),0,((pPlayer->Bag[a]->get_Index()*15)+15),15));
                sbItem.SetPosition(((17.f+(BagSlotX[a]))/16.f),((60.f+(BagSlotY[a]))/16.f));
                ShowingBag.push_back(sbItem);//put the sprite in a vector <<<<<<<<<THIS is what im wondering is even possible!!!

                cout << "Items Drawn! Position: " << BagSlotX[a] << "," << BagSlotY[a] << "Item: " << a << endl;
                //image pos resize subrect
            }
            BagUpdate = false;
        }
        for(int b=0;b<9;++b)
        {
            GameWind.Draw(ShowingBag[b]);//then draw it on the screen(please note that it [u]doesnt[/u] draw it)
            //cout << "Item " << b << " Created!" << endl;
        }



my main thing is vector storing....maybe i should use something else...

also would it be a good idea to make an inventory class just for this part?
if so then i will work on that next...

molmasepic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Showing an inventory.
« Reply #7 on: February 05, 2011, 09:28:16 pm »
alright i got it...finally

sorry for the trouble

 

anything