LCOV - code coverage report
Current view: top level - engine/interface - control.cpp (source / functions) Coverage Total Hit
Test: Libprimis Test Coverage Lines: 45.2 % 62 28
Test Date: 2025-08-24 05:32:49 Functions: 42.9 % 14 6

            Line data    Source code
       1              : /**
       2              :  * @brief misc engine utilities for program control
       3              :  *
       4              :  * control.cpp defines a handful of useful utilities for creating a useful program
       5              :  * using the library, including logging (to file), program crash handling, engine
       6              :  * timestate information, and engine version information
       7              :  */
       8              : #include "../libprimis-headers/cube.h"
       9              : #include "../../shared/stream.h"
      10              : 
      11              : #include "console.h"
      12              : #include "control.h"
      13              : #include "menus.h"
      14              : 
      15              : #include "render/renderwindow.h"
      16              : 
      17              : namespace
      18              : {
      19              :     // logging
      20              :     constexpr int logstrlen = 512;
      21              : 
      22          698 :     void writelog(FILE *file, const char *buf)
      23              :     {
      24              :         static uchar ubuf[logstrlen];
      25          698 :         size_t len = std::strlen(buf),
      26          698 :                carry = 0;
      27         1395 :         while(carry < len)
      28              :         {
      29          697 :             size_t numu = encodeutf8(ubuf, sizeof(ubuf)-1, &(reinterpret_cast<const uchar*>(buf))[carry], len - carry, &carry);
      30          697 :             if(carry >= len)
      31              :             {
      32          697 :                 ubuf[numu++] = '\n';
      33              :             }
      34          697 :             fwrite(ubuf, 1, numu, file);
      35              :         }
      36          698 :     }
      37              : 
      38          698 :     void writelogv(FILE *file, const char *fmt, va_list args)
      39              :     {
      40              :         static char buf[logstrlen];
      41          698 :         vformatstring(buf, fmt, args, sizeof(buf));
      42          698 :         writelog(file, buf);
      43          698 :     }
      44              : 
      45              :     int clockrealbase = 0,
      46              :         clockvirtbase = 0;
      47              : 
      48            0 :     void clockreset()
      49              :     {
      50            0 :         clockrealbase = SDL_GetTicks();
      51            0 :         clockvirtbase = totalmillis;
      52            0 :     }
      53              : }
      54              : 
      55              : 
      56              : bool inbetweenframes = false,
      57              :      renderedframe = true;
      58              : 
      59              : FILE *logfile = nullptr; //used in iengine.h
      60              : 
      61          698 : FILE *getlogfile()
      62              : {
      63              : #ifdef WIN32
      64              :     return logfile;
      65              : #else
      66          698 :     return logfile ? logfile : stdout;
      67              : #endif
      68              : }
      69              : 
      70              : /**
      71              :  * @brief calls the SDL 2 init procedure
      72              :  *
      73              :  * returns false on failure, true on success
      74              :  * used in iengine.h
      75              :  */
      76            0 : bool initsdl()
      77              : {
      78            0 :     return (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) < 0 ? false : true);
      79              : }
      80              : 
      81          698 : void logoutfv(const char *fmt, va_list args, FILE *f)
      82              : {
      83          698 :     if(f)
      84              :     {
      85          698 :         writelogv(f, fmt, args);
      86              :     }
      87          698 : }
      88              : 
      89          698 : void logoutf(const char *fmt, ...)
      90              : {
      91              :     va_list args;
      92          698 :     va_start(args, fmt);
      93          698 :     logoutfv(fmt, args, getlogfile());
      94          698 :     va_end(args);
      95          698 : }
      96              : 
      97              : //error handling
      98              : 
      99            0 : void fatal(const char *s, ...)    // failure exit
     100              : {
     101              :     static int errors = 0;
     102            0 :     errors++;
     103              : 
     104            0 :     if(errors <= 2) // print up to one extra recursive error
     105              :     {
     106            0 :         DEFV_FORMAT_STRING(msg,s,s);
     107            0 :         logoutf("%s", msg);
     108              : 
     109            0 :         if(errors <= 1) // avoid recursion
     110              :         {
     111            0 :             if(SDL_WasInit(SDL_INIT_VIDEO))
     112              :             {
     113            0 :                 SDL_ShowCursor(SDL_TRUE);
     114            0 :                 SDL_SetRelativeMouseMode(SDL_FALSE);
     115            0 :                 if(screen)
     116              :                 {
     117            0 :                     SDL_SetWindowGrab(screen, SDL_FALSE);
     118              :                 }
     119            0 :                 cleargamma();
     120              :             }
     121            0 :             SDL_Quit();
     122            0 :             SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Libprimis engine fatal error", msg, nullptr);
     123              :         }
     124              :     }
     125              : 
     126            0 :     exit(EXIT_FAILURE);
     127              : }
     128              : 
     129              : int curtime = 0,
     130              :     lastmillis = 1,
     131              :     elapsedtime = 0,
     132              :     totalmillis = 1;
     133              : 
     134              : dynent *player = nullptr;
     135              : 
     136              : int initing = Init_Not;
     137              : 
     138            1 : bool initwarning(const char *desc, int level, int type)
     139              : {
     140            1 :     if(initing < level)
     141              :     {
     142            1 :         addchange(desc, type);
     143            1 :         return true;
     144              :     }
     145            0 :     return false;
     146              : }
     147              : 
     148            0 : VARFP(clockerror, 990000, 1000000, 1010000, clockreset());
     149            0 : VARFP(clockfix, 0, 0, 1, clockreset());
     150              : 
     151            0 : int getclockmillis()
     152              : {
     153            0 :     int millis = SDL_GetTicks() - clockrealbase;
     154            0 :     if(clockfix)
     155              :     {
     156            0 :         millis = static_cast<int>(millis*(static_cast<double>(clockerror)/1000000));
     157              :     }
     158            0 :     millis += clockvirtbase;
     159            0 :     return std::max(millis, totalmillis);
     160              : }
     161              : 
     162              : //identification info about engine
     163            0 : std::string enginestr()
     164              : {
     165            0 :     return "Libprimis v0.56a";
     166              : }
     167              : 
     168            0 : std::string enginebuilddate()
     169              : {
     170            0 :     return __DATE__;
     171              : }
        

Generated by: LCOV version 2.0-1