Exemple #1
0
void TWorld::timerEvent(QTimerEvent *) {
    Time += 1;
    if (Players.size() < 2) {
        return; // rount not started yet
    }
    if (Time % 20 == 0) {
        UpdatePlanetEnergy(); // update energy every second
    }
    ProcessShips();
    CheckRoundEnd();
    SendWorld();
}
Exemple #2
0
//Main Event Loop
void EventHandler::EventLoop()
{
    struct timeval myTimeval;
    struct timezone myTimezone;
    gettimeofday(&myTimeval, &myTimezone);
    srand(myTimeval.tv_usec);

    Star star(-10.0f,0.0f,0.0f,10.0f); //top left
    Star star2(0.0f, 10.0f, -10.0f,0.0f); //bot right
    Star star3(-10.0f, 0.0f, -10.0f,0.0f); //bot left
    Star star4(0.0f, 10.0f, 0.0f,10.0f); //top right
    SDL_Event event;
    while(!m_bQuit)
    {
        if(SDL_PollEvent(&event))
        {          
            switch(event.type)
            {
                case SDL_QUIT:
                    m_bQuit=true;
                    break;
                case SDL_KEYDOWN:
                {
                    if(event.key.repeat)
                    {
                        break;
                    }
                    ShipIntf *pShip = 0;
                    uint32 command = 0;
                    switch(event.key.keysym.scancode)
                    {
                        case SDL_SCANCODE_B:
                            //TBD do test things
                            break;
                        case SDL_SCANCODE_X:
                            m_bQuit=true;
                            break;
                        case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
                        case SDL_SCANCODE_R: //Reset to 0 position
                        case SDL_SCANCODE_A: //left
                        case SDL_SCANCODE_D: //right
                        case SDL_SCANCODE_W: //up
                        case SDL_SCANCODE_S: //shoot
                        case SDL_SCANCODE_E: //bomb
                            pShip=m_ships[0];
                            break;

                        case SDL_SCANCODE_U: //Stop the ship exactly where it is,
                        case SDL_SCANCODE_P: //Reset to 0 position
                        case SDL_SCANCODE_J: //left
                        case SDL_SCANCODE_L: //right
                        case SDL_SCANCODE_I: //up
                        case SDL_SCANCODE_K: //shoot
                        case SDL_SCANCODE_O: //bomb
                            if(m_ships.Length() > 1)
                            {
                                pShip = m_ships[1];
                            }
                            break;
                        default:
                            break;
                    };
                    if(pShip)
                    {
                        command=KeyDown(event.key.keysym.scancode);
                        pShip->ProcessInput(command);
                    }   
                }
                break;
                case SDL_KEYUP:
                {
                    ShipIntf *pShip = 0;
                    uint32 command = 0;
                    switch(event.key.keysym.scancode)
                    {
                        case SDL_SCANCODE_Q: //Stop the ship exactly where it is,
                        case SDL_SCANCODE_R: //Reset to 0 position
                        case SDL_SCANCODE_A: //left
                        case SDL_SCANCODE_D: //right
                        case SDL_SCANCODE_W: //up
                        case SDL_SCANCODE_S: //shoot
                        case SDL_SCANCODE_E: //bomb
                            pShip=m_ships[0];
                            break;
                        case SDL_SCANCODE_U: //Stop the ship exactly where it is,
                        case SDL_SCANCODE_P: //Reset to 0 position
                        case SDL_SCANCODE_J: //left
                        case SDL_SCANCODE_L: //right
                        case SDL_SCANCODE_I: //up
                        case SDL_SCANCODE_K: //shoot
                        case SDL_SCANCODE_O: //bomb
                            if(m_ships.Length() > 1)
                            {
                                pShip = m_ships[1];
                            }
                            break;
                        default:
                            break;
                    };
                    if(pShip)
                    {
                        command=KeyUp(event.key.keysym.scancode);
                        pShip->ProcessInput(command);
                    }   

                }
                break;
                case SDL_USEREVENT:
                {
                    //All of the user event types should have a ship pointer
                    ShipIntf *pShip = reinterpret_cast<ShipIntf*>(event.user.data1);
                    pShip->ProcessInput(event.user.code);
                }                    
                break;
                default:
                    break;
            }
        }
        
        //Do the physics stepping
        m_world.Step(m_timeStep, m_velocityIterations, m_positionIterations);
        m_world.ClearForces();

        ProcessEffects();
        
        //clear the screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        star.Draw();
        star2.Draw();
        star3.Draw();
        star4.Draw();
        //Draw objects that need to be drawn
        m_walls.Draw();

        //Ships store/draw their own bullets
        ProcessShips();
        
        //Display the back buffer
        SDL_GL_SwapWindow(m_pWindow);
        
        //Sleep to not eat the machine
        SDL_Delay(60);
    }

}