]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_main.c
common: Wrap ContainerOf around parentheses to allow direct struct access
[xonotic/darkplaces.git] / cl_main.c
index 3026f669f72a2de847c1113ce3c7fe7beeed03f1..73fb69861d9e76cf193cfa97d3b7a348ebb8a62f 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -95,6 +95,18 @@ cvar_t cl_deathnoviewmodel = {CVAR_CLIENT, "cl_deathnoviewmodel", "1", "hides gu
 cvar_t cl_locs_enable = {CVAR_CLIENT | CVAR_SAVE, "locs_enable", "1", "enables replacement of certain % codes in chat messages: %l (location), %d (last death location), %h (health), %a (armor), %x (rockets), %c (cells), %r (rocket launcher status), %p (powerup status), %w (weapon status), %t (current time in level)"};
 cvar_t cl_locs_show = {CVAR_CLIENT, "locs_show", "0", "shows defined locations for editing purposes"};
 
+cvar_t cl_minfps = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps", "40", "minimum fps target - while the rendering performance is below this, it will drift toward lower quality"};
+cvar_t cl_minfps_fade = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_fade", "1", "how fast the quality adapts to varying framerate"};
+cvar_t cl_minfps_qualitymax = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualitymax", "1", "highest allowed drawdistance multiplier"};
+cvar_t cl_minfps_qualitymin = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualitymin", "0.25", "lowest allowed drawdistance multiplier"};
+cvar_t cl_minfps_qualitymultiply = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualitymultiply", "0.2", "multiplier for quality changes in quality change per second render time (1 assumes linearity of quality and render time)"};
+cvar_t cl_minfps_qualityhysteresis = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualityhysteresis", "0.05", "reduce all quality increments by this to reduce flickering"};
+cvar_t cl_minfps_qualitystepmax = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualitystepmax", "0.1", "maximum quality change in a single frame"};
+cvar_t cl_minfps_force = {CVAR_CLIENT, "cl_minfps_force", "0", "also apply quality reductions in timedemo/capturevideo"};
+cvar_t cl_maxfps = {CVAR_CLIENT | CVAR_SAVE, "cl_maxfps", "0", "maximum fps cap, 0 = unlimited, if game is running faster than this it will wait before running another frame (useful to make cpu time available to other programs)"};
+cvar_t cl_maxfps_alwayssleep = {CVAR_CLIENT | CVAR_SAVE, "cl_maxfps_alwayssleep","1", "gives up some processing time to other applications each frame, value in milliseconds, disabled if cl_maxfps is 0"};
+cvar_t cl_maxidlefps = {CVAR_CLIENT | CVAR_SAVE, "cl_maxidlefps", "20", "maximum fps cap when the game is not the active window (makes cpu time available to other programs"};
+
 client_static_t        cls;
 client_state_t cl;
 
@@ -343,7 +355,7 @@ void CL_Disconnect(void)
        if (cls.state == ca_dedicated)
                return;
 
-       if (COM_CheckParm("-profilegameonly"))
+       if (Sys_CheckParm("-profilegameonly"))
                Sys_AllowProfiling(false);
 
        Curl_Clear_forthismap();
@@ -403,7 +415,7 @@ void CL_Disconnect(void)
        cls.state = ca_disconnected;
        cl.islocalgame = false;
 
-       cls.demoplayback = cls.timedemo = false;
+       cls.demoplayback = cls.timedemo = host.restless = false;
        cls.signon = 0;
 
        // If we're dropped mid-connection attempt, it won't clear otherwise.
@@ -509,7 +521,7 @@ void CL_EstablishConnection(const char *address, int firstarg)
                return;
 
        // don't connect to a server if we're benchmarking a demo
-       if (COM_CheckParm("-benchmark"))
+       if (Sys_CheckParm("-benchmark"))
                return;
 
        // clear menu's connect error message
@@ -557,6 +569,11 @@ void CL_EstablishConnection(const char *address, int firstarg)
        }
 }
 
