SiPlus

WebQuake SCR.js

Dec 10th, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var SCR = {};
  2.  
  3. SCR.con_current = 0;
  4.  
  5. SCR.centerstring = [];
  6. SCR.centertime_off = 0.0;
  7.  
  8. SCR.CenterPrint = function(str)
  9. {
  10.     SCR.centerstring = str.split('\n');
  11.     SCR.centertime_off = SCR.centertime.value;
  12.     SCR.centertime_start = CL.state.time;
  13. }
  14.  
  15. SCR.DrawCenterString = function()
  16. {
  17.     SCR.centertime_off -= Host.frametime;
  18.     if (((SCR.centertime_off <= 0.0) && (CL.state.intermission === 0)) || (Key.dest.dest !== Key.dest.game))
  19.         return;
  20.  
  21.     var y;
  22.     if (SCR.centerstring.length <= 4)
  23.         y = Math.floor(VID.height * 0.35);
  24.     else
  25.         y = 48;
  26.  
  27.     var i;
  28.     if (CL.state.intermission)
  29.     {
  30.         var remaining = Math.floor(SCR.printspeed.value * (CL.state.time - SCR.centertime_start));
  31.         var str, x, j;
  32.         for (i = 0; i < SCR.centerstring.length; ++i)
  33.         {
  34.             str = SCR.centerstring[i];
  35.             x = (VID.width - (str.length << 3)) >> 1;
  36.             for (j = 0; j < str.length; ++j)
  37.             {
  38.                 Draw.Character(x, y, str.charCodeAt(j));
  39.                 if ((remaining--) === 0)
  40.                     return;
  41.                 x += 8;
  42.             }
  43.             y += 8;
  44.         }
  45.         return;
  46.     }
  47.  
  48.     for (i = 0; i < SCR.centerstring.length; ++i)
  49.     {
  50.         Draw.String((VID.width - (SCR.centerstring[i].length << 3)) >> 1, y, SCR.centerstring[i]);
  51.         y += 8;
  52.     }
  53. };
  54.  
  55. SCR.CalcFov = function(fov_y, width, height)
  56. {
  57.     if ((fov_y < 1.0) || (fov_y > 179.0))
  58.         Sys.Error('Bad fov: ' + fov_y);
  59.     return Math.atan(width / (height / Math.tan(fov_y * 0.82 * Math.PI / 360.0))) * 360.0 / Math.PI;
  60. };
  61.  
  62. SCR.CalcRefdef = function()
  63. {
  64.     SCR.recalc_refdef = false;
  65.  
  66.     if (SCR.viewsize.value < 30)
  67.         Cvar.Set('viewsize', '30');
  68.     else if (SCR.viewsize.value > 120)
  69.         Cvar.Set('viewsize', '120');
  70.  
  71.     var size, full;
  72.     if (CL.state.intermission !== 0)
  73.     {
  74.         full = true;
  75.         size = 1.0;
  76.         Sbar.lines = 0;
  77.     }
  78.     else
  79.     {
  80.         size = SCR.viewsize.value;
  81.         if (size >= 120.0)
  82.             Sbar.lines = 0;
  83.         else if (size >= 110.0)
  84.             Sbar.lines = 24;
  85.         else
  86.             Sbar.lines = 48;
  87.         if (size >= 100.0)
  88.         {
  89.             full = true;
  90.             size = 100.0;
  91.         }
  92.         size *= 0.01;
  93.     }
  94.  
  95.     var vrect = R.refdef.vrect;
  96.     vrect.width = Math.floor(VID.width * size);
  97.     if (vrect.width < 96)
  98.     {
  99.         size = 96.0 / vrect.width;
  100.         vrect.width = 96;
  101.     }
  102.     vrect.height = Math.floor(VID.height * size);
  103.     if (vrect.height > (VID.height - Sbar.lines))
  104.         vrect.height = VID.height - Sbar.lines;
  105.     vrect.x = (VID.width - vrect.width) >> 1;
  106.     if (full === true)
  107.         vrect.y = 0;
  108.     else
  109.         vrect.y = (VID.height - Sbar.lines - vrect.height) >> 1;
  110.  
  111.     if (SCR.fov.value < 10)
  112.         Cvar.Set('fov', '10');
  113.     else if (SCR.fov.value > 170)
  114.         Cvar.Set('fov', '170');
  115.     R.refdef.fov_x = SCR.CalcFov(SCR.fov.value, vrect.width, vrect.height);
  116.     R.refdef.fov_y = SCR.fov.value * 0.82;
  117.  
  118.     R.warpwidth = vrect.width;
  119.     R.warpheight = vrect.height;
  120.     if (R.warpwidth > 2048)
  121.         R.warpwidth = 2048;
  122.     if (R.warpheight > 2048)
  123.         R.warpheight = 2048;
  124.     if ((R.oldwarpwidth !== R.warpwidth) || (R.oldwarpheight !== R.warpheight))
  125.     {
  126.         R.oldwarpwidth = R.warpwidth;
  127.         R.oldwarpheight = R.warpheight;
  128.         GL.Bind(0, R.warptexture);
  129.         gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, R.warpwidth, R.warpheight, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  130.         gl.bindRenderbuffer(gl.RENDERBUFFER, R.warprenderbuffer);
  131.         gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, R.warpwidth, R.warpheight);
  132.         gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  133.     }
  134. };
  135.  
  136. SCR.SizeUp_f = function()
  137. {
  138.     Cvar.SetValue('viewsize', SCR.viewsize.value + 10);
  139.     SCR.recalc_refdef = true;
  140. };
  141.  
  142. SCR.SizeDown_f = function()
  143. {
  144.     Cvar.SetValue('viewsize', SCR.viewsize.value - 10);
  145.     SCR.recalc_refdef = true;
  146. };
  147.  
  148. SCR.Init = function()
  149. {
  150.     SCR.fov = Cvar.RegisterVariable('fov', '90');
  151.     SCR.viewsize = Cvar.RegisterVariable('viewsize', '100', true);
  152.     SCR.conspeed = Cvar.RegisterVariable('scr_conspeed', '300');
  153.     SCR.showpause = Cvar.RegisterVariable('showpause', '1');
  154.     SCR.centertime = Cvar.RegisterVariable('scr_centertime', '2');
  155.     SCR.printspeed = Cvar.RegisterVariable('scr_printspeed', '8');
  156.     Cmd.AddCommand('screenshot', SCR.ScreenShot_f);
  157.     Cmd.AddCommand('sizeup', SCR.SizeUp_f);
  158.     Cmd.AddCommand('sizedown', SCR.SizeDown_f);
  159.  
  160.     SCR.pause = Draw.CachePic('pause');
  161.  
  162.     SCR.loading = document.getElementById('loading');
  163.     SCR.loading.src = Draw.PicToDataURL(Draw.CachePic('loading'));
  164. };
  165.  
  166. SCR.DrawPause = function()
  167. {
  168.     if ((SCR.showpause.value === 0) || (CL.state.paused !== true))
  169.         return;
  170.     Draw.Pic((VID.width - SCR.pause.width) >> 1, (VID.height - 48 - SCR.pause.height) >> 1, SCR.pause);
  171. };
  172.  
  173. SCR.SetUpToDrawConsole = function()
  174. {
  175.     Con.forcedup = (CL.state.worldmodel == null) || (CL.cls.signon !== 4);
  176.  
  177.     if (Con.forcedup === true)
  178.     {
  179.         SCR.con_current = 200;
  180.         return;
  181.     }
  182.  
  183.     var conlines;
  184.     if (Key.dest.dest === Key.dest.console)
  185.         conlines = 100;
  186.     else
  187.         conlines = 0;
  188.  
  189.     if (conlines < SCR.con_current)
  190.     {
  191.         SCR.con_current -= SCR.conspeed.value * Host.frametime;
  192.         if (conlines > SCR.con_current)
  193.             SCR.con_current = conlines;
  194.     }
  195.     else if (conlines > SCR.con_current)
  196.     {
  197.         SCR.con_current += SCR.conspeed.value * Host.frametime;
  198.         if (conlines < SCR.con_current)
  199.             SCR.con_current = conlines;
  200.     }
  201. };
  202.  
  203. SCR.DrawConsole = function()
  204. {
  205.     if (SCR.con_current > 0)
  206.     {
  207.         Con.DrawConsole(SCR.con_current);
  208.         return;
  209.     }
  210.     if ((Key.dest.dest === Key.dest.game) || (Key.dest.dest === Key.dest.message))
  211.         Con.DrawNotify();
  212. };
  213.  
  214. SCR.ScreenShot_f = function()
  215. {
  216.     SCR.screenshot = true;
  217. };
  218.  
  219. SCR.BeginLoadingPlaque = function()
  220. {
  221.     var html = document.getElementsByTagName('html')[0];
  222.     var width = (html.clientWidth <= 320) ? 320 : html.clientWidth;
  223.     var height = (html.clientHeight <= 200) ? 200 : html.clientHeight;
  224.     SCR.loading.style.left = ((width - SCR.loading.width) >> 1) + 'px';
  225.     SCR.loading.style.top = ((height - 48 - SCR.loading.height) >> 1) + 'px';
  226.     SCR.loading.style.display = 'inline-block';
  227. };
  228.  
  229. SCR.EndLoadingPlaque = function()
  230. {
  231.     SCR.loading.style.display = 'none';
  232. };
  233.  
  234. SCR.UpdateScreen = function()
  235. {
  236.     var html = document.getElementsByTagName('html')[0];
  237.     var width = (html.clientWidth <= 320) ? 320 : html.clientWidth;
  238.     var height = (html.clientHeight <= 200) ? 200 : html.clientHeight;
  239.     if ((VID.width != width) || (VID.height != height))
  240.     {
  241.         VID.width = width;
  242.         VID.height = height;
  243.         VID.mainwindow.width = width;
  244.         VID.mainwindow.height = height;
  245.         SCR.recalc_refdef = true;
  246.     }
  247.  
  248.     if (SCR.oldfov !== SCR.fov.value)
  249.     {
  250.         SCR.oldfov = SCR.fov.value;
  251.         SCR.recalc_refdef = true;
  252.     }
  253.     if (SCR.oldscreensize !== SCR.viewsize.value)
  254.     {
  255.         SCR.oldscreensize = SCR.viewsize.value;
  256.         SCR.recalc_refdef = true;
  257.     }
  258.     if (SCR.recalc_refdef === true)
  259.         SCR.CalcRefdef();
  260.  
  261.     SCR.SetUpToDrawConsole();
  262.     V.RenderView();
  263.     GL.Set2D();
  264.     if (R.dowarp === true)
  265.         R.WarpScreen();
  266.  
  267.     if ((CL.state.intermission === 1) && (Key.dest.dest === Key.dest.game))
  268.         Sbar.IntermissionOverlay();
  269.     else if ((CL.state.intermission === 2) && (Key.dest.dest === Key.dest.game))
  270.     {
  271.         Sbar.FinaleOverlay();
  272.         SCR.DrawCenterString();
  273.     }
  274.     else if ((CL.state.intermission === 3) && (Key.dest.dest === Key.dest.game))
  275.         SCR.DrawCenterString();
  276.     else
  277.     {
  278.         if (V.crosshair.value !== 0)
  279.         {
  280.             Draw.Character(R.refdef.vrect.x + (R.refdef.vrect.width >> 1) + V.crossx.value,
  281.                 R.refdef.vrect.y + (R.refdef.vrect.height >> 1) + V.crossy.value, 43);
  282.         }
  283.         SCR.DrawPause();
  284.         SCR.DrawCenterString();
  285.         Sbar.Draw();
  286.         SCR.DrawConsole();
  287.         M.Draw();
  288.     }
  289.  
  290.     R.PolyBlend();
  291.  
  292.     gl.disable(gl.BLEND);
  293.  
  294.     if (SCR.screenshot === true)
  295.     {
  296.         SCR.screenshot = false;
  297.         gl.finish();
  298.         open(VID.mainwindow.toDataURL('image/jpeg'));
  299.     }
  300. };
Advertisement
Add Comment
Please, Sign In to add comment