Advertisement
Dekita

Accurate FPS

Oct 22nd, 2014
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.56 KB | None | 0 0
  1. if true # << Make false to disable script, true to enable.
  2. #===============================================================================
  3. #
  4. # ☆ Dekita - Accurate FPS
  5. # -- Author : Dekita
  6. # -- Version : 1.0
  7. # -- Level : Very Easy
  8. # -- Requires : N/A
  9. # -- Engine : RPG Maker VX Ace.
  10. #
  11. #===============================================================================
  12. # ☆ Import
  13. #-------------------------------------------------------------------------------
  14. ($DEK||={})[:Real_FPS] = 1.0
  15. #===============================================================================
  16. # ☆ Updates
  17. #-------------------------------------------------------------------------------
  18. # D /M /Y
  19. # 22/1o/2o14 - Started, Finished,
  20. #===============================================================================
  21. # ☆ Introduction
  22. #-------------------------------------------------------------------------------
  23. # This script was written by DekitaRPG.
  24. #
  25. # The purpose of this script is to give the developer an accurate representation
  26. # of your frames per second.
  27. #
  28. # The default FPS counter (seen by pressing F2 in game) can be wildly inaccurate.
  29. #
  30. # IMO - the purpose of a frames per second display, is to show how many times
  31. # your frames are being refreshed each second.
  32. # For some reason, the F2 FPS counter is quite different from the actual
  33. # amount of updates the Graphics module processes each second.
  34. #
  35. # Simply pop the script in your game in a new script page and a small hud will
  36. # be displayed. The hud settings can be configured slightly in the Real_FPS
  37. # customization module.
  38. #
  39. # Along with showing you the real FPS rate, this script gives you an average
  40. # FPS rate and a total target rate, target rate shows you the percentage of
  41. # which your games FPS has been equal to the target FPS since the last average
  42. # FPS refresh occurred..
  43. #
  44. #===============================================================================
  45. # ★☆★☆★☆★☆★☆★☆★☆★ TERMS AND CONDITIONS: ☆★☆★☆★☆★☆★☆★☆★☆★☆
  46. #===============================================================================
  47. # Please give credit to Dekita (or DekitaRPG). (requested, not required.)
  48. # Free to use for all projects (commercial / non commercial).
  49. #
  50. # Please do not repost this script on other sites - if you wish, you can
  51. # provide a link to the webpage below;
  52. # http://dekitarpg.wordpress.com/2014/10/22/accurate-fps/
  53. #
  54. #===============================================================================
  55. # ☆ Instructions
  56. #-------------------------------------------------------------------------------
  57. # Place Below " ▼ Materials " and Above " ▼ Main " in your script editor.
  58. #
  59. #===============================================================================
  60. # ☆ Script Calls
  61. #-------------------------------------------------------------------------------
  62. # Graphics.fps
  63. #--------------
  64. # This script call will return the value of the current fps.
  65. #
  66. #-------------------------------------------------------------------------------
  67. # Graphics.fps_average
  68. #----------------------
  69. # This script call will return a value equal to the average fps your game has
  70. # had since it began running.
  71. #
  72. #-------------------------------------------------------------------------------
  73. # Graphics.fps_target_rate
  74. #--------------------------
  75. # This script call will return a value equal to the percentage of time FPS
  76. # rate has been equal to the target FPS rate.
  77. #
  78. #===============================================================================
  79. # www.dekyde.com
  80. # www.dekitarpg.wordpress.com
  81. #===============================================================================
  82. module Real_FPS
  83. #===============================================================================
  84. #-----------------------------------------------------------------------------
  85. # // HUD Positioning
  86. #-----------------------------------------------------------------------------
  87. HUD_POS = { x: 4, y: 4, w: 90, h: 70 }
  88. #-----------------------------------------------------------------------------
  89. # // Draw FPS HUD Fade ?
  90. # Format = [ draw? , color a, color b]
  91. #-----------------------------------------------------------------------------
  92. Draw_Fade = [ true, Color.new(0,0,0,0), Color.new(0,0,0,128) ]
  93. #-----------------------------------------------------------------------------
  94. # // Seconds before Average resets.
  95. # format = [ use?, max_size ]
  96. # This is to ensure that the average array does not become excessive during
  97. # long gaming periods as this could cause additional processing times.
  98. # The data array is simply reduced by its furthest data entry if the array
  99. # size is above max_size. This only happens is use? = true.
  100. # NOTE: the array size would increase by 60 each minute of real time.
  101. # Therefore, if max_size is 60, the average counter will display the last
  102. # 1 minute of gameplays average FPS rate.
  103. #-----------------------------------------------------------------------------
  104. Average_Refresh = [ true , 300 ]
  105. #-----------------------------------------------------------------------------
  106. # // When HUD_Switch is not 0, the vaues will be used as the id of a
  107. # $game_switch to be used to determine wether to show the hud or not.
  108. #-----------------------------------------------------------------------------
  109. HUD_Switch = 0
  110. #-----------------------------------------------------------------------------
  111. # // Real_FPS.hud - Used to draw fps onto the fps hud (on screen).
  112. #-----------------------------------------------------------------------------
  113. def self.hud(hud,target_fps,fps,fps_average,fps_trg_rate)
  114. # // Shows Target FPS (generally 60)
  115. textA = "TRG: #{target_fps}"
  116. # // Shows Current FPS
  117. textB = "CUR: #{fps}"
  118. # // Shows Average FPS
  119. textC = "AVG: #{fps_average}"
  120. # // FPS Target Accuracy
  121. textD = "TOT: #{sprintf("%1.0f%",fps_trg_rate)}"
  122. # // The code below simply draws the text defined above onto the hud.
  123. hud.font.size = 16
  124. hud.draw_text(18,HUD_POS[:h]/4*0,HUD_POS[:w],HUD_POS[:h]/4,textA,0)
  125. hud.draw_text(18,HUD_POS[:h]/4*1,HUD_POS[:w],HUD_POS[:h]/4,textB,0)
  126. hud.draw_text(18,HUD_POS[:h]/4*2,HUD_POS[:w],HUD_POS[:h]/4,textC,0)
  127. hud.draw_text(18,HUD_POS[:h]/4*3,HUD_POS[:w],HUD_POS[:h]/4,textD,0)
  128. end
  129. #-----------------------------------------------------------------------------
  130. # [ end module ]
  131. #-----------------------------------------------------------------------------
  132. end
  133. #===============================================================================
  134. # // Customization Section Has Ceased.
  135. # // Only edit the code below if you are able to comprehend its processing.
  136. # // This is a precaution to ensure your sanity remains intact. :)
  137. #===============================================================================
  138. # www.dekyde.com
  139. # www.dekitarpg.wordpress.com
  140. #===============================================================================
  141. module Graphics
  142. #===============================================================================
  143. #-----------------------------------------------------------------------------
  144. # Alias List
  145. #-----------------------------------------------------------------------------
  146. class << self
  147. alias :update_realfps :update
  148. end
  149. #-----------------------------------------------------------------------------
  150. # // Graphics.update
  151. #-----------------------------------------------------------------------------
  152. def update
  153. update_realfps
  154. update_fps
  155. end
  156. #-----------------------------------------------------------------------------
  157. # // Important Variables
  158. #-----------------------------------------------------------------------------
  159. @@fps_tsec = Time.new.sec
  160. @@fps_tsol = @@fps_tsec
  161. @@fps_insc = @@fps = 0
  162. @@fps_aver = Array.new << frame_rate
  163. #-----------------------------------------------------------------------------
  164. # // [ new ] Graphics.fps
  165. #-----------------------------------------------------------------------------
  166. def update_fps
  167. @@fps_insc += 1
  168. @@fps_tsec = Time.new.sec
  169. return unless @@fps_tsec != @@fps_tsol
  170. @@fps_tsol = @@fps_tsec
  171. @@fps = @@fps_insc
  172. update_average
  173. @@fps_insc = 0
  174. end
  175. #-----------------------------------------------------------------------------
  176. # // [ new ] Graphics.update_average
  177. #-----------------------------------------------------------------------------
  178. def update_average
  179. av = Real_FPS::Average_Refresh
  180. if av[0] && @@fps_aver.size > av[1]
  181. @@fps_aver.clear
  182. end
  183. @@fps_aver << @@fps
  184. end
  185. #-----------------------------------------------------------------------------
  186. # // [ new ] Graphics.fps_average
  187. #-----------------------------------------------------------------------------
  188. def fps_average
  189. @@fps_aver.inject(0) {|r,i| r += i } / @@fps_aver.size
  190. end
  191. #-----------------------------------------------------------------------------
  192. # // [ new ] Graphics.fps_target_rate
  193. #-----------------------------------------------------------------------------
  194. def fps_target_rate
  195. (fps_average.to_f / frame_rate) * 100
  196. end
  197. #-----------------------------------------------------------------------------
  198. # // [ new ] Graphics.fps
  199. #-----------------------------------------------------------------------------
  200. def fps; @@fps; end
  201. #-----------------------------------------------------------------------------
  202. # // Module Functions
  203. #-----------------------------------------------------------------------------
  204. module_function :update
  205. module_function :update_fps
  206. module_function :update_average
  207. module_function :fps_average
  208. module_function :fps_target_rate
  209. module_function :fps
  210. #-----------------------------------------------------------------------------
  211. # [ end module ]
  212. #-----------------------------------------------------------------------------
  213. end
  214. #===============================================================================
  215. class FPS_HUD
  216. #===============================================================================
  217. #-----------------------------------------------------------------------------
  218. # //
  219. #-----------------------------------------------------------------------------
  220. def initialize(viewport)
  221. @viewport = viewport
  222. init_headsupdisplay
  223. end
  224. #-----------------------------------------------------------------------------
  225. # //
  226. #-----------------------------------------------------------------------------
  227. def init_headsupdisplay
  228. dispose
  229. @last_fps = Graphics.fps
  230. @target_fps = Graphics.frame_rate
  231. @main = Sprite.new(@viewport)
  232. @main.bitmap = Bitmap.new(Real_FPS::HUD_POS[:w],Real_FPS::HUD_POS[:h])
  233. @main.x = Real_FPS::HUD_POS[:x]
  234. @main.y = Real_FPS::HUD_POS[:y]
  235. @main.z = 201
  236. update
  237. end
  238. #-----------------------------------------------------------------------------
  239. # //
  240. #-----------------------------------------------------------------------------
  241. def update
  242. return unless @main
  243. if Real_FPS::HUD_Switch != 0
  244. @main.visible = $game_switches[Real_FPS::HUD_Switch]
  245. end
  246. return unless @main.visible
  247. new_fps = Graphics.fps
  248. new_ave = Graphics.fps_average
  249. can_fps = @last_fps != new_fps
  250. can_ave = @last_ave != new_ave
  251. return unless can_fps || can_ave
  252. @main.bitmap.clear
  253. if Real_FPS::Draw_Fade[0]
  254. w = Real_FPS::HUD_POS[:w]/4
  255. h = Real_FPS::HUD_POS[:h]
  256. ca= Real_FPS::Draw_Fade[1]
  257. cb= Real_FPS::Draw_Fade[2]
  258. @main.bitmap.gradient_fill_rect(0,0,w,h,ca,cb)
  259. @main.bitmap.fill_rect(w,0,w*2,h,cb)
  260. @main.bitmap.gradient_fill_rect(w*3,0,w,h,cb,ca)
  261. end
  262. new_rte = Graphics.fps_target_rate
  263. Real_FPS.hud(@main.bitmap,@target_fps,new_fps,new_ave,new_rte)
  264. @last_fps = new_fps
  265. @last_ave = new_ave
  266. end
  267. #-----------------------------------------------------------------------------
  268. # //
  269. #-----------------------------------------------------------------------------
  270. def dispose
  271. return unless @main
  272. @main.bitmap.dispose
  273. @main.dispose
  274. end
  275. #-----------------------------------------------------------------------------
  276. #
  277. #-----------------------------------------------------------------------------
  278. end
  279. #===============================================================================
  280. class Scene_Base
  281. #===============================================================================
  282. #-----------------------------------------------------------------------------
  283. # Aliased Methods
  284. #-----------------------------------------------------------------------------
  285. alias :init_fpshud :start
  286. alias :disp_fpshud :terminate
  287. alias :updt_fpshud :update
  288. #-----------------------------------------------------------------------------
  289. # //
  290. #-----------------------------------------------------------------------------
  291. def start
  292. init_fpshud
  293. strt_fps
  294. end
  295. #-----------------------------------------------------------------------------
  296. # //
  297. #-----------------------------------------------------------------------------
  298. def strt_fps
  299. @fps_hud = FPS_HUD.new(@viewport)
  300. end
  301. #-----------------------------------------------------------------------------
  302. # //
  303. #-----------------------------------------------------------------------------
  304. def terminate
  305. term_fps
  306. disp_fpshud
  307. end
  308. #-----------------------------------------------------------------------------
  309. # //
  310. #-----------------------------------------------------------------------------
  311. def term_fps
  312. return unless @fps_hud
  313. @fps_hud.dispose
  314. end
  315. #-----------------------------------------------------------------------------
  316. # //
  317. #-----------------------------------------------------------------------------
  318. def update
  319. updt_fps
  320. updt_fpshud
  321. end
  322. #-----------------------------------------------------------------------------
  323. # //
  324. #-----------------------------------------------------------------------------
  325. def updt_fps
  326. return unless @fps_hud
  327. @fps_hud.update
  328. end
  329. #-----------------------------------------------------------------------------
  330. # [ end class ]
  331. #-----------------------------------------------------------------------------
  332. end
  333. #===============================================================================
  334. # www.dekyde.com
  335. # www.dekitarpg.wordpress.com
  336. #===============================================================================
  337. end # if false / true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement