Hello Guys!
I have recently started learning SFML, and I am still a beginner, so my question might be too lame for you, but please try to help me, as it really bugs me out.
I am using Win7 And SFML 1.6 and trying to make simple games (Minesweeper, Snake)
The Problem I want to ask is : I have created sprites, drawn them on the screen and displayed, and then started my main game loop. Everything runs fine here. But when I edit my code and try draw/change sprites according to the events in the game loop and display them again, why it happens that two screens are displayed alternatively, one having the old sprites, and one completely black screen with just the updates sprites? I read the whole tutorials and came to this simple conclusion that I have to redraw the old sprites again and again as well in the main loop, along with the changed sprites. But Why? And is there any other way to this?
For Instance this is my code for snake (It's not completed, I am just checking how to move the snake around)
#include<SFML/Graphics.hpp>
using namespace sf;
int main()
{
RenderWindow Snake(VideoMode(800,600),"Snake");
Image BackGround, SnakeBody;
BackGround.LoadFromFile("C:/Users/Administrator/Desktop/BG.jpg");
SnakeBody.LoadFromFile("C:/Users/Administrator/Desktop/SB.jpg");
Sprite BG(BackGround), SB(SnakeBody);
for(int i = 0; i < 800; i += 20)
for(int j = 0; j < 600; j += 20)
{
BG.SetPosition(i,j);
Snake.Draw(BG);
}
for(int j = 0; j < 61; j += 20)
{
SB.SetPosition(400,j);
Snake.Draw(SB);
}
Snake.Display();
while (Snake.IsOpened())
{
Event Event;
while (Snake.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Snake.Close();
if (Snake.GetInput().IsKeyDown(sf::Key::Down))
{
SB.Move(0, 20);
Snake.Draw(SB);
}
Snake.Display();
}
}
}
The Grid displayed is fine and the starting snake too. But when I press the down key, the snake moves in alternative black screen. Same was the case in my minesweeper game, I get the starting tiles displayed well, but when I click on a tile, it opens in another black screen, and then clicking again on the black screen will open corresponding tile in the old screen. Its like both the screen keeps switching after a Event.
I know the simple workout, but I am not satisfied with that, and I need to know why 2 screen appears, so that I can try to find another way.
I have attached the source code, as well as the images.
Please do tell me the reason, another way if possible, and then a simple but satisfying algo for moving a snake when key pressed (I searched the net, I just got complex algo for that).
Thank You
[attachment deleted by admin]
I did as you said, but I don't know why I have to redraw the background? Doing that works, but I want to know why just drawing only the updates don't work.
I did the following changes :
for(int i = 0; i < 800; i += 20)
for(int j = 0; j < 600; j += 20)
{
BG.SetPosition(i,j);
Snake.Draw(BG);
}
for(int j = 0; j < 61; j += 20)
{
SB.SetPosition(400,j);
Snake.Draw(SB);
}
while (Snake.IsOpened())
{
Event Event;
while (Snake.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Snake.Close();
if (Snake.GetInput().IsKeyDown(sf::Key::Down))
{
SB.Move(0, 20);
Snake.Draw(SB);
}
}
Snake.Display();
}
One thing I have known is calling Display function once works fine. Calling It again produces an empty black screen. On third call, the old screen reappears. And sprites drawn in the consecutive calls are shown on the respective screens. If on alternative calls, old screen reappears with the consecutive updates, then it means old sprites are still stored ad displayed. Then why does this second screen comes in between? Is there any way to remove this??
Still getting no solution, this 2 Screens thing really bugs me...
And other than limiting frame rate, is there any way in which we can control the speed of our display, without producing imminent lag in events.
And also is there any command to pop all the events in a single go. Using while(App.GetEvent(Event));
works, but becomes jammy if continously events are given and the control gets stuck in the while loop..
All Help would be appreciated..