I'm pretty new to programming so apologies if this is more due to my inability to understand pointers rather than the library itself.
Anyway, my problem involves trying to pass sf::Texture* (I'm guessing the memory address of a texture) through a constructor of another class so that it can use that texture as its sprite. I feel I'm passing addresses around properly because I have no trouble passing a variable of type sf::Texture*, that is public, around different classes. I get the error "Access violation reading location 0xffffffffffffffff"
As for the bits of my code that are relevant (I'll post more if that's needed to fix the problem):
//in PuzzleRoom.cpp
roomObjects[i][j] = new RoomObject(objManager->getTextureLink(0), objManager->getObjName(0), i, j);
test = objManager->getTextureLink(0); //test variable accessed from other classes
//in RoomObject.cpp
RoomObject::RoomObject(sf::Texture* sprLoc, std::string name, int xLocation, int yLocation)
{
spriteLoc = sprLoc;
objName = name;
xLoc = xLocation;
yLoc = yLocation;
thisShape.setFillColor(sf::Color::White);
thisShape.setPosition(xLoc * 32, yLoc * 32);
thisShape.setSize(sf::Vector2f(32, 32));
thisShape.setTexture(spriteLoc);
}
The logic of my game is simple so I feel this is my only roadblock, so extra advanced thanks to anyone who knows the issue
Hi there!
I suspect that your sf::Texture* is uninitialized. Do you create your texture before you try to create your RoomObject? I got similar error when I used
sf::Texture *texture;
instead of
sf::Texture *texture = new sf::Texture();
After this you need to load a texture, because the line
thisShape.setTexture(spriteLoc);
will cause an error too, if spriteLoc is an initialized but empty texture.
I also have a tip for you. Use initializer lists in constructors, when you can:
RoomObject::RoomObject(sf::Texture* sprLoc, std::string name, int xLocation, int yLocation):
spriteLoc(sprLoc),objName(name),xLoc(xLocation),yLoc(yLocation)
{
thisShape.setFillColor(sf::Color::White);
thisShape.setPosition(xLoc * 32, yLoc * 32);
thisShape.setSize(sf::Vector2f(32, 32));
thisShape.setTexture(spriteLoc);
}
It works like this: you type a colon after the closing ) of your constructor, then you list your member variables as my_member(initialize_from_this) separated by comas. When you're done the open { follows, and you can continue with other statements (like setting the properties of thisShape).
This is better, because here the member variables are initialized directly from the variables. If you assign them in the body of the constructor, then they are created from some default values (this is unnecessary) and then assigned to your variables.