Exception thrown at 0x0F967B0E (sfml-graphics-d-2.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00000004.
If there is a handler for this exception, the program may be safely continued
I keep getting this error in a program i created for animation using sprites.I'm new to SFML so really any help will be appreciated.Before you all start telling me that i messed up to link the debug files,I have already triple,quadruple checked it.No i have linked to the debug files
I created a class for animation called animatrix and after scrutiny i found it which line was causing this particular error.(yeah i don't know how a line would cause a reading error in a dll)
Here's the main part:-
#include <SFML\Graphics.hpp>
#include <iostream>
#include"Animatrix.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1360, 720), "Larger SFML", sf::Style::Default);
sf::Texture texture;
texture.loadFromFile("bahamut.png");
texture.setSmooth(true);
Animatrix first(&texture,sf::Vector2u(4,4), 2.f);
while (window.isOpen())
{
sf::Clock fps;
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
std::cout << std::endl << "I wont let u close!MUAHAHAHAHAHA!!";
return 0;
break;
case sf::Event::Resized:
std::cout << "New height" << event.size.height << " New width" << event.size.width << std::endl;
break;
case sf::Event::TextEntered:
printf("You have pressed =%c \n", event.text.unicode);
break;
}
}
window.clear();
window.draw(first.Update(fps));
window.display();
}
return 0;
}
Heres the header file of animatrix.
#pragma once
#include "SFML\Graphics.hpp"
class Animatrix
{
public:
Animatrix(sf::Texture* texture,sf::Vector2u num,float stime);
sf::Vector2u currentimage;
sf::IntRect size;
float stime, fps;
float totaltime;
sf::Sprite show();
sf::Sprite Update(sf::Clock fps);
sf::Sprite sprite;
sf::Vector2u count;
~Animatrix();
};
Heres the definition of the functions and all of animatrix i.e animatrix.cpp:
#include "Animatrix.h"
#include<iostream>
Animatrix::Animatrix(sf::Texture* texture,sf::Vector2u num,float stime)
{
sprite.setTexture(*texture);
count.x = num.x;
count.y = num.y;
size.width= texture->getSize().x / count.x ;
size.height = texture->getSize().y / count.y ;
currentimage.x = 0;
currentimage.y = 0;
totaltime = 0.0;
this->stime = stime;
}
sf::Sprite Animatrix::Update(sf::Clock fps)
{
this->fps = fps.restart().asSeconds();
totaltime = totaltime + this->fps;
std::cout << totaltime;
if( totaltime >= stime ) //ERROR IS CAUSED HERE BY THE > CONDITION.Whenever i remove
{ // >condition it works fine.It works in = condition too but whenever i
//put > sign the error crops up
std::cout << "word";
totaltime = totaltime - stime;
sf::Sprite thing;
std::cout << std::endl << "does change occur here?" << currentimage.x << std::endl;
if (currentimage.x<count.x)
{
std::cout << "WORKED!";
currentimage.x++;
thing = show();
return thing;
}
else
{
std::cout << std::endl << "didit";
while (currentimage.x != 0)
{
std::cout << std::endl << "current<<" << currentimage.x;
currentimage.x--;
}
std::cout << std::endl << "HERE" << currentimage.x;
thing = show();
return thing;
}
}
}
sf::Sprite Animatrix::show()
{
sf::Vector2u tempos;
tempos.x = currentimage.x*size.width;
tempos.y = currentimage.y*size.height;
sprite.setTextureRect(sf::IntRect(tempos.x, tempos.y, size.width + tempos.x, size.height + tempos.y));
return sprite;
}
Animatrix::~Animatrix()
{
}
(*I included iosteam for checking the errors in various segments of the code)
PLEASE HELP I DON'T WANT TO STOP SFML HERE PLEASE!!!