コード例 #1
0
ファイル: GameWindow.cpp プロジェクト: haved/DafLanguage
int GameWindow::Run(uint32_t frameRate) {
	std::cout << "Running application!" << std::endl;

    //====================================================================INIT of SDL and GLFW
	if (SDL_Init(SDL_INIT_TIMER) < 0) {
		std::cerr << "SDL failed to Init! Aborting!" << std::endl;
		return -1;
	}
	glfwSetErrorCallback(error_callback);
    if(!glfwInit()) {
        std::cerr << "GLFW failed to init! Aborting!" << std::endl;
        exit(EXIT_FAILURE);
    }
    //====================================================================GLFW render window window creation
	glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
    glfwWindowHint(GLFW_SAMPLES, m_samples);
    m_renderWindow = glfwCreateWindow(m_width, m_height, m_title, NULL, NULL);
    if (!m_renderWindow) {
		std::cerr << "GLFW window creation failed!" << std::endl;
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
    const GLFWvidmode* vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); //Center the renderWindow on the screen
    glfwSetWindowPos(m_renderWindow, (vidmode->width-m_width)/2, (vidmode->height-m_height)/2);
    //=====================================Render window callbacks
    glfwSetWindowSizeCallback(m_renderWindow, window_resize_callback);
    glfwSetKeyCallback(m_renderWindow, window_keyevent_callback);
    //=====================================Thread window
    m_threadWindow = glfwCreateWindow(80, 60, "ThreadWindow", NULL, m_renderWindow);
    if (!m_threadWindow) {
		std::cerr << "GLFW thread window creation failed!" << std::endl;
		glfwDestroyWindow(m_renderWindow);
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
    //====================================================================INIT of GLEW
    glfwMakeContextCurrent(m_renderWindow);

    glewExperimental=true;
    GLenum err = glewInit();
	if(err!=GLEW_OK) {
		std::cerr << "Failed to initialize GLEW!" << glewGetErrorString(err) << std::endl;
		glfwDestroyWindow(m_threadWindow);
		glfwDestroyWindow(m_renderWindow);
		glfwTerminate();
		exit(EXIT_FAILURE);
	}

    //====================================================================GAME INIT and LOOP
	m_game->Init();
	m_game->OnResize(m_width, m_height);
    SDL_Thread* loaderThread = SDL_CreateThread(loadThread, "SmithGame_Loader_thread", NULL);//Making thread
    glfwShowWindow(m_renderWindow);
    uint32_t startMs = SDL_GetTicks();
    uint32_t framesAgo = 0;
    float msPerFrame = 1000.f/frameRate;
	while (!glfwWindowShouldClose(m_renderWindow)) {
        glfwMakeContextCurrent(m_renderWindow);
        UpdateDeltaTime();
		m_game->NextFrame();
		glfwSwapBuffers(m_renderWindow);
		glfwPollEvents();


        framesAgo++;
        uint32_t wantedTime = startMs+((uint32_t)(framesAgo*msPerFrame));
        uint32_t currentTime = SDL_GetTicks();
        if(wantedTime>currentTime)
            SDL_Delay(wantedTime-currentTime);
        if(framesAgo>=frameRate) {
            framesAgo=0;
            startMs+=1000;
        }
	}
	//====================================================================TELL and wait for the loading thread to stop,
    glfwSetWindowShouldClose(m_threadWindow, GL_TRUE);
    int threadReturnValue;
    SDL_WaitThread(loaderThread, &threadReturnValue);
    //====================================================================DESTROY everytning
    m_game->Destroy();
	glfwDestroyWindow(m_threadWindow);
	glfwDestroyWindow(m_renderWindow);
    glfwTerminate();
	SDL_Quit();
    std::cout << "Program end" << std::endl;
	return 0;
}
コード例 #2
0
ファイル: Game.cpp プロジェクト: shultays/pigment
void Game::Tick(){

  DInput->Poll();
  if(DInput->KeyPressed(DIK_F12) || DInput->KeyPressed(DIK_F11) ){
    consoleOn = 1 - consoleOn;
  }
  if(consoleOn){
    con.Tick();
    DInput->Clear();
  }

  GetLock();
  UpdateDeltaTime();

  
  Networks::getScene();
  if(nextWeapon != -1){
    weaponTimer -= DeltaTime*1000;
    if(weaponTimer < 0){
      weapon = nextWeapon;
      testplayer.weapon = weapon;
      nextWeapon = -1;
      testplayer.playhighAnim(RAISE);
      //ALERT;
    }
  }


  if( DInput->KeyPressed( DIK_C) ){
    thirdperson = 1-thirdperson;

    if(thirdperson){
      testplayer.addDrawList();
    }else{
      testplayer.removeDrawList();
    }
  }
  if( DInput->KeyPressed( DIK_F1 ) ) changeWeapon(0);
  if( DInput->KeyPressed( DIK_F2 ) ) changeWeapon(1);

  if( DInput->KeyPressed(DIK_F7)){
    ActiveCamera->Position = D3DXVECTOR3(0, 3000, 0);
  }
  if( DInput->KeyPressed(DIK_F5)){
    fstream file;
    file.open(L"save.txt", ios::out);

    file << ActiveCamera->Position[0] << " " 
         << ActiveCamera->Position[1] << " " 
         << ActiveCamera->Position[2] << "\n" ;
    
    file << ActiveCamera->Pitch<< " " 
         << ActiveCamera->Yaw << "\n" ;

    file.close();

    debug << "D3DXVECTOR3(" << ActiveCamera->Position[0] << "," 
         << ActiveCamera->Position[1] << "," 
         << ActiveCamera->Position[2] << ")," ;
    
  }
  if( DInput->KeyPressed( DIK_O ) ){
    side = 1-side; 
    testplayer.side = side;
  }

  if( DInput->KeyPressed(DIK_F6)){
    fstream file;
    file.open(L"save.txt", ios::in);
    file >> ActiveCamera->Position[0]
         >> ActiveCamera->Position[1]
         >> ActiveCamera->Position[2];

    
    file >> ActiveCamera->Pitch
         >> ActiveCamera->Yaw;


    file.close();

  }