//-------------------------------------------------------------------- boost::optional< FK2DEngine::CBlockAllocator::SBlock > FK2DEngine::CBlockAllocator::Alloc( unsigned int p_unWidth, unsigned int p_unHeight ) { if( p_unWidth > Width() || p_unHeight > Height() ) { return boost::optional< SBlock >(); } if( p_unWidth > m_pImpl->m_unMaxW && p_unHeight > m_pImpl->m_unMaxH ) { return boost::optional< SBlock >(); } SBlock b = SBlock( m_pImpl->m_unFirstX, m_pImpl->m_unFirstY, p_unWidth, p_unHeight ); if( m_pImpl->IsBlockFree(b) ) { m_pImpl->MarkBlockUsed( b, p_unWidth, p_unHeight ); return b; } unsigned int& x = b.m_unLeft; unsigned int& y = b.m_unTop; for( y = 0; y <= Height() - p_unHeight; y += 16 ) { for( x = 0; x <= Width() - p_unWidth; x += 8 ) { if( ! m_pImpl->IsBlockFree(b) ) { continue; } while( y > 0 && m_pImpl->IsBlockFree( SBlock( x, y - 1, p_unWidth, p_unHeight ) ) ) { --y; } while( x > 0 && m_pImpl->IsBlockFree( SBlock( x - 1, y, p_unWidth, p_unHeight ) ) ) { --x; } m_pImpl->MarkBlockUsed( b, p_unWidth, p_unHeight ); return b; } } m_pImpl->m_unMaxW = p_unWidth - 1; m_pImpl->m_unMaxH = p_unHeight - 1; return boost::optional< SBlock >( ); }
void CDisplay::GenerateNewBlockOrder() { bool placed[7]; for(int i = 0; i < 7; i++) placed[i] = false; for(int i = 0; i < 7; i++) { int r; do { r = rand() % 7; } while(placed[r]); placed[r] = true; switch(r) { case 0: BlockOrder[i] = IBlock(); break; case 1: BlockOrder[i] = OBlock(); break; case 2: BlockOrder[i] = TBlock(); break; case 3: BlockOrder[i] = LBlock(); break; case 4: BlockOrder[i] = JBlock(); break; case 5: BlockOrder[i] = SBlock(); break; case 6: BlockOrder[i] = ZBlock(); break; } BlockOrder[i]->Init(); BlockOrder[i]->SetColor((rand() % 5) + 1); } }