void GameWidget::newGame() { in_pause = false; fast_mode = false; num_level = 1; num_pieces_level = 2; num_points = 0; bg_sprite = (Sprite)(Sprite_Bg1 + random.getLong(num_bgs)); screen->setBackgroundSprite(bg_sprite); mirror->setBackgroundSprite(bg_sprite); next->setBackgroundSprite(bg_sprite); for (int i = 0; i < scr_width; ++i) mirror_sprites[i] = bg_sprite; for (int i = 0; i < 4; ++i) next_piece[i] = bg_sprite; for (int y = 0; y < scr_height; ++y) for (int x = 0; x < scr_width; ++x) ref(x, y) = bg_sprite; newPiece(); nextPiece(); updateMirror(); in_game = true; repaintChilds(); emit changedStats(num_level, num_points); timer_interval = 700; timer->start(timer_interval); }
unique_ptr<Element> Parser::parseElement(Piece &piece) { unique_ptr<Element> elem(new Element()); elem->m_name = std::move(piece.m_value); elem->m_attrs = std::move(*piece.m_attrs); if (piece.m_type == Piece::type::tag_empty) { return std::move(elem); } for (;;) { Piece next = nextPiece(); switch (next.m_type) { case Piece::type::text: elem->m_text.append(std::move(next.m_value)); break; case Piece::type::tag_open: case Piece::type::tag_empty: elem->m_children.push_back(std::move(parseElement(next))); break; case Piece::type::tag_close: if (next.m_value != elem->m_name) { fail("expected tag closing \"" + elem->m_name + "\" " + "but found \"" + next.m_value); } return std::move(elem); case Piece::type::tag_header: /* * Header should happen only as the first statement so I guess it * should fail here. But simply ignoring it means no charm, right? */ break; } } }
unique_ptr<Element> Parser::parse(std::istream &is) { m_lexer.setInput(is); Piece piece = std::move(nextPiece()); if (piece.m_type == Piece::type::tag_header) { // TODO: Extract some useful information from XML header. piece = std::move(nextPiece()); } switch (piece.m_type) { case Piece::type::tag_open: case Piece::type::tag_empty: return std::move(parseElement(piece)); default: fail("expected root tag"); return std::move(unique_ptr<Element>(nullptr)); } }
void GameWidget::blockPiece() { if (fast_mode) { timer->changeInterval(timer_interval); fast_mode = false; } putPiece(); bool xmap[scr_width * scr_height]; for (int yy = 0; yy < scr_height; ++yy) for (int x = 0; x < scr_width; ++x) xmap[yy*scr_width + x] = false; if (piece[2] != bg_sprite) checkSolePiece(xpos, ypos+1, xmap); if (piece[3] != bg_sprite) checkSolePiece(xpos+1, ypos+1, xmap); if (piece[2] == bg_sprite) checkSolePiece(xpos, ypos, xmap); if (piece[3] == bg_sprite) checkSolePiece(xpos+1, ypos, xmap); compact(); nextPiece(); }
char* GFolderSerializer::next(size_t* pOutSize) { if(m_pUncompressedBuf) // If we are supposed to compress the output... { if(m_state == 0) { memcpy(m_pBuf, "cgfs", 4); *pOutSize = 4; m_state = 4; m_bytesOut += 4; return m_pBuf; } if(m_compressedBufReady) // If there is a compressed buffer queued up... { // Return the compressed buffer *pOutSize = m_compressedSize; m_bytesOut += m_compressedSize; m_compressedBufReady = false; return (char*)m_pCompressedBuf; } while(true) // Fill up a buffer until we have enough stuff to compress { size_t size; char* pChunk = nextPiece(&size); if(pChunk) { // Add the chunk to the uncompressed buffer size_t partLen = std::min(size, COMPRESS_BUF_SIZE - m_uncompressedPos); memcpy(m_pUncompressedBuf + m_uncompressedPos, pChunk, partLen); m_uncompressedPos += partLen; if(m_uncompressedPos >= COMPRESS_BUF_SIZE) // if the uncompressed buffer is full... { // Compress it, and return the compressed bytes delete[] m_pCompressedBuf; m_pCompressedBuf = GCompressor::compress((unsigned char*)m_pUncompressedBuf, COMPRESS_BUF_SIZE, &m_compressedSize); memcpy(m_pUncompressedBuf, pChunk + partLen, size - partLen); m_uncompressedPos = size - partLen; m_compressedBufReady = true; *pOutSize = sizeof(unsigned int); m_bytesOut += sizeof(unsigned int); memcpy(m_pBuf, (char*)&m_compressedSize, sizeof(unsigned int)); return m_pBuf; } } else { // There is nothing left that needs to go in if(m_uncompressedPos > 0) { // Compress whatever is left delete[] m_pCompressedBuf; m_pCompressedBuf = GCompressor::compress((unsigned char*)m_pUncompressedBuf, (unsigned int)m_uncompressedPos, &m_compressedSize); m_compressedBufReady = true; *pOutSize = sizeof(unsigned int); m_bytesOut += sizeof(unsigned int); m_uncompressedPos = 0; memcpy(m_pBuf, (char*)&m_compressedSize, sizeof(unsigned int)); return m_pBuf; } else return NULL; } } } else { char* pRet = nextPiece(pOutSize); m_bytesOut += *pOutSize; return pRet; } }