查看: 8909|回复: 6

[原创发布] 自定义FPS+修复高刷新率显示器的BUG

[复制链接]

Mr.Gandum

梦石
0
星屑
226
在线时间
2070 小时
注册时间
2007-1-31
回帖
2982

贵宾

发表于 2015-10-26 22:47:22 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 feizhaodan 于 2015-11-24 19:35 编辑

MV使用requestAnimationFrame()来进行屏幕刷新,但是这个有一些特征不太适用于游戏。
这个插件添加一个简单的计时器来控制FPS,也提供了不使用计时器来解决这个问题的方法。
具体内容看插件的帮助栏内。
[pre lang="javascript" file="TimerRefreshRate.js"]//=============================================================================
// Alternative Screen Refresh Timer
// TimerRefreshRate.js
// Version: 1.02
// Changelog:         1.02:
//                                - Fix a bug that when Using Custom Timer the game not progress.
//                                1.01:
//                                - Fix Refresh Rate is not working at monitor with <60 Hz.
//=============================================================================
var Imported = Imported || {};
Imported.Kien_Alt_Screen_Refresh_Timer = true;

var Kien = Kien || {};
Kien.ASRT = {};
//=============================================================================
/*:
* @plugindesc Provide an alternative refresh rate process, or just fix the glitch
* when the game is played at monitor with higher refresh rate.
* @author Kien
*
* @param Use Alternative Timer
* @desc Use Alternative timer to solve the problem of refresh rate and background
* movements, but is not stable as default
* @default false

* @param Allow Backgronud
* @desc allow the game process when the tab/browser is not active. Only works
* when using alternative timer. Currently have no work
* @default true
*
* @param Custom FPS
* @desc use a refresh rate other than 60. don't chagne this unless you know
* what you are doing.
* @default 60
*
* @param Fix Refresh Rate
* @desc fix the problem with higher refresh rates. Only works when NOT using
* alternative timer.
* @default true
*
* @help
* ============================================================================
* Alternative Screen Refresh Timer (English Document)
* ============================================================================
*                 Provide an alternative refresh rate process, or just fix the glitch
* when the game is played at monitor with higher refresh rate.
* ============================================================================
* * About Refresh Rate
* ============================================================================
*                 In RPG Maker MV, the refresh rate is controlled by requestAnimationFrame()
* function provided by the browser. This function will automatically ensures
* the animation works in a proper rate, but there have some problem when we use
* this directly in a game.
* 1. Refresh Rate
*             Refresh rate of the monitor is not a fixed value, while we want our game
* have a fixed 60 fps. This will make the game have accelerated game speed in
* those monitor have a refresh ratemore than 60, and deccelerated game speed
* in those monitor have a refresh rate lower than 60.
*
* 2. No Background Proess
*             requestAnimationFrame() is used to show animation, so it will NOT give
* timing when the animation not need to show. That is, when the game is at
* a tab/browser that is not the active tab/browser, the game will not have any
* process.
* ============================================================================
* * What does this plugin do
* ============================================================================
*           This plugin will fix both problem by adding a timer like other games
* and (maybe) previous RPG Makers, or just solve the first problem by give
* pauses to default process. This can tweeks at Options provided by this
* plugin.
* ============================================================================
* * Note
* ============================================================================
*                 Make sure to put this plugin at top of the list, or maybe there have
* some problems. Also, it seems that setTimeout is not precise as
* requestAnimationFrame(), so in some time, this custom timer will not work.
* ============================================================================
* * End of Document (English Document)
* ============================================================================
* ============================================================================
* * 自作FPSタイマー(Japanese Document)
* ============================================================================
*   FPSタイマーを別に追加し、デフォルト状態の一部のバグを修復します。または、デフォルトで発生するリフレッシュ
* レートの高いモニターで発生するバグを修復します。
* ============================================================================
* * リフレッシュレートについて
* ============================================================================
*   RPGツクールMVでは、従来のタイマー式と推測されるFPS進行をブラウザーが提供する
* requestAnimationFrame()という関数で管理するようにしています。この関数は、ブラウザーが適切なタイミング
* でコールバックを呼び出すことで、アニメーションを正確に更新できるようにするためのものですが、ゲームとして使用する
* 場合、以下のような不都合が発生します:
* 1. リフレッシュレート
*   requestAnimationFrame()関数はモニターのリフレッシュレートに合わせるようにコールバックを呼び出し
* ますが、すべてのモニターが60FPSで更新されているわけではありません。そのため、60FPSとして想定されているRPG
* ツクールMV製のゲームをそれらのモニターで使用すると、ゲームが加速あるいは減速した状態で処理されてしまいます
* 。
* 2. バックグラウンド処理
*   requestAnimationFrame()はアニメーションを表示するために提供されている関数ですが、アニメーションを
* 表示する必要がない場合、つまりタブ・ブラウザーが非アクティブ時は全く処理されなくなるということです。
* ============================================================================
* * このプラグインについて
* ============================================================================
*   このプラグインは上記の問題を、自作のFPSタイマーでFPSを管理することで解消します。また、デフォルトのFPS
* 管理でいいという人のために、致命的な一番目の問題だけを解決する部分も提供します。
* ============================================================================
* * 注意
* ============================================================================
*   このプラグインは、可能な限りリストの一番上に来るようにしてください。また、タイマーとして用いている
* setTimeout() 関数はrequestAnimationFrame()ほど正確ではないため、動作環境によってはデフォルト
* タイマーのほうが優れている。
* ============================================================================
* * ドキュメント終了 (Japanese Document)
* ============================================================================
* ============================================================================
* * FPS时钟(Chinese Document)
* ============================================================================
*     提供另外一种FPS计算方法,或者只修复一个默认系统的BUG。
* ============================================================================
* * 关于刷新率
* ============================================================================
*     RPG Maker MV使用浏览器提供的requestAnimationFrame() 函数来控制游戏的FPS,而不是像
* 通常的游戏使用计时器来进行。虽然这个函数提供了稳定的FPS,但是会造成以下的两个问题:
* 1. 刷新率
*    requestAnimationFrame() 函数根据屏幕的刷新率来呼出回调函数。但是不是所有的屏幕都是
* 60Hz。在那些有不是60Hz刷新率的屏幕上执行MV的游戏会造成游戏速度变速,严重影响游戏体验。
* 2. 后台运行
*    requestAnimationFrame() 函数本来是用来绘制动画。当不需要绘制动画,即网页是在后台状
* 态时,requestAnimationFrame() 函数不会调用回调函数,令游戏无法后台运行。
* ============================================================================
* * 关于这个插件
* ============================================================================
*     本插件通过使用计时器来管理FPS,解决上面的两个问题。也提供不适用计时器的方法来解决比较
* 严重的第一个问题。另外,setTimeout()的精准度没有requestAnimationFrame()准确,因此根据
* 情况可能会造成更多延迟。
* ============================================================================
* * 注意
* ============================================================================
*     请将本插件放到插件列表的最上方以防止出现不可预料的冲突。
* ============================================================================
* * 文档结束 (Chinese Document)
* ============================================================================
*/

