SDL2 / C ++ - Execute loop at a specific frequency

1

I have a project of a emulator of CHIP8 that was already stopped for a long time so I went back to work on it today, I can extract and draw sprites and I have implemented almost all instructions, but sometimes the emulator runs very fast until it stops , this is my loop:

void Window::run()
{

    SDL_Event e;
    while(!quit){

        while(SDL_PollEvent(&e)){

            switch(e.type){

                case SDL_QUIT:
                    quit = true;
                    break;
            }

        }

        chip8->run();
            if(chip8->drawFrame){
                chip8->drawFrame = false;

                uint32_t pixels[2048];
                for (int i = 0; i < 2048; ++i) {
                    uint8_t pixel = chip8->gfx[i];
                    pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000;
                }

                SDL_UpdateTexture(m_Texture, nullptr, pixels, 64 * sizeof(Uint32));

                SDL_RenderClear(m_Renderer);
                SDL_RenderCopy(m_Renderer, m_Texture, nullptr, nullptr);
                SDL_RenderPresent(m_Renderer);

            }

    }

} 

When searching I found that the ideal frequency for CHIP8 is 500Hz and for Super CHIP8 it is 1000Hz, so how could I do that using pure C ++ or SDL2?

    
asked by anonymous 05.11.2018 / 13:37

0 answers