Example #1
0
nsresult nsTextAddress::ReadRecord( nsIFileSpec *pSrc, char *pLine, PRInt32 bufferSz, char delim, PRInt32 *pLineLen)
{
    PRBool        wasTruncated;
    PRBool        isEof;
    char *        pRead;
    PRInt32        lineLen = 0;
    nsresult    rv;
    do {
        if (lineLen) {
            if ((lineLen + 2) < bufferSz) {
                memcpy( pLine + lineLen, "\x0D\x0A", 2);
                lineLen += 2;
                pLine[lineLen] = 0;
            }
        }
        wasTruncated = PR_FALSE;
        pRead = pLine;
        pRead += lineLen;
        pSrc->Eof(&isEof);
        if (isEof)
          // If we get an EOF here, then the line isn't complete
          // so we must have an incorrect file.
          rv = NS_ERROR_FAILURE;
        else
        {
          rv = pSrc->ReadLine( &pRead, bufferSz - lineLen, &wasTruncated);
          if (wasTruncated) {
            pLine[bufferSz - 1] = 0;
            IMPORT_LOG0( "Unable to read line from file, buffer too small\n");
            rv = NS_ERROR_FAILURE;
          }
          else if (NS_SUCCEEDED( rv)) {
            lineLen = strlen( pLine);
          }
        }
    } while (NS_SUCCEEDED( rv) && !IsLineComplete( pLine, lineLen));

    *pLineLen = lineLen;
    return( rv);
}
void GameReferee::PlayGame()
{
    //Event Handler
    SDL_Event e;
    Shape* curShape = new Shape();
    srand(time(0));
    curShape->CreateShape(rand() % 7);
    m_board->SetCurrentShape(curShape);
    
    //If idle time is about 1 second auto drop block
    m_idleTime = SDL_GetTicks();
    
    //Game Loop
    bool quit = false;
    while(!quit)
    {
        uint32_t curTime = SDL_GetTicks();
        if(curTime - m_idleTime > 1200)
        {
            m_idleTime = SDL_GetTicks();
            if(!IsCollision(1, 0, curShape))
                MoveShapeDown(curShape);
            else
            {
                //Block done dropping
                m_board->AddShapeToBoard();
                int rowFinished = IsLineComplete();
                while(rowFinished != -1)
                {
                    //remove line and push down
                    m_board->RemoveAndDropRow(rowFinished);
                    rowFinished = IsLineComplete();
                    Draw();
                    sleep(1);
                }
                curShape = new Shape();
                curShape->CreateShape(rand() % 7);
                m_board->SetCurrentShape(curShape);
                if(IsCollision(1, 0, curShape))
                {
                    printf("Game is Over!");
                    return;
                }
            }
            
        }
        //Event Handlers FOR KEY PRESSES
        while(SDL_PollEvent( &e ) != 0)
        {
            //User wants to quit
            if(e.type == SDL_QUIT)
            {
                quit = true;
            }
            else if(e.type == SDL_KEYDOWN)
            {
                int ** rotatedTetroid;
                switch(e.key.keysym.sym)
                 {
                         
                 //Reference for keypresses
                 case SDLK_UP:
                         rotatedTetroid = curShape->RotateShape();
                         if(!IsRotateCollision(rotatedTetroid, curShape->m_startRow, curShape->m_startCol))
                         {
                             curShape->SetTetroid(rotatedTetroid);
                         }
                         break;
                 case SDLK_DOWN:
                         if(!IsCollision(1, 0, curShape))
                             MoveShapeDown(curShape);
                         break;
                 case SDLK_LEFT:
                         if(!IsCollision(0, -1, curShape))
                             MoveShapeLeft(curShape);
                         break;
                 case SDLK_RIGHT:
                         if(!IsCollision(0, 1, curShape))
                             MoveShapeRight(curShape);
                         break;
                case SDLK_SPACE:
                         DropShapeComplete(curShape);
                         break;
                 default:
                         break;
                 }
            }
            
        }
        Draw();
    }
}