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

Author Topic: Odd values for a sprite I'm trying to display  (Read 2345 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Odd values for a sprite I'm trying to display
« on: July 01, 2014, 09:12:06 pm »
Hey guys,

I have a vector of a class known has LevelBlock, each LevelBlock has an object known as DisplayObject
DisplayObject holds a sprite and other values here is the header file
#pragma once

#include "Enums.h"
#include "SFML\Graphics.hpp"

class DisplayObject
{
private:
        float xPos, yPos;
        sf::Sprite sprite;
        DisplayName name;
        std::string text;
        bool textObject;
public:
        DisplayObject();
        ~DisplayObject();

        sf::Text displayText;

        void SetSprite(sf::Texture texture){ sprite.setTexture(texture); }
        sf::Sprite GetSprite(){ return sprite; }
        void SetDisplayName(DisplayName setName){ name = setName; }
        DisplayName GetDisplayName(){ return name; }
        bool IsTextObject(){ return textObject; }
        void SetTextObject(bool set){ textObject = set; }
        void SetPosition(float xSet, float ySet);
};

 

Now in my level class I populate my vector of LevelBlocks to hold one block, I set the position for the sprite correctly, then I go ahead and pass a reference of the LevelBlocks DisplayObject for that block to my RenderService, so now the RenderService holds a reference to the block so it can render it, here is the code that converts my vector of LevelBlocks to vector of pointer DisplayObjects

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
        std::vector<DisplayObject*> tempBlocks;

        for (auto block : blocks)
        {
                tempBlocks.push_back(&block.displayObject);
        }

        return tempBlocks;
}

void Level::Setup()
{
        renderService->LevelDisplayObjects = GetLevelBlockDisplayObjects();
}

//Inside my renderService
//Display the level blocks
        for (const auto& object : LevelDisplayObjects)
        {
                Window.draw(object->GetSprite());
        }
 

Check my DisplayObjectSprite.png to see the values of the sprite at ""Window.draw(object->GetSprite());"" line.

Check my DisplayObjectCorrectSpriteValues.png to see the values before it goes thought my ""GetLevelBlockDisplayObjects"" function.

Also the sprite is never displayed, but I don't get any errors at all trying to render it

math1992

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
    • Email
Re: Odd values for a sprite I'm trying to display
« Reply #1 on: July 01, 2014, 10:04:29 pm »
Use reference.

sf::Sprite& GetSprite(){ return sprite; }
 

I am pretty sure it will works.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Odd values for a sprite I'm trying to display
« Reply #2 on: July 01, 2014, 10:11:48 pm »
I just tried that and i'm getting an error now

Basically on this line

Window.draw(object->displayObject.GetSprite());

When I step over it, I get this error at RenderTarget::Draw function

Unhandled exception at 0x5946E57E (sfml-graphics-d-2.dll) in SFML_Simple_Mario_Clone.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

Still confused on how my GetLevelBlockDisplayObjects() returns a fine looking vector, but when trying to access it looks like it has gone abit odd.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Odd values for a sprite I'm trying to display
« Reply #3 on: July 01, 2014, 10:21:14 pm »
I found my answer,

If you take a look at my old code

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
    std::vector<DisplayObject*> tempBlocks;

    for (auto block : blocks)
    {
        tempBlocks.push_back(&block.displayObject);
    }

    return tempBlocks;
}

I wasn't declaring auto as a reference, so it was creating an object, getting a reference of that object and then deleteing that object as it went out of scope. This was my bad, so I just changed my auto to be an auto& reference and it works fine now.

std::vector<DisplayObject*> Level::GetLevelBlockDisplayObjects()
{
        std::vector<DisplayObject*> tempBlocks;

        for (auto& block : blocks)
        {
                tempBlocks.push_back(block.GetDisplayObject());
        }

        return tempBlocks;
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Odd values for a sprite I'm trying to display
« Reply #4 on: July 01, 2014, 10:27:14 pm »
I was originally going to post this as a potential fix, but this would still be a huge style/design improvement so I'll post it anyway.

Add a draw() method to your DisplayObject.  Then you can actually draw your DisplayObjects properly, instead of forcibly yanking the drawables out of your DisplayObjects (which completely misses the point of encapsulating them in a DisplayObject).

See http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#creating-a-sfml-like-entity for how to implement draw().

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Odd values for a sprite I'm trying to display
« Reply #5 on: July 01, 2014, 11:13:38 pm »
Ixrec I will definitely take a look at that as that will make it so much easier for me to develop :D

 

anything