I am learning the basics of SFML (enough to make a basic pong game), but I keep getting this strange error with the window. When the window launches sometimes it has a black background (as intented), but about 50% of the time it seems to look like what was behind the window when it launched. I get no errors at compile time.
Here is the bit of the code that I think is causing problems:
//Game loop
while(window.isOpen()){
sf::Event Event;
while(window.pollEvent(Event)){
//Getting input
switch(Event.type){
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch(Event.key.code){
case sf::Keyboard::Right:
std::cout << "Right Key Pressed" << std::endl;
break;
case sf::Keyboard::Left:
std::cout << "Left Key Pressed" << std::endl;
break;
case sf::Keyboard::Up:
std::cout << "Up key pressed" << std::endl;
break;
}
break;
}
}
window.display();
}
return 0;
}
This is how I am making my window:
sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Pong!", sf::Style::Titlebar | sf::Style::Close);
Is there something extreemly obvious I am missing?
Also I am using Linux with xfce4 if that makes any difference.
I did not really understand your problem, but did you try to clear the window ?
add "window.clear();" just before "window.display();"
Related
I just started learning sfml and whenever I run the following code and try to move the window it crashes:
#include <SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(1920 , 1080), "Window", Style::Close | Style::Titlebar | Style::Resize);
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
switch(event.type)
{
case event.Closed:
window.close();
break;
case event.Resized:
std::cout << "New Window Width:" << event.size.width <<std::endl;
std::cout << "New Window Height:"<< event.size.height<<std::endl<<std::endl;
break;
}
}
window.display();
}
return 0;
}
When I remove window.display() from the code I can move the window without crashing it.
I am using Codeblocks 16.01 and SFML 2.4.2
Any ideas on why that happens?
Hellow, did you try to clear the window before displaying it like this:
window.clear();
And if it doesn't work, try to draw something before display nothing like a sf::Sprite for example.
Another thing that may causes that, is the size of your window. Because your not in full-screen mode so a size of 1920x1080 is too big because a external frame will be created by the os and may be it draws out the screen... but that would be really strange...
PS: prefer to use sf::Event::Closed for your switch/case
I had just started learning SFML 2.0 and I am facing a little bit problem. The problem is that when I press Button "A" then in Output it also show that "Mouse Left Button Pressed ".I am using code::block 16.04 and SFML 2.0.
Same thing happens with the "MouseWheelMoved" Event if i press any other number then it shows message that "MouseWheelMoved". Here is the code.
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(400,400),"All Events ");
sf::Event event;
window.setKeyRepeatEnabled(false);
while(window.isOpen())
{
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch(event.key.code)
{
case sf::Keyboard::A:
std::cout<<"A Button Pressed "<<std::endl;
break;
case sf::Keyboard::S:
std::cout<<"S Button Pressed "<<std::endl;
break;
case sf::Keyboard::D:
std::cout<<"D Button Pressed "<<std::endl;
break;
case sf::Keyboard::W:
std::cout<<"W Button Pressed "<<std::endl;
break;
case sf::Keyboard::Return:
std::cout<<"Enter Button Pressed"<<std::endl;
break;
case sf::Keyboard::Up:
std::cout<<"Up Button Pressed "<<std::endl;
break;
}
case sf::Event::MouseButtonPressed:
if(event.mouseButton.button == sf::Mouse::Left) {std::cout<<"Mouse Left Button Pressed "<<std::endl;
break;}
else if(event.mouseButton.button == sf::Mouse::Right){std::cout<<"Mouse Right Button Pressed"<<std::endl;
break;}
case sf::Event::MouseWheelMoved:
std::cout<<"Mouse Wheel Moved "<<event.mouseWheel.delta<<std::endl;
break;
}
}
}
}
You have two switch nested, you are missing a break in one of them
switch(event.type) // switch 1
{
// (...)
case sf::Event::KeyPressed:
switch(event.key.code) // switch 2
{
case sf::Keyboard::A:
std::cout<<"A Button Pressed "<<std::endl;
break; // <- this breaks switch 2 and...
// (...)
}
// ...you end up here. Here switch 1 doesn't have a break
// so it falls through to the next case.
case sf::Event::MouseButtonPressed:
// (...)
// here also falls through.
case sf::Event::MouseWheelMoved:
std::cout<<"Mouse Wheel Moved ";
break;
}
I'm making a basic breakout game using SFML in order to familiarize myself with C++ (moving up from C). Inside the game loop, I've placed an event poll that leads to a switch statement. When the mouse is moved, the switch-case will retrieve the mouse xy change and move the paddle accordingly, like so:
while (window.isOpen())
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
}
break;
case sf::Event::MouseMoved:
paddle.move(sf::Vector2f((sf::Mouse::getPosition(window).x - windowOffset.x) * sensitivity, 0));
break;
}
}
sf::Mouse::setPosition(windowOffset, window);
window.clear();
window.draw(paddle);
window.display();
}
Now, that works just fine and dandy, but I'm resetting the mouse position to the center of the window on every loop, which isn't necessary if the mouse hasn't moved. So, I tried moving sf::Mouse::setPosition() into the switch statement, underneath paddle.move, like so:
case sf::Event::MouseMoved:
paddle.move(sf::Vector2f((sf::Mouse::getPosition(window).x - windowOffset.x) * sensitivity, 0));
sf::Mouse::setPosition(windowOffset, window);
break;
However, when I rebuild the project, I now get a blank white window.
So, my question is: What's causing this to happen?
edit(minimal complete code)
int WinMain()
{
sf::Vector2i windowRes(700, 900);
sf::Vector2i windowOffset(350, 450);
sf::ContextSettings settings;
sf::RectangleShape paddle;
paddle.setSize(sf::Vector2f(150, 20));
paddle.setOrigin(350, 0);
paddle.setPosition(350, 825);
sf::RenderWindow window(sf::VideoMode(windowRes.x, windowRes.y), "Breakout", sf::Style::Default, settings);
window.setMouseCursorVisible(false);
sf::Mouse::setPosition(windowOffset, window);
sf::Event event;
while (window.isOpen())
{
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
switch (event.key.code)
{
case sf::Keyboard::Escape:
window.close();
break;
}
break;
case sf::Event::MouseMoved:
paddle.move(sf::Vector2f((sf::Mouse::getPosition(window).x - windowOffset.x), 0));
//sf::Mouse::setPosition(windowOffset, window);
break;
}
}
sf::Mouse::setPosition(windowOffset, window);
window.clear();
window.draw(paddle);
window.display();
}
return 0;
}
So, through some tinkering, it seems that sf::MOUSE::setposition() generates an event.type of sf::Event::MouseMoved, which would be added to the event queue, causing an infinite loop.
I seem to be having a bit of trouble when managing multiple windows in SFML C++. When I try to manage multiple windows, they both open correctly and I can interact in the larger one, however, the smaller one, which upon creation is overlapping the larger window, I can not interact with until I move the large window away. An image is below to help with the visual. Also below is the main loop for my code.
The main loop of the code is as follows:
while (MainWin.isOpen() || F1Menu.isOpen())
{
sf::Event Event;
if (MainWin.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
break;
case sf::Event::Resized:
MainView.reset(sf::FloatRect(0.f, 0.f, (MainWin.getSize().x*0.9f), (MainWin.getSize().y*0.9)));
MainWin.setView(MainView);
break;
case sf::Event::KeyPressed:
if (Event.key.code == sf::Keyboard::F1)
F1Menu.create(sf::VideoMode(200, 500), "SFML 2D - F1 Menu");
else if (Event.key.code == sf::Keyboard::Escape)
{
MainWin.close();
if (F1Menu.isOpen())
F1Menu.close();
}
break;
}
}
if (F1Menu.pollEvent(Event))
{
switch (Event.type)
{
case sf::Event::Closed:
F1Menu.close();
break;
case sf::Event::MouseButtonReleased:
if (Event.mouseButton.button == sf::Mouse::Left)
if (LMButton.mouseIn(F1Menu))
LoadMap("MapA.dat");
break;
case sf::Event::MouseMoved:
if (LMButton.mouseIn(F1Menu))
LMButton.setColor(sf::Color::Yellow);
else
LMButton.setColor(sf::Color::White);
break;
}
}
moveClock.restart();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
player.move(0, -4 * time);
player.setDirection(sfm::Direction::Up);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
player.move(0, 4 * time);
player.setDirection(sfm::Direction::Down);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
player.move(-4 * time, 0);
player.setDirection(sfm::Direction::Left);
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
player.move(4 * time, 0);
player.setDirection(sfm::Direction::Right);
}
if (F1Menu.isOpen())
{
F1Menu.clear();
F1Menu.draw(LMButton);
F1Menu.display();
}
if (MainWin.isOpen())
{
MainWin.clear();
if (SplashScreen.didAnimate)
SplashScreen.Animate(MainWin, sfg::Animation::FadeIn);
if (inMenu)
{
}
if (isPlaying)
{
DrawMap(MainWin);
MainWin.draw(player);
}
MainWin.display();
}
}
In my knowledge, you aren't able to poll two separate windows with one sf::Event object. this is because when polling events, you are essentially popping off of the event stack and handling each one. The signature of pollEvent is
bool pollEvent(sf::Event &event);
note that there is no const qualifier here, because each processed event gets popped off the event stack. By the time you finish polling your main window there are no events left for your other window. This could be the cause of your window being unable to focus. Your second window should use it's own separate sf::Event
On a side note I would highly recommend encapsulating your data inside classes. You can find a great reference here on how to go about doing so. It's great coding practice and helps minimize confusion when bug hunting
I'm new to programming and, since I find it easier to learn doing, I was playing around with C++ and SFML. I was trying to get a sprite to move, but for some reason, I can't do it. I've tried everything I found, but no luck. Anyone know why this isn't working? I'm using Visual Studio 2012 Express and SFML 2.1, by the way.
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 800), "Project");
window.setFramerateLimit(30);
glEnable(GL_TEXTURE_2D);
window.clear(sf::Color::White);
window.display();
sf::Texture charMain;
if (!charMain.loadFromFile("Images/playerFrontSprite.png"))
{
return 1;
}
sf::Sprite charaMain;
charaMain.setPosition(500.f , 500.f);
charaMain.setTexture(charMain);
window.draw(charaMain);
window.display();
sf::Font cavefont;
sf::Font::Font(cavefont);
if (!cavefont.loadFromFile("Cave-Story.ttf"))
return 1;
while (window.isOpen())
{
sf::Event playerAction;
while (window.pollEvent(playerAction))
{
switch(playerAction.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
std::cout << "sf::Event::KeyPressed" << std::endl;
switch(playerAction.key.code)
{
case sf::Keyboard::W:
charaMain.move(0 , 1);
break;
case sf::Keyboard::A:
charaMain.move(-1 , 0);
break;
case sf::Keyboard::S:
charaMain.move(0 , -1);
break;
case sf::Keyboard::D:
charaMain.move(1 , 0);
break;
}
break;
}
}
}
}
Your problem is that you're moving the object, but you're not drawing it on the screen at its new position. Your drawing code is outside the main loop, so it never gets called after you move the sprite, so you never see the effects.
You basically need to pull the rendering code (ie. window.draw(charaMain) and window.display()) inside the while (window.isOpen()) loop, outside the event handling loop.