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

Author Topic: Problems with passing sf::Texture* as a parameter  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

vinheim3

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Problems with passing sf::Texture* as a parameter
« on: July 28, 2014, 08:12:15 pm »
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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problems with passing sf::Texture* as a parameter
« Reply #1 on: July 28, 2014, 08:49:34 pm »
Hi, and welcome! :)

What is the return type of objManager->getTextureLink(0)? Is it sf::Texture*?

You should consider reading this post (preferably the entire thread! ;))
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problems with passing sf::Texture* as a parameter
« Reply #2 on: July 28, 2014, 09:08:37 pm »
You don't really provide enough of the code to tell what's going wrong. But, the obvious guess is that you fail to keep the object, which the pointer points to, alive. You need to manage object lifetimes so that the pointer remains valid as long as someone might use it.
Besides, I don't see you testing that pointer for being == nullptr. If it can't ever be, why not pass a reference instead? (comment about lifetime management still applies though)
I also think reading the following would do you good: http://www.bromeon.ch/articles/raii.html
« Last Edit: July 28, 2014, 09:10:55 pm by Jesper Juhl »

janszy

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Problems with passing sf::Texture* as a parameter
« Reply #3 on: July 29, 2014, 09:54:23 am »
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.

 

anything