SDL2/C++ - Loop at a specific frequency

Asked

Viewed 47 times

1

I have a project of an emulator CHIP8 that was already stopped a long time ago so I started working on it again today, I can already extract and draw sprites and I’ve implemented almost all the instructions, but sometimes the emulator runs very fast until crashing, 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);

            }

    }

} 

While researching I found that the ideal frequency for CHIP8 is 500Hz and for Super CHIP8 is 1000Hz, so how could I do this either using pure C++ or with SDL2?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.