SFML community forums

Help => General => Topic started by: TheGoldenFyre on April 11, 2016, 04:15:31 pm

Title: Exception Thrown when trying to run program
Post by: TheGoldenFyre on April 11, 2016, 04:15:31 pm
Code: [Select]
#include "SFML\Graphics.hpp"
#include <random>
#include <ctime>

int main() {
   srand(time(NULL));
   sf::RenderWindow TestingWindow(sf::VideoMode(100, 100), "Testing Window");
   sf::Image img;
   sf::Texture txt;
   sf::Sprite spr;
   int x = 1, y = 1, z;
   while (y < 100){
      while (x < 100) {
         z = rand() % 2 + 1;
         if (z == 1) {
            img.setPixel(x, y, sf::Color::Black);
         }
         else {                                 
            img.setPixel(x, y, sf::Color::Black);
         }
         x++;
      }
      x = 1; y++;
   }
   txt.loadFromImage(img);
   spr.setTexture(txt);
   while (TestingWindow.isOpen())
   {
      sf::Event event;
      while (TestingWindow.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            TestingWindow.close();
      }

      TestingWindow.clear();
      TestingWindow.draw(spr);
      TestingWindow.display();
   }
}


So.... I'm trying to run this program, and it should be creating a sort of fuzz you see on a faulty television, but everytime I run the program, this exception is thrown.

Unhandled exception at 0x6B8C5C98 (sfml-graphics-2.dll) in SFML 2.0 APPLICATION.exe: 0xC0000005: Access violation writing location 0x00000004.

Is there something I can do to fix this?


(And yes, I know there is a lack of comments. I'm trying to put more in my code, but I keep forgetting because I'm the only one who reads it)
Title: Re: Exception Thrown when trying to run program
Post by: Mr_Blame on April 11, 2016, 04:18:56 pm
You didn't set the size of image.
Title: AW: Exception Thrown when trying to run program
Post by: eXpl0it3r on April 11, 2016, 04:19:20 pm
You don't create the image, yet you use setPixel on it. That's undefined behavior as documented in the docs. ;)
Title: Re: Exception Thrown when trying to run program
Post by: TheGoldenFyre on April 11, 2016, 04:21:27 pm
Thanks!
Title: Re: Exception Thrown when trying to run program
Post by: Mr_Blame on April 11, 2016, 04:25:20 pm
http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Image.php#a2a67930e2fd9ad97cf004e918cf5832b   ;D
Title: Re: Exception Thrown when trying to run program
Post by: TheGoldenFyre on April 11, 2016, 04:30:21 pm
One more thing... It is running now, but I only get to see a black screen. Any ideas?
Title: Re: Exception Thrown when trying to run program
Post by: Mr_Blame on April 11, 2016, 04:31:26 pm
Your code gives an black image as result, see:
if (z == 1) {
            //for black pixel
            img.setPixel(x, y, sf::Color::Black);
         }
         else {  
            //again black pixel                              
            img.setPixel(x, y, sf::Color::Black);
         }
//conclusion: in any case you get black pixel
 
Title: Re: Exception Thrown when trying to run program
Post by: nicox11 on April 11, 2016, 04:31:45 pm
 if (z == 1) {
    img.setPixel(x, y, sf::Color::Black);
}
else {                                
    img.setPixel(x, y, sf::Color::Black);
}

what's the difference here ? You either way define black as the pixel color.
Title: Re: Exception Thrown when trying to run program
Post by: TheGoldenFyre on April 11, 2016, 04:33:45 pm
I'm stupid as sh*t... I meant the second one to be white. Thanks for the help guys!


Edit:

Everything's working now. Thanks again!