+static void CL_EstablishConnection_Local(void)
+{
+       CL_EstablishConnection("local:1", -2);
+}
+
 /*
 ==============
 CL_PrintEntities_f
@@ -749,11 +766,11 @@ entity_render_t *CL_NewTempEntity(double shadertime)
        return render;
 }
 
-void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
+void CL_Effect(vec3_t org, dp_model_t *model, int startframe, int framecount, float framerate)
 {
        int i;
        cl_effect_t *e;
-       if (!modelindex) // sanity check
+       if (!model) // sanity check
                return;
        if (framerate < 1)
        {
@@ -771,7 +788,7 @@ void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float
                        continue;
                e->active = true;
                VectorCopy(org, e->origin);
-               e->modelindex = modelindex;
+               e->model = model;
                e->starttime = cl.time;
                e->startframe = startframe;
                e->endframe = startframe + framecount;
@@ -1771,7 +1788,7 @@ static void CL_RelinkEffects(void)
                                }
 
                                // normal stuff
-                               entrender->model = CL_GetModelByIndex(e->modelindex);
+                               entrender->model = e->model;
                                entrender->alpha = 1;
                                VectorSet(entrender->colormod, 1, 1, 1);
                                VectorSet(entrender->glowmod, 1, 1, 1);
@@ -2698,6 +2715,159 @@ void CL_UpdateEntityShading(void)
                CL_UpdateEntityShading_Entity(r_refdef.scene.entities[i]);
 }
 
+extern cvar_t host_framerate;
+extern cvar_t host_speeds;
+
+double CL_Frame (double time)
+{
+       static double clframetime;
+       static double cl_timer = 0;
+       static double time1 = 0, time2 = 0, time3 = 0;
+       static double wait;
+       int pass1, pass2, pass3;
+
+       CL_VM_PreventInformationLeaks();
+
+       // get new key events
+       Key_EventQueue_Unblock();
+       SndSys_SendKeyEvents();
+       Sys_SendKeyEvents();
+
+       if((cl_timer += time) < 0)
+               return cl_timer;
+
+       // limit the frametime steps to no more than 100ms each
+       if (cl_timer > 0.1)
+               cl_timer = 0.1;
+
+       if (cls.state != ca_dedicated && (cl_timer > 0 || cls.timedemo || ((vid_activewindow ? cl_maxfps : cl_maxidlefps).value < 1)))
+       {
+               R_TimeReport("---");
+               Collision_Cache_NewFrame();
+               R_TimeReport("photoncache");
+#ifdef CONFIG_VIDEO_CAPTURE
+               // decide the simulation time
+               if (cls.capturevideo.active)
+               {
+                       //***
+                       if (cls.capturevideo.realtime)
+                               clframetime = cl.realframetime = max(time, 1.0 / cls.capturevideo.framerate);
+                       else
+                       {
+                               clframetime = 1.0 / cls.capturevideo.framerate;
+                               cl.realframetime = max(time, clframetime);
+                       }
+               }
+               else if (vid_activewindow && cl_maxfps.value >= 1 && !cls.timedemo)
+
+#else
+               if (vid_activewindow && cl_maxfps.value >= 1 && !cls.timedemo)
+#endif
+               {
+                       clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxfps.value);
+                       // when running slow, we need to sleep to keep input responsive
+                       wait = bound(0, cl_maxfps_alwayssleep.value * 1000, 100000);
+                       if (wait > 0)
+                               Sys_Sleep((int)wait);
+               }
+               else if (!vid_activewindow && cl_maxidlefps.value >= 1 && !cls.timedemo)
+                       clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxidlefps.value);
+               else
+                       clframetime = cl.realframetime = cl_timer;
+
+               // apply slowmo scaling
+               clframetime *= cl.movevars_timescale;
+               // scale playback speed of demos by slowmo cvar
+               if (cls.demoplayback)
+               {
+                       clframetime *= host_timescale.value;
+                       // if demo playback is paused, don't advance time at all
+                       if (cls.demopaused)
+                               clframetime = 0;
+               }
+               else
+               {
+                       // host_framerate overrides all else
+                       if (host_framerate.value)
+                               clframetime = host_framerate.value;
+
+                       if (cl.paused || host.paused)
+                               clframetime = 0;
+               }
+
+               if (cls.timedemo)
+                       clframetime = cl.realframetime = cl_timer;
+
+               // deduct the frame time from the accumulator
+               cl_timer -= cl.realframetime;
+
+               cl.oldtime = cl.time;
+               cl.time += clframetime;
+
+               // update video
+               if (host_speeds.integer)
+                       time1 = Sys_DirtyTime();
+               R_TimeReport("pre-input");
+
+               // Collect input into cmd
+               CL_Input();
+
+               R_TimeReport("input");
+
+               // check for new packets
+               NetConn_ClientFrame();
+
+               // read a new frame from a demo if needed
+               CL_ReadDemoMessage();
+               R_TimeReport("clientnetwork");
+
+               // now that packets have been read, send input to server
+               CL_SendMove();
+               R_TimeReport("sendmove");
+
+               // update client world (interpolate entities, create trails, etc)
+               CL_UpdateWorld();
+               R_TimeReport("lerpworld");
+
+               CL_Video_Frame();
+
+               R_TimeReport("client");
+
+               CL_UpdateScreen();
+               R_TimeReport("render");
+
+               if (host_speeds.integer)
+                       time2 = Sys_DirtyTime();
+
+               // update audio
+               if(cl.csqc_usecsqclistener)
+               {
+                       S_Update(&cl.csqc_listenermatrix);
+                       cl.csqc_usecsqclistener = false;
+               }
+               else
+                       S_Update(&r_refdef.view.matrix);
+
+               CDAudio_Update();
+               R_TimeReport("audio");
+
+               // reset gathering of mouse input
+               in_mouse_x = in_mouse_y = 0;
+
+               if (host_speeds.integer)
+               {
+                       pass1 = (int)((time1 - time3)*1000000);
+                       time3 = Sys_DirtyTime();
+                       pass2 = (int)((time2 - time1)*1000000);
+                       pass3 = (int)((time3 - time2)*1000000);
+                       Con_Printf("%6ius total %6ius server %6ius gfx %6ius snd\n",
+                                               pass1+pass2+pass3, pass1, pass2, pass3);
+               }
+       }
+       // if there is some time remaining from this frame, reset the timer
+       return cl_timer >= 0 ? 0 : cl_timer;
+}
+
 /*
 ===========
 CL_Shutdown
@@ -2705,11 +2875,34 @@ CL_Shutdown
 */
 void CL_Shutdown (void)
 {
+       // be quiet while shutting down
+       S_StopAllSounds();
+       
+       // disconnect client from server if active
+       CL_Disconnect();
+       
+       CL_Video_Shutdown();
+
+#ifdef CONFIG_MENU
+       // Shutdown menu
+       if(MR_Shutdown)
+               MR_Shutdown();
+#endif
+
+       CDAudio_Shutdown ();
+       S_Terminate ();
+       
+       R_Modules_Shutdown();
+       VID_Shutdown();
+
        CL_Screen_Shutdown();
        CL_Particles_Shutdown();
        CL_Parse_Shutdown();
        CL_MeshEntities_Shutdown();
 
+       Key_Shutdown();
+       S_Shutdown();
+
        Mem_FreePool (&cls.permanentmempool);
        Mem_FreePool (&cls.levelmempool);
 }
