English 中文(简体)
野生动物饲养方案——CPU使用有限 lo
原标题:Game programming - CPU usage of an infinite loop

I want to create a simple 2D game. I don t want to use threads. The programming language will be Java (but could be any language, does not really matter...).

如果我使用像<条码>(真实性)这样的主要通道,是否有可能避免使用CPU。

最佳回答

在一场游戏中,你通常拥有一个能够管理一切的主要通道。 然而,为了避免做不必要的事情,通常只能以某种框架的形式更新游戏(例如60个框架)。

大部分游戏都取得了这一成绩,使邮联睡觉,直到需要计算/提取新框架。 在python游戏图书馆,pyega, 采用

一定数量的微秒将暂停使用。 这项职能为将处理员与其他方案分享的过程提供了方便。 哪怕只剩下几毫秒的节目将耗费很少的加工时间。

问题回答

为了说明Darthfett的答案,青少年游戏的主要通道通常会看一看:

#define FRAMELENGTH (1.0 / 60.0) // 60hz = 16.6ms per NTSC frame. Different for PAL. 
while ( !QuitSignalled() ) 
{
   double frameStartTime = GetTime(); // imagine microsecond precision
   HandleUserInput( PollController() );
   SimulateEntities(); 
   Physics();
   Render();
   Sound();
   etc();

   double timeUntilNextFrameShouldStart = GetTime() - frameStartTime + FRAMELENGTH;
   sleep( timeUntilNextFrameShouldStart  );   
}
// if down here, we got the  quit  signal 
ExitToDesktop();

当然,如果一个框架需要16.6万多人执行,那么上述漏洞就会消失,因此,我们有特别的守则可以发现这一点,并赶上或缩小范围,但这符合基本的想法。 而且,直到最近,我们才实际使用浮动点号码,但固定点微观秒。

你们都必须做的是“投入的锁定”......并且你获得zero。 CPU的使用。 直到事情发生为止。

简言之,任何袖珍计划都将有类似之处:

  while (true) {
    select ()
    ...

您可以轻而易地等待“getchar()”或“

人们可以认为,“I/O的锁”实际上是optimal设计战略。 它是“污染”的替代物之一(Polling is Evil)。

IMHO。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...