I'm new to SFML (2.0), C++, and new to programming, so please be nice on my code.. I apologize it's so long, but this is the bare minimum I think I can post to explain my problem since I'm stumped. (If you want the whole code, I have no problem giving it out to anyone). With that said...
I'm trying to make an RPG game (here are some screenshots:
http://imgur.com/a/vxd9B - pardon the Paint graphics, but I suck at drawing and I'm fine with that), and up until now everything's been going fine. I stressed for about a week about how to make an inventory system, and came up with a solution that semi-works, but is giving me issues.
I declared some global variables:
sf::Texture textureArray[95]; // textures for sprites for slots 1-96
sf::Sprite spriteArray[95]; // sprites for spots 1-96
sf::FloatRect invRect[95]; // floatRect for interacting with spots 1-96
In main(), before the game's while loop, I create/load all my textures, and then I do this from 0-95:spriteArray[0].setPosition(INV_1_X, INV_1_Y); // INV_1_X and Y are pre-defined coords for where to draw the sprites
invRect[0] = sf::FloatRect(INV_1_X, INV_1_Y, INV_SIZE_X, INV_SIZE_Y); // INV_SIZE_X and Y are pre-defined sizes for the boxes
I then set all of the textureArray[] assets from 0-95 to empty by doing this:
for (int i = 0; i < 95; i++) { textureArray[i] = invEmpty; }
Then I give the player some test items:
addItem(invHPot); // I added this line several times with various items
In the game's while loop, if the inventory window is open I do this:for (int i = 0; i < 96; i++)
{
if (cursor_box.intersects(invRect[i])) // cursor_box is the floatRect for the cursor
{
selectInv = i;
std::cout << "SELECTED INV SLOT # " << i + 1 << "." << std::endl; // for testing
}
}
if (cursor_box.intersects(buttonDispose_box)) // if they click DISPOSE after selecting an item
{
delItem(selectInv);
std::cout<<"DISPOSE THIS ITEM"<<std::endl; // for testing
selectInv = -1; // reset selectInv
}
And temporarily, if the player clicks the MOVE button below DISPOSE, I made it run addItem(invHPot) just to test if the system's working. (this function is below)
Here are the two functions for adding and deleting items from the inventory:
void delItem(int selectInv) // selectInv is a number 0-95 and is decided from the functions above (when the player clicks in a space)
{
sf::Texture invEmpty;
invEmpty.loadFromFile("res/images/inv/invEmpty.png");
textureArray[selectInv] = invEmpty; // a blank 5x5 png texture
}
void addItem(sf::Texture textureName) // Inv spot 0-95
{
int i;
for (i = 0; i < 95; i++)
{
if (textureArray[i].getSize().x == 5) // invEmpty is 5x5 pixels, others are 43x43
{
textureArray[i] = textureName;
break;
}
}
}
Then toward the end of main(), before showing the screen I do the following:// Link/Set sprites to appropriate textures
for (int i = 0; i < 95; i++)
{ spriteArray[i].setTexture(textureArray[i]); // now the spriteArray and textureArray elements should be linked}
// Display inventory items
for (int i = 0; i < 95; i++)
{ Screen.draw(spriteArray[i]); // draw all of the sprites }
------------------------------------
Phew, that was long. So what the problem is, is sometimes the program crashes randomly when I open the inventory screen. When it doesn't crash, this is what happens:
The items display fine (I usually try addItem with at least 5-30 items, and they display fine). The selecting and deleting items works fine. If I delete an item, it goes away properly. But when I try to add an item while playing, it will only replace one of the empty spots. It won't let me just add items, unless I've "deleted" one already using delItem (the DISPOSE button). I'm not sure how to fix this. I've tried re-arranging things, but each time I get the same problem (when I was testing it with only 12 elements, before changing it to the full 96, it didn't crash, but I had the same problem).
Please tell me I'm stupid "because there's such an easy solution." I hope someone here can help me out.