當前位置: 首頁>>代碼示例>>Java>>正文


Java Clock類代碼示例

本文整理匯總了Java中org.jsfml.system.Clock的典型用法代碼示例。如果您正苦於以下問題:Java Clock類的具體用法?Java Clock怎麽用?Java Clock使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Clock類屬於org.jsfml.system包,在下文中一共展示了Clock類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import org.jsfml.system.Clock; //導入依賴的package包/類
public static void main(String[] args) {
	// Create a simulation.
	Sim simulation = new Sim(WIDTH, HEIGHT, TITLE);
	simulation.init();
	
	// FPS/ticks count.
	int ticks = 0;
	int frames = 0;
	Time fpsTime = Time.ZERO;
	
	//Main loop
	Clock clock = new Clock();
	Time elapsed = Time.ZERO;
	Time frameTime = Time.ZERO;
	int skippedFrames = 0;
	
	boolean hasFocus = true;
	
	while(simulation.getWindow().isOpen()) {
		frameTime = clock.restart();
	    elapsed = Time.add(frameTime, elapsed);
		
	    // Update 60 times per seconds
	    while(Time.ratio(elapsed, TIME_PER_TICK) >= 1.f && skippedFrames < MAX_SKIPPED_FRAMES) {
			elapsed = Time.sub(elapsed, TIME_PER_TICK);
			skippedFrames++;

	    	// Handle events
		    for(Event event : simulation.getWindow().pollEvents()) {
		        if(event.type == Event.Type.CLOSED) {
		        	simulation.getWindow().close();
		        }
		        else if(event.type == Event.Type.GAINED_FOCUS) {
		        	simulation.onGainFocus();
		        }
		        else if(event.type == Event.Type.LOST_FOCUS) {
		        	simulation.onLostFocus();
		        }
		        else if(event.type == Event.Type.KEY_PRESSED && event.asKeyEvent().key == Keyboard.Key.ESCAPE) {
		        	simulation.getWindow().close();
		        }
		        else if(hasFocus) {
		        	simulation.handleEvent(event);
		        }
		    }
		    
		    // Update
	    	simulation.update(TIME_PER_TICK);
	    	
	    	// Tick count.
			ticks++;
	    }
	    
	    simulation.render(elapsed);
	    frames++;
	    skippedFrames = 0;
	    
	    // Compute average FPS and display it in the window's title.
	    fpsTime = Time.add(frameTime, fpsTime);
	    if(fpsTime.asSeconds() > 1f) {
	    	fpsTime = Time.sub(fpsTime, Time.getSeconds(1.0f));
	    	simulation.getWindow().setTitle(TITLE  + " | Ticks : " + ticks + ", FPS : " + frames);
			
	    	// Reset counters.
	    	ticks = 0;
			frames = 0;
		}
	}
}
 
開發者ID:om3g4zell,項目名稱:CityBuilderJSFML,代碼行數:70,代碼來源:Main.java


注:本文中的org.jsfml.system.Clock類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。