I've been trying to recreate the game, called Snake, in C++, using the SFML library.
I was programming it my way, with certain code aspects taken from this video:
I was having a lagging problem. The snake wouldn't move downwards smoothly without lagging. And I couldn't figure out why.
So I was forced to strip my code down similar to the code in the video.
Even after modifying every line, and testing, I kept getting the lagging.
Even when the code was exactly the same as the one in the video, I kept getting the lagging (I omitted the fruit code and my aesthetics were different, but the code was the same).
After a few hours of testing, I finally traced where the lagging is coming from.
This is what is bizarre.
Why would NOT drawing something, be causing a lag?
If you omit (or comment out) this block of code, or any of the invididual lines here, lagging occurs. This code is meant to draw the background tiles.
for (int i=0; i<N; i++)
for (int j=0; j<M; j++)
{ sprite1.setPosition(i*size, j*size); window.draw(sprite1); }
The source code and folder with images is downloadable at the YouTube video link I posted above. But you can simply use any 16x16 pixel square to represent the snake (sprite2) and background (sprite1).
Here is the code. It's actually not that long at all, and the code block above appears right after window.clear().
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int N=30,M=20;
int size=16;
int w = size*N;
int h = size*M;
int dir,num=4;
struct Snake
{ int x,y;} s[100];
void Tick()
{
for (int i=num;i>0;--i)
{s[i].x=s[i-1].x; s[i].y=s[i-1].y;}
if (dir==0) s[0].y+=1;
if (dir==1) s[0].x-=1;
if (dir==2) s[0].x+=1;
if (dir==3) s[0].y-=1;
}
int main()
{
srand(time(0));
RenderWindow window(VideoMode(w, h), "Snake Game!");
Texture t1,t2;
t1.loadFromFile("images/white.png");
t2.loadFromFile("images/red.png");
Sprite sprite1(t1);
Sprite sprite2(t2);
Clock clock;
float time = 0;
float timer=0, delay=0.1;
while (window.isOpen())
{
time = clock.getElapsedTime().asSeconds();
clock.restart();
timer+=time;
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
if (Keyboard::isKeyPressed(Keyboard::Left)) dir=1;
if (Keyboard::isKeyPressed(Keyboard::Right)) dir=2;
if (Keyboard::isKeyPressed(Keyboard::Up)) dir=3;
if (Keyboard::isKeyPressed(Keyboard::Down)) dir=0;
if (timer>delay) {timer=0; Tick();}
////// draw ///////
window.clear();
for (int i=0; i<N; i++) //IF YOU COMMENT OUT THIS LINE
for (int j=0; j<M; j++) //OR THIS LINE
{ sprite1.setPosition(i*size, j*size); window.draw(sprite1); } //OR THIS LINE, LAGS OCCUR.
//BUT IF YOU KEEP THIS BLOCK IN, THE LAGS DON'T OCCUR. THAT DOESN'T MAKE ANY SENSE TO ME.
for (int i=0;i<num;i++)
{ sprite2.setPosition(s[i].x*size, s[i].y*size); window.draw(sprite2); }
window.display();
}
return 0;
}
I'm simply trying to understand why NOT drawing something, is causing a problem? But if the code is left in there, there is no problem at all.
In other words, if you decide to not draw the background tiles, a lagging problem will occur with the snake. The snake will freeze and move irregulary. But if the background tiles are drawn, the snake moves smoothly and perfectly along.
The only thing that comes to mind, that would even cause a potential problem, is the code that deals with the clock and timing. But it still doesn't make any sense to me.
By omitting the background tile drawing, this should not impact the movement of the snake in the slightest.
Does anyone know what exactly is going on here?
The only reason I am posting this topic, is because I do find it very bizarre that leaving out a code that is meant to draw something is very weird. What if I didn't want a background? NOT drawing something shouldn't be causing a problem.
Thank you for taking the time to look at this.