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