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

Author Topic: 'Unhandled exception at 0x00E884B9 in: 0xC0000005: Access violation' SFML 2.3  (Read 2998 times)

0 Members and 1 Guest are viewing this topic.

starrybolt

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hi,

I am currently learning Object Orientated Programming and I am attempting to use an animation class that will be able to be used with every sprite which I will need in my SFML game.

I am a beginner trying to implement this class (https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite) to make my game object orientated and further understand how it works.

I have two separate .cpp files: AnimateSprite.cpp and AnimatedSprite.cpp.

Here is code from the AnimateSprite.cpp file.

#include "AnimateSprite.h"
#include "AssetManager.h"

Animation::Animation() : m_texture(NULL)
{

}

void Animation::addFrame(sf::IntRect rect)
{
    m_frames.push_back(rect);
}

void Animation::setSpriteSheet(const sf::Texture& texture)
{
    m_texture = &texture;
}

const sf::Texture* Animation::getSpriteSheet() const
{
    return m_texture;
}

std::size_t Animation::getSize() const
{
    return m_frames.size();
}

const sf::IntRect& Animation::getFrame(std::size_t n) const
{
    return m_frames[n];
}
 


and here is a minimised version of code from my AnimatedSprite.cpp file.
void AnimatedSprite::setAnimation(const Animation& animation)
{
    m_animation = &animation;
    m_texture = m_animation->getSpriteSheet();
    m_currentFrame = 0;
    setFrame(m_currentFrame);
}

void AnimatedSprite::play(const Animation& animation)
{
    if (getAnimation() != &animation)
        setAnimation(animation);
    play();
}

const Animation* AnimatedSprite::getAnimation() const
{
    return m_animation;
}

void AnimatedSprite::setFrame(std::size_t newFrame, bool resetTime)
{
    if (m_animation)
    {
        //calculate new vertex positions and texture coordiantes
        sf::IntRect rect = m_animation->getFrame(newFrame);

        m_vertices[0].position = sf::Vector2f(0.f, 0.f);
        m_vertices[1].position = sf::Vector2f(0.f, static_cast<float>(rect.height));
        m_vertices[2].position = sf::Vector2f(static_cast<float>(rect.width), static_cast<float>(rect.height));
        m_vertices[3].position = sf::Vector2f(static_cast<float>(rect.width), 0.f);

        float left = static_cast<float>(rect.left) + 0.0001f;
        float right = left + static_cast<float>(rect.width);
        float top = static_cast<float>(rect.top);
        float bottom = top + static_cast<float>(rect.height);

        m_vertices[0].texCoords = sf::Vector2f(left, top);
        m_vertices[1].texCoords = sf::Vector2f(left, bottom);
        m_vertices[2].texCoords = sf::Vector2f(right, bottom);
        m_vertices[3].texCoords = sf::Vector2f(right, top);
    }

    if (resetTime)
        m_currentTime = sf::Time::Zero;
}

void AnimatedSprite::update(sf::Time deltaTime)
{
    // if not paused and we have a valid animation
    if (!m_isPaused && m_animation)
    {
        // add delta time
        m_currentTime += deltaTime;

        // if current time is bigger then the frame time advance one frame
        if (m_currentTime >= m_frameTime)
        {
            // reset time, but keep the remainder
            m_currentTime = sf::microseconds(m_currentTime.asMicroseconds() % m_frameTime.asMicroseconds());

            // get next Frame index
            if (m_currentFrame + 1 < m_animation->getSize())
                m_currentFrame++;
            else
            {
                // animation has ended
                m_currentFrame = 0; // reset to start

                if (!m_isLooped)
                {
                    m_isPaused = true;
                }

            }

            // set the current frame, not reseting the time
            setFrame(m_currentFrame, false);
        }
    }
}

void AnimatedSprite::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    if (m_animation && m_texture)
    {
        states.transform *= getTransform();
        states.texture = m_texture;
        target.draw(m_vertices, 4, sf::Quads, states);
    }
}

 

The unhandled exception is thrown at this line of code:

leftAnimation->setSpriteSheet(playerLeft);

 void Animation::setSpriteSheet(const sf::Texture& texture)
{
    m_texture = &texture;
}
 

This then takes me too my setSpritesheet function within my animation class.



In the load content function of the game.cpp this is how the playerLeft texture is loaded in.


  if(!playerLeft.loadFromFile("left_spritesheet.png"))
    {
        std::cout << "Could not load texture from file" << std::endl;
    }

 

Can anyone please advise?
Any help would be greatly appreciated.

Many thanks,
starrybolt.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Access violation usually means you are trying to use an invalid pointer somewhere.

leftAnimation->setSpriteSheet(playerLeft);
What is leftAnimation? Is it a valid Animation pointer? Double check that you are using pointers correctly.

Satus

  • Guest
Quote
The unhandled exception is thrown at this line of code:

Than handle it and provide us some more information?  :)
My guess is that leftAnimation is not valid object.

starrybolt

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hi,

Thankyou for the quick reply.

leftAnimation is an object of my Animation class.

Thanks I will double check my pointers.

Kind regards,
starrybolt.

Satus

  • Guest
You should use c++11 smart pointers to minimize risks of having invalid pointers in your program.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Next time please re-use your already opened thread on the same topic. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything