]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/engineinfo.qc
db376a2ce859b389541b763cbcfc1177d448e9fa
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / engineinfo.qc
1 #include "engineinfo.qh"
2
3 #include <client/autocvars.qh>
4 #include <client/miscfunctions.qh>
5
6 // Engine info (#13)
7
8 void HUD_EngineInfo_Export(entity panel, int fh)
9 {
10         // allow saving cvars that aesthetically change the panel into hud skin files
11         HUD_Write_Cvar("hud_panel_engineinfo_framecounter_time");
12         HUD_Write_Cvar("hud_panel_engineinfo_framecounter_decimals");
13 }
14
15 float prevfps;
16 float prevfps_time;
17 int framecounter;
18
19 float frametimeavg;
20 float frametimeavg1; // 1 frame ago
21 float frametimeavg2; // 2 frames ago
22 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage;
23 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight;
24 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold;
25 void HUD_EngineInfo()
26 {
27         if(!autocvar__hud_configure)
28         {
29                 if(!autocvar_hud_panel_engineinfo) return;
30         }
31
32         HUD_Panel_LoadCvars();
33         vector pos, mySize;
34         pos = panel_pos;
35         mySize = panel_size;
36
37         if (autocvar_hud_panel_engineinfo_dynamichud)
38                 HUD_Scale_Enable();
39         else
40                 HUD_Scale_Disable();
41         HUD_Panel_DrawBg();
42         if(panel_bg_padding)
43         {
44                 pos += '1 1 0' * panel_bg_padding;
45                 mySize -= '2 2 0' * panel_bg_padding;
46         }
47
48         float currentTime = gettime(GETTIME_REALTIME);
49         if(autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage)
50         {
51                 float currentframetime = currentTime - prevfps_time;
52                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
53                 frametimeavg2 = frametimeavg1;
54                 frametimeavg1 = frametimeavg;
55
56                 float weight;
57                 weight = autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight;
58                 if(currentframetime > 0.0001) // filter out insane values which sometimes seem to occur and throw off the average? If you are getting 10,000 fps or more, then you don't need a framerate counter.
59                 {
60                         if(fabs(prevfps - (1/frametimeavg)) > prevfps * autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold) // if there was a big jump in fps, just force prevfps at current (1/currentframetime) to make big updates instant
61                                 prevfps = (1/currentframetime);
62                         prevfps = (1 - weight) * prevfps + weight * (1/frametimeavg); // framecounter just used so there's no need for a new variable, think of it as "frametime average"
63                 }
64                 prevfps_time = currentTime;
65         }
66         else
67         {
68                 framecounter += 1;
69                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
70                 {
71                         prevfps = framecounter/(currentTime - prevfps_time);
72                         framecounter = 0;
73                         prevfps_time = currentTime;
74                 }
75         }
76
77         vector color;
78         color = HUD_Get_Num_Color (prevfps, 100);
79         drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
80 }