From 0a1172b75f571685c264a8b9d8ee224bbf11381f Mon Sep 17 00:00:00 2001 From: xolatile Date: Wed, 6 Aug 2025 22:54:55 +0200 Subject: Please do not hate me, it makes sense... --- src/engine/main.cpp | 464 ++++++++++++++-------------------------------------- 1 file changed, 123 insertions(+), 341 deletions(-) (limited to 'src/engine/main.cpp') diff --git a/src/engine/main.cpp b/src/engine/main.cpp index 5d264ed..a059c13 100644 --- a/src/engine/main.cpp +++ b/src/engine/main.cpp @@ -10,8 +10,7 @@ float speedmodifier = 0.0f; extern void cleargamma(); -void cleanup() -{ +void cleanup() { cleanupserver(); SDL_ShowCursor(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_FALSE); @@ -28,8 +27,7 @@ void cleanup() extern void writeinitcfg(); -void quit() // normal exit -{ +void quit() { // normal exit { writeinitcfg(); writeservercfg(); abortconnect(); @@ -40,20 +38,14 @@ void quit() // normal exit exit(EXIT_SUCCESS); } -void fatal(const char *s, ...) // failure exit -{ +void fatal(const char *s, ...) { // failure exit { static int errors = 0; errors++; - - if(errors <= 2) // print up to one extra recursive error - { + if(errors <= 2) { // print up to one extra recursive error { defvformatstring(msg,s,s); logoutf("%s", msg); - - if(errors <= 1) // avoid recursion - { - if(SDL_WasInit(SDL_INIT_VIDEO)) - { + if(errors <= 1) { // avoid recursion { + if(SDL_WasInit(SDL_INIT_VIDEO)) { SDL_ShowCursor(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_FALSE); if(screen) SDL_SetWindowGrab(screen, SDL_FALSE); @@ -63,7 +55,6 @@ void fatal(const char *s, ...) // failure exit SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Cube 2: Sauerbraten fatal error", msg, NULL); } } - exit(EXIT_FAILURE); } @@ -73,10 +64,8 @@ dynent *player = NULL; int initing = NOT_INITING; -bool initwarning(const char *desc, int level, int type) -{ - if(initing < level) - { +bool initwarning(const char *desc, int level, int type) { + if(initing < level) { addchange(desc, type); return true; } @@ -100,8 +89,7 @@ VARF(scr_h, SCR_MINH, -1, SCR_MAXH, initwarning("screen resolution")); VARF(depthbits, 0, 0, 32, initwarning("depth-buffer precision")); VARF(fsaa, -1, -1, 16, initwarning("anti-aliasing")); -void writeinitcfg() -{ +void writeinitcfg() { stream *f = openutf8file("init.cfg", "w"); if(!f) return; f->printf("// automatically written on exit, DO NOT MODIFY\n// modify settings in game\n"); @@ -124,8 +112,7 @@ void writeinitcfg() COMMAND(quit, ""); -static void getbackgroundres(int &w, int &h) -{ +static void getbackgroundres(int &w, int &h) { float wk = 1, hk = 1; if(w < 1024) wk = 1024.0f/w; if(h < 768) hk = 768.0f/h; @@ -139,31 +126,26 @@ Texture *backgroundmapshot = NULL; string backgroundmapname = ""; char *backgroundmapinfo = NULL; -void setbackgroundinfo(const char *caption = NULL, Texture *mapshot = NULL, const char *mapname = NULL, const char *mapinfo = NULL) -{ +void setbackgroundinfo(const char *caption = NULL, Texture *mapshot = NULL, const char *mapname = NULL, const char *mapinfo = NULL) { renderedframe = false; copystring(backgroundcaption, caption ? caption : ""); backgroundmapshot = mapshot; copystring(backgroundmapname, mapname ? mapname : ""); - if(mapinfo != backgroundmapinfo) - { + if(mapinfo != backgroundmapinfo) { DELETEA(backgroundmapinfo); if(mapinfo) backgroundmapinfo = newstring(mapinfo); } } -void restorebackground(bool force = false) -{ - if(renderedframe) - { +void restorebackground(bool force = false) { + if(renderedframe) { if(!force) return; setbackgroundinfo(); } renderbackground(backgroundcaption[0] ? backgroundcaption : NULL, backgroundmapshot, backgroundmapname[0] ? backgroundmapname : NULL, backgroundmapinfo, true); } -void bgquad(float x, float y, float w, float h, float tx = 0, float ty = 0, float tw = 1, float th = 1) -{ +void bgquad(float x, float y, float w, float h, float tx = 0, float ty = 0, float tw = 1, float th = 1) { gle::begin(GL_TRIANGLE_STRIP); gle::attribf(x, y); gle::attribf(tx, ty); gle::attribf(x+w, y); gle::attribf(tx + tw, ty); @@ -172,48 +154,36 @@ void bgquad(float x, float y, float w, float h, float tx = 0, float ty = 0, floa gle::end(); } -void renderbackground(const char *caption, Texture *mapshot, const char *mapname, const char *mapinfo, bool restore, bool force) -{ +void renderbackground(const char *caption, Texture *mapshot, const char *mapname, const char *mapinfo, bool restore, bool force) { if(!inbetweenframes && !force) return; - if(!restore || force) stopsounds(); // stop sounds while loading - int w = screenw, h = screenh; if(forceaspect) w = int(ceil(h*forceaspect)); getbackgroundres(w, h); gettextres(w, h); - static int lastupdate = -1, lastw = -1, lasth = -1; static int numdecals = 0; static struct decal { float x, y, size; int side; } decals[12]; - if((renderedframe && !mainmenu && lastupdate != lastmillis) || lastw != w || lasth != h) - { + if((renderedframe && !mainmenu && lastupdate != lastmillis) || lastw != w || lasth != h) { lastupdate = lastmillis; lastw = w; lasth = h; - numdecals = sizeof(decals)/sizeof(decals[0]); numdecals = numdecals/3 + rnd((numdecals*2)/3 + 1); float maxsize = min(w, h)/16.0f; - loopi(numdecals) - { + loopi(numdecals) { decal d = { rndscale(w), rndscale(h), maxsize/2 + rndscale(maxsize/2), rnd(2) }; decals[i] = d; } } else if(lastupdate != lastmillis) lastupdate = lastmillis; - - loopi(restore ? 1 : 3) - { + loopi(restore ? 1 : 3) { hudmatrix.ortho(0, w, h, 0, -1, 1); resethudmatrix(); - hudshader->set(); gle::colorf(1, 1, 1); - gle::defvertex(2); gle::deftexcoord0(); - settexture("background/daemex.png", 0); bgquad(0, 0, screenw, screenh, 0, 0, 1, 1); glEnable(GL_BLEND); @@ -221,8 +191,7 @@ void renderbackground(const char *caption, Texture *mapshot, const char *mapname lx = 0.5f*(w - lw), ly = 0.5f*(h*0.5f - lh); settexture((maxtexsize ? min(maxtexsize, hwtexsize) : hwtexsize) >= 1024 && (screenw > 1280 || screenh > 800) ? "data/logo_1024.png" : "data/logo.png", 3); bgquad(lx, ly, lw, lh); - if(caption) - { + if(caption) { int tw = text_width(caption); float tsz = 0.04f*min(w, h)/FONTH, tx = 0.5f*(w - tw*tsz), ty = h - 0.075f*1.5f*min(w, h) - 1.25f*FONTH*tsz; @@ -233,23 +202,19 @@ void renderbackground(const char *caption, Texture *mapshot, const char *mapname draw_text(caption, 0, 0); pophudmatrix(); } - if(mapshot || mapname) - { + if(mapshot || mapname) { int infowidth = 12*FONTH; float sz = 0.35f*min(w, h), msz = (0.75f*min(w, h) - sz)/(infowidth + FONTH), x = 0.5f*(w-sz), y = ly+lh - sz/15; - if(mapinfo) - { + if(mapinfo) { int mw, mh; text_bounds(mapinfo, mw, mh, infowidth); x -= 0.5f*(mw*msz + FONTH*msz); } - if(mapshot && mapshot!=notexture) - { + if(mapshot && mapshot!=notexture) { glBindTexture(GL_TEXTURE_2D, mapshot->id); bgquad(x, y, sz, sz); } - else - { + else { int qw, qh; text_bounds("?", qw, qh); float qsz = sz*0.5f/max(qw, qh); @@ -263,8 +228,7 @@ void renderbackground(const char *caption, Texture *mapshot, const char *mapname } settexture("data/mapshot_frame.png", 3); bgquad(x, y, sz, sz); - if(mapname) - { + if(mapname) { int tw = text_width(mapname); float tsz = sz/(8*FONTH), tx = 0.9f*sz - tw*tsz, ty = 0.9f*sz - FONTH*tsz; @@ -276,8 +240,7 @@ void renderbackground(const char *caption, Texture *mapshot, const char *mapname draw_text(mapname, 0, 0); pophudmatrix(); } - if(mapinfo) - { + if(mapinfo) { pushhudmatrix(); hudmatrix.translate(x+sz+FONTH*msz, y, 0); hudmatrix.scale(msz, msz, 1); @@ -289,7 +252,6 @@ void renderbackground(const char *caption, Texture *mapshot, const char *mapname glDisable(GL_BLEND); if(!restore) swapbuffers(false); } - if(!restore) setbackgroundinfo(caption, mapshot, mapname, mapinfo); } @@ -297,42 +259,31 @@ VAR(progressbackground, 0, 0, 1); float loadprogress = 0; -void renderprogress(float bar, const char *text, GLuint tex, bool background) // also used during loading -{ +void renderprogress(float bar, const char *text, GLuint tex, bool background) { // also used during loading { if(!inbetweenframes || drawtex) return; - extern int menufps, maxfps; int fps = menufps ? (maxfps ? min(maxfps, menufps) : menufps) : maxfps; - if(fps) - { + if(fps) { static int lastprogress = 0; int ticks = SDL_GetTicks(), diff = ticks - lastprogress; if(bar > 0 && diff >= 0 && diff < (1000 + fps-1)/fps) return; lastprogress = ticks; } - clientkeepalive(); // make sure our connection doesn't time out while loading maps etc. - SDL_PumpEvents(); // keep the event queue awake to avoid 'beachball' cursor - extern int mesa_swap_bug, curvsync; bool forcebackground = progressbackground || (mesa_swap_bug && (curvsync || totalmillis==1)); if(background || forcebackground) restorebackground(forcebackground); - int w = screenw, h = screenh; if(forceaspect) w = int(ceil(h*forceaspect)); getbackgroundres(w, h); gettextres(w, h); - hudmatrix.ortho(0, w, h, 0, -1, 1); resethudmatrix(); - hudshader->set(); gle::colorf(1, 1, 1); - gle::defvertex(2); gle::deftexcoord0(); - float fh = 0.075f*min(w, h), fw = fh*10, fx = renderedframe ? w - fw - fh/4 : 0.5f*(w - fw), fy = renderedframe ? fh/4 : h - fh*1.5f, @@ -340,10 +291,8 @@ void renderprogress(float bar, const char *text, GLuint tex, bool background) fv1 = 0/64.0f, fv2 = 52/64.0f; settexture("data/loading_frame.png", 3); bgquad(fx, fy, fw, fh, fu1, fv1, fu2-fu1, fv2-fv1); - glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - float bw = fw*(511 - 2*17)/511.0f, bh = fh*20/52.0f, bx = fx + fw*17/511.0f, by = fy + fh*16/52.0f, bv1 = 0/32.0f, bv2 = 20/32.0f, @@ -351,29 +300,24 @@ void renderprogress(float bar, const char *text, GLuint tex, bool background) eu1 = 23/32.0f, eu2 = 30/32.0f, ew = fw*7/511.0f, mw = bw - sw - ew, ex = bx+sw + max(mw*bar, fw*7/511.0f); - if(bar > 0) - { + if(bar > 0) { settexture("data/loading_bar.png", 3); gle::begin(GL_QUADS); gle::attribf(bx, by); gle::attribf(su1, bv1); gle::attribf(bx+sw, by); gle::attribf(su2, bv1); gle::attribf(bx+sw, by+bh); gle::attribf(su2, bv2); gle::attribf(bx, by+bh); gle::attribf(su1, bv2); - gle::attribf(bx+sw, by); gle::attribf(su2, bv1); gle::attribf(ex, by); gle::attribf(eu1, bv1); gle::attribf(ex, by+bh); gle::attribf(eu1, bv2); gle::attribf(bx+sw, by+bh); gle::attribf(su2, bv2); - gle::attribf(ex, by); gle::attribf(eu1, bv1); gle::attribf(ex+ew, by); gle::attribf(eu2, bv1); gle::attribf(ex+ew, by+bh); gle::attribf(eu2, bv2); gle::attribf(ex, by+bh); gle::attribf(eu1, bv2); gle::end(); } - - if(text) - { + if(text) { int tw = text_width(text); float tsz = bh*0.8f/FONTH; if(tw*tsz > mw) tsz = mw/tw; @@ -384,22 +328,17 @@ void renderprogress(float bar, const char *text, GLuint tex, bool background) draw_text(text, 0, 0); pophudmatrix(); } - glDisable(GL_BLEND); - - if(tex) - { + if(tex) { glBindTexture(GL_TEXTURE_2D, tex); float sz = 0.35f*min(w, h), x = 0.5f*(w-sz), y = 0.5f*min(w, h) - sz/15; bgquad(x, y, sz, sz); - glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); settexture("data/mapshot_frame.png", 3); bgquad(x, y, sz, sz); glDisable(GL_BLEND); } - swapbuffers(false); } @@ -407,25 +346,20 @@ int keyrepeatmask = 0, textinputmask = 0; Uint32 textinputtime = 0; VAR(textinputfilter, 0, 5, 1000); -void keyrepeat(bool on, int mask) -{ +void keyrepeat(bool on, int mask) { if(on) keyrepeatmask |= mask; else keyrepeatmask &= ~mask; } -void textinput(bool on, int mask) -{ - if(on) - { - if(!textinputmask) - { +void textinput(bool on, int mask) { + if(on) { + if(!textinputmask) { SDL_StartTextInput(); textinputtime = SDL_GetTicks(); } textinputmask |= mask; } - else if(textinputmask) - { + else if(textinputmask) { textinputmask &= ~mask; if(!textinputmask) SDL_StopTextInput(); } @@ -439,34 +373,27 @@ bool shouldgrab = false, grabinput = false, minimized = false, canrelativemouse VAR(sdl_xgrab_bug, 0, 0, 1); #endif -void inputgrab(bool on, bool delay = false) -{ +void inputgrab(bool on, bool delay = false) { #ifdef SDL_VIDEO_DRIVER_X11 bool wasrelativemouse = relativemouse; #endif - if(on) - { + if(on) { SDL_ShowCursor(SDL_FALSE); - if(canrelativemouse && userelativemouse) - { - if(SDL_SetRelativeMouseMode(SDL_TRUE) >= 0) - { + if(canrelativemouse && userelativemouse) { + if(SDL_SetRelativeMouseMode(SDL_TRUE) >= 0) { SDL_SetWindowGrab(screen, SDL_TRUE); relativemouse = true; } - else - { + else { SDL_SetWindowGrab(screen, SDL_FALSE); canrelativemouse = false; relativemouse = false; } } } - else - { + else { SDL_ShowCursor(SDL_TRUE); - if(relativemouse) - { + if(relativemouse) { SDL_SetWindowGrab(screen, SDL_FALSE); SDL_SetRelativeMouseMode(SDL_FALSE); relativemouse = false; @@ -475,15 +402,12 @@ void inputgrab(bool on, bool delay = false) shouldgrab = delay; #ifdef SDL_VIDEO_DRIVER_X11 - if((relativemouse || wasrelativemouse) && sdl_xgrab_bug) - { + if((relativemouse || wasrelativemouse) && sdl_xgrab_bug) { // Workaround for buggy SDL X11 pointer grabbing union { SDL_SysWMinfo info; uchar buf[sizeof(SDL_SysWMinfo) + 128]; }; SDL_GetVersion(&info.version); - if(SDL_GetWindowWMInfo(screen, &info) && info.subsystem == SDL_SYSWM_X11) - { - if(relativemouse) - { + if(SDL_GetWindowWMInfo(screen, &info) && info.subsystem == SDL_SYSWM_X11) { + if(relativemouse) { uint mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask; XGrabPointer(info.info.x11.display, info.info.x11.window, True, mask, GrabModeAsync, GrabModeAsync, info.info.x11.window, None, CurrentTime); } @@ -495,17 +419,14 @@ void inputgrab(bool on, bool delay = false) bool initwindowpos = false; -void setfullscreen(bool enable) -{ +void setfullscreen(bool enable) { if(!screen) return; //initwarning(enable ? "fullscreen" : "windowed"); extern int fullscreendesktop; SDL_SetWindowFullscreen(screen, enable ? (fullscreendesktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN) : 0); - if(!enable) - { + if(!enable) { SDL_SetWindowSize(screen, scr_w, scr_h); - if(initwindowpos) - { + if(initwindowpos) { int winx = SDL_WINDOWPOS_CENTERED, winy = SDL_WINDOWPOS_CENTERED; SDL_SetWindowPosition(screen, winx, winy); initwindowpos = false; @@ -519,74 +440,62 @@ VARF(fullscreen, 0, 0, 1, setfullscreen(fullscreen!=0)); VARF(fullscreen, 0, 1, 1, setfullscreen(fullscreen!=0)); #endif -void resetfullscreen() -{ +void resetfullscreen() { setfullscreen(false); setfullscreen(true); } VARF(fullscreendesktop, 0, 0, 1, if(fullscreen) resetfullscreen()); -void screenres(int w, int h) -{ +void screenres(int w, int h) { scr_w = clamp(w, SCR_MINW, SCR_MAXW); scr_h = clamp(h, SCR_MINH, SCR_MAXH); - if(screen) - { - if(fullscreendesktop) - { + if(screen) { + if(fullscreendesktop) { scr_w = min(scr_w, desktopw); scr_h = min(scr_h, desktoph); } - if(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN) - { + if(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN) { if(fullscreendesktop) gl_resize(); else resetfullscreen(); initwindowpos = true; } - else - { + else { SDL_SetWindowSize(screen, scr_w, scr_h); SDL_SetWindowPosition(screen, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); initwindowpos = false; } } - else - { + else { initwarning("screen resolution"); } } ICOMMAND(screenres, "ii", (int *w, int *h), screenres(*w, *h)); -static void setgamma(int val) -{ +static void setgamma(int val) { if(screen && SDL_SetWindowBrightness(screen, val/100.0f) < 0) conoutf(CON_ERROR, "Could not set gamma: %s", SDL_GetError()); } static int curgamma = 100; -VARFNP(gamma, reqgamma, 30, 100, 300, -{ +VARFNP(gamma, reqgamma, 30, 100, 300, { if(initing || reqgamma == curgamma) return; curgamma = reqgamma; setgamma(curgamma); }); -void restoregamma() -{ +void restoregamma() { if(initing || reqgamma == 100) return; curgamma = reqgamma; setgamma(curgamma); } -void cleargamma() -{ +void cleargamma() { if(curgamma != 100 && screen) SDL_SetWindowBrightness(screen, 1.0f); } int curvsync = -1; -void restorevsync() -{ +void restorevsync() { if(initing || !glcontext) return; extern int vsync, vsynctear; if(!SDL_GL_SetSwapInterval(vsync ? (vsynctear ? -1 : 1) : 0)) @@ -596,40 +505,31 @@ void restorevsync() VARFP(vsync, 0, 0, 1, restorevsync()); VARFP(vsynctear, 0, 0, 1, { if(vsync) restorevsync(); }); -void setupscreen() -{ - if(glcontext) - { +void setupscreen() { + if(glcontext) { SDL_GL_DeleteContext(glcontext); glcontext = NULL; } - if(screen) - { + if(screen) { SDL_DestroyWindow(screen); screen = NULL; } curvsync = -1; - SDL_Rect desktop; if(SDL_GetDisplayBounds(0, &desktop) < 0) fatal("failed querying desktop bounds: %s", SDL_GetError()); desktopw = desktop.w; desktoph = desktop.h; - if(scr_h < 0) scr_h = fullscreen ? desktoph : SCR_DEFAULTH; if(scr_w < 0) scr_w = (scr_h*desktopw)/desktoph; scr_w = clamp(scr_w, SCR_MINW, SCR_MAXW); scr_h = clamp(scr_h, SCR_MINH, SCR_MAXH); - if(fullscreendesktop) - { + if(fullscreendesktop) { scr_w = min(scr_w, desktopw); scr_h = min(scr_h, desktoph); } - int winx = SDL_WINDOWPOS_UNDEFINED, winy = SDL_WINDOWPOS_UNDEFINED, winw = scr_w, winh = scr_h, flags = SDL_WINDOW_RESIZABLE; - if(fullscreen) - { - if(fullscreendesktop) - { + if(fullscreen) { + if(fullscreendesktop) { winw = desktopw; winh = desktoph; flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; @@ -637,42 +537,35 @@ void setupscreen() else flags |= SDL_WINDOW_FULLSCREEN; initwindowpos = true; } - SDL_GL_ResetAttributes(); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); - static const int configs[] = - { + static const int configs[] = { 0x3, /* try everything */ 0x2, 0x1, /* try disabling one at a time */ 0 /* try disabling everything */ }; int config = 0; if(!depthbits) SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - if(!fsaa) - { + if(!fsaa) { SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); } - loopi(sizeof(configs)/sizeof(configs[0])) - { + loopi(sizeof(configs)/sizeof(configs[0])) { config = configs[i]; if(!depthbits && config&1) continue; if(fsaa<=0 && config&2) continue; if(depthbits) SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, config&1 ? depthbits : 24); - if(fsaa>0) - { + if(fsaa>0) { SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, config&2 ? 1 : 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, config&2 ? fsaa : 0); } screen = SDL_CreateWindow("Cube 2: Sauerbraten", winx, winy, winw, winh, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_MOUSE_FOCUS | flags); if(!screen) continue; - static const int glversions[] = { 33, 32, 31, 30, 20 }; - loopj(sizeof(glversions)/sizeof(glversions[0])) - { + loopj(sizeof(glversions)/sizeof(glversions[0])) { glcompat = glversions[j] <= 30 ? 1 : 0; SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, glversions[j] / 10); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, glversions[j] % 10); @@ -684,24 +577,18 @@ void setupscreen() } if(!screen) fatal("failed to create OpenGL window: %s", SDL_GetError()); else if(!glcontext) fatal("failed to create OpenGL context: %s", SDL_GetError()); - else - { + else { if(depthbits && (config&1)==0) conoutf(CON_WARN, "%d bit z-buffer not supported - disabling", depthbits); if(fsaa>0 && (config&2)==0) conoutf(CON_WARN, "%dx anti-aliasing not supported - disabling", fsaa); } - SDL_SetWindowMinimumSize(screen, SCR_MINW, SCR_MINH); SDL_SetWindowMaximumSize(screen, SCR_MAXW, SCR_MAXH); - SDL_GetWindowSize(screen, &screenw, &screenh); } -void resetgl() -{ +void resetgl() { clearchanges(CHANGE_GFX); - renderbackground("resetting OpenGL"); - extern void cleanupva(); extern void cleanupparticles(); extern void cleanupdecals(); @@ -719,11 +606,9 @@ void resetgl() cleanshadowmap(); cleanupshaders(); cleanupgl(); - setupscreen(); inputgrab(grabinput); gl_init(); - inbetweenframes = false; if(!reloadtexture(*notexture) || !reloadtexture("data/logo.png") || @@ -750,13 +635,10 @@ COMMAND(resetgl, ""); static queue events; -static inline bool filterevent(const SDL_Event &event) -{ - switch(event.type) - { +static inline bool filterevent(const SDL_Event &event) { + switch(event.type) { case SDL_MOUSEMOTION: - if(grabinput && !relativemouse && !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) - { + if(grabinput && !relativemouse && !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) { if(event.motion.x == screenw / 2 && event.motion.y == screenh / 2) return false; // ignore any motion events generated by SDL_WarpMouse } @@ -765,10 +647,8 @@ static inline bool filterevent(const SDL_Event &event) return true; } -template static inline bool pumpevents(queue &events) -{ - while(events.empty()) - { +template static inline bool pumpevents(queue &events) { + while(events.empty()) { SDL_PumpEvents(); databuf buf = events.reserve(events.capacity()); int n = SDL_PeepEvents(buf.getbuf(), buf.remaining(), SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT); @@ -781,14 +661,11 @@ template static inline bool pumpevents(queue &events static int interceptkeysym = 0; -static int interceptevents(void *data, SDL_Event *event) -{ - switch(event->type) - { +static int interceptevents(void *data, SDL_Event *event) { + switch(event->type) { case SDL_MOUSEMOTION: return 0; case SDL_KEYDOWN: - if(event->key.keysym.sym == interceptkeysym) - { + if(event->key.keysym.sym == interceptkeysym) { interceptkeysym = -interceptkeysym; return 0; } @@ -797,20 +674,16 @@ static int interceptevents(void *data, SDL_Event *event) return 1; } -static void clearinterceptkey() -{ +static void clearinterceptkey() { SDL_DelEventWatch(interceptevents, NULL); interceptkeysym = 0; } -bool interceptkey(int sym) -{ - if(!interceptkeysym) - { +bool interceptkey(int sym) { + if(!interceptkeysym) { interceptkeysym = sym; SDL_FilterEvents(interceptevents, NULL); - if(interceptkeysym < 0) - { + if(interceptkeysym < 0) { interceptkeysym = 0; return true; } @@ -818,8 +691,7 @@ bool interceptkey(int sym) } else if(abs(interceptkeysym) != sym) interceptkeysym = sym; SDL_PumpEvents(); - if(interceptkeysym < 0) - { + if(interceptkeysym < 0) { clearinterceptkey(); interceptkeysym = sym; SDL_FilterEvents(interceptevents, NULL); @@ -829,24 +701,19 @@ bool interceptkey(int sym) return false; } -static void ignoremousemotion() -{ +static void ignoremousemotion() { SDL_PumpEvents(); SDL_FlushEvent(SDL_MOUSEMOTION); } -static void resetmousemotion() -{ - if(grabinput && !relativemouse && !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) - { +static void resetmousemotion() { + if(grabinput && !relativemouse && !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) { SDL_WarpMouseInWindow(screen, screenw / 2, screenh / 2); } } -static void checkmousemotion(int &dx, int &dy) -{ - while(pumpevents(events)) - { +static void checkmousemotion(int &dx, int &dy) { + while(pumpevents(events)) { SDL_Event &event = events.removing(); if(event.type != SDL_MOUSEMOTION) return; dx += event.motion.xrel; @@ -855,46 +722,35 @@ static void checkmousemotion(int &dx, int &dy) } } -void checkinput() -{ +void checkinput() { if(interceptkeysym) clearinterceptkey(); //int lasttype = 0, lastbut = 0; bool mousemoved = false; int focused = 0; - while(pumpevents(events)) - { + while(pumpevents(events)) { SDL_Event &event = events.remove(); - if(focused && event.type!=SDL_WINDOWEVENT) { if(grabinput != (focused>0)) inputgrab(grabinput = focused>0, shouldgrab); focused = 0; } - - switch(event.type) - { + switch(event.type) { case SDL_QUIT: quit(); return; - case SDL_TEXTINPUT: - if(textinputmask && int(event.text.timestamp-textinputtime) >= textinputfilter) - { + if(textinputmask && int(event.text.timestamp-textinputtime) >= textinputfilter) { uchar buf[SDL_TEXTINPUTEVENT_TEXT_SIZE+1]; size_t len = decodeutf8(buf, sizeof(buf)-1, (const uchar *)event.text.text, strlen(event.text.text)); if(len > 0) { buf[len] = '\0'; processtextinput((const char *)buf, len); } } break; - case SDL_KEYDOWN: case SDL_KEYUP: if(keyrepeatmask || !event.key.repeat) processkey(event.key.keysym.sym, event.key.state==SDL_PRESSED, event.key.keysym.mod | SDL_GetModState()); break; - case SDL_WINDOWEVENT: - switch(event.window.event) - { + switch(event.window.event) { case SDL_WINDOWEVENT_CLOSE: quit(); break; - case SDL_WINDOWEVENT_FOCUS_GAINED: shouldgrab = true; break; @@ -902,30 +758,23 @@ void checkinput() shouldgrab = false; focused = 1; break; - case SDL_WINDOWEVENT_LEAVE: case SDL_WINDOWEVENT_FOCUS_LOST: shouldgrab = false; focused = -1; break; - case SDL_WINDOWEVENT_MINIMIZED: minimized = true; break; - case SDL_WINDOWEVENT_MAXIMIZED: case SDL_WINDOWEVENT_RESTORED: minimized = false; break; - case SDL_WINDOWEVENT_RESIZED: break; - - case SDL_WINDOWEVENT_SIZE_CHANGED: - { + case SDL_WINDOWEVENT_SIZE_CHANGED: { SDL_GetWindowSize(screen, &screenw, &screenh); - if(!fullscreendesktop || !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) - { + if(!fullscreendesktop || !(SDL_GetWindowFlags(screen) & SDL_WINDOW_FULLSCREEN)) { scr_w = clamp(screenw, SCR_MINW, SCR_MAXW); scr_h = clamp(screenh, SCR_MINH, SCR_MAXH); } @@ -934,10 +783,8 @@ void checkinput() } } break; - case SDL_MOUSEMOTION: - if(grabinput) - { + if(grabinput) { int dx = event.motion.xrel, dy = event.motion.yrel; checkmousemotion(dx, dy); if(!g3d_movecursor(dx, dy)) mousemove(dx, dy); @@ -945,12 +792,10 @@ void checkinput() } else if(shouldgrab) inputgrab(grabinput = true); break; - case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: //if(lasttype==event.type && lastbut==event.button.button) break; // why?? get event twice without it - switch(event.button.button) - { + switch(event.button.button) { case SDL_BUTTON_LEFT: processkey(-1, event.button.state==SDL_PRESSED); break; case SDL_BUTTON_MIDDLE: processkey(-2, event.button.state==SDL_PRESSED); break; case SDL_BUTTON_RIGHT: processkey(-3, event.button.state==SDL_PRESSED); break; @@ -960,7 +805,6 @@ void checkinput() //lasttype = event.type; //lastbut = event.button.button; break; - case SDL_MOUSEWHEEL: if(event.wheel.y > 0) { processkey(-4, true); processkey(-4, false); } else if(event.wheel.y < 0) { processkey(-5, true); processkey(-5, false); } @@ -971,8 +815,7 @@ void checkinput() if(mousemoved) resetmousemotion(); } -void swapbuffers(bool overlay) -{ +void swapbuffers(bool overlay) { gle::disable(); SDL_GL_SwapWindow(screen); } @@ -980,23 +823,19 @@ void swapbuffers(bool overlay) VAR(menufps, 0, 60, 1000); VARP(maxfps, 0, 200, 1000); -void limitfps(int &millis, int curmillis) -{ +void limitfps(int &millis, int curmillis) { int limit = (mainmenu || minimized) && menufps ? (maxfps ? min(maxfps, menufps) : menufps) : maxfps; if(!limit) return; static int fpserror = 0; int delay = 1000/limit - (millis-curmillis); if(delay < 0) fpserror = 0; - else - { + else { fpserror += 1000%limit; - if(fpserror >= limit) - { + if(fpserror >= limit) { ++delay; fpserror -= limit; } - if(delay > 0) - { + if(delay > 0) { SDL_Delay(delay); millis += delay; } @@ -1007,36 +846,30 @@ void limitfps(int &millis, int curmillis) int fpspos = 0, fpshistory[MAXFPSHISTORY]; -void resetfpshistory() -{ +void resetfpshistory() { loopi(MAXFPSHISTORY) fpshistory[i] = 1; fpspos = 0; } -void updatefpshistory(int millis) -{ +void updatefpshistory(int millis) { fpshistory[fpspos++] = max(1, min(1000, millis)); if(fpspos>=MAXFPSHISTORY) fpspos = 0; } -void getfps(int &fps, int &bestdiff, int &worstdiff) -{ +void getfps(int &fps, int &bestdiff, int &worstdiff) { int total = fpshistory[MAXFPSHISTORY-1], best = total, worst = total; - loopi(MAXFPSHISTORY-1) - { + loopi(MAXFPSHISTORY-1) { int millis = fpshistory[i]; total += millis; if(millis < best) best = millis; if(millis > worst) worst = millis; } - fps = (1000*MAXFPSHISTORY)/total; bestdiff = 1000/best-fps; worstdiff = fps-1000/worst; } -void getfps_(int *raw) -{ +void getfps_(int *raw) { int fps, bestdiff, worstdiff; if(*raw) fps = 1000/fpshistory[(fpspos+MAXFPSHISTORY-1)%MAXFPSHISTORY]; else getfps(fps, bestdiff, worstdiff); @@ -1047,8 +880,7 @@ COMMANDN(getfps, getfps_, "i"); bool inbetweenframes = false, renderedframe = true; -static bool findarg(int argc, char **argv, const char *str) -{ +static bool findarg(int argc, char **argv, const char *str) { for(int i = 1; i0, dedicated>1); // never returns if dedicated ASSERT(dedicated <= 1); game::initclient(); - logoutf("init: video"); SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "0"); SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); setupscreen(); SDL_ShowCursor(SDL_FALSE); SDL_StopTextInput(); // workaround for spurious text-input events getting sent on first text input toggle? - logoutf("init: gl"); gl_checkextensions(); gl_init(); notexture = textureload("packages/textures/texture_error.png"); if(!notexture) fatal("could not find core textures"); - logoutf("init: console"); if(!execfile("data/stdlib.cfg", false)) fatal("cannot find data files (you are running from the wrong folder, try .bat file in the main folder)"); // this is the first file we load. if(!execfile("packages/fonts/default.cfg", false)) fatal("cannot find font definitions"); if(!setfont("default")) fatal("no default font specified"); - inbetweenframes = true; renderbackground("initializing..."); - logoutf("init: world"); camera1 = player = game::iterdynents(0); emptymap(0, true, NULL, false); - logoutf("init: sound"); initsound(); - logoutf("init: cfg"); initing = INIT_LOAD; execfile("data/keymap.cfg"); @@ -1189,52 +999,35 @@ int main(int argc, char **argv) execfile("data/game.cfg"); execfile("data/custom_maps_menu.cfg"); if(game::savedservers()) execfile(game::savedservers(), false); - identflags |= IDF_PERSIST; - - if(!execfile(game::savedconfig(), false)) - { + if(!execfile(game::savedconfig(), false)) { execfile(game::defaultconfig()); writecfg(game::restoreconfig()); } execfile(game::autoexec(), false); - identflags &= ~IDF_PERSIST; - initing = INIT_GAME; game::loadconfigs(); - initing = NOT_INITING; - logoutf("init: render"); restoregamma(); restorevsync(); loadshaders(); initparticles(); initdecals(); - identflags |= IDF_PERSIST; - logoutf("init: mainloop"); - if(execfile("once.cfg", false)) remove(findfile("once.cfg", "rb")); - - if(load) - { + if(load) { logoutf("init: localconnect"); //localconnect(); game::changemap(load); } - if(initscript) execute(initscript); - resetfpshistory(); - inputgrab(grabinput = true); ignoremousemotion(); - - for(;;) - { + for(;;) { static int frames = 0; int millis = getclockmillis(); limitfps(millis, totalmillis); @@ -1248,37 +1041,26 @@ int main(int argc, char **argv) lastmillis += curtime; totalmillis = millis; updatetime(); - checkinput(); menuprocess(); tryedit(); - if(lastmillis) game::updateworld(); - checksleep(lastmillis); - serverslice(false, 0); - if(frames) updatefpshistory(elapsedtime); frames++; - // miscellaneous general game effects recomputecamera(); updateparticles(); updatesounds(); - if(minimized) continue; - inbetweenframes = false; - glClearColor(0,0,0,1);///TODO - if(mainmenu) gl_drawmainmenu(); else gl_drawframe(); swapbuffers(); renderedframe = inbetweenframes = true; } - ASSERT(0); return EXIT_FAILURE; } -- cgit v1.2.3