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-02-21 06:59:27 Functions: 42.9 % 14 6

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

Generated by: LCOV version 2.0-1