std::size_t RingBuffer::WriteBytes(const uint8_t *buf, std::size_t len) { std::size_t copied = std::min(len, SpaceAvailable()); if ( copied != 0 ) { if ( _writePos < _readPos ) { std::memcpy(&_buffer[_writePos], buf, len); _writePos += len; } else { std::size_t __t = _capacity - _writePos; if ( __t >= len ) { std::memcpy(&_buffer[_writePos], buf, len); _writePos += len; } else { std::size_t __b = len - __t; std::memcpy(&_buffer[_writePos], buf, __t); std::memcpy(_buffer, &buf[__t], __b); _writePos = __b; } } if ( _writePos == _capacity ) _writePos = 0; _numBytes += len; } return copied; }
RunLoop::EventSourcePtr AsyncByteStream::EventDispatchSource() { return RunLoop::EventSource::New([this](RunLoop::EventSource&) { if ( _err != 0 ) { if ( bool(_eventHandler) ) _eventHandler(AsyncEvent::ErrorOccurred, this); if ( bool(_eventDispatchSource) ) _eventDispatchSource->Cancel(); if ( bool(_eventSource) ) _eventSource->Cancel(); return; } if ( _eof ) { if ( bool(_eventHandler) ) _eventHandler(AsyncEvent::EndEncountered, this); if ( bool(_eventDispatchSource) ) _eventDispatchSource->Cancel(); if ( bool(_eventSource) ) _eventSource->Cancel(); return; } if ( BytesAvailable() && bool(_eventHandler) ) _eventHandler(AsyncEvent::HasBytesAvailable, this); if ( SpaceAvailable() && bool(_eventHandler) ) _eventHandler(AsyncEvent::HasSpaceAvailable, this); }); }
bool GetSpace (int iCellsNeeded, int *retx, int *rety) { int x = 0; int y = 0; while (!SpaceAvailable(x, y, iCellsNeeded)) { x++; if (x >= m_iCols) { x = 0; y++; if (y >= m_iRows) { int i; // If no room, then increase to a larger // number of rows int iNewRows = m_iRows + iCellsNeeded; bool *pNewCellMap = new bool [m_iCols * iNewRows]; int iOldCount = m_iCols * m_iRows; int iNewCount = m_iCols * iNewRows; for (i = 0; i < iOldCount; i++) pNewCellMap[i] = m_pCellMap[i]; for (i = iOldCount; i < iNewCount; i++) pNewCellMap[i] = false; // Swap delete [] m_pCellMap; m_pCellMap = pNewCellMap; m_iRows = iNewRows; // Restart a little ways up y = Max(0, y - iCellsNeeded); } } } for (int y1 = 0; y1 < iCellsNeeded; y1++) for (int x1 = 0; x1 < iCellsNeeded; x1++) MarkUsed(x + x1, y + y1); *retx = x; *rety = y; return true; }
void PlayGame() { char userChoice = 'k'; printf("Going to start game. Use AD for left, right. WS for up, down.\n"); printf("k to kill game.\n"); InitTiles(); NextStep(); do{ if(!NextStep()){ break; } PrintTiles(); refresh(); // printing on screen - via ncurses. userChoice = getch(); // if(userChoice=='\n') userChoice = getchar(); switch(userChoice){ case 'a': case 'A': MergeLeft(); ShiftLeft(); break; case 'd': case 'D': MergeRight(); ShiftRight(); break; case 'w': case 'W': MergeUp(); ShiftUp(); break; case 's': case 'S': MergeDown(); ShiftDown(); break; } clear(); } while(SpaceAvailable() && userChoice != 'k'); printf("\nGame over.\n"); }