I'm making a game in C that works on the linux framebuffer. So far I have a red 100x100 square that moves in tandem with the mouse, just like a pointer. I have implemented double buffering and it works correctly (the square doesn't flicker). The problem is, there is quite a lot of tearing happening horizontally. What I mean by that is, when the square moves on the x axis, it's like it is divided horizontally, and one part of it advances slithgly more than the other. This "rip" slowly propagates from top to bottom on the square if i keep moving it.
I believe it happens because the hardware reads the framebuffer during the moment when I copy the double buffer into the main buffer.
I have tried to combat the problem with the FBIO_WAITFORVSYNC ioctl, but no success.
Any ideas how I could do VSync on the framebuffer? Please add detail to your explanations as this is the first time I am programming such stuff on a Linux based OS so I might not understand otherwise.
Here is my code: http://pastebin.com/KJ4iaVEL