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:
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.