Merge branch 'terencehill/spectate_player' into 'master'
[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 float prevfps;
9 float prevfps_time;
10 int framecounter;
11
12 float frametimeavg;
13 float frametimeavg1; // 1 frame ago
14 float frametimeavg2; // 2 frames ago
15 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage;
16 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight;
17 float autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_instantupdate_change_threshold;
18 void HUD_EngineInfo()
19 {
20         if(!autocvar__hud_configure)
21         {
22                 if(!autocvar_hud_panel_engineinfo) return;
23         }
24
25         HUD_Panel_LoadCvars();
26         vector pos, mySize;
27         pos = panel_pos;
28         mySize = panel_size;
29
30         if (autocvar_hud_panel_engineinfo_dynamichud)
31                 HUD_Scale_Enable();
32         else
33                 HUD_Scale_Disable();
34         HUD_Panel_DrawBg();
35         if(panel_bg_padding)
36         {
37                 pos += '1 1 0' * panel_bg_padding;
38                 mySize -= '2 2 0' * panel_bg_padding;
39         }
40
41         float currentTime = gettime(GETTIME_REALTIME);
42         if(autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage)
43         {
44                 float currentframetime = currentTime - prevfps_time;
45                 frametimeavg = (frametimeavg + frametimeavg1 + frametimeavg2 + currentframetime)/4; // average three frametimes into framecounter for slightly more stable fps readings :P
46                 frametimeavg2 = frametimeavg1;
47                 frametimeavg1 = frametimeavg;
48
49                 float weight;
50                 weight = autocvar_hud_panel_engineinfo_framecounter_exponentialmovingaverage_new_weight;
51                 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.
52                 {
53                         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
54                                 prevfps = (1/currentframetime);
55                         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"
56                 }
57                 prevfps_time = currentTime;
58         }
59         else
60         {
61                 framecounter += 1;
62                 if(currentTime - prevfps_time > autocvar_hud_panel_engineinfo_framecounter_time)
63                 {
64                         prevfps = framecounter/(currentTime - prevfps_time);
65                         framecounter = 0;
66                         prevfps_time = currentTime;
67                 }
68         }
69
70         vector color;
71         color = HUD_Get_Num_Color (prevfps, 100);
72         drawstring_aspect(pos, sprintf(_("FPS: %.*f"), autocvar_hud_panel_engineinfo_framecounter_decimals, prevfps), mySize, color, panel_fg_alpha, DRAWFLAG_NORMAL);
73 }