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

Author Topic: Exception thrown at 0x00000000 in ConsoleApplication1.exe: 0xC0000005: Access vi  (Read 5506 times)

0 Members and 1 Guest are viewing this topic.

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
After people told me to shorten the program I did it and here is the shortened version of the program with the same error as stated above.It only appears after a few moments into the program.If i hit continue the program works fine.However see the movement function?It does't work.The sprite refuses to move in any direction.However if i give a very large floating value in the move,then the sprite is displaced from it's position when i start the program and it stays there in that position with no further movement.For example if i write sprite.move(400.f,400.f) the sprite moves from (0,0) to (400,400) and stays there.It doesn't move any more. Here's the shortened version of the code:
   

       #include"SFML\Graphics.hpp"
       #include<iostream>
        int main()

    {
   sf::RenderWindow window(sf::VideoMode(640, 480), "CHECK",sf::Style::Default);
   std::cout << "WORKS";
   sf::Texture text;
   text.loadFromFile("bahamut.png");
   sf::Sprite sprite;
   sf::Clock frap;
   sprite.setTexture(text);
   while (window.isOpen())
   {
      float fps = frap.restart().asSeconds();
      sf::Vector2f movements;
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
      {
         movements.y = 0;
         movements.x = -1 * fps;
      }
      else
           {
               if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key: :D ))
               {
               movements.y = 0;
               movements.x = 1 * fps;
                }
                else
                     {
                            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
                            {
                            movements.y = 1 * fps;
                            movements.x = 0;
                            }
                               else
                                  {
                                       if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
                                        {
                                        movements.y = -1 * fps;
                                        movements.x = 0;
                                        }  
                                       else
                                           {
                                            movements.x = 0;
                                            movements.y = 0;
                                           }
                                  }
                    }
            }
        sprite.move(movements);
      window.clear();
      window.draw(sprite);
      window.display();
     
   }
    return 0;
    }
I improved upon the code to no effect.It still produces the same bug and error.
I used the dissassembler an found the error was caused in
00B37AEE  cmp         esi,esp 
in window.display().
Are the if else statements clear now?
« Last Edit: January 03, 2017, 07:50:56 am by KoniGTA »

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
That smiley over there is supposed to be :  D   ....smh
« Last Edit: January 03, 2017, 08:12:13 am by KoniGTA »

K.F

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Your else is only applied to your last if, it is overwriting all the other ifs, and "fps" is always very small since you are restarting it as seconds every frame. Also, use [ code] to make things easier to read:

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Thanks.Man I kept thinking if I had done something stupid and I was right...

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
I edited the code to no effect can someone please help?!

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Your code blocks are quite difficult to follow. The indentation is rather inconsistent. I mean, what are those four closing braces on consecutive lines doing? (just above "sprite.move(movements);")

For your information, "else if" can be much clearer than placing separate elses with ifs and their elses nested inside. For example:
if (thisThing == 0)
{
    std::cout << "This thing is zero." << std::endl;
}
else if (thisThing > 0)
{
    std::cout << "This thing is positive." << std::endl;
}
else
{
    std::cout << "This thing therefore must be negative." << std::endl;
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
Is it clear now?The 4 brackets above sprite.move() are the closing braces of the else statements

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
#include"SFML\Graphics.hpp"
#include<iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "CHECK",sf::Style::Default);
    std::cout << "WORKS";
    sf::Texture text;
    text.loadFromFile("bahamut.png");
    sf::Sprite sprite;
    sf::Clock frap;
    sprite.setTexture(text);

    constexpr float speed = 1;  // fyi, this is in "pixels per second", so quite slow to say the least

    while (window.isOpen())
    {
        float fps = frap.restart().asSeconds();
        sf::Vector2f movements;  // default constructed to {0.f, 0.f}

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A))
        {
            movements = {-speed * fps, 0.f};
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D))
        {
            movements = {speed * fps, 0.f};
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S))
        {
            movements = {0.f, speed * fps};
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W))
        {
            movements = {0.f, -speed * fps};
        }

        sprite.move(movements);

        window.clear();
        window.draw(sprite);
        window.display();

    }
    return 0;
}
 
If you look at the minimal example here
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}
You shall realize that you lack event handling in your program. This cannot work. The bare minimum is polling events and checking for sf::Event::Closed. Try fixing that and format your code properly, that will make finding the problem easier.

EDIT: Maybe fix highlighting to highlight 'constexpr' and such? Just a suggestion.
« Last Edit: January 03, 2017, 10:11:36 am by Raincode »

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
I know about event handling and such I just wanted to show you all the minimum code required to pose the problem faced by me.OHH I see what you meant by that if else statement yeah it's much clearer now. :D Thanks.
I found the problem.It shows me error is caused by the window.display line.However if I use a function to display in the window there's no error apparently.
« Last Edit: January 04, 2017, 07:53:04 am by KoniGTA »

KoniGTA

  • Newbie
  • *
  • Posts: 17
    • View Profile
    • Email
I would like to add that this is just a part of a class to move an animation.So if some parts seem useless or missing just try to remember that this is not the entire code