The first thing is to setup a proper game loop. As you can see, I created 4 functions: init_, input_, update_ and render_. The game loop call input_, update_, render_ and vsync. That way, each role is separated and the screen is the last thing that is updated on each frame (That is to avoid some unexpected behaviors).