博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
固定frame rate的windows-event-loop
阅读量:2340 次
发布时间:2019-05-10

本文共 1553 字,大约阅读时间需要 5 分钟。

Link:

 

They typical Windows event loop in a game or tool may look something like:

while( running ) {    if( PeekMessage( ... ) ) {      TranslateMessage( ... );        DispatchMessage( ... );    }    Render();  }
Bad Code

There are at least two problems with that:

1) You're calling PeekMessage(), which immediately returns, so the loop will just spin. GetMessage() will wait until there's a message available (and yield the CPU).

2) You're calling Render() once per windows message. This is really bad, as certain windows transactions take many messages. Your look should look something like:

HANDLE h = (HANDLE)CreateEvent( 0, false, false, 0 );  while( running ) {    while( PeekMessage( ... ) ) {      TranslateMessage( ... );      DispatchMessage( ... );    }    timeout = CalculateNextFrameTimeout();    if( timeout <= 0 ) { // time to render?      Render();      timeout = CalculateNextFrameTimeout();    }    // use an alertable wait    MsgWaitForMultipleObjectsEx( 1, &h, timeout, QS_ALLEVENTS,         MWMO_ALERTABE|MWMO_INPUTAVAILABLE );    }  CloseHandle( h );
Good Code

This code assumes that Render() does something that makes the next call to CalculateNextFrameTimeout() return greater than 0 for a while :-)

This code will keep the frame rate limited, and give up any CPU not needed. It will efficiently dispatch cascades of Windows events. And, most importantly, it will immediately wake up and dispatch any events generated by the user (such as from mousemove, keydown, etc) because it uses an alterable sleep. Sleep() is not alertable.

 

转载地址:http://fdzvb.baihongyu.com/

你可能感兴趣的文章
周总结——第一周(9月5号到9月12)
查看>>
2017招商银行笔试01
查看>>
坦克项目总结
查看>>
设计模式之——单例模式
查看>>
ArrayList、Linkedlist和Vector
查看>>
数据库常用
查看>>
简单的学生信息管理系统
查看>>
条理性搭建SSH框架
查看>>
整合Struts和Spring
查看>>
Hibernate和Spring的整合
查看>>
我的校招——同花顺
查看>>
Ego Surfing = Ego + Surfing
查看>>
13日cnblog会谈摘要
查看>>
尝试解决MT的Add to My Yahoo!的字符集问题
查看>>
MoreGoogle提供的网页缩略图服务
查看>>
每天到REFERER到我的网站上来的主页上去溜达一下
查看>>
北京羽毛球场地预定电话
查看>>
本周CNBlog例会:Grassland搜索的后台迁移
查看>>
Flickr的网络收藏夹服务
查看>>
用sed批量替换文件中的字符
查看>>