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

Author Topic: sf::RenderWindow problem  (Read 3005 times)

0 Members and 1 Guest are viewing this topic.

buster

  • Newbie
  • *
  • Posts: 5
    • View Profile
sf::RenderWindow problem
« on: April 11, 2015, 07:56:02 pm »
I apoligize for my laziness if a thread with this problem has already been created.

I have a problem with RenderWindow. Specifically, after compilation, I get a crash report like this:
Quote from: Windows
gamePrototype.exe stopped working
An error caused a program to stop working correctly. Windows will close the program and notify you of available solutions.
The window doesn't want to open, but the console still works.
My code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>
float falling(float time)
{
    return 10*(time/1000);
}
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600, 32), "My Window");   //1
    window.setFramerateLimit(60);
    sf::Texture texture;
    sf::Sprite sprite;
    sf::Clock clock;
    sf::Event event;
    texture.loadFromFile("cos.png");
    sprite.setTexture(texture);
    sprite.setScale(sf::Vector2f(0.1f, 0.1f));
    sf::RectangleShape rectangle(sf::Vector2f(30, 600));
    rectangle.setFillColor(sf::Color::Red);
    rectangle.setPosition(510,0);
    int a = -30, mode = 1;
    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::F10))
                window.close();
        }
        if(mode==0)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))sprite.move(-4,0);
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))sprite.move(4,0);
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
            {
                sf::Time playerTime=clock.getElapsedTime();
                float time=playerTime.asMilliseconds();
                sprite.move(0,a+falling(time));
                mode = 1;
            }
            if(sprite.getPosition().x>510&&sprite.getPosition().x<540)
            {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))mode = 2;
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))mode = 2;
            }
            clock.restart();
        }
        if(mode==1)
        {
            sf::Time playerTime=clock.getElapsedTime();
            float time=playerTime.asMilliseconds();
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))sprite.move(-4,0);
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))sprite.move(4,0);
            if(sprite.getPosition().y<500)
            {
                sprite.move(0,falling(time));
            }
            else mode = 0;
            if(sprite.getPosition().x>510&&sprite.getPosition().x<540)
            {
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))mode = 2;
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))mode = 2;
            }
        }
        if(mode==2)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))sprite.move(0,-4);
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
            {
                if(sprite.getPosition().y<500)sprite.move(0,4);
                else mode = 0;
            }
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))mode = 1;
            clock.restart();
        }
        window.clear(sf::Color::White);
        window.draw(rectangle);
        window.draw(sprite);
        window.display();
    }

    return 0;
}
// 1 = everything except for this line is alright
 
I use Code::Blocks.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::RenderWindow problem
« Reply #1 on: April 12, 2015, 11:45:35 am »
I would suggest editing the code so that mode is set to a fixed value and isn't changed by the program. Then try mode as 0,1 and 2 by changing the initialisation to see in which part of the program the problem is. If this doesn't change anything then you've narrowed down the problem to the initialisation, the draw call or the event handling. This may not completely solve the problem but it has helped me in the past and if you know where the problem is then it is a lot easier to correct. It is, after all entirely possible that the window contruction is okay and the problem is elsewhere which is stopping it from showing.

Also, why have you included <iostream> and <cmath>?

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: sf::RenderWindow problem
« Reply #2 on: April 12, 2015, 01:47:10 pm »
It was with me to. :) You may use release libs for debud or debug libs for release - this is the reason for crashing! So if you want to stop crashing use sfml-xxx-d.lib in linker when you compile the debug version of programm or sfml-xxx.lib for release version. :)

MasterDeveloper

  • Newbie
  • *
  • Posts: 14
  • Forever, is not forever!
    • View Profile
    • Email
Re: sf::RenderWindow problem
« Reply #3 on: April 12, 2015, 03:58:53 pm »
Did you fixed your problem???
if(problem == NotFixed()) {

What bit Windows do you have? (x86 - x64)????
Because its important for SFML (for me is important).
What mode are you using for compiling???
else if(!problem == NotFixed()){
   
    std::cout << "MasterDeveloper" << std::endl;
    getchar();
    return 0;
}
 
« Last Edit: April 12, 2015, 11:28:18 pm by eXpl0it3r »
Born to be C++ Programmer!

buster

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: sf::RenderWindow problem
« Reply #4 on: April 12, 2015, 05:19:57 pm »
I would suggest editing the code so that mode is set to a fixed value and isn't changed by the program. Then try mode as 0,1 and 2 by changing the initialisation to see in which part of the program the problem is. If this doesn't change anything then you've narrowed down the problem to the initialisation, the draw call or the event handling. This may not completely solve the problem but it has helped me in the past and if you know where the problem is then it is a lot easier to correct. It is, after all entirely possible that the window contruction is okay and the problem is elsewhere which is stopping it from showing.

Also, why have you included <iostream> and <cmath>?
It crashes when it calls any SFML - related function, apparently.

This is not in the code, but for communicates about loaded files in the console and trygonometric functions.
What bit Windows do you have? (x86 - x64)????
What mode are you using for compiling???
x86
What mode?
It was with me to. :) You may use release libs for debud or debug libs for release - this is the reason for crashing! So if you want to stop crashing use sfml-xxx-d.lib in linker when you compile the debug version of programm or sfml-xxx.lib for release version. :)
I relinked the files and it still doesn't work.
« Last Edit: April 12, 2015, 05:27:53 pm by buster »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::RenderWindow problem
« Reply #5 on: April 12, 2015, 11:31:27 pm »
Don't listen what MasterDeveloper said. He seems to be a troll.

Do you link dynamically against SFML?
What's your compiler (Code::Blocks is an IDE not a compiler) and from where did you get SFML?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

buster

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: sf::RenderWindow problem
« Reply #6 on: April 14, 2015, 09:25:46 pm »
Don't listen what MasterDeveloper said. He seems to be a troll.

Do you link dynamically against SFML?
What's your compiler (Code::Blocks is an IDE not a compiler) and from where did you get SFML?
Yes.
MinGW32 and from official website. I'm using v2.2

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::RenderWindow problem
« Reply #7 on: April 14, 2015, 09:27:04 pm »
And did you build SFML yourself or from where did you get it?
Because none of the version on the SFML download site use the "official" MinGW compiler, thus if you just downloaded an SFML package, it won't work. You need a matching compiler or build from source.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/