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: 2025-08-24 05:32:49 Functions: 20.7 % 29 6

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

Generated by: LCOV version 2.0-1