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 697 : void writelog(FILE *file, const char *buf)
23 : {
24 : static uchar ubuf[logstrlen];
25 697 : size_t len = std::strlen(buf),
26 697 : carry = 0;
27 1393 : while(carry < len)
28 : {
29 696 : size_t numu = encodeutf8(ubuf, sizeof(ubuf)-1, &(reinterpret_cast<const uchar*>(buf))[carry], len - carry, &carry);
30 696 : if(carry >= len)
31 : {
32 696 : ubuf[numu++] = '\n';
33 : }
34 696 : fwrite(ubuf, 1, numu, file);
35 : }
36 697 : }
37 :
38 697 : void writelogv(FILE *file, const char *fmt, va_list args)
39 : {
40 : static char buf[logstrlen];
41 697 : vformatstring(buf, fmt, args, sizeof(buf));
42 697 : writelog(file, buf);
43 697 : }
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 0 : VARFP(clockerror, 990000, 1000000, 1010000, clockreset());
55 0 : VARFP(clockfix, 0, 0, 1, clockreset());
56 :
57 697 : void logoutfv(const char *fmt, va_list args, FILE *f)
58 : {
59 697 : if(f)
60 : {
61 697 : writelogv(f, fmt, args);
62 : }
63 697 : }
64 : }
65 :
66 : FILE *logfile = nullptr; //used in iengine.h
67 :
68 697 : FILE *getlogfile() //used in iengine.h
69 : {
70 : #ifdef WIN32
71 : return logfile;
72 : #else
73 697 : return logfile ? logfile : stdout;
74 : #endif
75 : }
76 :
77 : /**
78 : * @brief calls the SDL 2 init procedure
79 : *
80 : * returns false on failure, true on success
81 : * used in iengine.h
82 : */
83 0 : bool initsdl()
84 : {
85 0 : return (SDL_Init(SDL_INIT_TIMER|SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) < 0 ? false : true);
86 : }
87 :
88 697 : void logoutf(const char *fmt, ...) //used in iengine.h
89 : {
90 : va_list args;
91 697 : va_start(args, fmt);
92 697 : logoutfv(fmt, args, getlogfile());
93 697 : va_end(args);
94 697 : }
95 :
96 : //error handling
97 :
98 0 : void fatal(const char *s, ...) // failure exit
99 : {
100 : static int errors = 0;
101 0 : errors++;
102 :
103 0 : if(errors <= 2) // print up to one extra recursive error
104 : {
105 0 : DEFV_FORMAT_STRING(msg,s,s);
106 0 : logoutf("%s", msg);
107 :
108 0 : if(errors <= 1) // avoid recursion
109 : {
110 0 : if(SDL_WasInit(SDL_INIT_VIDEO))
111 : {
112 0 : SDL_ShowCursor(SDL_TRUE);
113 0 : SDL_SetRelativeMouseMode(SDL_FALSE);
114 0 : if(screen)
115 : {
116 0 : SDL_SetWindowGrab(screen, SDL_FALSE);
117 : }
118 0 : cleargamma();
119 : }
120 0 : SDL_Quit();
121 0 : SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Libprimis engine fatal error", msg, nullptr);
122 : }
123 : }
124 :
125 0 : exit(EXIT_FAILURE);
126 : }
127 :
128 : //all four used in iengine
129 : int curtime = 0,
130 : lastmillis = 1,
131 : elapsedtime = 0,
132 : totalmillis = 1;
133 :
134 : //used in iengine
135 : dynent *player = nullptr;
136 :
137 : //used in iengine
138 : int initing = Init_Not;
139 :
140 1 : bool initwarning(const char *desc, int level, int type)
141 : {
142 1 : if(initing < level)
143 : {
144 1 : addchange(desc, type);
145 1 : return true;
146 : }
147 0 : return false;
148 : }
149 :
150 : //used in iengine
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.60a";
166 : }
167 :
168 0 : std::string enginebuilddate()
169 : {
170 0 : return __DATE__;
171 : }
|