LCOV - code coverage report
Current view: top level - engine/render - renderwindow.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 9.2 % 390 36
Test Date: 2026-03-06 06:39:57 Functions: 20.7 % 29 6

            Line data    Source code
       1              : /**
       2              :  * @brief screen rendering functionality
       3              :  *
       4              :  * screen rendering functions, such as background, progress bar
       5              :  * also handles stuff such as main menu rendering and other non-intensive rendering
       6              :  * as well as global rendering settings such as gamma
       7              :  */
       8              : #include "SDL_ttf.h"
       9              : 
      10              : #include "../libprimis-headers/cube.h"
      11              : #include "../../shared/geomexts.h"
      12              : #include "../../shared/glemu.h"
      13              : #include "../../shared/glexts.h"
      14              : 
      15              : #include "hud.h"
      16              : #include "octarender.h"
      17              : #include "rendergl.h"
      18              : #include "renderlights.h"
      19              : #include "rendermodel.h"
      20              : #include "renderparticles.h"
      21              : #include "rendertext.h"
      22              : #include "renderttf.h"
      23              : #include "renderva.h"
      24              : #include "renderwindow.h"
      25              : #include "shader.h"
      26              : #include "shaderparam.h"
      27              : #include "stain.h"
      28              : #include "texture.h"
      29              : 
      30              : #include "interface/console.h"
      31              : #include "interface/control.h"
      32              : #include "interface/input.h"
      33              : #include "interface/menus.h"
      34              : 
      35              : #include "world/octaedit.h"
      36              : 
      37            0 : VARFN(screenw, scr_w, SCR_MINW, -1, SCR_MAXW, initwarning("screen resolution"));
      38            0 : VARFN(screenh, scr_h, SCR_MINH, -1, SCR_MAXH, initwarning("screen resolution"));
      39              : 
      40              : static VAR(menufps, 0, 60, 1000);   //maximum framerate while in main menu
      41              : static VARP(maxfps, 0, 240, 1000);  //maximum framerate while world is being rendered
      42              : 
      43              : VAR(desktopw, 1, 0, 0); //used in iengine.h
      44              : VAR(desktoph, 1, 0, 0); //used in iengine.h
      45              : 
      46              : int screenw = 0,
      47              :     screenh = 0;
      48              : 
      49              : SDL_Window   *screen    = nullptr;
      50              : static SDL_GLContext glcontext = nullptr;
      51              : 
      52              : bool inbetweenframes = false, //used in iengine.h
      53              :      renderedframe = true; //used in iengine.h, set to true by main() call in game and false by setbackgroundinfo(), so true during most circumstances
      54              : 
      55              : /**
      56              :  * @brief helper function for main menu rendering routines
      57              :  *
      58              :  * @return w and h if both are above 1024x768
      59              :  * @return w and h multiplied by the factor by which the smallest dimension is smaller than 1024x768
      60              :  */
      61            0 : static void getbackgroundres(int &w, int &h)
      62              : {
      63            0 :     float wk = 1,
      64            0 :           hk = 1;
      65            0 :     if(w < 1024)
      66              :     {
      67            0 :         wk = 1024.0f/w; //calculate w subsize factor (if greater than 1)
      68              :     }
      69            0 :     if(h < 768)
      70              :     {
      71            0 :         hk = 768.0f/h; //calculate h subsize factor (if greater than 1)
      72              :     }
      73            0 :     wk = hk = std::max(wk, hk); //pick the largest factor and multiply both by this
      74            0 :     w = static_cast<int>(std::ceil(w*wk));
      75            0 :     h = static_cast<int>(std::ceil(h*hk));
      76            0 : }
      77              : 
      78              : static std::string backgroundcaption   = "",
      79              :                    backgroundmapname   = "",
      80              :                    backgroundmapinfo   = "";
      81              : static const Texture *backgroundmapshot = nullptr;
      82              : 
      83            0 : static void bgquad(float x, float y, float w, float h, float tx = 0, float ty = 0, float tw = 1, float th = 1)
      84              : {
      85            0 :     gle::begin(GL_TRIANGLE_STRIP);
      86            0 :     gle::attribf(x,   y);   gle::attribf(tx,      ty);
      87            0 :     gle::attribf(x+w, y);   gle::attribf(tx + tw, ty);
      88            0 :     gle::attribf(x,   y+h); gle::attribf(tx,      ty + th);
      89            0 :     gle::attribf(x+w, y+h); gle::attribf(tx + tw, ty + th);
      90            0 :     gle::end();
      91            0 : }
      92              : 
      93              : // void renderbackgroundview(int, int, const char*, Texture*, const char*, const char*)
      94              : //   Background picture / text handler
      95              : 
      96              : /*
      97              : Notes:
      98              :     * Unsure what 'w' and 'h' refers to, maybe screen resolution?
      99              : */
     100              : 
     101            0 : static void renderbackgroundview(int win_w, int win_h, const char *caption, const Texture *mapshot, const char *mapname, const char *mapinfo)
     102              : {
     103              :     static int lastupdate  = -1,
     104              :                lastw       = -1,
     105              :                lasth       = -1;
     106              :     static float backgroundu = 0,
     107              :                  backgroundv = 0;
     108            0 :     const bool needsRefresh =
     109            0 :         (renderedframe && !mainmenu && lastupdate != lastmillis)
     110            0 :         || lastw != win_w
     111            0 :         || lasth != win_h;
     112            0 :     if(needsRefresh)
     113              :     {
     114            0 :         lastupdate = lastmillis;
     115            0 :         lastw = win_w;
     116            0 :         lasth = win_h;
     117              : 
     118            0 :         backgroundu = randomfloat(1);
     119            0 :         backgroundv = randomfloat(1);
     120              :     }
     121            0 :     else if(lastupdate != lastmillis)
     122              :     {
     123            0 :         lastupdate = lastmillis;
     124              :     }
     125            0 :     hudmatrix.ortho(0, win_w, win_h, 0, -1, 1);
     126            0 :     resethudmatrix();
     127            0 :     resethudshader();
     128              : 
     129            0 :     gle::defvertex(2);
     130            0 :     gle::deftexcoord0();
     131            0 :     settexture("media/interface/background.png", 0); //main menu background
     132            0 :     const float bu = win_w*0.67f/256.0f,
     133            0 :                 bv = win_h*0.67f/256.0f;
     134            0 :     bgquad(0, 0, win_w, win_h, backgroundu, backgroundv, bu, bv);
     135              : 
     136            0 :     glEnable(GL_BLEND);
     137            0 :     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     138            0 :     settexture("media/interface/shadow.png", 3); //peripheral shadow effect
     139            0 :     bgquad(0, 0, win_w, win_h);
     140            0 :     glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
     141              :     // Set position and size of logo
     142            0 :     const float logo_h = (1.f/3.f)*std::min(win_w, win_h),
     143            0 :                 logo_w = logo_h*(2.f/1.f), // Aspect ratio of logo, defined here
     144            0 :                 logo_x = 0.5f*(win_w - logo_w),
     145            0 :                 logo_y = 0.5f*(win_h*0.5f - logo_h);
     146              : 
     147            0 :     settexture( (maxtexsize >= 1024 || maxtexsize == 0) && (hudw() > 1280 || hudh() > 800)
     148              :               ? "<premul>media/interface/logo_1024.png" //1024x wide logo
     149              :               : "<premul>media/interface/logo.png", //512x wide logo for small screens
     150              :         3);
     151            0 :     bgquad(logo_x, logo_y, logo_w, logo_h);
     152              : 
     153            0 :     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     154              : 
     155            0 :     if(caption)
     156              :     {
     157            0 :         int tw = text_width(caption);
     158            0 :         const float tsz = 0.04f*std::min(win_w, win_h)/FONTH,
     159            0 :                     tx  = 0.5f*(win_w - tw*tsz),
     160            0 :                     ty  = win_h - 0.075f*1.5f*std::min(win_w, win_h) - FONTH*tsz;
     161            0 :         pushhudtranslate(tx, ty, tsz);
     162              :         //draw_text(caption, 0, 0);
     163            0 :         ttr.renderttf(caption, {0xFF, 0xFF, 0xFF, 0}, 0, 0);
     164            0 :         pophudmatrix();
     165              :     }
     166            0 :     if(mapshot || mapname)
     167              :     {
     168            0 :         float infowidth = 14*FONTH,
     169            0 :               sz  = 0.35f*std::min(win_w, win_h),
     170            0 :               msz = (0.85f*std::min(win_w, win_h) - sz)/(infowidth + FONTH),
     171            0 :               x   = 0.5f*win_w,
     172            0 :               y   = logo_y+logo_h - sz/15,
     173            0 :               mx  = 0,
     174            0 :               my  = 0,
     175            0 :               mw  = 0,
     176            0 :               mh  = 0;
     177              :         // Prepare text area for map info
     178            0 :         if(mapinfo)
     179              :         {
     180            0 :             text_boundsf(mapinfo, mw, mh, infowidth);
     181            0 :             x -= 0.5f * mw * msz;
     182            0 :             if (mapshot && mapshot!=notexture)
     183              :             {
     184            0 :                 x -= 0.5f*FONTH * msz;
     185            0 :                 mx = sz + FONTH * msz;
     186              :             }
     187              :         }
     188              :         // Map shot was provided and isn't empty
     189            0 :         if(mapshot && mapshot!=notexture)
     190              :         {
     191            0 :             x -= 0.5f * sz;
     192            0 :             resethudshader();
     193            0 :             glBindTexture(GL_TEXTURE_2D, mapshot->id);
     194            0 :             bgquad(x, y, sz, sz);
     195              :         }
     196              :         // Map name was provided
     197            0 :         if(mapname)
     198              :         {
     199            0 :             float tw  = text_widthf(mapname),
     200            0 :                   tsz = sz/(8*FONTH),
     201            0 :                   tx  = std::max(0.5f * (mw*msz - tw * tsz), 0.0f);
     202            0 :             pushhudtranslate(x + mx + tx, y, tsz);
     203              :             //draw_text(mapname, 0, 0);
     204            0 :             ttr.fontsize(42);
     205            0 :             ttr.renderttf(mapname, {0xFF, 0xFF, 0xFF, 0}, 0, 0);
     206            0 :             pophudmatrix();
     207            0 :             my = 1.5f*FONTH*tsz;
     208              :         }
     209              :         // Map info was provided
     210            0 :         if(mapinfo)
     211              :         {
     212            0 :             pushhudtranslate(x + mx, y + my, msz);
     213              :             //draw_text(mapinfo, 0, 0, 0xFF, 0xFF, 0xFF, 0xFF, -1, infowidth);
     214            0 :             ttr.fontsize(42);
     215            0 :             ttr.renderttf(mapinfo, {0xFF, 0xFF, 0xFF, 0}, 0, 0);
     216            0 :             pophudmatrix();
     217              :         }
     218              :     }
     219            0 :     glDisable(GL_BLEND);
     220            0 : }
     221              : 
     222            0 : void swapbuffers(bool)
     223              : {
     224            0 :     gle::disable();
     225            0 :     SDL_GL_SwapWindow(screen);
     226            0 : }
     227              : 
     228            0 : static void setbackgroundinfo(const char *caption = nullptr, const Texture *mapshot = nullptr, const char *mapname = nullptr, const char *mapinfo = nullptr)
     229              : {
     230            0 :     renderedframe = false;
     231            0 :     backgroundcaption = std::string(caption ? caption : "");
     232            0 :     backgroundmapshot = mapshot;
     233            0 :     backgroundmapname = std::string(mapname ? mapname : "");
     234            0 :     std::string minfo = std::string(mapinfo ? mapinfo : "");
     235            0 :     if(minfo != backgroundmapinfo)
     236              :     {
     237            0 :         backgroundmapinfo = "";
     238            0 :         if(!minfo.empty())
     239              :         {
     240            0 :             backgroundmapinfo = std::string(mapinfo);
     241              :         }
     242              :         else
     243              :         {
     244            0 :             backgroundmapinfo = "";
     245              :         }
     246              :     }
     247            0 : }
     248              : 
     249            0 : void renderbackground(const char *caption, const Texture *mapshot, const char *mapname, const char *mapinfo, bool force)
     250              : {
     251            0 :     if(!inbetweenframes && !force)
     252              :     {
     253            0 :         return;
     254              :     }
     255            0 :     int w = hudw(),
     256            0 :         h = hudh();
     257            0 :     if(forceaspect)
     258              :     {
     259            0 :         w = std::ceil(h*forceaspect);
     260              :     }
     261            0 :     getbackgroundres(w, h);
     262            0 :     gettextres(w, h);
     263            0 :     if(force)
     264              :     {
     265            0 :         renderbackgroundview(w, h, caption, mapshot, mapname, mapinfo);
     266            0 :         return;
     267              :     }
     268              :     //renders renderbackgroundview three times, with an identical call each time
     269            0 :     for(int i = 0; i < 3; ++i)
     270              :     {
     271            0 :         renderbackgroundview(w, h, caption, mapshot, mapname, mapinfo);
     272            0 :         swapbuffers(false);
     273              :     }
     274            0 :     setbackgroundinfo(caption, mapshot, mapname, mapinfo);
     275              : }
     276              : 
     277            0 : static void restorebackground(int w, int h, bool force = false)
     278              : {
     279            0 :     if(renderedframe)
     280              :     {
     281            0 :         if(!force)
     282              :         {
     283            0 :             return;
     284              :         }
     285            0 :         setbackgroundinfo();
     286              :     }
     287            0 :     renderbackgroundview(w, h, backgroundcaption.c_str(), backgroundmapshot, backgroundmapname.c_str(), backgroundmapinfo.c_str());
     288              : }
     289              : 
     290              : float loadprogress = 0;
     291              : 
     292            0 : static void renderprogressview(int w, int h, float bar, const char *text)   // also used during loading
     293              : {
     294            0 :     hudmatrix.ortho(0, w, h, 0, -1, 1);
     295            0 :     resethudmatrix();
     296            0 :     resethudshader();
     297              : 
     298            0 :     gle::defvertex(2);
     299            0 :     gle::deftexcoord0();
     300              : 
     301            0 :     const float fh = 0.060f*std::min(w, h),
     302            0 :                 fw = fh * 15,
     303            0 :                 fx = renderedframe ? w - fw - fh/4 : 0.5f * (w - fw),
     304            0 :                 fy = renderedframe ? fh/4 : h - fh * 1.5f;
     305            0 :     settexture("media/interface/loading_frame.png", 3);
     306            0 :     bgquad(fx, fy, fw, fh);
     307              : 
     308            0 :     glEnable(GL_BLEND);
     309            0 :     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     310              : 
     311            0 :     const float bw  = fw * (512 - 2*8)/512.0f,
     312            0 :                 bh  = fh * 20/32.0f,
     313            0 :                 bx  = fx + fw * 8/512.0f,
     314            0 :                 by  = fy + fh * 6/32.0f,
     315            0 :                 su1 = 0/32.0f,
     316            0 :                 su2 = 8/32.0f,
     317            0 :                 sw  = fw * 8/512.0f,
     318            0 :                 eu1 = 24/32.0f,
     319            0 :                 eu2 = 32/32.0f,
     320            0 :                 ew  = fw * 8/512.0f,
     321            0 :                 mw  = bw - sw - ew,
     322            0 :                 ex  = bx+sw + std::max(mw*bar, fw * 8/512.0f);
     323            0 :     if(bar > 0)
     324              :     {
     325            0 :         settexture("media/interface/loading_bar.png", 3);
     326            0 :         bgquad(bx, by, sw, bh, su1, 0, su2-su1, 1);
     327            0 :         bgquad(bx+sw, by, ex-(bx+sw), bh, su2, 0, eu1-su2, 1);
     328            0 :         bgquad(ex, by, ew, bh, eu1, 0, eu2-eu1, 1);
     329              :     }
     330            0 :     if(text)
     331              :     {
     332            0 :         const int tw = text_width(text);
     333            0 :         float tsz = bh * 0.6f/FONTH;
     334            0 :         if(tw * tsz > mw)
     335              :         {
     336            0 :             tsz = mw/tw;
     337              :         }
     338            0 :         pushhudtranslate(bx+sw, by + (bh - FONTH*tsz)/2, tsz);
     339              :         //draw_text(text, 0, 0);
     340            0 :         ttr.fontsize(50);
     341            0 :         ttr.renderttf(text, {0xFF, 0xFF, 0xFF, 0}, 0, 4);
     342            0 :         pophudmatrix();
     343              :     }
     344            0 :     glDisable(GL_BLEND);
     345            0 : }
     346              : 
     347              : static VAR(progressbackground, 0, 0, 1); //force rendering progress bar background texture
     348              : static int curvsync = -1;
     349              : 
     350            6 : void renderprogress(float bar, const char *text, bool background)   // also used during loading
     351              : {
     352            6 :     if(!inbetweenframes || drawtex)
     353              :     {
     354            6 :         return;
     355              :     }
     356            0 :     int fps = menufps ? (maxfps ? std::min(maxfps, menufps) : menufps) : maxfps;
     357            0 :     if(fps)
     358              :     {
     359              :         static int lastprogress = 0;
     360            0 :         int ticks = SDL_GetTicks(),
     361            0 :             diff = ticks - lastprogress;
     362            0 :         if(bar > 0 && diff >= 0 && diff < (1000 + fps-1)/fps)
     363              :         {
     364            0 :             return;
     365              :         }
     366            0 :         lastprogress = ticks;
     367              :     }
     368            0 :     int w = hudw(),
     369            0 :         h = hudh();
     370            0 :     if(forceaspect)
     371              :     {
     372            0 :         w = static_cast<int>(std::ceil(h*forceaspect));
     373              :     }
     374            0 :     getbackgroundres(w, h);
     375            0 :     gettextres(w, h);
     376              : 
     377            0 :     bool forcebackground = progressbackground || (mesa_swap_bug && (curvsync || totalmillis==1));
     378            0 :     if(background || forcebackground)
     379              :     {
     380            0 :         restorebackground(w, h, forcebackground);
     381              :     }
     382            0 :     renderprogressview(w, h, bar, text);
     383            0 :     swapbuffers(false);
     384              : }
     385              : 
     386              : static bool initwindowpos = false;
     387              : 
     388            0 : void setfullscreen(bool enable)
     389              : {
     390            0 :     if(!screen)
     391              :     {
     392            0 :         return;
     393              :     }
     394              :     //initwarning(enable ? "fullscreen" : "windowed");
     395            0 :     SDL_SetWindowFullscreen(screen, enable ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
     396            0 :     if(!enable)
     397              :     {
     398            0 :         SDL_SetWindowSize(screen, scr_w, scr_h);
     399            0 :         if(initwindowpos)
     400              :         {
     401            0 :             int winx = SDL_WINDOWPOS_CENTERED,
     402            0 :                 winy = SDL_WINDOWPOS_CENTERED;
     403            0 :             SDL_SetWindowPosition(screen, winx, winy);
     404            0 :             initwindowpos = false;
     405              :         }
     406              :     }
     407              : }
     408              : 
     409            0 : VARF(fullscreen, 0, 1, 1, setfullscreen(fullscreen!=0));
     410              : 
     411              : /* screenres: sets the window size to w * h pixels, or reduces fullscreen
     412              :  * resolution to w * h pixels
     413              :  *
     414              :  * arguments:
     415              :  *    w: width of new screen res
     416              :  *    h: height of new screen res
     417              :  */
     418            1 : void screenres(int w, int h)
     419              : {
     420              :     //need to cast enum to int for std's clamp implementation
     421            1 :     scr_w = std::clamp(w, static_cast<int>(SCR_MINW), static_cast<int>(SCR_MAXW));
     422            1 :     scr_h = std::clamp(h, static_cast<int>(SCR_MINH), static_cast<int>(SCR_MAXH));
     423            1 :     if(screen)
     424              :     {
     425            0 :         scr_w = std::min(scr_w, desktopw);
     426            0 :         scr_h = std::min(scr_h, desktoph);
     427            0 :         if(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)
     428              :         {
     429            0 :             gl_resize();
     430              :         }
     431              :         else
     432              :         {
     433            0 :             SDL_SetWindowSize(screen, scr_w, scr_h);
     434              :         }
     435              :     }
     436              :     else
     437              :     {
     438            1 :         initwarning("screen resolution");
     439              :     }
     440            1 : }
     441              : 
     442              : 
     443            0 : static void setgamma(int val)
     444              : {
     445            0 :     if(screen && SDL_SetWindowBrightness(screen, val/100.0f) < 0)
     446              :     {
     447            0 :         conoutf(Console_Error, "Could not set gamma: %s", SDL_GetError());
     448              :     }
     449            0 : }
     450              : 
     451              : static int curgamma = 100;
     452            0 : static VARFNP(gamma, reqgamma, 30, 100, 300,
     453              : {
     454              :     if(initing || reqgamma == curgamma)
     455              :     {
     456              :         return;
     457              :     }
     458              :     curgamma = reqgamma;
     459              :     setgamma(curgamma);
     460              : });
     461              : 
     462              : /* restoregamma: sets gamma to the previous set value, useful for reverting bad-
     463              :  * looking gamma trial settings
     464              :  *
     465              :  * used in iengine.h
     466              :  */
     467            0 : void restoregamma()
     468              : {
     469            0 :     if(initing || reqgamma == 100)
     470              :     {
     471            0 :         return;
     472              :     }
     473            0 :     curgamma = reqgamma;
     474            0 :     setgamma(curgamma);
     475              : }
     476              : 
     477            0 : void cleargamma()
     478              : {
     479            0 :     if(curgamma != 100 && screen)
     480              :     {
     481            0 :         SDL_SetWindowBrightness(screen, 1.0f);
     482              :     }
     483            0 : }
     484              : 
     485              : void restorevsync(); //prototype to fix chicken-egg initialization problem caused by VARFP
     486              : 
     487            0 : static VARFP(vsync, 0, 0, 1, restorevsync());                      //vertical sync of framebuffer to refresh rate
     488            0 : static VARFP(vsynctear, 0, 0, 1, { if(vsync) restorevsync(); });   //toggles sdl2's adaptive sync function
     489              : 
     490            0 : void restorevsync()
     491              : {
     492            0 :     if(initing || !glcontext)
     493              :     {
     494            0 :         return;
     495              :     }
     496            0 :     if(!SDL_GL_SetSwapInterval(vsync ? (vsynctear ? -1 : 1) : 0))
     497              :     {
     498            0 :         curvsync = vsync;
     499              :     }
     500              : }
     501              : 
     502              : //used in iengine.h
     503            0 : void setupscreen()
     504              : {
     505              :     //clear prior gl context/screen if present
     506            0 :     if(glcontext)
     507              :     {
     508            0 :         SDL_GL_DeleteContext(glcontext);
     509            0 :         glcontext = nullptr;
     510              :     }
     511            0 :     if(screen)
     512              :     {
     513            0 :         SDL_DestroyWindow(screen);
     514            0 :         screen = nullptr;
     515              :     }
     516            0 :     curvsync = -1;
     517              : 
     518              :     SDL_Rect desktop;
     519            0 :     if(SDL_GetDisplayBounds(0, &desktop) < 0)
     520              :     {
     521            0 :         fatal("failed querying desktop bounds: %s", SDL_GetError());
     522              :     }
     523            0 :     desktopw = desktop.w;
     524            0 :     desktoph = desktop.h;
     525              : 
     526            0 :     if(scr_h < 0)
     527              :     {
     528            0 :         scr_h = SCR_DEFAULTH;
     529              :     }
     530            0 :     if(scr_w < 0)
     531              :     {
     532            0 :         scr_w = (scr_h*desktopw)/desktoph;
     533              :     }
     534            0 :     scr_w = std::min(scr_w, desktopw);
     535            0 :     scr_h = std::min(scr_h, desktoph);
     536              : 
     537            0 :     int winx  = SDL_WINDOWPOS_UNDEFINED,
     538            0 :         winy  = SDL_WINDOWPOS_UNDEFINED,
     539            0 :         winw  = scr_w,
     540            0 :         winh  = scr_h,
     541            0 :         flags = SDL_WINDOW_RESIZABLE;
     542            0 :     if(fullscreen)
     543              :     {
     544            0 :         winw   = desktopw;
     545            0 :         winh   = desktoph;
     546            0 :         flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
     547            0 :         initwindowpos = true;
     548              :     }
     549              : 
     550            0 :     SDL_GL_ResetAttributes();
     551            0 :     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
     552            0 :     SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 0);
     553            0 :     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
     554              : 
     555            0 :     uint32_t windowflags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | flags;
     556              :     //create new screen       title          x     y     w     h  flags
     557            0 :     screen = SDL_CreateWindow("Imprimis", winx, winy, winw, winh, windowflags);
     558            0 :     ttr.initttf();
     559            0 :     ttr.openfont("media/interface/font/default.ttf", 24);
     560              : 
     561            0 :     if(!screen)
     562              :     {
     563            0 :         fatal("failed to create OpenGL window: %s", SDL_GetError());
     564              :     }
     565            0 :     SDL_Surface *icon = loadsurface("media/interface/icon.png"); //path to taskbar icon
     566            0 :     if(icon)
     567              :     {
     568            0 :         SDL_SetWindowIcon(screen, icon);
     569            0 :         SDL_FreeSurface(icon); //don't need it any more
     570              :     }
     571              : 
     572            0 :     SDL_SetWindowMinimumSize(screen, SCR_MINW, SCR_MINH);
     573            0 :     SDL_SetWindowMaximumSize(screen, SCR_MAXW, SCR_MAXH);
     574              :     //set opengl version to 4.0
     575            0 :     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
     576            0 :     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
     577              :     //set core profile
     578            0 :     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
     579            0 :     glcontext = SDL_GL_CreateContext(screen);
     580              : 
     581              :     //
     582            0 :     GLenum err = glewInit();
     583            0 :     if (GLEW_OK != err)
     584              :     {
     585              :       /* Problem: glewInit failed, something is seriously wrong. */
     586            0 :       logoutf("Error: %s", glewGetErrorString(err));
     587              :     }
     588            0 :     logoutf("init: GLEW %s", glewGetString(GLEW_VERSION));
     589              :     //check if OpenGL context is sane
     590            0 :     if(!glcontext)
     591              :     {
     592            0 :         fatal("failed to create OpenGL context: %s", SDL_GetError());
     593              :     }
     594            0 :     SDL_GetWindowSize(screen, &screenw, &screenh);
     595            0 : }
     596              : 
     597              : //full reset of renderer
     598            2 : void resetgl()
     599              : {
     600            2 :     if(!glslversion)
     601              :     {
     602            2 :         conoutf(Console_Error, "Cannot reset GL without GL initialized, operation not performed");
     603            2 :         return;
     604              :     }
     605            0 :     clearchanges(Change_Graphics|Change_Shaders);
     606              : 
     607            0 :     renderbackground("resetting OpenGL");
     608              : 
     609            0 :     rootworld.cleanupva();
     610            0 :     cleanupparticles();
     611            0 :     cleanupstains();
     612            0 :     cleanupmodels();
     613            0 :     cleanupprefabs();
     614            0 :     cleanuptextures();
     615            0 :     cleanuplights();
     616            0 :     cleanupshaders();
     617            0 :     cleanupgl();
     618              : 
     619            0 :     setupscreen();
     620              : 
     621            0 :     inputgrab(grabinput);
     622              : 
     623            0 :     gl_init();
     624              : 
     625            0 :     inbetweenframes = false;
     626              :     //texture reloading
     627            0 :     if(!notexture->reload() ||
     628            0 :        !reloadtexture("<premul>media/interface/logo.png") ||
     629            0 :        !reloadtexture("<premul>media/interface/logo_1024.png") ||
     630            0 :        !reloadtexture("media/interface/background.png") ||
     631            0 :        !reloadtexture("media/interface/shadow.png") ||
     632            0 :        !reloadtexture("media/interface/mapshot_frame.png") ||
     633            0 :        !reloadtexture("media/interface/loading_frame.png") ||
     634            0 :        !reloadtexture("media/interface/loading_bar.png"))
     635              :     {
     636            0 :         fatal("failed to reload core texture");
     637              :     }
     638            0 :     reloadfonts();
     639            0 :     inbetweenframes = true;
     640            0 :     renderbackground("initializing...");
     641            0 :     restoregamma();
     642            0 :     restorevsync();
     643            0 :     initgbuffer();
     644            0 :     reloadshaders();
     645            0 :     reloadtextures();
     646            0 :     rootworld.allchanged(true);
     647              : }
     648              : 
     649              : /**
     650              :  * @brief Delays the frame to the prescribed rate.
     651              :  *
     652              :  * Uses SDL_Delay to delay a frame, given the time the last frame was
     653              :  * rendered and the current time.
     654              :  *
     655              :  * @param millis the time (in ms) since program started
     656              :  * @param curmillis the last registered frame time
     657              :  */
     658            0 : void limitfps(int &millis, int curmillis)
     659              : {
     660            0 :     int limit = (mainmenu || minimized) && menufps ? (maxfps ? std::min(maxfps, menufps) : menufps) : maxfps;
     661            0 :     if(!limit)
     662              :     {
     663            0 :         return;
     664              :     }
     665              :     static int fpserror = 0;
     666            0 :     int delay = 1000/limit - (millis-curmillis);
     667            0 :     if(delay < 0)
     668              :     {
     669            0 :         fpserror = 0;
     670              :     }
     671              :     else
     672              :     {
     673            0 :         fpserror += 1000%limit;
     674            0 :         if(fpserror >= limit)
     675              :         {
     676            0 :             ++delay;
     677            0 :             fpserror -= limit;
     678              :         }
     679            0 :         if(delay > 0)
     680              :         {
     681            0 :             SDL_Delay(delay);
     682            0 :             millis += delay;
     683              :         }
     684              :     }
     685              : }
     686              : 
     687              : #ifdef WIN32
     688              :     // Force Optimus setups to use the NVIDIA GPU
     689              :     // or also for AMD dual graphics
     690              :     extern "C"
     691              :     {
     692              :         #ifdef __GNUC__
     693              :             __attribute__((dllexport))
     694              :         #else
     695              :             __declspec(dllexport)
     696              :         #endif
     697              :             DWORD NvOptimusEnablement = 1;
     698              : 
     699              :         #ifdef __GNUC__
     700              :             __attribute__((dllexport))
     701              :         #else
     702              :             __declspec(dllexport)
     703              :         #endif
     704              :         DWORD AmdPowerXpressRequestHighPerformance = 1;
     705              :     }
     706              : #endif
     707              : 
     708              : static constexpr int maxfpshistory = 60;
     709              : 
     710              : int fpspos = 0;
     711              : std::array<int, maxfpshistory> fpshistory;
     712              : 
     713            0 : void resetfpshistory()
     714              : {
     715            0 :     fpshistory.fill(1);
     716            0 :     fpspos = 0;
     717            0 : }
     718              : 
     719            0 : void updatefpshistory(int millis)
     720              : {
     721            0 :     fpshistory[fpspos++] = std::max(1, std::min(1000, millis));
     722            0 :     if(fpspos>=maxfpshistory)
     723              :     {
     724            0 :         fpspos = 0;
     725              :     }
     726            0 : }
     727              : 
     728            1 : void getfps(int &fps, int &bestdiff, int &worstdiff)
     729              : {
     730            1 :     int total = fpshistory.at(maxfpshistory-1),
     731            1 :         best = total,
     732            1 :         worst = total;
     733           61 :     for(const int &millis : fpshistory)
     734              :     {
     735           60 :         total += millis;
     736           60 :         if(millis < best)
     737              :         {
     738            0 :             best = millis;
     739              :         }
     740           60 :         if(millis > worst)
     741              :         {
     742            0 :             worst = millis;
     743              :         }
     744              :     }
     745            1 :     if(total) //guard against div by 0
     746              :     {
     747            0 :         fps = (1000*maxfpshistory)/total;
     748            0 :         bestdiff = 1000/best-fps;
     749            0 :         worstdiff = fps-1000/worst;
     750              :     }
     751              :     else
     752              :     {
     753            1 :         fps = 0;
     754            1 :         bestdiff = 0;
     755            1 :         worstdiff = 0;
     756              :     }
     757            1 : }
     758              : 
     759            1 : void getfpscmd(const int *raw)
     760              : {
     761            1 :     if(*raw)
     762              :     {
     763            0 :         floatret(1000.0f/fpshistory[(fpspos+maxfpshistory-1)%maxfpshistory]);
     764              :     }
     765              :     else
     766              :     {
     767              :         int fps, bestdiff, worstdiff;
     768            1 :         getfps(fps, bestdiff, worstdiff);
     769            1 :         intret(fps);
     770              :     }
     771            1 : }
     772              : 
     773            1 : void initrenderwindowcmds()
     774              : {
     775            1 :     addcommand("getfps", reinterpret_cast<identfun>(getfpscmd), "i", Id_Command);
     776            1 :     addcommand("resetgl", reinterpret_cast<identfun>(resetgl), "", Id_Command);
     777            1 :     addcommand("screenres", reinterpret_cast<identfun>(screenres), "ii", Id_Command);
     778            1 : }
        

Generated by: LCOV version 2.0-1