void DoMainLoop() { const std::chrono::milliseconds FRAME_PERIOD(16); SDL_Event event; CChronometer chronometer; while (true) { while (SDL_PollEvent(&event) != 0) { if (!ConsumeEvent(event) && m_pClient) { DispatchEvent(event, *m_pClient); } } if (m_isTerminated) { break; } // Очистка буфера кадра, обновление и рисование сцены, вывод буфера кадра. Clear(); if (m_pClient) { const float deltaSeconds = chronometer.GrabDeltaTime(); m_pClient->OnUpdateWindow(deltaSeconds); } CUtils::ValidateOpenGLErrors(); SwapBuffers(); chronometer.WaitNextFrameTime(FRAME_PERIOD); } }
bool ProcessEvent(Event* event){ if (current_state_ == 0) { return false; } // ConsumeEvent may change the next_state_ if (!ConsumeEvent(event)) { return false; } if (next_state_) { EnterNextState(); } return true; }
// simplest of all tests. Just set a file descriptor and see that it's available // repeat many times HRESULT CTestPolling::Test1() { HRESULT hr = S_OK; HRESULT hrResult; size_t size; PollEvent event; int fd; int count = 0; srand(100); ChkA(TestInit(10, 10)); size = _pipes.size(); hrResult = _spPolling->WaitForNextEvent(&event, 0); ChkIfA(hrResult != S_FALSE, E_UNEXPECTED); // one event at a time model for (int index = 0; index < 100; index++) { size_t item = rand() % size; ChkA(WritePipe(&_pipes[item])); ConsumeEvent(&fd, &count); ChkIfA(fd != _pipes[item].readpipe, E_UNEXPECTED); ChkIfA(count != 1, E_UNEXPECTED); } hrResult = _spPolling->WaitForNextEvent(&event, 0); ChkIfA(hrResult != S_FALSE, E_UNEXPECTED); Cleanup: return hr; }
// create a polling set HRESULT CTestPolling::Test2() { // simulate the following events in random order: // socket added (did it succeed as expected) // incoming data (write to a random pipe) // WaitForNextEvent called (did it return an expected result/socket) // Remove socket last notified about HRESULT hr = S_OK; HRESULT hrResult; PollEvent event; const size_t c_maxSockets = 10; srand(100); ChkA(TestInit(c_maxSockets, 0)); hrResult = _spPolling->WaitForNextEvent(&event, 0); ChkIfA(hrResult != S_FALSE, E_UNEXPECTED); for (size_t index = 0; index < 1000; index++) { int randresult = ::rand() % 4; switch (randresult) { case 0: { // simulate a new socket being added if (_pipes.size() >= c_maxSockets) { continue; } ChkA(CreateAndAddPipe()); break; } case 1: { // simulate incoming data size_t size = _pipes.size(); size_t itemindex; if (size == 0) { continue; } itemindex = rand() % size; ChkA(WritePipe(&_pipes[itemindex])); break; } case 2: case 3: { int fd; size_t pending = GetPendingCount(); if (pending == 0) { continue; } ChkA(ConsumeEvent(&fd, NULL)); if (randresult == 3) { // simulate removing this pipe from the set ChkA(RemovePipe(FindPipePairIndex(fd))); } break; } // case } // switch } // for Cleanup: return hr; }