Kien.ASRT.parameters = PluginManager.parameters("TimerRefreshRate");
Kien.ASRT.useTimer = Kien.ASRT.parameters["Use Alternative Timer"];
Kien.ASRT.allowBackground = Kien.ASRT.parameters["Allow Backgronud"];
Kien.ASRT.fps = Kien.ASRT.parameters["Custom FPS"];
Kien.ASRT.fixFPS = Kien.ASRT.parameters["Fix Refresh Rate"];

Kien.ASRT.Timer = function(){
        this._fps = Kien.ASRT.fps;
        this._lastDate = Date.now();
        this._msToNext10 = 10000 / this._fps
        this._msPassed10 = 0;
}

Kien.ASRT.Timer.prototype.updateTimer = function(){
        var nowDate = Date.now();
        this._msPassed10 += (nowDate - this._lastDate) * 10;
        this._lastDate = nowDate;
}

Kien.ASRT.Timer.prototype.checkTimer = function(){
        if (this._msPassed10 > this._msToNext10){
                this._msPassed10 -= this._msToNext10;
                return true;
        }
        return false;
}

Kien.ASRT.SceneManager_requestUpdate = SceneManager.requestUpdate;
SceneManager.requestUpdate = function() {
        if (!Kien.ASRT.useTimer){
                Kien.ASRT.SceneManager_requestUpdate.call(this);
        } else {
                this._timer.updateTimer();
                while (this._timer.checkTimer()){
                        this.update();
                }
                setTimeout(this.requestUpdate.bind(this),1);
        }
};

Kien.ASRT.SceneManager_initialize = SceneManager.initialize;
SceneManager.initialize = function() {
        Kien.ASRT.SceneManager_initialize.call(this);
        this._timer = new Kien.ASRT.Timer();
};


if(Kien.ASRT.fixFPS && !Kien.ASRT.useTimer){
        Kien.ASRT.SceneManager_update = SceneManager.update;
        SceneManager.update = function() {
                this._timer.updateTimer()
                while (this._timer.checkTimer()){
                        Kien.ASRT.SceneManager_update.call(this);
                }
        }
};[/pre]
注意关于高刷新率显示器的部分是未测试,因为我没有高刷新率的显示器囧

评分

参与人数 1星屑 +66 梦石 +2 收起 理由
余烬之中 + 66 + 2 图书馆收录

查看全部评分

梦石
0
星屑
664
在线时间
466 小时
注册时间
2009-9-29
回帖
54

开拓者

发表于 2015-10-26 23:15:07 | 显示全部楼层
救命稻草!!!昨天还在纠结这个,今天就有了现成的肉可以拿!!大感谢!!
回复

使用道具 举报

梦石
5
星屑
3061
在线时间
613 小时
注册时间
2012-11-12
回帖
449

开拓者

发表于 2015-10-27 00:02:22 | 显示全部楼层
官方好像也发现这个bug了,今天更新了补丁
回复

使用道具 举报

头像被屏蔽
梦石
0
星屑
88
在线时间
631 小时
注册时间
2014-8-4
回帖
3243
发表于 2015-10-27 12:51:11 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

永夜蟄居の玖瀨

梦石
0
星屑
71
在线时间
1018 小时
注册时间
2011-9-5
回帖
2707

开拓者贵宾

发表于 2015-10-27 16:36:08 | 显示全部楼层
三国语言大触(

回复

使用道具 举报

梦石
0
星屑
950
在线时间
4 小时
注册时间
2015-10-26
回帖
3
发表于 2015-10-27 17:46:35 | 显示全部楼层
不可思议 原来的帧数过低解决了
回复

使用道具 举报

梦石
0
星屑
50
在线时间
42 小时
注册时间
2017-6-8
回帖
27
发表于 2017-7-24 15:15:20 | 显示全部楼层
鸟猫 发表于 2015-10-27 00:02
官方好像也发现这个bug了,今天更新了补丁

也就是说现在不管屏幕刷新率是多少,游戏帧率都是60吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部