@@ -2727,7 +2920,7 @@ void CL_Init (void)
        }
        else
        {
-               Con_DPrintf("Initializing client\n");
+               Con_Printf("Initializing client\n");
 
                R_Modules_Init();
                Palette_Init();
@@ -2740,7 +2933,7 @@ void CL_Init (void)
                S_Init();
                CDAudio_Init();
                Key_Init();
-
+               V_Init();
 
                cls.levelmempool = Mem_AllocPool("client (per-level memory)", 0, NULL);
                cls.permanentmempool = Mem_AllocPool("client (long term memory)", 0, NULL);
@@ -2759,6 +2952,8 @@ void CL_Init (void)
        //
        // register our commands
        //
+               CL_InitCommands();
+
                Cvar_RegisterVariable (&cl_upspeed);
                Cvar_RegisterVariable (&cl_forwardspeed);
                Cvar_RegisterVariable (&cl_backspeed);
@@ -2840,6 +3035,18 @@ void CL_Init (void)
 
                Cvar_RegisterVariable(&csqc_polygons_defaultmaterial_nocullface);
 
+               Cvar_RegisterVariable (&cl_minfps);
+               Cvar_RegisterVariable (&cl_minfps_fade);
+               Cvar_RegisterVariable (&cl_minfps_qualitymax);
+               Cvar_RegisterVariable (&cl_minfps_qualitymin);
+               Cvar_RegisterVariable (&cl_minfps_qualitystepmax);
+               Cvar_RegisterVariable (&cl_minfps_qualityhysteresis);
+               Cvar_RegisterVariable (&cl_minfps_qualitymultiply);
+               Cvar_RegisterVariable (&cl_minfps_force);
+               Cvar_RegisterVariable (&cl_maxfps);
+               Cvar_RegisterVariable (&cl_maxfps_alwayssleep);
+               Cvar_RegisterVariable (&cl_maxidlefps);
+
                CL_Parse_Init();
                CL_Particles_Init();
                CL_Screen_Init();
@@ -2847,6 +3054,8 @@ void CL_Init (void)
 
                CL_Video_Init();
 
+               host.hook.ConnectLocal = CL_EstablishConnection_Local;
+
                #ifdef CONFIG_MENU
                Cbuf_InsertText(&cmd_client,"menu_start\n");
                #endif