protected int waitForNextFrame() {
        long current = System.currentTimeMillis();
        // handle first frame case
        if (lastUpdate == 0) {
            lastUpdate = current;
            return 1;
        }

        long elapsed = current - lastUpdate;

        int frames = (int)(elapsed / FRAME_RATE) + 1;
        long delay = FRAME_RATE * frames - elapsed;

        try {
            sleep(delay);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return frames;
    }