Exemplo n.º 1
0
	/*
	=====================
	GameControl::GameLoop
	=====================
	*/
	void GameControl::GameLoop() {
		quit = false;

		while (!quit) {
			HandleEvents();

			// Get the DT
			float dt = CalculateDeltaTime();
			if (dt < maxDelta) {
#ifdef WIN32
				Sleep(DWORD(ceil((maxDelta-dt)*1000.f)));
				dt += CalculateDeltaTime();
#else
				usleep(ceil((maxDelta-dt)*1000000.f));
				dt += CalculateDeltaTime();
#endif
			}

			if (!paused) {
#if defined(_DEBUG) && defined(WIN32)
				ConsoleReader::GetSingleton()->Dispatch();
				ClearDeleteQueue();
#endif /* _DEBUG && WIN32 */

				Input::GetSingleton()->Dispatch();
				ClearDeleteQueue();

				DispatchPrerender(dt);
			} else {
				// Dispatch input to all children of pauseLayer
				Input::GetSingleton()->DispatchPaused(pauseLayer);
				ClearDeleteQueue();

				DispatchPausedPreRender(dt);
			}

			ClearDeleteQueue();

			renderWindow->RenderFrame();

			AudioManager::GetSingleton()->UpdateSoundBuffers();

			// Was the game recently unpaused?
			if (pauseLayer && !paused) {
				scene->RemoveLayer(pauseLayer);
				pauseLayer = NULL;
			}

			// If a new scene has been set, transition it here
			SceneTransition();
		}

		ClearDeleteQueue();
	}
Exemplo n.º 2
0
void Server::handleNetworkMessages() {
	RakNet::Packet* pPacket = nullptr;

	while (true) {

		CalculateDeltaTime();

		for (	pPacket = m_pPeerInterface->Receive();
				pPacket;
				m_pPeerInterface->DeallocatePacket(pPacket), pPacket = m_pPeerInterface->Receive()) {

			switch (pPacket->data[0]) {
			case ID_NEW_INCOMING_CONNECTION: {
				addNewConnection(pPacket->systemAddress);
				std::cout << "A connection is incoming.\n";
				break;
			}
			case ID_DISCONNECTION_NOTIFICATION:
				std::cout << "A client has disconnected.\n";
				removeConnection(pPacket->systemAddress);
				break;
			case ID_CONNECTION_LOST:
				std::cout << "A client lost the connection.\n";
				removeConnection(pPacket->systemAddress);
				break;
			case ID_CLIENT_CREATE_OBJECT:
			{
				RakNet::BitStream bsIn(pPacket->data, pPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				createNewObject(bsIn, pPacket->systemAddress);
				break;
			}
			case ID_CLIENT_UPDATE_OBJECT_POSITION:
			{
				RakNet::BitStream bsIn(pPacket->data, pPacket->length, false);
				bsIn.IgnoreBytes(sizeof(RakNet::MessageID));
				moveGameObject(bsIn, pPacket->systemAddress);
				break;
			}
			case ID_CLIENT_UPDATE_OBJECT_VELOCITY:
			{
				pPacket->data[0] = ID_SERVER_VELOCITY_OBJECT_DATA;
				RakNet::BitStream bsIn(pPacket->data, pPacket->length, false);
				//bsIn.IgnoreBytes(sizeof(RakNet::MessageID));			
				m_pPeerInterface->Send(&bsIn, IMMEDIATE_PRIORITY, RELIABLE_ORDERED, 0, RakNet::UNASSIGNED_SYSTEM_ADDRESS, true);
				break;
			}
			default:
				std::cout << "Received a message with a unknown ID: " << pPacket->data[0];
				break;
			}
		}
	}
}
Exemplo n.º 3
0
	void GLUTGame::Idle()
	{
		CalculateDeltaTime();

		m_milliSecondsSinceLastFrame += deltaTime;
		if (m_milliSecondsSinceLastFrame * 1000 >= 1.f / 60.f)
		{
			glutPostRedisplay();
			m_milliSecondsSinceLastFrame = 0.f;
		}
	}
Exemplo n.º 4
0
	/*
	=====================
	GameControl::SceneTransition
	=====================
	*/
	void GameControl::SceneTransition() {
		if (newScene) {
			ShaderManager::GetSingleton()->ClearShaders();

			if (scene) {
				delete scene;
			}

			ClearDeleteQueue();

			scene = newScene;
			scene->LoadResources();

			newScene = NULL;

			// Discard the new (and too high) delta time
			CalculateDeltaTime();
		}
	}
Exemplo n.º 5
0
Game::Game(char *title_,int w,int h){
    if (instance == NULL){
        isClosing = false;
        instance = this;
        canDebug = false;
        backBuffer = NULL;
        canDelete = false;
        if (not SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK)){
            std::cout <<"Sdl started.";
        }
        if (not IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF)){
            printf("IMG FAIL");
        }
        if (TTF_Init()  == -1 ){
            printf("TTF FAIL");
        }

        window = SDL_CreateWindow( title_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_RESIZABLE ); //SDL_WINDOW_RESIZABLE
        strcpy(title,title_);
        renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
        if (!renderer){
            printf("Fail render!");
        }

        SDL_SetRenderDrawBlendMode(GetRenderer(),SDL_BLENDMODE_BLEND);

        dt = frameStart = frames = 0;
        nextUpdate =  1000;
        CalculateDeltaTime();
        srand(time(NULL));

        storedState  =new Title();
        olt = SDL_GetTicks();
        InputManager::GetInstance().InitJoySticks();


    }

}
Exemplo n.º 6
0
void Game::Run(){
    if (storedState != NULL){
        stateStack.emplace(storedState);
        storedState =NULL;
    }

    while (!SDL_QuitRequested() and not stateStack.top()->RequestedQuit()){
        restart_cycle:
        frameStart = dt;
        CalculateDeltaTime();

        InputManager::GetInstance().Update();
        if (ConfigManager::GetInstance().IsResized())
            goto restart_cycle;
        Update();
        if (InputManager::GetInstance().KeyPress(SDLK_F11)){
            static bool full = false;
            if (!full)
                SDL_SetWindowFullscreen(window, SDL_TRUE);
            else
                SDL_SetWindowFullscreen(window, SDL_FALSE);
            full = !full;
        }

        Render();
        SDL_RenderPresent(GetRenderer() );

        //Limitar dinamicamente o tempo de cada frame para atingir sempre que possivel 30 fps
        float n = GetDeltaTime()*100.0;

        if (stateStack.top()->RequestedDeleted()){
            if (stateStack.empty() && storedState == NULL){
                break;
            }else{
                stateStack.pop();
            }
        }

        if (storedState != NULL){
            stateStack.emplace(storedState);
            stateStack.top()->Begin();
            storedState =NULL;
        }


        n = n-(1000.0/MAXFPS);
        n  = n < 0 ? 0 : n;
        n  = (1000.0/MAXFPS)-n < 0 ? 0 : n;


        //SDL_Delay( std::max( (1000.0/MAXFPS)-n - 5, 0.0) );

        frames++;

        UpdateTitleAsFps((1000.0/MAXFPS)-n  );

    }
    isClosing = true;


};