diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d4296d362..6870268fe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -292,7 +292,6 @@ if(${SRB2_CONFIG_HAVE_BLUA}) lua_script.c lua_skinlib.c lua_thinkerlib.c - lua_udatalib.c lua_glib.c ) set(SRB2_LUA_HEADERS @@ -302,7 +301,6 @@ if(${SRB2_CONFIG_HAVE_BLUA}) lua_profile.h lua_libs.h lua_script.h - lua_udatalib.h lua_glib.h ) diff --git a/src/Sourcefile b/src/Sourcefile index f09a143bd..375197e7e 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -97,6 +97,5 @@ lua_playerlib.c lua_script.c lua_skinlib.c lua_thinkerlib.c -lua_udatalib.c lua_glib.c lua_profile.cpp diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 500fa9b16..098a763ad 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -1043,6 +1043,8 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_rumble[i]); CV_RegisterVar(&cv_rumble_strength[i]); + + CV_RegisterVar(&cv_rumble_wallbonk[i]); CV_RegisterVar(&cv_rumble_spinout[i]); CV_RegisterVar(&cv_rumble_sneakerboost[i]); CV_RegisterVar(&cv_rumble_offroad[i]); diff --git a/src/g_demo.c b/src/g_demo.c index d39bbaec7..e9f5236ef 100644 --- a/src/g_demo.c +++ b/src/g_demo.c @@ -63,7 +63,6 @@ // for replay dates #include -#include // menu demo things UINT32 demoDelayTime = 15*TICRATE; @@ -2387,7 +2386,9 @@ void G_LoadDemoInfo(menudemo_t *pdemo) #if defined (_WIN32) // return the file creation time // useful for demos that were renamed -static long G_GetCreationTime(char *filepath) +// windows only, as file creation times are +// very unreliable (or non existant depending on fs) on linux +static time_t G_GetCreationTime(char *filepath) { struct stat fileinfo; @@ -2398,35 +2399,24 @@ static long G_GetCreationTime(char *filepath) } #endif -static char *G_GetDemoDate(menudemo_t *pdemo) +static char *G_GetDemoDate(menudemo_t *pdemo, const char *dateformat) { char *endPos = NULL; - static char datetime[11]; + char *datetime; time_t file_time = 0; - const char *format = NULL; - struct tm *tm_buf = NULL; + struct tm tm_buf; CLEANUP(pfree) char *filename = NULL; // get le filepath filename = strdup(pdemo->filepath); if (!filename) - { -#if defined (_WIN32) - // if we cant get a filename try just getting the file creation time - file_time = G_GetCreationTime(pdemo->filepath); - goto skipfilenametime; -#else return NULL; -#endif - } // get the actual filename Zzz... nameonly(filename); - // the first 10 characters of a replay name usually is a unix timestamp - - // not long enough to contain a valid timestamp + // the first 10 characters of a replay name is a unix timestamp if (strlen(filename) < 10) { #if defined (_WIN32) @@ -2458,67 +2448,49 @@ static char *G_GetDemoDate(menudemo_t *pdemo) #endif } -#if defined (_WIN32) -skipfilenametime: -#endif - // then throw it into localtime to get an actual human readable format lmao - tm_buf = localtime(&file_time); - - // cant believe we ended up in 1970 - if (tm_buf == NULL || tm_buf->tm_year <= 110) +#if defined(_WIN32) +skipfilenametime: + if ((localtime_s(&tm_buf, &file_time) != 0) || + (tm_buf.tm_year <= 110)) // cant believe we ended up in 1970 { -#if defined (_WIN32) // uh ohh, we got an invalid time // try one more time getting the creation time file_time = G_GetCreationTime(pdemo->filepath); - tm_buf = localtime(&file_time); - if (tm_buf == NULL || tm_buf->tm_year <= 110) + if ((localtime_s(&tm_buf, &file_time) != 0) || + (tm_buf.tm_year <= 110)) return NULL; - - goto gotcreationtime; + } #else + if ((localtime_r(&file_time, &tm_buf) == NULL) || + (tm_buf.tm_year <= 110)) // cant believe we ended up in 1970 return NULL; #endif - } -#if defined (_WIN32) -gotcreationtime: -#endif + datetime = malloc(sizeof(pdemo->date)); // mallocma balls - // US ppl are special (: - if (cv_demodateformat.value == 2) - format = "%m.%d.%Y"; - else if (cv_demodateformat.value == 1) - format = "%d.%m.%Y"; - else - { - const char *locale = setlocale(LC_TIME, NULL); + // no mallocma balls... :c + if (!datetime) + return NULL; - if (locale == NULL) - format = "%d.%m.%Y"; // fallback to non US format - else if (strstr(locale, "en_US")) - format = "%m.%d.%Y"; - else - format = "%d.%m.%Y"; - } - - if (strftime(datetime, sizeof(datetime), format, tm_buf) == 0) + if (strftime(datetime, sizeof(pdemo->date), dateformat, &tm_buf) == 0) { + free(datetime); return NULL; } return datetime; } -void G_LoadDemoTitle(menudemo_t *pdemo) +void G_LoadDemoTitle(menudemo_t *pdemo, const char *dateformat) { UINT8 infobuffer[96], *info_p; UINT16 pdemoversion; - char *demodate = NULL; size_t count; + memset(pdemo->date, 0, sizeof(pdemo->date)); + FILE *handle = fopen(pdemo->filepath, "rb"); if (!handle) @@ -2546,8 +2518,6 @@ void G_LoadDemoTitle(menudemo_t *pdemo) pdemoversion = READUINT16(info_p); - memset(pdemo->date, 0, sizeof(pdemo->date)); - switch (pdemoversion) { case DEMOVERSION: // latest always supported @@ -2555,13 +2525,12 @@ void G_LoadDemoTitle(menudemo_t *pdemo) memcpy(pdemo->title, info_p, 64); // demo date - demodate = G_GetDemoDate(pdemo); - + char *demodate = G_GetDemoDate(pdemo, dateformat); if (demodate) { strlcpy(pdemo->date, demodate, sizeof(pdemo->date)); + free(demodate); } - break; #ifdef DEMO_COMPAT_100 case 0x0001: diff --git a/src/g_demo.h b/src/g_demo.h index 280185ce6..d880f3038 100644 --- a/src/g_demo.h +++ b/src/g_demo.h @@ -98,7 +98,7 @@ void G_SetDemoTime(UINT32 ptime, UINT32 plap); UINT8 G_CmpDemoTime(char *oldname, char *newname); void G_LoadDemoInfo(menudemo_t *pdemo); -void G_LoadDemoTitle(menudemo_t *pdemo); // For use in replay search feature +void G_LoadDemoTitle(menudemo_t *pdemo, const char *dateformat); // For use in replay search feature void G_DeferedPlayDemo(const char *demo); typedef enum diff --git a/src/g_input.c b/src/g_input.c index 31df065fc..8401722f4 100644 --- a/src/g_input.c +++ b/src/g_input.c @@ -17,14 +17,9 @@ #include "g_game.h" #include "keys.h" #include "hu_stuff.h" // need HUFONT start & end -#include "d_net.h" #include "console.h" -#include "m_menu.h" - #include "i_system.h" -#include "i_video.h" -#include "r_draw.h" #include "v_video.h" #define MAXMOUSESENSITIVITY 100 // sensitivity steps @@ -36,10 +31,11 @@ static CV_PossibleValue_t turnsmooth_cons_t[] = {{2, "Slow"}, {1, "Fast"}, {0, " // mouse values are used once consvar_t cv_mousexsens = {"mousexsens", "20", CV_SAVE, mousesens_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_mouseysens = {"mouseysens", "20", CV_SAVE, mousesens_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; + consvar_t cv_controlperkey = {"controlperkey", "One", CV_SAVE, onecontrolperkey_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_turnsmooth[MAXSPLITSCREENPLAYERS] = { - {"turnsmoothing", "Slow", CV_SAVE, turnsmooth_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}, + {"turnsmoothing", "Slow", CV_SAVE, turnsmooth_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}, {"turnsmoothing2", "Slow", CV_SAVE, turnsmooth_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}, {"turnsmoothing3", "Slow", CV_SAVE, turnsmooth_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}, {"turnsmoothing4", "Slow", CV_SAVE, turnsmooth_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL} @@ -81,6 +77,7 @@ consvar_t cv_rumble_##type [MAXSPLITSCREENPLAYERS] = {\ {"rumble4_" #type, "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}\ }; +IMPL_RUMBLE_TYPE(wallbonk); IMPL_RUMBLE_TYPE(spinout); IMPL_RUMBLE_TYPE(sneakerboost); IMPL_RUMBLE_TYPE(offroad); @@ -89,7 +86,6 @@ IMPL_RUMBLE_TYPE(stairjank); IMPL_RUMBLE_TYPE(brakedrift); IMPL_RUMBLE_TYPE(driftcharge); - static CV_PossibleValue_t gamepadled_cons_t[] = {{0, "Off"}, {1, "Skincolor"}, {2, "Mobjcolor"}, {0, NULL}}; consvar_t cv_gamepadled[MAXSPLITSCREENPLAYERS] = { {"gamepadled", "Skincolor", CV_SAVE|CV_CALL|CV_NOINIT, gamepadled_cons_t, led_off_handle, 0, NULL, NULL, 0, 0, NULL}, @@ -195,7 +191,6 @@ void G_MapEventsToControls(event_t *ev) { CONS_Debug(DBG_GAMELOGIC, "Bad downkey input %d\n",ev->data1); } - #endif break; @@ -951,16 +946,15 @@ void G_SetPlayerGamepadIndicatorColor(INT32 playernum, UINT8 color) I_Assert(playernum >= 0 && playernum < MAXSPLITSCREENPLAYERS); - if (cv_gamepadled[playernum].value == 0 || (window_notinfocus && cv_gamepadledifunfocused.value == 0) - || color >= MAXTRANSLATIONS) + if (cv_gamepadled[playernum].value == 0 || + (window_notinfocus && cv_gamepadledifunfocused.value == 0) || + color >= MAXTRANSLATIONS) { return; } if (!I_GamepadHasLED(playernum)) - { return; - } // so we can override this skincolor = color ? color : G_GetSkinColorForGamepad(playernum); @@ -990,29 +984,21 @@ void G_DeviceLEDTick(void) UINT8 i, newcolor; if (numcontrollers == 0) - { return; - } for (i = 0; i <= splitscreen; i++) { if (!cv_usejoystick[i].value || !cv_gamepadled[i].value) - { continue; - } // does not have a configurable led! :c if (!I_GamepadHasLED(i)) - { continue; - } newcolor = G_GetSkinColorForGamepad(i); if (curcolor[i] == newcolor) // dont update if same colour - { continue; - } G_SetPlayerGamepadIndicatorColor(i, newcolor); curcolor[i] = newcolor; @@ -1046,9 +1032,7 @@ void G_PlayerDeviceRumble(INT32 playernum, UINT16 low_strength, UINT16 high_stre I_Assert(playernum >= 0 && playernum < MAXSPLITSCREENPLAYERS); if (cv_rumble[playernum].value == 0 || (window_notinfocus && cv_rumbleifunfocused.value == 0)) - { return; - } // doesent need to be super precise // but ensure this is within range (0-65535) @@ -1077,46 +1061,34 @@ void G_DeviceRumbleTick(void) UINT8 i; if (dedicated || numcontrollers == 0 || gamestate != GS_LEVEL || demo.playback) - { return; - } for (i = 0; i <= splitscreen; i++) { if (!cv_usejoystick[i].value || !cv_rumble[i].value) - { continue; - } // does not support rumble! :c if (!I_GamepadHasRumble(i)) - { continue; - } if (camera[i].freecam) - { continue; - } // SDL rumble strength goes from a range or 0-65535 respectively // the higher, the stronger it is UINT16 low = 0, high = 0; // for how long the action should rumble, in ms - UINT16 lenght = 57; // 57ms is a short pulse, matches super well with this updating once per tic + UINT16 length = 57; // 57ms is a short pulse, matches super well with this updating once per tic const player_t *player = P_GetLocalPlayerForNum(i); if (!player) - { continue; - } // allow lua to do some crap for spectators if (player->spectator || !player->mo) - { continue; - } // reset the rumble if you exit or are ded lel if (player->exiting || @@ -1127,21 +1099,28 @@ void G_DeviceRumbleTick(void) continue; } - if (cv_rumble_spinout[i].value - && player->kartstuff[k_spinouttimer]) + if (cv_rumble_wallbonk[i].value && + (player->mo->eflags & MFE_JUSTBOUNCEDWALL)) + { + high = RUMBLE_STRONG; + low = RUMBLE_MODERATE; + } + else if (cv_rumble_spinout[i].value && + player->kartstuff[k_spinouttimer]) { //low = high = FRACUNIT / 6; - low = high = FixedMul((RUMBLE_VERYSTRONG), (FixedDiv(player->kartstuff[k_spinouttimer], (3*TICRATE / 2)))); // try do some some kinda fadeout, 3*TICRATE / 2 is the "default" spinout time + // try do some some kinda fadeout, 3*TICRATE / 2 is the "default" spinout time + low = high = FixedMul(RUMBLE_VERYSTRONG, FixedDiv(player->kartstuff[k_spinouttimer], (3*TICRATE / 2))); } - else if (cv_rumble_sneakerboost[i].value - && (player->kartstuff[k_sneakertimer] > (sneakertime-(TICRATE/2)))) + else if (cv_rumble_sneakerboost[i].value && + (player->kartstuff[k_sneakertimer] > (sneakertime-(TICRATE/2)))) { low = high = RUMBLE_STRONG; } - else if (cv_rumble_offroad[i].value - && (player->kartstuff[k_offroad]) - && (player->speed != 0) - && P_IsObjectOnGround(player->mo)) + else if (cv_rumble_offroad[i].value && + (player->kartstuff[k_offroad]) && + (player->speed != 0) && + P_IsObjectOnGround(player->mo)) { // weaken this depending on if you got hyu or invinc if (player->kartstuff[k_hyudorotimer]) @@ -1157,50 +1136,48 @@ void G_DeviceRumbleTick(void) low = high = RUMBLE_MODERATE; } } - else if (cv_rumble_bananadrag[i].value - && (player->kartstuff[k_bananadrag] > TICRATE) - && (player->speed != 0) - && P_IsObjectOnGround(player->mo)) + else if (cv_rumble_bananadrag[i].value && + (player->kartstuff[k_bananadrag] > TICRATE) && + (player->speed != 0) && + P_IsObjectOnGround(player->mo)) { if (leveltime & 1) // this is actually funny lel high = RUMBLE_MODERATE; } - else if (cv_rumble_stairjank[i].value - && (player->stairjank > 8) - && (player->speed != 0) - && P_IsObjectOnGround(player->mo)) + else if (cv_rumble_stairjank[i].value && + (player->stairjank > 8) && + (player->speed != 0) && + P_IsObjectOnGround(player->mo)) { low = high = RUMBLE_VERYWEAK; } - if (cv_rumble_brakedrift[i].value - && player->kartstuff[k_brakedrift]) + if (cv_rumble_brakedrift[i].value && + player->kartstuff[k_brakedrift]) { high = CLAMP((high + RUMBLE_VERYWEAK), 0, UINT16_MAX); } // pulse when gettin new driftlevel // let this come last - if (cv_rumble_driftcharge[i].value - && player->kartstuff[k_driftcharge] - && player->driftlevel) + if (cv_rumble_driftcharge[i].value && + player->kartstuff[k_driftcharge] && + player->driftlevel) { high = CLAMP((high + RUMBLE_VERYWEAK), 0, UINT16_MAX); // make it rumble slightly longer the higher your driftlevel! if (player->driftlevel == 2) - lenght = 114; + length = 114; else if (player->driftlevel == 3) - lenght = 174; + length = 174; } // hack alert! i just dont want this thing constantly resetting the rumble lol if (low == 0 && high == 0) - { continue; - } - G_PlayerDeviceRumble(i, low, high, lenght); + G_PlayerDeviceRumble(i, low, high, length); } } diff --git a/src/g_input.h b/src/g_input.h index bed5beb0e..310b82900 100644 --- a/src/g_input.h +++ b/src/g_input.h @@ -118,6 +118,8 @@ extern consvar_t cv_gamepadled[MAXSPLITSCREENPLAYERS]; extern consvar_t cv_rumble[MAXSPLITSCREENPLAYERS]; extern consvar_t cv_rumble_strength[MAXSPLITSCREENPLAYERS]; +// rumble types +extern consvar_t cv_rumble_wallbonk[MAXSPLITSCREENPLAYERS]; extern consvar_t cv_rumble_spinout[MAXSPLITSCREENPLAYERS]; extern consvar_t cv_rumble_sneakerboost[MAXSPLITSCREENPLAYERS]; extern consvar_t cv_rumble_offroad[MAXSPLITSCREENPLAYERS]; diff --git a/src/hardware/hw_bsp.c b/src/hardware/hw_bsp.c index 0a4b0af36..a714e2334 100644 --- a/src/hardware/hw_bsp.c +++ b/src/hardware/hw_bsp.c @@ -54,10 +54,6 @@ typedef struct { // if no portals are found, the portal scanning phase can be skipped while rendering, saving a bit of time. boolean gl_maphasportals = false; -// Determine on mapload if current map has any Horizonlines present -// avoids doing a bunch of work during rendering -boolean gl_maphashorizonlines = false; - // ========================================================================== // FLOOR & CEILING CONVEX POLYS GENERATION // ========================================================================== @@ -96,7 +92,7 @@ static void HWR_FreePoly(poly_t *poly) // divfrac = how far along partline vector is crossing pt static boolean fracdivline(fdivline_t *partline, polyvertex_t *v1, polyvertex_t *v2) { - double frac1, frac2; + double frac; double num; // numerator double den; // denominator double v1x, v1y, v1dx, v1dy; // polygon side vector, v1->v2 @@ -122,24 +118,23 @@ static boolean fracdivline(fdivline_t *partline, polyvertex_t *v1, polyvertex_t // first check the frac along the polygon segment, // (do not accept hit with the extensions) num = (v3x - v1x)*v3dy + (v1y - v3y)*v3dx; - frac1 = num / den; + frac = num / den; // 0= cross at v1, 1.0= cross at v2 - if (frac1 < 0.0 || frac1 > 1.0) // double + if (frac < 0.0 || frac > 1.0) // double return false; // not within the polygon side // now get the frac along the BSP line // which is useful to determine what is left, what is right num = (v3x - v1x)*v1dy + (v1y - v3y)*v1dx; - frac2 = num / den; - partline->divfrac = frac2; // how far along partline vector + partline->divfrac = num / den; // how far along partline vector // [WDJ] find the interception point along the segment. // It should be slightly more accurate because it is always closer to the // crossing point than arbitrary positions on the partition line. - partline->divpt.x = v1x + v1dx*frac1; - partline->divpt.y = v1y + v1dy*frac1; + partline->divpt.x = v1x + v1dx*frac; + partline->divpt.y = v1y + v1dy*frac; return true; } @@ -161,6 +156,71 @@ static boolean SameVertex(polyvertex_t *p1, polyvertex_t *p2) return true; } +// Adapted from function in prboom. +// Point is to rightside of divline when result > 0, +// but result is multiplied by length of divline. +// Returns near 0, when point is on, or nearly on, the divline. +static inline double point_rightside(fdivline_t *dl, polyvertex_t *v4) +{ + // Cross product of dl and vector dl->(x,y) to v4, + // is > 0 when v4 is to right side of divline. + // Viewed along divline from vertex, looking towards positive dx,dy. + // If divline is rotated until dy>0 and dx = 0, then true when rotated + // vertex position is to the right of the divline (v4->x > dl->x). + return ((((double)(v4->x) - (double)(dl->x)) * (double)(dl->dy)) + - (((double)(v4->y) - (double)(dl->y)) * (double)(dl->dx))); +} + +// [WDJ] The front and back MUST match the BSP determination, else it will assign +// this poly to the wrong sector. +// May have 1 point on the line, and other points might be very close to the line. +// Can only trust a test of a point that is NOT on the line. +// Using a tstdist = 0.5f worked, but it might break on very small poly. +// Avactor.wad had polys with d = 9.09, 5552, 1.64, 1011, 14252, 0.0136, 0.00634, 14655, 3, +// -0.3044, 7, 1.53, etc.., +// and one poly with d = -0.023 with the rest of the points d > 0. +// One 14 point poly with divline len=8, had to test 9 points to find d = 36. +// Typical: divline len=70 d=6828, divline len=1.0 d=35, divline len=1.414 d=5. +// The result of point_rightside is proportional to the length of divline. +// The divline length can be from 1.414 to 14000. +static void UnsplitPoly(fdivline_t *dlnp, poly_t *poly, + poly_t **frontpoly, poly_t **backpoly) +{ + int32_t i; + double d; + const double tstd = hypot((double)dlnp->dx, (double)dlnp->dy) / 2.0; + double sumd = 0; // accumulated left or rightness. + + for (i = 0; i < poly->numpts; i++) + { + // Look for a point that is obviously to the left or right. + d = point_rightside(dlnp, &poly->pts[i]); // d > 0 is right + sumd += d; + + if (d > tstd) + goto poly_rightside; + + if (d < -tstd) + goto poly_leftside; + } + + // Did not find an obvious left or right point, then + if (sumd < 0) + goto poly_leftside; + +poly_rightside: + // poly is to rightside of divline. + *frontpoly = poly; + *backpoly = NULL; + return; + +poly_leftside: + // poly is to leftside of divline. + *frontpoly = NULL; + *backpoly = poly; + return; +} + // split a _CONVEX_ polygon in two convex polygons // outputs: // frontpoly : polygon on right side of bsp line @@ -263,10 +323,7 @@ static void SplitPoly (fdivline_t *dlnp, // splitting parametric line // linedefs where each side point the same sector, that is: // the deep water effect with the original Doom - //TODO: make sure front poly is to front of partition line? - - *frontpoly = poly; - *backpoly = NULL; + UnsplitPoly(dlnp, poly, frontpoly, backpoly); return; } @@ -274,16 +331,14 @@ static void SplitPoly (fdivline_t *dlnp, // splitting parametric line if (pe < 0) { //I_Error("SplitPoly: only one point for split line (%d %d)", ps, pe); - *frontpoly = poly; - *backpoly = NULL; + UnsplitPoly(dlnp, poly, frontpoly, backpoly); return; } if (pe <= ps) { //I_Error("SplitPoly: invalid splitting line (%d %d)", ps, pe); - *frontpoly = poly; - *backpoly = NULL; + UnsplitPoly(dlnp, poly, frontpoly, backpoly); return; } @@ -346,7 +401,7 @@ static void SplitPoly (fdivline_t *dlnp, // splitting parametric line } // checks if any line is a portal or horizonline, for optimization -static inline void CheckForPortalsAndHorizonLines(boolean firstside, const line_t *line) +static inline void CheckForPortalsAndHorizonLines(boolean firstside, const line_t *line, subsector_t *sub) { // portal check if (!gl_maphasportals && firstside && line->special == PORTALSPECIAL) @@ -362,8 +417,8 @@ static inline void CheckForPortalsAndHorizonLines(boolean firstside, const line_ } // horizonline check - if (!gl_maphashorizonlines && line->special == HORIZONSPECIAL) - gl_maphashorizonlines = true; + if (line->special == HORIZONSPECIAL) + sub->hashorizon = true; } // Use each seg of the poly as a partition line, keep only the @@ -376,7 +431,7 @@ static inline void CheckForPortalsAndHorizonLines(boolean firstside, const line_ // segcount : number of seg in the array // Return frontpoly, which may be new or ptr to poly. // Called from: HWR_SubsecPoly -static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 segcount, poly_t *poly) +static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 segcount, poly_t *poly, subsector_t *sub) { INT32 i, j; poly_t* frontpoly = poly; // default, return same poly @@ -397,7 +452,7 @@ static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 segcount, poly_t *poly) if (!line) continue; - CheckForPortalsAndHorizonLines(lseg->side == 0, line); + CheckForPortalsAndHorizonLines(lseg->side == 0, line, sub); if (line->sidenum[1] != NO_INDEX) { @@ -489,6 +544,23 @@ static poly_t *CutOutSubsecPoly(seg_t *lseg, INT32 segcount, poly_t *poly) // need 2 points if (pe >= 0) { + // Cuts that miss the polygon. + if (ps_frac < 0.0 && cutseg.divfrac < 0.0) + continue; + + if (ps_frac > 1.0 && cutseg.divfrac > 1.0) + continue; +#define FRAC_EP 0.01f + // Reject cuts that could make the polygon non-convex. + + // Cannot make normal cut, ps_frac end is loose. + if ((ps_frac < (0.0 - FRAC_EP)) || (ps_frac > (1.0f + FRAC_EP))) + continue; + + // Cannot make normal cut, cutseg.divfrac end is loose. + if ((cutseg.divfrac < (0.0 - FRAC_EP)) || (cutseg.divfrac > (1.0f + FRAC_EP))) + continue; +#undef FRAC_EP // generate FRONT poly frontpoly = HWR_AllocPoly(poly_num_pts); @@ -543,7 +615,7 @@ static inline void HWR_SubsecPoly(INT32 ssindex, poly_t *poly) if (poly) { - poly = CutOutSubsecPoly(lseg, segcount, poly); + poly = CutOutSubsecPoly(lseg, segcount, poly, sub); #ifdef DEBUG_HWBSP totalsubsecpolys++; #endif @@ -672,7 +744,7 @@ static void WalkBSPNode(INT32 bspnum, poly_t *poly, UINT16 *leafnode, fixed_t *b #endif // Recursively divide front space. - if (LIKELY(frontpoly)) + if (frontpoly) { WalkBSPNode(bsp->children[0], frontpoly, &bsp->children[0],bsp->bbox[0]); @@ -680,7 +752,11 @@ static void WalkBSPNode(INT32 bspnum, poly_t *poly, UINT16 *leafnode, fixed_t *b memcpy(bbox, bsp->bbox[0], 4*sizeof (fixed_t)); } else - I_Error("WalkBSPNode: no front poly?"); + { + // [WDJ] Having no front poly is as likely as no back poly, since + // logic in Split Poly was changed to check poly direction. + // I_Error("HWR_WalkBSPNode: no front poly, bspnum= %d\n", bspnum); + } // Recursively divide back space. if (backpoly) @@ -795,7 +871,9 @@ static boolean PointInSeg(polyvertex_t *va, polyvertex_t *v1, polyvertex_t *v2) #endif } +#ifdef DEBUG_HWBSP static INT32 numsplitpoly; +#endif static void SearchSegInBSP(INT32 bspnum, polyvertex_t *p, poly_t *poly) { @@ -834,8 +912,9 @@ static void SearchSegInBSP(INT32 bspnum, polyvertex_t *p, poly_t *poly) for (n = k+1; n < newpoly->numpts; n++) newpoly->pts[n] = q->pts[n-1]; - +#ifdef DEBUG_HWBSP numsplitpoly++; +#endif extrasubsectors[bspnum].planepoly = newpoly; HWR_FreePoly(q); return; @@ -881,7 +960,9 @@ static void SolveTProblem(void) I_FinishUpdate(); // page flip or blit buffer #endif +#ifdef DEBUG_HWBSP numsplitpoly = 0; +#endif for (ssnum = 0; ssnum < num_extra_subsector; ssnum++) { @@ -918,8 +999,8 @@ void HWR_CreatePlanePolygons(INT32 bspnum) I_FinishUpdate(); // page flip or blit buffer #endif - // reset the portal and horizonline flag - gl_maphasportals = gl_maphashorizonlines = false; + // reset the portal flag + gl_maphasportals = false; // find min/max boundaries of map #ifdef DEBUG_HWBSP diff --git a/src/hardware/hw_main.cpp b/src/hardware/hw_main.cpp index 80549f240..5f7e6838f 100644 --- a/src/hardware/hw_main.cpp +++ b/src/hardware/hw_main.cpp @@ -16,6 +16,7 @@ #include #include +#include "../d_main.h" #include "../doomstat.h" #include "../doomdef.h" @@ -120,7 +121,7 @@ ps_metric_t ps_hw_batchdrawtime = {}; static void HWR_SplitWall(sector_t *sector, FOutVector *wallVerts, INT32 texnum, boolean noencore, FSurfaceInfo* Surf, INT32 cutflag, ffloor_t *pfloor, FBITFIELD polyflags); static void HWR_RenderWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap); -static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, boolean fogplane, extracolormap_t *planecolormap); +static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, boolean fogplane, extracolormap_t *planecolormap, subsector_t *subsector); static void HWR_AddTransparentWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, INT32 texnum, boolean noencore, FBITFIELD blend, boolean fogwall, INT32 lightlevel, extracolormap_t *wallcolormap); static void HWR_AddTransparentPolyobjectFloor(lumpnum_t lumpnum, polyobj_t *polysector, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, extracolormap_t *planecolormap); @@ -204,7 +205,8 @@ static CV_PossibleValue_t glpalettedepth_cons_t[] = {{16, "16 bits"}, {24, "24 b consvar_t cv_glscreentextures = {"gr_screentextures", "All", CV_CALL|CV_SAVE, glscreentextures_cons_t, CV_screentextures_OnChange, 0, NULL, NULL, 0, 0, NULL}; #ifdef USE_FBO_OGL -consvar_t cv_glframebuffer = {"gr_framebuffer", "Off", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, CV_glframebuffer_OnChange, 0, NULL, NULL, 0, 0, NULL}; +static CV_PossibleValue_t glframebuffer_cons_t[] = {{0, "Off"}, {1, "Downsample"}, {2, "Always"}, {0, NULL}}; +consvar_t cv_glframebuffer = {"gr_framebuffer", "Off", CV_SAVE|CV_CALL, glframebuffer_cons_t, CV_glframebuffer_OnChange, 0, NULL, NULL, 0, 0, NULL}; #endif consvar_t cv_glmdls = {"gr_mdls", "Off", CV_SAVE|CV_CALL, CV_OnOff, M_UpdateOGLMenu, 0, NULL, NULL, 0, 0, NULL}; @@ -255,6 +257,7 @@ static void CV_screentextures_OnChange(void) #endif intermissionbginit = false; } + GL_SetSpecialState(HWD_SET_SCREEN_TEXTURES, cv_glscreentextures.value); M_UpdateOGLMenu(); } @@ -263,12 +266,11 @@ static void CV_screentextures_OnChange(void) static void CV_glframebuffer_OnChange(void) { ONLY_IF_GL_LOADED - if ((cv_glframebuffer.value != 0 && cv_glscreentextures.value != 2) - || (!supportFBO && cv_glframebuffer.value != 0)) // screen FBO needs screen textures - CV_SetValue(&cv_glframebuffer, 0); - - I_DownSample(); - RefreshOGLSDLSurface(); + // screen FBO needs screen textures currently + if (cv_glframebuffer.value != 0 && ((cv_glscreentextures.value != 2) || !supportFBO)) + CV_StealthSetValue(&cv_glframebuffer, 0); // dont recursively call this lul + if (loaded_config) // dont do this crap on startup, this is handled elsewhere + I_CheckDownSampleState(true); M_UpdateOGLMenu(); intermissionbginit = false; } @@ -721,7 +723,7 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool HWR_ProcessPolygon(&Surf, planeVerts, nrPlaneVerts, PolyFlags, shader, false); - if (gl_maphashorizonlines && subsector && !FOFsector && cv_glhorizonlines.value) + if (subsector && subsector->hashorizon && cv_glhorizonlines.value) { // Horizon lines FOutVector horizonpts[6]; @@ -806,6 +808,268 @@ static void HWR_RenderPlane(subsector_t *subsector, extrasubsector_t *xsub, bool #undef SETUP3DVERT } +static void HWR_RenderFOFHorizonPlane(subsector_t *subsector, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, FBITFIELD PolyFlags, INT32 lightlevel, lumpnum_t lumpnum, sector_t *FOFsector, UINT8 alpha, extracolormap_t *planecolormap) +{ + FSurfaceInfo Surf = {}; + polyvertex_t *pv; + pslope_t *slope = NULL; + INT32 shader = SHADER_NONE; + + size_t nrPlaneVerts; + INT32 i; + + float height; // constant y for all points on the convex flat polygon + static float flatxref = 0.0f, flatyref = 0.0f; + static float fflatsize = 64.0f; + static INT32 flatflag = 63; + size_t len; + + float tempxsow, tempytow; + float scrollx = 0.0f, scrolly = 0.0f; + float anglef = 0.0f, cosangf = 0.0f, sinangf = 0.0f; + angle_t angle = 0; + + static FOutVector *planeVerts = NULL; + static UINT16 numAllocedPlaneVerts = 0; + + poly_t *planepoly = xsub->planepoly; + + if (!cv_glhorizonlines.value) + return; + + if (!subsector || !subsector->hashorizon) + return; + + // no convex poly were generated for this subsector + if (!planepoly) + return; + + nrPlaneVerts = planepoly->numpts; + + if (nrPlaneVerts < 3) // not even a triangle ? + return; + + const sector_t *sec = FOFsector; + + if (!(PolyFlags & PF_NoTexture)) + { + HWR_GetFlat(lumpnum, R_NoEncore(FOFsector, !isceiling)); + } + + // Get the slope pointer to simplify future code + if (!isceiling && sec->f_slope) + slope = sec->f_slope; + else if (isceiling && sec->c_slope) + slope = sec->c_slope; + + height = FixedToFloat(fixedheight); + + // Allocate plane-vertex buffer if we need to + if (!planeVerts || nrPlaneVerts > numAllocedPlaneVerts) + { + numAllocedPlaneVerts = (UINT16)nrPlaneVerts; + Z_Free(planeVerts); + Z_Malloc(numAllocedPlaneVerts * sizeof (FOutVector), PU_LEVEL, &planeVerts); + } + + len = W_LumpLength(lumpnum); + + switch (len) + { + case 4194304: // 2048x2048 lump + fflatsize = 2048.0f; + flatflag = 2047; + break; + case 1048576: // 1024x1024 lump + fflatsize = 1024.0f; + flatflag = 1023; + break; + case 262144: // 512x512 lump + fflatsize = 512.0f; + flatflag = 511; + break; + case 65536: // 256x256 lump + fflatsize = 256.0f; + flatflag = 255; + break; + case 16384: // 128x128 lump + fflatsize = 128.0f; + flatflag = 127; + break; + case 1024: // 32x32 lump + fflatsize = 32.0f; + flatflag = 31; + break; + default: // 64x64 lump + fflatsize = 64.0f; + flatflag = 63; + break; + } + + pv = planepoly->pts; + + // reference point for flat texture coord for each vertex around the polygon + flatxref = static_cast(static_cast(pv->x) & (~flatflag)) / fflatsize; + flatyref = static_cast(static_cast(pv->y) & (~flatflag)) / fflatsize; + + if (!isceiling) // it's a floor + { + scrollx = FixedToFloat(sec->floor_xoffs)/fflatsize; + scrolly = FixedToFloat(sec->floor_yoffs)/fflatsize; + angle = sec->floorpic_angle; + } + else // it's a ceiling + { + scrollx = FixedToFloat(sec->ceiling_xoffs)/fflatsize; + scrolly = FixedToFloat(sec->ceiling_yoffs)/fflatsize; + angle = sec->ceilingpic_angle; + } + + if (angle) // Only needs to be done if there's an altered angle + { + // This needs to be done so that it scrolls in a different direction after rotation like software + tempxsow = flatxref; + tempytow = flatyref; + anglef = ANG2RAD(InvAngle(angle)); + + // This needs to be done so everything aligns after rotation + // It would be done so that rotation is done, THEN the translation, but I couldn't get it to rotate AND scroll like software does + cosangf = cosf(anglef); + sinangf = sinf(anglef); + flatxref = (tempxsow * cosangf) - (tempytow * sinangf); + flatyref = (tempxsow * sinangf) + (tempytow * cosangf); + } + +#define SETUP3DVERT(vert, vx, vy) {\ + /* Hurdler: add scrolling texture on floor/ceiling */\ + vert->s = (float)(((vx) / fflatsize) - flatxref + scrollx);\ + vert->t = (float)(flatyref - ((vy) / fflatsize) + scrolly);\ +\ + /* Need to rotate before translate */\ + if (angle) /* Only needs to be done if there's an altered angle */\ + {\ + tempxsow = vert->s;\ + tempytow = vert->t;\ + vert->s = (tempxsow * cosangf) - (tempytow * sinangf);\ + vert->t = (tempxsow * sinangf) + (tempytow * cosangf);\ + }\ +\ + if (slope)\ + {\ + fixedheight = P_GetSlopeZAt(slope, FloatToFixed((vx)), FloatToFixed((vy)));\ + height = FixedToFloat(fixedheight);\ + }\ +\ + vert->x = (vx);\ + vert->y = height;\ + vert->z = (vy);\ +\ +} + + HWR_Lighting(&Surf, lightlevel, planecolormap, false); + + if (PolyFlags & (PF_Translucent|PF_Fog|PF_Additive|PF_Subtractive|PF_ReverseSubtract|PF_Multiplicative|PF_Environment)) + { + Surf.PolyColor.s.alpha = (UINT8)alpha; + PolyFlags |= PF_Modulated; + } + else + PolyFlags |= PF_Masked|PF_Modulated; + + if (HWR_UseShader()) + { + if (PolyFlags & PF_Fog) + shader = SHADER_FOG; + else if (cv_ripplewater.value && (PolyFlags & PF_Ripple)) + shader = SHADER_WATER; + else + shader = SHADER_FLOOR; + + PolyFlags |= PF_ColorMapped; + } + + // Horizon lines + FOutVector horizonpts[6]; + float dist, vx, vy; + float x1, y1, xd, yd; + UINT8 numplanes, j; + vertex_t v; // For determining the closest distance from the line to the camera, to split render planes for minimum distortion; + + static constexpr float renderdist = 27000.0f; // How far out to properly render the plane + static constexpr float farrenderdist = 32768.0f; // From here, raise plane to horizon level to fill in the line with some texture distortion + + const seg_t *line = &segs[subsector->firstline]; + + for (i = 0; i < subsector->numlines; i++, line++) + { + if (line->polyseg) + continue; + + line_t* ld = line->linedef; + + if (LIKELY(ld->special != HORIZONSPECIAL)) + continue; + + if (R_PointOnSegSide(viewx, viewy, line) != 0) + continue; + + P_ClosestPointOnLine(viewx, viewy, ld, &v); + dist = FixedToFloat(R_PointToDist(v.x, v.y)); + + x1 = (line->fv1.x); + y1 = (line->fv1.y); + + xd = (line->fv2.x) - x1; + yd = (line->fv2.y) - y1; + + // Based on the seg length and the distance from the line, split horizon into multiple poly sets to reduce distortion + dist = sqrtf((xd*xd) + (yd*yd)) / dist / 16.0f; + + if (dist > 100.0f) + numplanes = 100; + else + numplanes = (UINT8)dist + 1; + + for (j = 0; j < numplanes; j++) + { + // Left side + vx = x1 + xd * j / numplanes; + vy = y1 + yd * j / numplanes; + SETUP3DVERT((&horizonpts[1]), vx, vy); + + dist = sqrtf(powf(vx - gl_viewx, 2) + powf(vy - gl_viewy, 2)); + vx = (vx - gl_viewx) * renderdist / dist + gl_viewx; + vy = (vy - gl_viewy) * renderdist / dist + gl_viewy; + SETUP3DVERT((&horizonpts[0]), vx, vy); + + // Right side + vx = x1 + xd * (j+1) / numplanes; + vy = y1 + yd * (j+1) / numplanes; + SETUP3DVERT((&horizonpts[2]), vx, vy); + + dist = sqrtf(powf(vx - gl_viewx, 2) + powf(vy - gl_viewy, 2)); + vx = (vx - gl_viewx) * renderdist / dist + gl_viewx; + vy = (vy - gl_viewy) * renderdist / dist + gl_viewy; + SETUP3DVERT((&horizonpts[3]), vx, vy); + + // Horizon fills + vx = (horizonpts[0].x - gl_viewx) * farrenderdist / renderdist + gl_viewx; + vy = (horizonpts[0].z - gl_viewy) * farrenderdist / renderdist + gl_viewy; + SETUP3DVERT((&horizonpts[5]), vx, vy); + horizonpts[5].y = gl_viewz; + + vx = (horizonpts[3].x - gl_viewx) * farrenderdist / renderdist + gl_viewx; + vy = (horizonpts[3].z - gl_viewy) * farrenderdist / renderdist + gl_viewy; + SETUP3DVERT((&horizonpts[4]), vx, vy); + horizonpts[4].y = gl_viewz; + + // Draw + HWR_ProcessPolygon(&Surf, horizonpts, 6, PolyFlags, shader, true); + } + } +#undef SETUP3DVERT +} + #ifdef WALLSPLATS static void HWR_DrawSegsSplats(FSurfaceInfo * pSurf) { @@ -2960,16 +3224,14 @@ static void HWR_Subsector(size_t num) UINT8 alpha; light = R_GetPlaneLight(gl_frontsector, centerHeight, underside); - alpha = HWR_FogBlockAlpha(*gl_frontsector->lightlist[light].lightlevel, rover->master->frontsector->extra_colormap); - HWR_AddTransparentFloor(0, &extrasubsectors[num], !bottom, roverheight, *gl_frontsector->lightlist[light].lightlevel, alpha, rover->master->frontsector, PF_Fog|PF_NoTexture, - true, rover->master->frontsector->extra_colormap); + true, rover->master->frontsector->extra_colormap, sub); } else if ((rover->flags & FF_TRANSLUCENT && rover->alpha < 256) || rover->blend) // SoM: Flags are more efficient { @@ -2981,13 +3243,12 @@ static void HWR_Subsector(size_t num) roverheight, *gl_frontsector->lightlist[light].lightlevel, CLAMP(rover->alpha, 0, 255), rover->master->frontsector, HWR_RippleBlend(gl_frontsector, rover, false) | (rover->blend ? HWR_GetBlendModeFlag(rover->blend) : static_cast(PF_Translucent)), - false, gl_frontsector->lightlist[light].extra_colormap); + false, gl_frontsector->lightlist[light].extra_colormap, sub); } else { HWR_GetFlat(flatlump, R_NoEncore(rover->master->frontsector, bottom)); light = R_GetPlaneLight(gl_frontsector, centerHeight, underside); - HWR_RenderPlane(sub, &extrasubsectors[num], !bottom, roverheight, HWR_RippleBlend(gl_frontsector, rover, false)|PF_Occlude, *gl_frontsector->lightlist[light].lightlevel, flatlump, rover->master->frontsector, 255, gl_frontsector->lightlist[light].extra_colormap); } @@ -4191,7 +4452,7 @@ static void HWR_AddTransparentWall(FOutVector *wallVerts, FSurfaceInfo *pSurf, I wallinfo->wallcolormap = wallcolormap; } -static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, boolean fogplane, extracolormap_t *planecolormap) +static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, boolean isceiling, fixed_t fixedheight, INT32 lightlevel, INT32 alpha, sector_t *FOFSector, FBITFIELD blend, boolean fogplane, extracolormap_t *planecolormap, subsector_t *subsector) { planeinfo_t *planeinfo = static_cast(HWR_CreateDrawNode(DRAWNODE_PLANE)); @@ -4205,6 +4466,9 @@ static void HWR_AddTransparentFloor(lumpnum_t lumpnum, extrasubsector_t *xsub, b planeinfo->blend = blend; planeinfo->fogplane = fogplane; planeinfo->planecolormap = planecolormap; + + // horizonplanes need to be drawn BEFORE skywalls, so lets hope this works without too many issues.... + HWR_RenderFOFHorizonPlane(subsector, planeinfo->xsub, planeinfo->isceiling, planeinfo->fixedheight, planeinfo->blend, planeinfo->lightlevel, planeinfo->lumpnum, planeinfo->FOFSector, (UINT8)planeinfo->alpha, planeinfo->planecolormap); } // Adding this for now until I can create extrasubsector info for polyobjects @@ -5899,7 +6163,7 @@ void HWR_Startup(void) HWR_TogglePaletteRendering(); if (msaa) - GL_SetSpecialState(HWD_SET_MSAA, a2c ? 2 : 1); + GL_SetSpecialState(HWD_SET_MSAA, 1); HWR_RegisterCommands(); } @@ -5987,7 +6251,7 @@ void HWR_Shutdown(void) GL_FlushScreenTextures(); HWR_ClearAllTextures(); #ifdef USE_FBO_OGL - GL_Framebuffer_Disable(); + GL_FBO_Disable(); #endif } diff --git a/src/hardware/hw_main.h b/src/hardware/hw_main.h index 4856f7574..e82ff9c54 100644 --- a/src/hardware/hw_main.h +++ b/src/hardware/hw_main.h @@ -43,7 +43,6 @@ void HWR_Startup(void); void HWR_Shutdown(void); extern unsigned msaa; -extern boolean a2c; extern FTransform atransform; extern float gl_viewsin, gl_viewcos; @@ -172,7 +171,6 @@ void HWR_ProcessSeg(void); // Sort of like GLWall::Process in GZDoom // hw_bsp.c void HWR_CreatePlanePolygons(INT32 bspnum); extern boolean gl_maphasportals; -extern boolean gl_maphashorizonlines; // Console variables extern CV_PossibleValue_t glanisotropicmode_cons_t[]; diff --git a/src/hardware/hw_shaders.h b/src/hardware/hw_shaders.h index baedebf64..51c0ec001 100644 --- a/src/hardware/hw_shaders.h +++ b/src/hardware/hw_shaders.h @@ -193,26 +193,6 @@ "}\n" \ "#endif\0" -#define GLSL_SOFTWARE_FRAGMENT_SHADER_NOPAL \ - "uniform sampler2D tex;\n" \ - "uniform vec4 poly_color;\n" \ - "uniform vec4 tint_color;\n" \ - "uniform vec4 fade_color;\n" \ - "uniform float lighting;\n" \ - "uniform float fade_start;\n" \ - "uniform float fade_end;\n" \ - GLSL_DOOM_COLORMAP \ - GLSL_DOOM_LIGHT_EQUATION \ - "void main(void) {\n" \ - "vec4 texel = texture2D(tex, gl_TexCoord[0].st);\n" \ - "vec4 base_color = texel * poly_color;\n" \ - "vec4 final_color = base_color;\n" \ - GLSL_SOFTWARE_TINT_EQUATION \ - GLSL_SOFTWARE_FADE_EQUATION \ - "final_color.a = texel.a * poly_color.a;\n" \ - "gl_FragColor = final_color;\n" \ - "}\n" \ - // hand tuned adjustments for light level calculation #define GLSL_FLOOR_FUDGES \ "#define STARTMAP_FUDGE 1.06\n" \ diff --git a/src/hardware/r_opengl/r_opengl.c b/src/hardware/r_opengl/r_opengl.c index 77e0ef9d9..dd6252ce7 100644 --- a/src/hardware/r_opengl/r_opengl.c +++ b/src/hardware/r_opengl/r_opengl.c @@ -136,32 +136,61 @@ enum { FBO_NONE, FBO_ARB, - FBO_EXT, - FBO_EXT_STENCIL // supports stencil attachment + FBO_EXT }; typedef struct { - GLuint fboobj; // actual Framebuffer Object - GLuint rboobj; // Renderbuffer Object - GLuint tex; // Framebuffer texture + GLuint fboobj; // normal single sample fbo + GLuint rboobj; // normal single sample rbo for depth and stencil stuff + GLuint tex; // fbo texture for color + GLuint msaa_fboobj; // msaa fbo + GLuint msaa_rboobj; // msaa rbo for color this time + GLuint msaa_depthrboobj; // msaa rbo for depth and stencil stuff boolean init; + boolean msaa; // true if the msaa fbo is the render target ugh + boolean bound; // true if the fbo is currently bound as the framebuffer } fboobj_t; +// this is a little messy but less messy than having two of those tbh static fboobj_t framebufferobject = {}; int supportFBO = FBO_NONE; // older gl versions might not have these, so prevent compile errors -// this should still give a simple depth only rbo +#ifndef GL_DEPTH_COMPONENT +#define GL_DEPTH_COMPONENT 0x1902 +#endif +#ifndef GL_DEPTH_ATTACHMENT_EXT +#define GL_DEPTH_ATTACHMENT_EXT 0x8D00 +#endif #ifndef GL_DEPTH24_STENCIL8 -GL_DEPTH24_STENCIL8 GL_DEPTH_COMPONENT_EXT +#define GL_DEPTH24_STENCIL8 0x88F0 #endif #ifndef GL_DEPTH_STENCIL_ATTACHMENT -GL_DEPTH_STENCIL_ATTACHMENT GL_DEPTH_ATTACHMENT_EXT +#define GL_DEPTH_STENCIL_ATTACHMENT 0x821A +#endif +#ifndef GL_COLOR_ATTACHMENT0_EXT +#define GL_COLOR_ATTACHMENT0_EXT 0x8CE0 +#endif +#ifndef GL_READ_FRAMEBUFFER_EXT +#define GL_READ_FRAMEBUFFER_EXT 0x8CA8 +#endif +#ifndef GL_DRAW_FRAMEBUFFER_EXT +#define GL_DRAW_FRAMEBUFFER_EXT 0x8CA9 +#endif +#ifndef GL_FRAMEBUFFER_EXT +#define GL_FRAMEBUFFER_EXT 0x8D40 +#endif +#ifndef GL_RENDERBUFFER_EXT +#define GL_RENDERBUFFER_EXT 0x8D41 +#endif +#ifndef GL_FRAMEBUFFER_COMPLETE_EXT +#define GL_FRAMEBUFFER_COMPLETE_EXT 0x8CD5 #endif -static void GL_Framebuffer_DeleteAttachments(void); +static void GL_FBO_DeleteAttachments(void); +static void GL_FBO_Resolve(GLbitfield mask); #endif // needed for glportals @@ -479,6 +508,10 @@ typedef void (APIENTRY * PFNglRenderbufferStorage) (GLenum target, GLenum intern static PFNglRenderbufferStorage pglRenderbufferStorage; typedef void (APIENTRY * PFNglFramebufferRenderbuffer) (GLenum target, GLenum attachment, GLenum renderbuffertarget, GLenum renderbuffer); static PFNglFramebufferRenderbuffer pglFramebufferRenderbuffer; +typedef void (APIENTRY * PFNglRenderbufferStorageMultisample) (GLenum target, GLsizei samples, GLenum internalformat, GLsizei width, GLsizei height); +static PFNglRenderbufferStorageMultisample pglRenderbufferStorageMultisample; +typedef void (APIENTRY * PFNglBlitFramebuffer) (GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1, GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1, GLbitfield mask, GLenum filter); +static PFNglBlitFramebuffer pglBlitFramebuffer; /* 2.1 extension functions for framebuffers and renderbuffers */ typedef void (APIENTRY * PFNglGenFramebuffersEXT) (GLsizei n, GLuint *ids); @@ -769,28 +802,22 @@ static GLRGBAFloat shader_defaultcolor = {1.0f, 1.0f, 1.0f, 1.0f}; #ifdef USE_FBO_OGL static int GLFramebuffer_CheckExt(void) { - // in opengl 3.0 and up fbos are standart and always supported - if (majorGL >= 3) - return FBO_ARB; - - // gl versions from 2.1 may still support framebuffer objects - - // maybe, just maybe, we support the standart extensions + // check for the standard arb extensions + // these should be there from version 3.0 onwards if (GL_isExtAvailable("GL_ARB_framebuffer_no_attachments", gl_extensions) && GL_isExtAvailable("GL_ARB_framebuffer_object", gl_extensions) && GL_isExtAvailable("GL_ARB_framebuffer_sRGB", gl_extensions)) + { return FBO_ARB; + } - // nope, try the older 2.1 extensions + // gl versions from 2.1 and older may still support framebuffer objects + // depending on vendor and driver support if (GL_isExtAvailable("GL_EXT_framebuffer_no_attachments", gl_extensions) && GL_isExtAvailable("GL_EXT_framebuffer_object", gl_extensions) && - GL_isExtAvailable("GL_EXT_framebuffer_sRGB", gl_extensions)) + GL_isExtAvailable("GL_EXT_framebuffer_sRGB", gl_extensions) && + GL_isExtAvailable("GL_EXT_packed_depth_stencil", gl_extensions)) // we use the stencil buffer for stuff like portals, so its better to make stencil extension a requirement { - // perhaps we may even support a stencil attachment - if (GL_isExtAvailable("GL_EXT_packed_depth_stencil", gl_extensions)) - return FBO_EXT_STENCIL; - - // no stencil support, sad return FBO_EXT; } @@ -906,38 +933,69 @@ void SetupGLFunc4(void) GetGLfunc(glRenderbufferStorage); GetGLfunc(glFramebufferRenderbuffer); + // multisample support, this should be in core per gl 3.0 and newer + pglRenderbufferStorageMultisample = GetGLFunc("glRenderbufferStorageMultisample"); + pglBlitFramebuffer = GetGLFunc("glBlitFramebuffer"); + + if (!pglRenderbufferStorageMultisample || !pglBlitFramebuffer) + { + pglRenderbufferStorageMultisample = NULL; + pglBlitFramebuffer = NULL; + GL_DBG_Printf("\nFBO: No multisample support\n"); + } + GL_DBG_Printf("\nFBO: ARB extensions found\n"); } - else if (fbocheck == FBO_EXT || fbocheck == FBO_EXT_STENCIL) // uh oh only support for EXT prefix fbos... + else if (fbocheck == FBO_EXT) // uh oh only support old fbo extensions... { - GetGLfunc(glGenFramebuffersEXT); - GetGLfunc(glBindFramebufferEXT); - GetGLfunc(glDeleteFramebuffersEXT); - GetGLfunc(glFramebufferTexture2DEXT); - GetGLfunc(glCheckFramebufferStatusEXT); - GetGLfunc(glGenRenderbuffersEXT); - GetGLfunc(glBindRenderbufferEXT); - GetGLfunc(glDeleteRenderbuffersEXT); - GetGLfunc(glRenderbufferStorageEXT); - GetGLfunc(glFramebufferRenderbufferEXT); - // remapping galore huehue // do this so we dont have to dupe half our code + GetGLfunc(glGenFramebuffersEXT); pglGenFramebuffers = pglGenFramebuffersEXT; + + GetGLfunc(glBindFramebufferEXT); pglBindFramebuffer = pglBindFramebufferEXT; + + GetGLfunc(glDeleteFramebuffersEXT); pglDeleteFramebuffers = pglDeleteFramebuffersEXT; + + GetGLfunc(glFramebufferTexture2DEXT); pglFramebufferTexture2D = pglFramebufferTexture2DEXT; + + GetGLfunc(glCheckFramebufferStatusEXT); pglCheckFramebufferStatus = pglCheckFramebufferStatusEXT; + + GetGLfunc(glGenRenderbuffersEXT); pglGenRenderbuffers = pglGenRenderbuffersEXT; + + GetGLfunc(glBindRenderbufferEXT); pglBindRenderbuffer = pglBindRenderbufferEXT; + + GetGLfunc(glDeleteRenderbuffersEXT); pglDeleteRenderbuffers = pglDeleteRenderbuffersEXT; + + GetGLfunc(glRenderbufferStorageEXT); pglRenderbufferStorage = pglRenderbufferStorageEXT; + + GetGLfunc(glFramebufferRenderbufferEXT); pglFramebufferRenderbuffer = pglFramebufferRenderbufferEXT; - if (fbocheck == FBO_EXT) - GL_DBG_Printf("\nFBO: EXT extensions found, no stencil\n"); - else if (fbocheck == FBO_EXT_STENCIL) - GL_DBG_Printf("\nFBO: EXT extensions with packed depth-stencil\n"); + // multisample support needs the EXT multisample and blit extensions + if (GL_isExtAvailable("GL_EXT_framebuffer_multisample", gl_extensions) && + GL_isExtAvailable("GL_EXT_framebuffer_blit", gl_extensions)) + { + pglRenderbufferStorageMultisample = GetGLFunc("glRenderbufferStorageMultisampleEXT"); + pglBlitFramebuffer = GetGLFunc("glBlitFramebufferEXT"); + } + + if (!pglRenderbufferStorageMultisample || !pglBlitFramebuffer) + { + pglRenderbufferStorageMultisample = NULL; + pglBlitFramebuffer = NULL; + GL_DBG_Printf("\nFBO: No multisample support\n"); + } + + GL_DBG_Printf("\nFBO: EXT extensions with packed depth-stencil\n"); } supportFBO = fbocheck; @@ -1151,7 +1209,7 @@ void GL_SetModelView(GLint w, GLint h) GL_FlushScreenTextures(); #ifdef USE_FBO_OGL - GL_Framebuffer_DeleteAttachments(); + GL_FBO_DeleteAttachments(); #endif } @@ -1457,6 +1515,15 @@ void GL_ReadScreenTexture(int tex, UINT8 *restrict dest, INT32 scale) const INT32 stride = (screen_width/scale)*3; INT32 scanlines = screen_height; GLubyte * restrict image; +#ifdef USE_FBO_OGL + const boolean msaa_fbo = (framebufferobject.bound && framebufferobject.msaa); +#endif + +#ifdef USE_FBO_OGL + // cant read from the msaa fbo, so gotta blit it first into either the single sample fbo or the screen framebuffer.... + if (msaa_fbo) + GL_FBO_Resolve(GL_COLOR_BUFFER_BIT); +#endif // at the time this function is called, generic2 can be found drawn on the framebuffer // if some other screen texture is needed, draw it to the framebuffer @@ -1498,9 +1565,16 @@ void GL_ReadScreenTexture(int tex, UINT8 *restrict dest, INT32 scale) memcpy(dest + i, image + i*scale, 3); dest += stride; } + // this probably could be done with fbos, might mess around with that some time :3 // ...yet still, restrict doesn't make the inner loop any faster free(image - ((screen_height % scale) * screen_width*3)); + +#ifdef USE_FBO_OGL + // this“ll reenable the msaa fbo.... + if (msaa_fbo) + GL_FBO_Enable(); +#endif } // -----------------+ @@ -2254,7 +2328,7 @@ static void GL_Shader_SetUniforms(FSurfaceInfo *Surface, GLRGBAFloat *poly, GLRG UNIFORM_1(shader->uniforms[gluniform_leveltime], shader_leveltime, pglUniform1f); - UNIFORM_2(shader->uniforms[gluniform_scr_resolution], (GLfloat)vid.width, (GLfloat)vid.height, pglUniform2f); + UNIFORM_2(shader->uniforms[gluniform_scr_resolution], (GLfloat)screen_width, (GLfloat)screen_height, pglUniform2f); #undef UNIFORM_1 #undef UNIFORM_2 @@ -2641,11 +2715,7 @@ void GL_SetSpecialState(hwdspecialstate_t IdState, INT32 Value) case HWD_SET_MSAA: if (Value) - { pglEnable(GL_MULTISAMPLE); - if (Value == 2) - pglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); - } break; case HWD_SET_TEXTUREANISOTROPICMODE: @@ -3246,9 +3316,9 @@ void GL_SetTransform(FTransform *stransform) } #ifdef USE_FBO_OGL -static void GL_Framebuffer_DeleteAttachments(void) +static void GL_FBO_DeleteAttachments(void) { - if (supportFBO == FBO_NONE || !framebufferobject.init) + if (supportFBO == FBO_NONE) return; // Unbind the framebuffer @@ -3261,14 +3331,45 @@ static void GL_Framebuffer_DeleteAttachments(void) if (framebufferobject.rboobj) pglDeleteRenderbuffers(1, &framebufferobject.rboobj); + if (framebufferobject.msaa_rboobj) + pglDeleteRenderbuffers(1, &framebufferobject.msaa_rboobj); + + if (framebufferobject.msaa_depthrboobj) + pglDeleteRenderbuffers(1, &framebufferobject.msaa_depthrboobj); + framebufferobject.tex = 0; framebufferobject.rboobj = 0; + framebufferobject.msaa_rboobj = 0; + framebufferobject.msaa_depthrboobj = 0; framebufferobject.init = false; + framebufferobject.bound = false; } -static void GL_Framebuffer_Generate(void) +// blit the msaa fbo into the normal single sample fbo +// so we can use it to copy from and stuff +static void GL_FBO_Resolve(GLbitfield mask) { - if (supportFBO == FBO_NONE || framebufferobject.init || !UseScreenFBO()) + if (supportFBO == FBO_NONE || !framebufferobject.init || !framebufferobject.msaa) + return; + + if (!pglBlitFramebuffer) + return; + + pglBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, framebufferobject.msaa_fboobj); + pglBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, framebufferobject.fboobj); + + pglBlitFramebuffer(0, 0, screen_width, screen_height, + 0, 0, screen_width, screen_height, + mask, GL_NEAREST); + + pglBindFramebuffer(GL_FRAMEBUFFER_EXT, framebufferobject.fboobj); +} + +static void GL_FBO_Generate(void) +{ + GLint msaasamples; + + if (supportFBO == FBO_NONE || framebufferobject.init) return; // Generate the framebuffer @@ -3283,7 +3384,7 @@ static void GL_Framebuffer_Generate(void) { pglGenTextures(1, &framebufferobject.tex); pglBindTexture(GL_TEXTURE_2D, framebufferobject.tex); - pglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, screen_width, screen_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + pglTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, screen_width, screen_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); Clamp2D(GL_TEXTURE_WRAP_S); @@ -3306,18 +3407,13 @@ static void GL_Framebuffer_Generate(void) pglRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_DEPTH24_STENCIL8, screen_width, screen_height); pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, framebufferobject.rboobj); } - else if (supportFBO == FBO_EXT || supportFBO == FBO_EXT_STENCIL) + else if (supportFBO == FBO_EXT) { - pglRenderbufferStorage(GL_RENDERBUFFER_EXT, (supportFBO == FBO_EXT_STENCIL) ? GL_DEPTH_STENCIL_EXT : GL_DEPTH_COMPONENT, screen_width, screen_height); - - // attach a renderbuffer to depth attachment point + pglRenderbufferStorage(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL_EXT, screen_width, screen_height); + // attach the renderbuffer to depth attachment point pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, framebufferobject.rboobj); - - if (supportFBO == FBO_EXT_STENCIL) - { - // attach a renderbuffer to stencil attachment point - pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, framebufferobject.rboobj); - } + // attach the renderbuffer to stencil attachment point + pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, framebufferobject.rboobj); } // Clear the renderbuffer @@ -3329,48 +3425,124 @@ static void GL_Framebuffer_Generate(void) if (pglCheckFramebufferStatus(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) { //pglGetError(); TODO:implement this or smth - GL_MSG_Error("GL_Framebuffer_Generate: Failed to create Framebuffer Object"); + GL_MSG_Error("GL_FBO_Generate: Failed to create Framebuffer Object"); // if this fails, dont retry it a gazillion times - // this wouldnt recover + // this probably wouldnt recover and we dont want any more black screens... supportFBO = FBO_NONE; pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); return; } + // Try to set up a multisampled render target for MSAA + framebufferobject.msaa = false; + msaasamples = (GLint)msaa; + + // maybe should just make more "supportFBO" cases or smth but :chaosleep: + if (msaasamples > 1 && pglRenderbufferStorageMultisample && pglBlitFramebuffer) + { + if (!framebufferobject.msaa_fboobj) + pglGenFramebuffers(1, &framebufferobject.msaa_fboobj); + + pglBindFramebuffer(GL_FRAMEBUFFER_EXT, framebufferobject.msaa_fboobj); + + // Generate the multisampled color renderbuffer + if (!framebufferobject.msaa_rboobj) + { + pglGenRenderbuffers(1, &framebufferobject.msaa_rboobj); + pglBindRenderbuffer(GL_RENDERBUFFER_EXT, framebufferobject.msaa_rboobj); + pglRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, msaasamples, GL_RGBA8, screen_width, screen_height); + pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, framebufferobject.msaa_rboobj); + } + + // Generate the multisampled depth/stencil renderbuffer + if (!framebufferobject.msaa_depthrboobj) + { + pglGenRenderbuffers(1, &framebufferobject.msaa_depthrboobj); + pglBindRenderbuffer(GL_RENDERBUFFER_EXT, framebufferobject.msaa_depthrboobj); + + if (supportFBO == FBO_ARB) + { + pglRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, msaasamples, GL_DEPTH24_STENCIL8, screen_width, screen_height); + pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER_EXT, framebufferobject.msaa_depthrboobj); + } + else if (supportFBO == FBO_EXT) + { + pglRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, msaasamples, GL_DEPTH_STENCIL_EXT, screen_width, screen_height); + // attach a renderbuffer to depth attachment point + pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, framebufferobject.msaa_depthrboobj); + // attach a renderbuffer to stencil attachment point + pglFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, framebufferobject.msaa_depthrboobj); + } + } + + pglBindRenderbuffer(GL_RENDERBUFFER_EXT, 0); + + if (pglCheckFramebufferStatus(GL_FRAMEBUFFER_EXT) == GL_FRAMEBUFFER_COMPLETE_EXT) + { + framebufferobject.msaa = true; + GL_ClearBuffer(true, true, true, NULL); + GL_DBG_Printf("\nGL_FBO_Generate: Multisampled framebuffer created with %d samples\n", msaasamples); + } + else + { + GL_MSG_Warning("GL_FBO_Generate: Failed to create multisampled Framebuffer Object, falling back to no MSAA"); + + if (framebufferobject.msaa_rboobj) + pglDeleteRenderbuffers(1, &framebufferobject.msaa_rboobj); + if (framebufferobject.msaa_depthrboobj) + pglDeleteRenderbuffers(1, &framebufferobject.msaa_depthrboobj); + + framebufferobject.msaa_rboobj = 0; + framebufferobject.msaa_depthrboobj = 0; + } + } + // Unbind the framebuffer pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); framebufferobject.init = true; } -void GL_Framebuffer_Unbind(void) +void GL_FBO_BindMainFrameBuffer(void) { - if (supportFBO == FBO_NONE || !framebufferobject.init) + if (supportFBO == FBO_NONE) return; pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); pglBindRenderbuffer(GL_RENDERBUFFER_EXT, 0); + + framebufferobject.bound = false; } -void GL_Framebuffer_Enable(void) +void GL_FBO_Enable(void) { - if (supportFBO == FBO_NONE || !UseScreenFBO()) + if (supportFBO == FBO_NONE) return; - GL_Framebuffer_Generate(); + GL_FBO_Generate(); // failed - if (supportFBO == FBO_NONE || !framebufferobject.init) + if (!framebufferobject.init) return; - pglBindFramebuffer(GL_FRAMEBUFFER_EXT, framebufferobject.fboobj); - pglBindRenderbuffer(GL_RENDERBUFFER_EXT, framebufferobject.rboobj); + if (framebufferobject.msaa) + { + pglBindFramebuffer(GL_FRAMEBUFFER_EXT, framebufferobject.msaa_fboobj); + pglBindRenderbuffer(GL_RENDERBUFFER_EXT, framebufferobject.msaa_rboobj); + } + else + { + pglBindFramebuffer(GL_FRAMEBUFFER_EXT, framebufferobject.fboobj); + pglBindRenderbuffer(GL_RENDERBUFFER_EXT, framebufferobject.rboobj); + } + + framebufferobject.bound = true; } -void GL_Framebuffer_Disable(void) +void GL_FBO_Disable(void) { - if (supportFBO == FBO_NONE || !framebufferobject.init) + if (supportFBO == FBO_NONE) return; pglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); @@ -3381,7 +3553,14 @@ void GL_Framebuffer_Disable(void) pglDeleteFramebuffers(1, &framebufferobject.fboobj); framebufferobject.fboobj = 0; - GL_Framebuffer_DeleteAttachments(); + if (framebufferobject.msaa_fboobj) + pglDeleteFramebuffers(1, &framebufferobject.msaa_fboobj); + framebufferobject.msaa_fboobj = 0; + + GL_FBO_DeleteAttachments(); + + framebufferobject.msaa = false; + framebufferobject.bound = false; } #endif @@ -3732,10 +3911,20 @@ void GL_RenderVhsEffect(fixed_t upbary, fixed_t downbary, UINT8 updistort, UINT8 void GL_MakeScreenTexture(int tex) { boolean firstTime; + static GLint lastfiltermode[NUMSCREENTEXTURES] = {-1, -1, -1, -1, -1}; +#ifdef USE_FBO_OGL + const boolean msaa_fbo = (framebufferobject.bound && framebufferobject.msaa); +#endif if (!gl_enable_screen_textures) return; +#ifdef USE_FBO_OGL + // cant read from the msaa fbo, so gotta blit it first into either the single sample fbo or the screen framebuffer.... + if (msaa_fbo) + GL_FBO_Resolve(GL_COLOR_BUFFER_BIT); +#endif + firstTime = (screenTextures[tex] == 0); // Create screen texture @@ -3744,18 +3933,36 @@ void GL_MakeScreenTexture(int tex) pglBindTexture(GL_TEXTURE_2D, screenTextures[tex]); + // if were downsampling, choose to do that with linear filter instead of nearest neighbor + // otherwise well get ugly mixels + GLint filtermode = I_IsDownsampling() ? GL_LINEAR : GL_NEAREST; + + // avoid redundant calls to this, prob doesent really matter tbh + if (filtermode != lastfiltermode[tex] || firstTime) + { + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtermode); + pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtermode); + + lastfiltermode[tex] = filtermode; + } + if (firstTime) { - pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); Clamp2D(GL_TEXTURE_WRAP_S); Clamp2D(GL_TEXTURE_WRAP_T); pglCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, screen_texsizew, screen_texsizeh, 0); } else + { pglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, screen_texsizew, screen_texsizeh); + } tex_downloaded = screenTextures[tex]; + +#ifdef USE_FBO_OGL + if (msaa_fbo) + GL_FBO_Enable(); +#endif } void GL_DrawScreenFinalTexture(int tex, INT32 width, INT32 height) diff --git a/src/hardware/r_opengl/r_opengl.h b/src/hardware/r_opengl/r_opengl.h index 13ba9f6f5..5f9e99101 100644 --- a/src/hardware/r_opengl/r_opengl.h +++ b/src/hardware/r_opengl/r_opengl.h @@ -98,9 +98,9 @@ extern PFNglGetString pglGetString; #endif #ifdef USE_FBO_OGL -void GL_Framebuffer_Unbind(void); -void GL_Framebuffer_Enable(void); -void GL_Framebuffer_Disable(void); +void GL_FBO_BindMainFrameBuffer(void); +void GL_FBO_Enable(void); +void GL_FBO_Disable(void); extern int supportFBO; #endif diff --git a/src/i_video.h b/src/i_video.h index 9a43f82a7..71872ac43 100644 --- a/src/i_video.h +++ b/src/i_video.h @@ -118,19 +118,14 @@ void I_UpdateNoVsync(void); */ void I_ReadScreen(UINT8 * restrict scr, INT32 scale); - UINT32 I_GetRefreshRate(void); -boolean I_CheckNativeRes(void); - void I_SetBorderlessWindow(void); #ifdef USE_FBO_OGL - -void RefreshOGLSDLSurface(void); -void I_DownSample(void); +void I_CheckDownSampleState(boolean forcerefresh); +boolean I_IsDownsampling(void); boolean UseScreenFBO(void); -extern boolean downsample; #endif // So we can call this from i_video event loop diff --git a/src/lua_maplib.c b/src/lua_maplib.c index 87639b77b..4a913ed55 100644 --- a/src/lua_maplib.c +++ b/src/lua_maplib.c @@ -18,7 +18,6 @@ #include "p_slopes.h" #include "r_main.h" -#include "lua_udatalib.h" #include "lua_script.h" #include "lua_libs.h" #include "lua_hud.h" // hud_running errors @@ -256,15 +255,6 @@ static const char *const vector_opt[] = { static const char *const array_opt[] ={"iterate",NULL}; static const char *const valid_opt[] ={"valid",NULL}; -#define pushsubsector(L, subsector) LUA_PushUserdata(L, subsector, META_SUBSECTOR) -int udatalib_getter_subsector(lua_State *L) -UDATALIB_SIMPLE_GETTER(subsector_t*, pushsubsector) -#undef pushsubsector - -#define pushslope(L, slope) LUA_PushUserdata(L, slope, META_SLOPE) -int udatalib_getter_slope(lua_State *L) -UDATALIB_SIMPLE_GETTER(pslope_t*, pushslope) -#undef pushslope // iterates through a sector's thinglist! static int lib_iterateSectorThinglist(lua_State *L) { diff --git a/src/lua_mobjlib.c b/src/lua_mobjlib.c index 9bd5f5203..3b6fceac2 100644 --- a/src/lua_mobjlib.c +++ b/src/lua_mobjlib.c @@ -28,712 +28,378 @@ #include "lua_hud.h" // hud_running errors #include "lua_hook.h" // cmd errors -#include "lua_udatalib.h" +#define MOBJ_FIELDS(X) \ + X(valid) \ + X(x) \ + X(y) \ + X(z) \ + X(snext) \ + X(sprev) \ + X(angle) \ + X(pitch) \ + X(roll) \ + X(rollangle) \ + X(sloperoll) \ + X(slopepitch) \ + X(rollsum) \ + X(sprite) \ + X(frame) \ + X(anim_duration) \ + X(spritexscale) \ + X(spriteyscale) \ + X(spritexoffset) \ + X(spriteyoffset) \ + X(touching_sectorlist) \ + X(lightlevel) \ + X(subsector) \ + X(floorz) \ + X(ceilingz) \ + X(radius) \ + X(height) \ + X(momx) \ + X(momy) \ + X(momz) \ + X(pmomz) \ + X(tics) \ + X(state) \ + X(flags) \ + X(flags2) \ + X(eflags) \ + X(skin) \ + X(localskin) \ + X(color) \ + X(blendmode) \ + X(bnext) \ + X(bprev) \ + X(hnext) \ + X(hprev) \ + X(type) \ + X(info) \ + X(health) \ + X(movedir) \ + X(movecount) \ + X(target) \ + X(reactiontime) \ + X(threshold) \ + X(player) \ + X(lastlook) \ + X(spawnpoint) \ + X(tracer) \ + X(friction) \ + X(movefactor) \ + X(fuse) \ + X(watertop) \ + X(waterbottom) \ + X(mobjnum) \ + X(scale) \ + X(destscale) \ + X(scalespeed) \ + X(extravalue1) \ + X(extravalue2) \ + X(cusval) \ + X(cvmem) \ + X(standingslope) \ + X(colorized) \ + X(mirrored) \ + X(islocal) -int mobj_nosetpos_x(lua_State *L); -int mobj_nosetpos_y(lua_State *L); -int mobj_nosetpos_subsector(lua_State *L); -int mobj_nosetpos_floorz(lua_State *L); -int mobj_nosetpos_ceilingz(lua_State *L); -int mobj_snext_noset(lua_State *L); -int mobj_z_setter(lua_State *L); -int mobj_sprev_unimplemented(lua_State *L); -int mobj_angle_setter(lua_State *L); -int mobj_sloperoll_noop(lua_State *L); -int mobj_spritescale_setter(lua_State *L); -int mobj_spriteoffset_setter(lua_State *L); -int mobj_touching_sectorlist_unimplemented(lua_State *L); -int mobj_radius_setter(lua_State *L); -int mobj_height_setter(lua_State *L); -int mobj_pmomz_setter(lua_State *L); -int mobj_state_getter(lua_State *L); -int mobj_state_setter(lua_State *L); -int mobj_flags_setter(lua_State *L); -int mobj_skin_getter(lua_State *L); -int mobj_skin_setter(lua_State *L); -int mobj_localskin_getter(lua_State *L); -int mobj_localskin_setter(lua_State *L); -int mobj_color_setter(lua_State *L); -int mobj_blendmode_setter(lua_State *L); -int mobj_bnext_noset(lua_State *L); -int mobj_bprev_unimplemented(lua_State *L); -int mobj_hnext_setter(lua_State *L); -int mobj_hprev_setter(lua_State *L); -int mobj_type_setter(lua_State *L); -int mobj_info_getter(lua_State *L); -int mobj_info_noset(lua_State *L); -int mobj_target_getter(lua_State *L); -int mobj_target_setter(lua_State *L); -int mobj_player_noset(lua_State *L); -int mobj_spawnpoint_setter(lua_State *L); -int mobj_tracer_getter(lua_State *L); -int mobj_tracer_setter(lua_State *L); -int mobj_mobjnum_unimplemented(lua_State *L); -int mobj_scale_setter(lua_State *L); -int mobj_destscale_setter(lua_State *L); -int mobj_standingslope_noset(lua_State *L); -int mobj_rollsum_getter(lua_State *L); -int mobj_rollsum_noset(lua_State *L); +enum mobj_e { +#define ENUMITEM(name) mobj_##name, + MOBJ_FIELDS(ENUMITEM) +#undef ENUMITEM + NUMMOBJFIELDS, +}; + +static const char *mobj_opt[] = { +#define STRITEM(name) (#name), + MOBJ_FIELDS(STRITEM) + NULL, +#undef STRITEM +}; + +static int mobj_fields_ref = LUA_NOREF; static const char *const array_opt[] ={"iterate",NULL}; -#define FIELD(type, field_name, getter, setter) { #field_name, offsetof(type, field_name), getter, setter } -static const udata_field_t mobj_fields[] = { - FIELD(mobj_t, x, udatalib_getter_fixed, mobj_nosetpos_x), - FIELD(mobj_t, y, udatalib_getter_fixed, mobj_nosetpos_y), - FIELD(mobj_t, z, udatalib_getter_fixed, mobj_z_setter), - FIELD(mobj_t, snext, udatalib_getter_mobj, mobj_snext_noset), - FIELD(mobj_t, sprev, mobj_sprev_unimplemented, mobj_sprev_unimplemented), - FIELD(mobj_t, angle, udatalib_getter_angle, mobj_angle_setter), - FIELD(mobj_t, pitch, udatalib_getter_angle, udatalib_setter_angle), - FIELD(mobj_t, roll, udatalib_getter_angle, udatalib_setter_angle), - FIELD(mobj_t, rollangle, udatalib_getter_angle, udatalib_setter_angle), - FIELD(mobj_t, sloperoll, udatalib_getter_angle, mobj_sloperoll_noop), - FIELD(mobj_t, slopepitch, udatalib_getter_angle, mobj_sloperoll_noop), - // Macro fails here - { "rollsum", 0, mobj_rollsum_getter, mobj_rollsum_noset }, - FIELD(mobj_t, sprite, udatalib_getter_spritenum, udatalib_setter_spritenum), - FIELD(mobj_t, frame, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(mobj_t, anim_duration, udatalib_getter_uint16, udatalib_setter_uint16), - FIELD(mobj_t, spritexscale, udatalib_getter_fixed, mobj_spritescale_setter), - FIELD(mobj_t, spriteyscale, udatalib_getter_fixed, mobj_spritescale_setter), - FIELD(mobj_t, spritexoffset, udatalib_getter_fixed, mobj_spriteoffset_setter), - FIELD(mobj_t, spriteyoffset, udatalib_getter_fixed, mobj_spriteoffset_setter), - FIELD(mobj_t, touching_sectorlist, mobj_touching_sectorlist_unimplemented, mobj_touching_sectorlist_unimplemented), - FIELD(mobj_t, lightlevel, udatalib_getter_int16, udatalib_setter_int16), - FIELD(mobj_t, subsector, udatalib_getter_subsector, mobj_nosetpos_subsector), - FIELD(mobj_t, floorz, udatalib_getter_fixed, mobj_nosetpos_floorz), - FIELD(mobj_t, ceilingz, udatalib_getter_fixed, mobj_nosetpos_ceilingz), - FIELD(mobj_t, radius, udatalib_getter_fixed, mobj_radius_setter), - FIELD(mobj_t, height, udatalib_getter_fixed, mobj_height_setter), - FIELD(mobj_t, momx, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, momy, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, momz, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, pmomz, udatalib_getter_fixed, mobj_pmomz_setter), - FIELD(mobj_t, tics, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, state, mobj_state_getter, mobj_state_setter), - FIELD(mobj_t, flags, udatalib_getter_uint32, mobj_flags_setter), - FIELD(mobj_t, flags2, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(mobj_t, eflags, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(mobj_t, skin, mobj_skin_getter, mobj_skin_setter), - FIELD(mobj_t, localskin, mobj_localskin_getter, mobj_localskin_setter), - FIELD(mobj_t, color, udatalib_getter_uint8, mobj_color_setter), - FIELD(mobj_t, blendmode, udatalib_getter_int32, mobj_blendmode_setter), - FIELD(mobj_t, bnext, udatalib_getter_mobj, mobj_bnext_noset), - FIELD(mobj_t, bprev, mobj_bprev_unimplemented, mobj_bprev_unimplemented), - FIELD(mobj_t, hnext, udatalib_getter_mobj, mobj_hnext_setter), - FIELD(mobj_t, hprev, udatalib_getter_mobj, mobj_hprev_setter), - FIELD(mobj_t, type, udatalib_getter_mobjtype, mobj_type_setter), - FIELD(mobj_t, info, mobj_info_getter, mobj_info_noset), - FIELD(mobj_t, health, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, movedir, udatalib_getter_angle, udatalib_setter_angle), // Differs a bit from original setter and getter - FIELD(mobj_t, movecount, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, target, mobj_target_getter, mobj_target_setter), - FIELD(mobj_t, reactiontime, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, threshold, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, player, udatalib_getter_player, mobj_player_noset), - FIELD(mobj_t, lastlook, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, spawnpoint, udatalib_getter_mapthing, mobj_spawnpoint_setter), - FIELD(mobj_t, tracer, mobj_tracer_getter, mobj_tracer_setter), - FIELD(mobj_t, friction, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, movefactor, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, fuse, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, watertop, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, waterbottom, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, mobjnum, mobj_mobjnum_unimplemented, mobj_mobjnum_unimplemented), - FIELD(mobj_t, scale, udatalib_getter_fixed, mobj_scale_setter), - FIELD(mobj_t, destscale, udatalib_getter_fixed, mobj_destscale_setter), - FIELD(mobj_t, scalespeed, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(mobj_t, extravalue1, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, extravalue2, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, cusval, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, cvmem, udatalib_getter_int32, udatalib_setter_int32), - FIELD(mobj_t, standingslope, udatalib_getter_slope, mobj_standingslope_noset), - FIELD(mobj_t, colorized, udatalib_getter_boolean, udatalib_setter_boolean), - FIELD(mobj_t, mirrored, udatalib_getter_boolean, udatalib_setter_boolean), - FIELD(mobj_t, islocal, udatalib_getter_boolean, udatalib_setter_boolean), - { NULL, 0, NULL, NULL }, -}; -#undef FIELD - -// First, implement udatalib simple getters -#define pushmobj(L, mobj) LUA_PushUserdata(L, mobj, META_MOBJ) -int udatalib_getter_mobj(lua_State *L) -UDATALIB_SIMPLE_GETTER(mobj_t*, pushmobj) -#undef pushmobj - -int udatalib_getter_mobjtype(lua_State *L) -UDATALIB_SIMPLE_GETTER(mobjtype_t, lua_pushinteger) - -#define pushmapthing(L, mapthing) LUA_PushUserdata(L, mapthing, META_MAPTHING) -int udatalib_getter_mapthing(lua_State *L) -UDATALIB_SIMPLE_GETTER(mapthing_t*, pushmapthing) -#undef pushmapthing - -// Now specific fields related to mobj positions that cannot be set directly -#define NOSETPOS(field) \ -int mobj_nosetpos_ ## field(lua_State *L) \ -{ \ - return luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " should not be set directly. Use " LUA_QL("P_Move") ", " LUA_QL("P_TryMove") ", or " LUA_QL("P_SetOrigin") ", or " LUA_QL("P_MoveOrigin") " instead.", #field); \ -} - -NOSETPOS(x) -NOSETPOS(y) -NOSETPOS(subsector) -NOSETPOS(floorz) -NOSETPOS(ceilingz) - -#undef NOSETPOS - -// Other fields that cannot be set directly -#define NOSET(field) \ -int mobj_ ## field ## _noset(lua_State *L) \ -{ \ - return luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " should not be set directly.", #field); \ -} - -#define NOSET_USE(field, use) \ -int mobj_ ## field ## _noset(lua_State *L) \ -{ \ - return luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " should not be set directly. Use " LUA_QL(use) " instead.", #field); \ -} - -NOSET(snext) -NOSET(bnext) -NOSET(info) -NOSET(player) -NOSET(standingslope) -NOSET_USE(rollsum, "rollangle") - -#undef NOSET -#undef NOSET_USE - -// Unimplemented fields (why would you need to set them like that explicitly? -// No idea, i'm keeping it for synch reasons) -#define UNIMPLEMENTED(field) \ -int mobj_ ## field ## _unimplemented(lua_State *L) \ -{ \ - return luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " is not implemented for Lua and cannot be accessed.", #field); \ -} - -UNIMPLEMENTED(sprev) -UNIMPLEMENTED(touching_sectorlist) -UNIMPLEMENTED(bprev) -UNIMPLEMENTED(mobjnum) - -#undef UNIMPLEMENTED - -// For some dumb reason it is valid to set sloperoll, even though it is read -// only -int mobj_sloperoll_noop(lua_State *L) { (void)L; return 0; } - -// Now other getters/setters with arbitary logic - -// Getters get light userdata, which in this case is just mobj_t pointer -#define GETMO() (mobj_t*)lua_touserdata(L, 1) - -int mobj_z_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - // z doesn't cross sector bounds so it's okay. - mobj_t *ptmthing = tmthing; - mo->z = luaL_checkfixed(L, 2); - P_CheckPosition(mo, mo->x, mo->y); - mo->floorz = tmfloorz; - mo->ceilingz = tmceilingz; - P_SetTarget(&tmthing, ptmthing); - - return 0; -} - -int mobj_angle_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - mo->angle = luaL_checkangle(L, 2); - if (mo->player) - P_ForceLocalAngle(mo->player, mo->angle); - - return 0; -} - -int mobj_spritescale_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - fixed_t *spritescale; - UDATALIB_GETFIELD(fixed_t, spritescale); - - if (!mo->player) - *spritescale = luaL_checkfixed(L, 2); - else - { - // Mmm yea - if (spritescale == &mo->spritexscale) - mo->realxscale = luaL_checkfixed(L, 2); - else - mo->realyscale = luaL_checkfixed(L, 2); - } - return 0; -} - -int mobj_spriteoffset_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - fixed_t *spriteoffset; - UDATALIB_GETFIELD(fixed_t, spriteoffset); - - if (!mo->player) - *spriteoffset = luaL_checkfixed(L, 2); - else - { - // Mmm yea - if (spriteoffset == &mo->spritexoffset) - mo->realxoffset = luaL_checkfixed(L, 2); - else - mo->realyoffset = luaL_checkfixed(L, 2); - } - return 0; -} - -int mobj_radius_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - mobj_t *ptmthing = tmthing; - mo->radius = luaL_checkfixed(L, 2); - if (mo->radius < 0) - mo->radius = 0; - P_CheckPosition(mo, mo->x, mo->y); - mo->floorz = tmfloorz; - mo->ceilingz = tmceilingz; - P_SetTarget(&tmthing, ptmthing); - - return 0; -} - -int mobj_height_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - mobj_t *ptmthing = tmthing; - mo->height = luaL_checkfixed(L, 2); - if (mo->height < 0) - mo->height = 0; - P_CheckPosition(mo, mo->x, mo->y); - mo->floorz = tmfloorz; - mo->ceilingz = tmceilingz; - P_SetTarget(&tmthing, ptmthing); - - return 0; -} - -int mobj_pmomz_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - mo->pmomz = luaL_checkfixed(L, 2); - mo->eflags |= MFE_APPLYPMOMZ; - - return 0; -} - -int mobj_state_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - lua_pushinteger(L, mo->state-states); - - return 1; -} - -int mobj_state_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (mo->player) - P_SetPlayerMobjState(mo, luaL_checkinteger(L, 2)); - else - P_SetMobjState(mo, luaL_checkinteger(L, 2)); - - return 0; -} - -int mobj_flags_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - UINT32 flags = luaL_checkinteger(L, 2); - if ((flags & (MF_NOBLOCKMAP|MF_NOSECTOR)) != (mo->flags & (MF_NOBLOCKMAP|MF_NOSECTOR))) - { - P_UnsetThingPosition(mo); - mo->flags = flags; - if (flags & MF_NOSECTOR && sector_list) - { - P_DelSeclist(sector_list); - sector_list = NULL; - } - mo->snext = NULL, mo->sprev = NULL; - mo->bnext = NULL, mo->bprev = NULL; - P_SetThingPosition(mo); - } - else - mo->flags = flags; - - return 0; -} - -int mobj_skin_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (!mo->skin) - return 0; - - // HUD ONLY!!!!!!!!!! - if (hud_running && cv_luaimmersion.value) - { - lua_pushstring(L, K_GetMobjSkin(mo)->name); - } - else - lua_pushstring(L, ((skin_t *)mo->skin)->name); - - return 1; -} - -int mobj_skin_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - INT32 i; - char skin[SKINNAMESIZE+1]; // all skin names are limited to this length - strlcpy(skin, luaL_checkstring(L, 2), sizeof skin); - strlwr(skin); // all skin names are lowercase - for (i = 0; i < numskins; i++) - { - if (fastcmp(skins[i].name, skin)) - { - mo->skin = &skins[i]; - return 0; - } - } - - return luaL_error(L, "mobj.skin '%s' not found!", skin); -} - -int mobj_localskin_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (mo->localskin) - lua_pushstring(L, ((skin_t *)mo->localskin)->name); - else - lua_pushnil(L); - - return 1; -} - -int mobj_localskin_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (mo->player) - { - SetLocalPlayerSkin(mo->player - players, luaL_optstring(L, 2, "none"), NULL); - } - else - { - INT32 i; - char skin[SKINNAMESIZE+1]; // all skin names are limited to this length - strlcpy(skin, luaL_optstring(L, 2, "none"), sizeof skin); - strlwr(skin); // all skin names are lowercase - - if (!fasticmp(skin, "none")) - { - // Try localskins - for (i = 0; i < numlocalskins; i++) - { - if (fasticmp(localskins[i].name, skin)) - { - mo->localskin = &localskins[i]; - mo->skinlocal = true; - return 0; - } - } - - // Try other skins - for (i = 0; i < numskins; i++) - { - if (fasticmp(skins[i].name, skin)) - { - mo->localskin = &skins[i]; - mo->skinlocal = false; - return 0; - } - } - } - else - { - mo->localskin = NULL; - mo->skinlocal = false; - } - } - - return 0; -} - -int mobj_color_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - UINT8 newcolor = (UINT8)luaL_checkinteger(L, 2); - if (newcolor >= MAXTRANSLATIONS) - return luaL_error(L, "mobj.color %d out of range (0 - %d).", newcolor, MAXTRANSLATIONS-1); - mo->color = newcolor; - - return 0; -} - -int mobj_blendmode_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - INT32 blendmode = (INT32)luaL_checkinteger(L, 2); - if (blendmode < 0 || blendmode > AST_OVERLAY) - return luaL_error(L, "mobj.blendmode %d out of range (0 - %d).", blendmode, AST_OVERLAY); - mo->blendmode = blendmode; - - return 0; -} - -int mobj_hnext_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (lua_isnil(L, 2)) - P_SetTarget(&mo->hnext, NULL); - else - { - mobj_t *hnext = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&mo->hnext, hnext); - } - - return 0; -} - -int mobj_hprev_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (lua_isnil(L, 2)) - P_SetTarget(&mo->hprev, NULL); - else - { - mobj_t *hprev = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&mo->hprev, hprev); - } - - return 0; -} - -int mobj_type_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - mobjtype_t newtype = luaL_checkinteger(L, 2); - if (newtype >= NUMMOBJTYPES) - return luaL_error(L, "mobj.type %d out of range (0 - %d).", newtype, NUMMOBJTYPES-1); - mo->type = newtype; - mo->info = &mobjinfo[newtype]; - P_SetScale(mo, mo->scale); - - return 0; -} - -int mobj_info_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - LUA_PushUserdata(L, &mobjinfo[mo->type], META_MOBJINFO); - - return 1; -} - -int mobj_target_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (mo->target && P_MobjWasRemoved(mo->target)) - { // don't put invalid mobj back into Lua. - P_SetTarget(&mo->target, NULL); - return 0; - } - LUA_PushUserdata(L, mo->target, META_MOBJ); - - return 1; -} - -int mobj_target_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (lua_isnil(L, 2)) - P_SetTarget(&mo->target, NULL); - else - { - mobj_t *target = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&mo->target, target); - } - - return 0; -} - -int mobj_spawnpoint_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (lua_isnil(L, 2)) - mo->spawnpoint = NULL; - else - { - mapthing_t *spawnpoint = *((mapthing_t **)luaL_checkudata(L, 2, META_MAPTHING)); - mo->spawnpoint = spawnpoint; - } - - return 0; -} - -int mobj_tracer_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (mo->tracer && P_MobjWasRemoved(mo->tracer)) - { // don't put invalid mobj back into Lua. - P_SetTarget(&mo->tracer, NULL); - return 0; - } - LUA_PushUserdata(L, mo->tracer, META_MOBJ); - - return 1; -} - -int mobj_tracer_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - if (lua_isnil(L, 2)) - P_SetTarget(&mo->tracer, NULL); - else - { - mobj_t *tracer = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&mo->tracer, tracer); - } - - return 0; -} - -int mobj_scale_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - fixed_t scale = luaL_checkfixed(L, 2); - if (scale < FRACUNIT/100) - scale = FRACUNIT/100; - mo->destscale = scale; - P_SetScale(mo, scale); - mo->old_scale = scale; - - return 0; -} - -int mobj_destscale_setter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - fixed_t scale = luaL_checkfixed(L, 2); - if (scale < FRACUNIT/100) - scale = FRACUNIT/100; - mo->destscale = scale; - - return 0; -} - -// WARNING: Not synch safe! -// Don't use this field in game logic code! -int mobj_rollsum_getter(lua_State *L) -{ - mobj_t *mo = GETMO(); - - angle_t pitchnroll = P_MobjPitchAndRoll(mo); - - angle_t rollsum = mo->rollangle + pitchnroll; - - if (mo->player) - { - rollsum += R_PlayerSliptideAngle(mo->player); - } - - lua_pushangle(L, rollsum); - - return 1; -} - +#define UNIMPLEMENTED luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " is not implemented for Lua and cannot be accessed.", mobj_opt[field]) static int mobj_get(lua_State *L) { mobj_t *mo = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); - - // We still gonna have 2 strcmp for "valid" field - const char *field = luaL_checkstring(L, 2); - + enum mobj_e field = Lua_optoption(L, 2, -1, mobj_fields_ref); lua_settop(L, 2); if (!mo) { - if (fastcmp(field, "valid")) { + if (field == mobj_valid) { lua_pushboolean(L, 0); return 1; } return LUA_ErrInvalid(L, "mobj_t"); - } else if (fastcmp(field, "valid")) { - lua_pushboolean(L, 1); - return 1; - } + } - lua_getmetatable(L, 1); - - lua_pushvalue(L, 2); // Push field name - lua_rawget(L, -2); // Get getter/setter table from metatable - - // If field exists, run getter for it - if (!lua_isnil(L, -1)) { - //CONS_Printf("Running getter for field %s\n", field); - lua_rawgeti(L, -1, UDATALIB_GETTER); - lua_pushlightuserdata(L, mo); - lua_call(L, 1, 1); - //CONS_Printf("Getter returned %s\n", lua_typename(L, lua_type(L, -1))); - return 1; - } - - lua_pop(L, 1); - - //CONS_Printf("Getting custom field %s\n", field); - - // Othervise, return extra value - // extra custom variables in Lua memory - lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); - I_Assert(lua_istable(L, -1)); - lua_pushlightuserdata(L, mo); - lua_rawget(L, -2); - if (!lua_istable(L, -1)) { // no extra values table - CONS_Debug(DBG_LUA, M_GetText("'%s' has no extvars table or field named '%s'; returning nil.\n"), "mobj_t", lua_tostring(L, 2)); - return 0; - } - lua_pushvalue(L, 2); // field name - lua_gettable(L, -2); - if (lua_isnil(L, -1)) // no value for this field - CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; returning nil.\n"), "mobj_t", lua_tostring(L, 2)); - - return 1; + switch(field) + { + case mobj_valid: + lua_pushboolean(L, 1); + break; + case mobj_x: + lua_pushfixed(L, mo->x); + break; + case mobj_y: + lua_pushfixed(L, mo->y); + break; + case mobj_z: + lua_pushfixed(L, mo->z); + break; + case mobj_snext: + LUA_PushUserdata(L, mo->snext, META_MOBJ); + break; + case mobj_sprev: + // sprev is actually the previous mobj's snext pointer, + // or the subsector->sector->thing_list if there is no previous mobj, + // i.e. it will always ultimately point to THIS mobj -- so that's actually not useful to Lua and won't be included. + return UNIMPLEMENTED; + case mobj_angle: + lua_pushangle(L, mo->angle); + break; + case mobj_sprite: + lua_pushinteger(L, mo->sprite); + break; + case mobj_frame: + lua_pushinteger(L, mo->frame); + break; + case mobj_anim_duration: + lua_pushinteger(L, mo->anim_duration); + break; + case mobj_touching_sectorlist: + return UNIMPLEMENTED; + case mobj_subsector: + LUA_PushUserdata(L, mo->subsector, META_SUBSECTOR); + break; + case mobj_floorz: + lua_pushfixed(L, mo->floorz); + break; + case mobj_ceilingz: + lua_pushfixed(L, mo->ceilingz); + break; + case mobj_radius: + lua_pushfixed(L, mo->radius); + break; + case mobj_height: + lua_pushfixed(L, mo->height); + break; + case mobj_momx: + lua_pushfixed(L, mo->momx); + break; + case mobj_momy: + lua_pushfixed(L, mo->momy); + break; + case mobj_momz: + lua_pushfixed(L, mo->momz); + break; + case mobj_pmomz: + lua_pushfixed(L, mo->pmomz); + break; + case mobj_tics: + lua_pushinteger(L, mo->tics); + break; + case mobj_state: // state number, not struct + lua_pushinteger(L, mo->state-states); + break; + case mobj_flags: + lua_pushinteger(L, mo->flags); + break; + case mobj_flags2: + lua_pushinteger(L, mo->flags2); + break; + case mobj_eflags: + lua_pushinteger(L, mo->eflags); + break; + case mobj_skin: // skin name or nil, not struct + if (!mo->skin) + return 0; + lua_pushstring(L, ((skin_t *)mo->skin)->name); + break; + case mobj_color: + lua_pushinteger(L, mo->color); + break; + case mobj_bnext: + LUA_PushUserdata(L, mo->bnext, META_MOBJ); + break; + case mobj_bprev: + // bprev -- same deal as sprev above, but for the blockmap. + return UNIMPLEMENTED; + case mobj_hnext: + LUA_PushUserdata(L, mo->hnext, META_MOBJ); + break; + case mobj_hprev: + // implimented differently from sprev and bprev because SSNTails. + LUA_PushUserdata(L, mo->hprev, META_MOBJ); + break; + case mobj_type: + lua_pushinteger(L, mo->type); + break; + case mobj_info: + LUA_PushUserdata(L, &mobjinfo[mo->type], META_MOBJINFO); + break; + case mobj_health: + lua_pushinteger(L, mo->health); + break; + case mobj_movedir: + lua_pushinteger(L, mo->movedir); + break; + case mobj_movecount: + lua_pushinteger(L, mo->movecount); + break; + case mobj_target: + if (mo->target && P_MobjWasRemoved(mo->target)) + { // don't put invalid mobj back into Lua. + P_SetTarget(&mo->target, NULL); + return 0; + } + LUA_PushUserdata(L, mo->target, META_MOBJ); + break; + case mobj_reactiontime: + lua_pushinteger(L, mo->reactiontime); + break; + case mobj_threshold: + lua_pushinteger(L, mo->threshold); + break; + case mobj_player: + LUA_PushUserdata(L, mo->player, META_PLAYER); + break; + case mobj_lastlook: + lua_pushinteger(L, mo->lastlook); + break; + case mobj_spawnpoint: + LUA_PushUserdata(L, mo->spawnpoint, META_MAPTHING); + break; + case mobj_tracer: + if (mo->tracer && P_MobjWasRemoved(mo->tracer)) + { // don't put invalid mobj back into Lua. + P_SetTarget(&mo->tracer, NULL); + return 0; + } + LUA_PushUserdata(L, mo->tracer, META_MOBJ); + break; + case mobj_friction: + lua_pushfixed(L, mo->friction); + break; + case mobj_movefactor: + lua_pushfixed(L, mo->movefactor); + break; + case mobj_fuse: + lua_pushinteger(L, mo->fuse); + break; + case mobj_watertop: + lua_pushfixed(L, mo->watertop); + break; + case mobj_waterbottom: + lua_pushfixed(L, mo->waterbottom); + break; + case mobj_mobjnum: + // mobjnum is a networking thing generated for $$$.sav + // and therefore shouldn't be used by Lua. + return UNIMPLEMENTED; + case mobj_scale: + lua_pushfixed(L, mo->scale); + break; + case mobj_destscale: + lua_pushfixed(L, mo->destscale); + break; + case mobj_scalespeed: + lua_pushfixed(L, mo->scalespeed); + break; + case mobj_extravalue1: + lua_pushinteger(L, mo->extravalue1); + break; + case mobj_extravalue2: + lua_pushinteger(L, mo->extravalue2); + break; + case mobj_cusval: + lua_pushinteger(L, mo->cusval); + break; + case mobj_cvmem: + lua_pushinteger(L, mo->cvmem); + break; + case mobj_standingslope: + LUA_PushUserdata(L, mo->standingslope, META_SLOPE); + break; + case mobj_colorized: + lua_pushboolean(L, mo->colorized); + break; + // Saturn + case mobj_pitch: + lua_pushangle(L, mo->pitch); + break; + case mobj_roll: + lua_pushangle(L, mo->roll); + break; + case mobj_rollangle: + lua_pushangle(L, mo->rollangle); + break; + case mobj_sloperoll: + lua_pushangle(L, mo->sloperoll); + break; + case mobj_slopepitch: + lua_pushangle(L, mo->slopepitch); + break; + case mobj_rollsum: + { + angle_t pitchnroll = P_MobjPitchAndRoll(mo); + angle_t rollsum = mo->rollangle + pitchnroll; + if (mo->player) + { + rollsum += R_PlayerSliptideAngle(mo->player); + } + lua_pushangle(L, rollsum); + } + break; + case mobj_spritexscale: + lua_pushfixed(L, mo->spritexscale); + break; + case mobj_spriteyscale: + lua_pushfixed(L, mo->spriteyscale); + break; + case mobj_spritexoffset: + lua_pushfixed(L, mo->spritexoffset); + break; + case mobj_spriteyoffset: + lua_pushfixed(L, mo->spriteyoffset); + break; + case mobj_lightlevel: + lua_pushinteger(L, mo->lightlevel); + break; + case mobj_localskin: + if (mo->localskin) + lua_pushstring(L, ((skin_t *)mo->localskin)->name); + else + lua_pushnil(L); + break; + case mobj_blendmode: + lua_pushinteger(L, mo->blendmode); + break; + case mobj_mirrored: + lua_pushboolean(L, mo->mirrored); + break; + case mobj_islocal: + lua_pushboolean(L, mo->islocal); + break; + default: // extra custom variables in Lua memory + lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); + I_Assert(lua_istable(L, -1)); + lua_pushlightuserdata(L, mo); + lua_rawget(L, -2); + if (!lua_istable(L, -1)) { // no extra values table + CONS_Debug(DBG_LUA, M_GetText("'%s' has no extvars table or field named '%s'; returning nil.\n"), "mobj_t", lua_tostring(L, 2)); + return 0; + } + lua_pushvalue(L, 2); // field name + lua_gettable(L, -2); + if (lua_isnil(L, -1)) // no value for this field + CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; returning nil.\n"), "mobj_t", lua_tostring(L, 2)); + break; + } + return 1; } #define NOSET luaL_error(L, LUA_QL("mobj_t") " field " LUA_QS " should not be set directly.", mobj_opt[field]) @@ -741,58 +407,418 @@ static int mobj_get(lua_State *L) static int mobj_set(lua_State *L) { mobj_t *mo = *((mobj_t **)luaL_checkudata(L, 1, META_MOBJ)); - - lua_settop(L, 3); + enum mobj_e field = Lua_optoption(L, 2, mobj_valid, mobj_fields_ref);; + lua_settop(L, 3); if (!mo) return LUA_ErrInvalid(L, "mobj_t"); - if (hud_running) - return luaL_error(L, "Do not alter mobj_t in HUD rendering code!"); + if (hud_running) + return luaL_error(L, "Do not alter mobj_t in HUD rendering code!"); - if (hook_cmd_running) - return luaL_error(L, "Do not alter mobj_t in BuildCMD code!"); + if (hook_cmd_running) + return luaL_error(L, "Do not alter mobj_t in BuildCMD code!"); - lua_getmetatable(L, 1); // Push metatable + switch(field) + { + case mobj_valid: + return NOSET; + case mobj_x: + return NOSETPOS; + case mobj_y: + return NOSETPOS; + case mobj_z: + { + // z doesn't cross sector bounds so it's okay. + mobj_t *ptmthing = tmthing; + mo->z = luaL_checkfixed(L, 3); + P_CheckPosition(mo, mo->x, mo->y); + mo->floorz = tmfloorz; + mo->ceilingz = tmceilingz; + P_SetTarget(&tmthing, ptmthing); + } + break; + case mobj_snext: + return NOSETPOS; + case mobj_sprev: + return UNIMPLEMENTED; + case mobj_angle: + mo->angle = luaL_checkangle(L, 3); + if (mo->player) + P_ForceLocalAngle(mo->player, mo->angle); + break; + case mobj_sprite: + mo->sprite = luaL_checkinteger(L, 3); + break; + case mobj_frame: + mo->frame = (UINT32)luaL_checkinteger(L, 3); + break; + case mobj_anim_duration: + mo->anim_duration = (UINT16)luaL_checkinteger(L, 3); + break; + case mobj_touching_sectorlist: + return UNIMPLEMENTED; + case mobj_subsector: + return NOSETPOS; + case mobj_floorz: + return NOSETPOS; + case mobj_ceilingz: + return NOSETPOS; + case mobj_radius: + { + mobj_t *ptmthing = tmthing; + mo->radius = luaL_checkfixed(L, 3); + if (mo->radius < 0) + mo->radius = 0; + P_CheckPosition(mo, mo->x, mo->y); + mo->floorz = tmfloorz; + mo->ceilingz = tmceilingz; + P_SetTarget(&tmthing, ptmthing); + } + break; + case mobj_height: + { + mobj_t *ptmthing = tmthing; + mo->height = luaL_checkfixed(L, 3); + if (mo->height < 0) + mo->height = 0; + P_CheckPosition(mo, mo->x, mo->y); + mo->floorz = tmfloorz; + mo->ceilingz = tmceilingz; + P_SetTarget(&tmthing, ptmthing); + } + break; + case mobj_momx: + mo->momx = luaL_checkfixed(L, 3); + break; + case mobj_momy: + mo->momy = luaL_checkfixed(L, 3); + break; + case mobj_momz: + mo->momz = luaL_checkfixed(L, 3); + break; + case mobj_pmomz: + mo->pmomz = luaL_checkfixed(L, 3); + mo->eflags |= MFE_APPLYPMOMZ; + break; + case mobj_tics: + mo->tics = luaL_checkinteger(L, 3); + break; + case mobj_state: // set state by enum + if (mo->player) + P_SetPlayerMobjState(mo, luaL_checkinteger(L, 3)); + else + P_SetMobjState(mo, luaL_checkinteger(L, 3)); + break; + case mobj_flags: // special handling for MF_NOBLOCKMAP and MF_NOSECTOR + { + UINT32 flags = luaL_checkinteger(L, 3); + if ((flags & (MF_NOBLOCKMAP|MF_NOSECTOR)) != (mo->flags & (MF_NOBLOCKMAP|MF_NOSECTOR))) + { + P_UnsetThingPosition(mo); + mo->flags = flags; + if (flags & MF_NOSECTOR && sector_list) + { + P_DelSeclist(sector_list); + sector_list = NULL; + } + mo->snext = NULL, mo->sprev = NULL; + mo->bnext = NULL, mo->bprev = NULL; + P_SetThingPosition(mo); + } + else + mo->flags = flags; + } + break; + case mobj_flags2: + mo->flags2 = (UINT32)luaL_checkinteger(L, 3); + break; + case mobj_eflags: + mo->eflags = (UINT32)luaL_checkinteger(L, 3); + break; + case mobj_skin: // set skin by name + { + INT32 i; + char skin[SKINNAMESIZE+1]; // all skin names are limited to this length + strlcpy(skin, luaL_checkstring(L, 3), sizeof skin); + strlwr(skin); // all skin names are lowercase + for (i = 0; i < numskins; i++) + { + if (fastcmp(skins[i].name, skin)) + { + mo->skin = &skins[i]; + return 0; + } + } + return luaL_error(L, "mobj.skin '%s' not found!", skin); + } + case mobj_color: + { + UINT8 newcolor = (UINT8)luaL_checkinteger(L,3); + if (newcolor >= MAXTRANSLATIONS) + return luaL_error(L, "mobj.color %d out of range (0 - %d).", newcolor, MAXTRANSLATIONS-1); + mo->color = newcolor; + } + break; + case mobj_bnext: + return NOSETPOS; + case mobj_bprev: + return UNIMPLEMENTED; + case mobj_hnext: + if (lua_isnil(L, 3)) + P_SetTarget(&mo->hnext, NULL); + else + { + mobj_t *hnext = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&mo->hnext, hnext); + } + break; + case mobj_hprev: + if (lua_isnil(L, 3)) + P_SetTarget(&mo->hprev, NULL); + else + { + mobj_t *hprev = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&mo->hprev, hprev); + } + break; + case mobj_type: // yeah sure, we'll let you change the mobj's type. + { + mobjtype_t newtype = luaL_checkinteger(L, 3); + if (newtype >= NUMMOBJTYPES) + return luaL_error(L, "mobj.type %d out of range (0 - %d).", newtype, NUMMOBJTYPES-1); + mo->type = newtype; + mo->info = &mobjinfo[newtype]; + P_SetScale(mo, mo->scale); + } + break; + case mobj_info: + return NOSET; + case mobj_health: + mo->health = luaL_checkinteger(L, 3); + break; + case mobj_movedir: + mo->movedir = (angle_t)luaL_checkinteger(L, 3); + break; + case mobj_movecount: + mo->movecount = luaL_checkinteger(L, 3); + break; + case mobj_target: + if (lua_isnil(L, 3)) + P_SetTarget(&mo->target, NULL); + else + { + mobj_t *target = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&mo->target, target); + } + break; + case mobj_reactiontime: + mo->reactiontime = luaL_checkinteger(L, 3); + break; + case mobj_threshold: + mo->threshold = luaL_checkinteger(L, 3); + break; + case mobj_player: + return NOSET; + case mobj_lastlook: + mo->lastlook = luaL_checkinteger(L, 3); + break; + case mobj_spawnpoint: + if (lua_isnil(L, 3)) + mo->spawnpoint = NULL; + else + { + mapthing_t *spawnpoint = *((mapthing_t **)luaL_checkudata(L, 3, META_MAPTHING)); + mo->spawnpoint = spawnpoint; + } + break; + case mobj_tracer: + if (lua_isnil(L, 3)) + P_SetTarget(&mo->tracer, NULL); + else + { + mobj_t *tracer = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&mo->tracer, tracer); + } + break; + case mobj_friction: + mo->friction = luaL_checkfixed(L, 3); + break; + case mobj_movefactor: + mo->movefactor = luaL_checkfixed(L, 3); + break; + case mobj_fuse: + mo->fuse = luaL_checkinteger(L, 3); + break; + case mobj_watertop: + mo->watertop = luaL_checkfixed(L, 3); + break; + case mobj_waterbottom: + mo->waterbottom = luaL_checkfixed(L, 3); + break; + case mobj_mobjnum: + return UNIMPLEMENTED; + case mobj_scale: + { + fixed_t scale = luaL_checkfixed(L, 3); + if (scale < FRACUNIT/100) + scale = FRACUNIT/100; + mo->destscale = scale; + P_SetScale(mo, scale); + mo->old_scale = scale; + } + break; + case mobj_destscale: + { + fixed_t scale = luaL_checkfixed(L, 3); + if (scale < FRACUNIT/100) + scale = FRACUNIT/100; + mo->destscale = scale; + } + break; + case mobj_scalespeed: + mo->scalespeed = luaL_checkfixed(L, 3); + break; + case mobj_extravalue1: + mo->extravalue1 = luaL_checkinteger(L, 3); + break; + case mobj_extravalue2: + mo->extravalue2 = luaL_checkinteger(L, 3); + break; + case mobj_cusval: + mo->cusval = luaL_checkinteger(L, 3); + break; + case mobj_cvmem: + mo->cvmem = luaL_checkinteger(L, 3); + break; + case mobj_standingslope: + return NOSET; + case mobj_colorized: + mo->colorized = luaL_checkboolean(L, 3); + break; + case mobj_pitch: + mo->pitch = luaL_checkangle(L, 3); + break; + case mobj_roll: + mo->roll = luaL_checkangle(L, 3); + break; + case mobj_rollangle: + mo->rollangle = luaL_checkangle(L, 3); + break; + case mobj_sloperoll: + case mobj_slopepitch: + //return NOSET; // Thanks to airbrake script, this has to be a noop + break; + case mobj_rollsum: + return NOSET; + break; + case mobj_spritexscale: + if (mo->player) + mo->realxscale = luaL_checkfixed(L, 3); + else + mo->spritexscale = luaL_checkfixed(L, 3); + break; + case mobj_spriteyscale: + if (mo->player) + mo->realyscale = luaL_checkfixed(L, 3); + else + mo->spriteyscale = luaL_checkfixed(L, 3); + break; + case mobj_spritexoffset: + if (mo->player) + mo->realxoffset = luaL_checkfixed(L, 3); + else + mo->spritexoffset = luaL_checkfixed(L, 3); + break; + case mobj_spriteyoffset: + if (mo->player) + mo->realyoffset = luaL_checkfixed(L, 3); + else + mo->spriteyoffset = luaL_checkfixed(L, 3); + break; + case mobj_lightlevel: + mo->lightlevel = (INT16)luaL_checkinteger(L, 3); + break; + case mobj_localskin: + { + if (mo->player) + { + SetLocalPlayerSkin(mo->player - players, luaL_optstring(L, 3, "none"), NULL); + } + else + { + INT32 i; + char skin[SKINNAMESIZE+1]; // all skin names are limited to this length + strlcpy(skin, luaL_optstring(L, 3, "none"), sizeof skin); + strlwr(skin); // all skin names are lowercase - lua_pushvalue(L, 2); // Push field name - lua_rawget(L, -2); // Get getter/setter table from metatable + if (!fasticmp(skin, "none")) + { + // Try localskins + for (i = 0; i < numlocalskins; i++) + { + if (fasticmp(localskins[i].name, skin)) + { + mo->localskin = &localskins[i]; + mo->skinlocal = true; + return 0; + } + } - // If field exists, run setter for it - if (!lua_isnil(L, -1)) { - //CONS_Printf("Running setter for field %s\n", luaL_checkstring(L, 2)); - lua_rawgeti(L, -1, UDATALIB_SETTER); - lua_pushlightuserdata(L, mo); - lua_pushvalue(L, 3); - lua_call(L, 2, 0); - return 0; - } - - lua_pop(L, 1); - - //CONS_Printf("Adding custom field %s\n", luaL_checkstring(L, 2)); - - // Otherwise, set custom field - - lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); - I_Assert(lua_istable(L, -1)); - lua_pushlightuserdata(L, mo); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - // This index doesn't have a table for extra values yet, let's make one. - lua_pop(L, 1); - CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; adding it as Lua data.\n"), "mobj_t", lua_tostring(L, 2)); - lua_newtable(L); - lua_pushlightuserdata(L, mo); - lua_pushvalue(L, -2); // ext value table - lua_rawset(L, -4); // LREG_EXTVARS table - } - lua_pushvalue(L, 2); // key - lua_pushvalue(L, 3); // value to store - lua_settable(L, -3); - lua_pop(L, 2); - - return 0; + // Try other skins + for (i = 0; i < numskins; i++) + { + if (fasticmp(skins[i].name, skin)) + { + mo->localskin = &skins[i]; + mo->skinlocal = false; + return 0; + } + } + } + else + { + mo->localskin = NULL; + mo->skinlocal = false; + } + } + } + break; + case mobj_blendmode: + { + INT32 blendmode = (INT32)luaL_checkinteger(L, 3); + if (blendmode < 0 || blendmode > AST_OVERLAY) + return luaL_error(L, "mobj.blendmode %d out of range (0 - %d).", blendmode, AST_OVERLAY); + mo->blendmode = blendmode; + } + break; + case mobj_mirrored: + mo->mirrored = luaL_checkboolean(L, 3); + break; + case mobj_islocal: + mo->islocal = luaL_checkboolean(L, 3); + break; + default: + lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); + I_Assert(lua_istable(L, -1)); + lua_pushlightuserdata(L, mo); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + // This index doesn't have a table for extra values yet, let's make one. + lua_pop(L, 1); + CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; adding it as Lua data.\n"), "mobj_t", lua_tostring(L, 2)); + lua_newtable(L); + lua_pushlightuserdata(L, mo); + lua_pushvalue(L, -2); // ext value table + lua_rawset(L, -4); // LREG_EXTVARS table + } + lua_pushvalue(L, 2); // key + lua_pushvalue(L, 3); // value to store + lua_settable(L, -3); + lua_pop(L, 2); + break; + } + return 0; } #undef UNIMPLEMENTED @@ -917,13 +943,13 @@ static int mapthing_set(lua_State *L) mt->z = (INT16)luaL_checkinteger(L, 3); break; case mapthing_extrainfo: - { - INT32 extrainfo = luaL_checkinteger(L, 3); - if (extrainfo & ~15) - return luaL_error(L, "mapthing_t extrainfo set %d out of range (%d - %d)", extrainfo, 0, 15); - mt->extrainfo = (UINT8)extrainfo; + { + INT32 extrainfo = luaL_checkinteger(L, 3); + if (extrainfo & ~15) + return luaL_error(L, "mapthing_t extrainfo set %d out of range (%d - %d)", extrainfo, 0, 15); + mt->extrainfo = (UINT8)extrainfo; + } break; - } case mapthing_mobj: mt->mobj = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); break; @@ -982,14 +1008,14 @@ FUNCINLINE static ATTRINLINE int lib_nummapthings(lua_State *L) int LUA_MobjLib(lua_State *L) { + mobj_fields_ref = Lua_CreateFieldTable(L, mobj_opt); + luaL_newmetatable(L, META_MOBJ); lua_pushcfunction(L, mobj_get); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, mobj_set); lua_setfield(L, -2, "__newindex"); - - udatalib_addfields(L, -1, mobj_fields); lua_pop(L,1); luaL_newmetatable(L, META_MAPTHING); diff --git a/src/lua_playerlib.c b/src/lua_playerlib.c index aa5fde833..ac4a4d3ff 100644 --- a/src/lua_playerlib.c +++ b/src/lua_playerlib.c @@ -26,365 +26,127 @@ #include "lua_hud.h" // hud_running errors #include "lua_hook.h" // hook_cmd_running -#include "lua_udatalib.h" +#define PLAYER_FIELDS(X) \ + X(valid) \ + X(name) \ + X(mo) \ + X(cmd) \ + X(playerstate) \ + X(viewz) \ + X(viewheight) \ + X(aiming) \ + X(health) \ + X(pity) \ + X(currentweapon) \ + X(ringweapons) \ + X(powers) \ + X(kartstuff) \ + X(frameangle) \ + X(pflags) \ + X(panim) \ + X(flashcount) \ + X(flashpal) \ + X(skincolor) \ + X(localskin) \ + X(score) \ + X(dashspeed) \ + X(dashtime) \ + X(kartspeed) \ + X(kartweight) \ + X(charflags) \ + X(lives) \ + X(continues) \ + X(xtralife) \ + X(gotcontinue) \ + X(speed) \ + X(jumping) \ + X(secondjump) \ + X(fly1) \ + X(scoreadd) \ + X(glidetime) \ + X(climbing) \ + X(deadtimer) \ + X(exiting) \ + X(homing) \ + X(skidtime) \ + X(cmomx) \ + X(cmomy) \ + X(rmomx) \ + X(rmomy) \ + X(numboxes) \ + X(totalring) \ + X(realtime) \ + X(laps) \ + X(ctfteam) \ + X(gotflag) \ + X(weapondelay) \ + X(tossdelay) \ + X(starpostx) \ + X(starposty) \ + X(starpostz) \ + X(starpostnum) \ + X(starposttime) \ + X(starpostangle) \ + X(angle_pos) \ + X(old_angle_pos) \ + X(axis1) \ + X(axis2) \ + X(bumpertime) \ + X(flyangle) \ + X(drilltimer) \ + X(linkcount) \ + X(linktimer) \ + X(anotherflyangle) \ + X(nightstime) \ + X(drillmeter) \ + X(drilldelay) \ + X(bonustime) \ + X(capsule) \ + X(mare) \ + X(marebegunat) \ + X(startedtime) \ + X(finishedtime) \ + X(finishedrings) \ + X(marescore) \ + X(lastmarescore) \ + X(lastmare) \ + X(maxlink) \ + X(texttimer) \ + X(textvar) \ + X(lastsidehit) \ + X(lastlinehit) \ + X(losstime) \ + X(timeshit) \ + X(onconveyor) \ + X(awayviewmobj) \ + X(awayviewtics) \ + X(awayviewaiming) \ + X(spectator) \ + X(bot) \ + X(jointime) \ + X(spectatorreentry) \ + X(grieftime) \ + X(griefstrikes) \ + X(splitscreenindex) \ + X(fovadd) \ + X(sliproll) \ + X(viewrollangle) \ + X(ping) -int player_name_getter(lua_State *L); -int player_name_noset(lua_State *L); -int player_mo_getter(lua_State *L); -int player_mo_setter(lua_State *L); -int player_cmd_noset(lua_State *L); -int player_aiming_setter(lua_State *L); -int player_powers_noset(lua_State *L); -int player_kartstuff_noset(lua_State *L); -int player_skincolor_setter(lua_State *L); -int player_axis_setter(lua_State *L); -int player_capsule_setter(lua_State *L); -int player_awayviewmobj_setter(lua_State *L); -int player_awayviewtics_setter(lua_State *L); -int player_bot_noset(lua_State *L); -int player_splitscreenindex_noset(lua_State *L); -int player_ping_getter(lua_State *L); -int player_ping_noset(lua_State *L); -int player_localskin_getter(lua_State *L); -int player_localskin_setter(lua_State *L); - -// Non synch safe! -int player_sliproll_getter(lua_State *L); -int player_sliproll_noset(lua_State *L); - -int player_viewrollangle_getter(lua_State *L); -int player_viewrollangle_noset(lua_State *L); - -#define FIELD(type, field_name, getter, setter) { #field_name, offsetof(type, field_name), getter, setter } -static const udata_field_t player_fields[] = { - // Player doesn't actually have field "name" so macro fails. Need to declare field manually. - { "name", 0, player_name_getter, player_name_noset }, - FIELD(player_t, mo, player_mo_getter, player_mo_setter), - FIELD(player_t, cmd, udatalib_getter_cmd, player_cmd_noset), - FIELD(player_t, playerstate, udatalib_getter_playerstate, udatalib_setter_playerstate), - FIELD(player_t, viewz, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, viewheight, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, aiming, udatalib_getter_angle, player_aiming_setter), - FIELD(player_t, health, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, pity, udatalib_getter_sint8, udatalib_setter_sint8), - FIELD(player_t, currentweapon, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, ringweapons, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, powers, udatalib_getter_powers, player_powers_noset), - FIELD(player_t, kartstuff, udatalib_getter_kartstuff, player_kartstuff_noset), - FIELD(player_t, frameangle, udatalib_getter_angle, udatalib_setter_angle), - FIELD(player_t, pflags, udatalib_getter_pflags, udatalib_setter_pflags), - FIELD(player_t, panim, udatalib_getter_panim, udatalib_setter_panim), - FIELD(player_t, flashcount, udatalib_getter_uint16, udatalib_setter_uint16), - FIELD(player_t, flashpal, udatalib_getter_uint16, udatalib_setter_uint16), - FIELD(player_t, skincolor, udatalib_getter_uint8, player_skincolor_setter), - FIELD(player_t, localskin, player_localskin_getter, player_localskin_setter), - FIELD(player_t, score, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(player_t, dashspeed, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, dashtime, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, kartspeed, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, kartweight, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, charflags, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(player_t, lives, udatalib_getter_sint8, udatalib_setter_sint8), - FIELD(player_t, continues, udatalib_getter_sint8, udatalib_setter_sint8), - FIELD(player_t, xtralife, udatalib_getter_sint8, udatalib_setter_sint8), - FIELD(player_t, gotcontinue, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, speed, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, jumping, udatalib_getter_boolean, udatalib_setter_boolean), - FIELD(player_t, secondjump, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, fly1, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, scoreadd, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, glidetime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, climbing, udatalib_getter_uint8, udatalib_setter_sint8), // For whatever reason, original setter casted to INT32... - FIELD(player_t, deadtimer, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, exiting, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, homing, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, skidtime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, cmomx, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, cmomy, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, rmomx, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, rmomy, udatalib_getter_fixed, udatalib_setter_fixed), - FIELD(player_t, numboxes, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, totalring, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, realtime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, laps, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, ctfteam, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, gotflag, udatalib_getter_uint16, udatalib_setter_uint16), - FIELD(player_t, weapondelay, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, tossdelay, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, starpostx, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, starposty, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, starpostz, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, starpostnum, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, starposttime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, starpostangle, udatalib_getter_angle, udatalib_setter_angle), - FIELD(player_t, angle_pos, udatalib_getter_angle, udatalib_setter_angle), - FIELD(player_t, old_angle_pos, udatalib_getter_angle, udatalib_setter_angle), - FIELD(player_t, axis1, udatalib_getter_mobj, player_axis_setter), - FIELD(player_t, axis2, udatalib_getter_mobj, player_axis_setter), - FIELD(player_t, bumpertime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, flyangle, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, drilltimer, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, linkcount, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, linktimer, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, anotherflyangle, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, nightstime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, drillmeter, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, drilldelay, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, bonustime, udatalib_getter_boolean, udatalib_setter_boolean), - FIELD(player_t, capsule, udatalib_getter_mobj, player_capsule_setter), - FIELD(player_t, mare, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, marebegunat, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, startedtime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, finishedtime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, finishedrings, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, marescore, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(player_t, lastmarescore, udatalib_getter_uint32, udatalib_setter_uint32), - FIELD(player_t, lastmare, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, maxlink, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, texttimer, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, textvar, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, lastsidehit, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, lastlinehit, udatalib_getter_int16, udatalib_setter_int16), - FIELD(player_t, losstime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, timeshit, udatalib_getter_uint8, udatalib_setter_uint8), // Haha, time shit, funni - FIELD(player_t, onconveyor, udatalib_getter_int32, udatalib_setter_int32), - FIELD(player_t, awayviewmobj, udatalib_getter_mobj, player_awayviewmobj_setter), - FIELD(player_t, awayviewtics, udatalib_getter_int32, player_awayviewtics_setter), - FIELD(player_t, awayviewaiming, udatalib_getter_angle, udatalib_setter_angle), - FIELD(player_t, spectator, udatalib_getter_boolean, udatalib_setter_boolean_nocheck), // ffs - FIELD(player_t, bot, udatalib_getter_uint8, player_bot_noset), - FIELD(player_t, jointime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, spectatorreentry, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, grieftime, udatalib_getter_tic, udatalib_setter_tic), - FIELD(player_t, griefstrikes, udatalib_getter_uint8, udatalib_setter_uint8), - FIELD(player_t, splitscreenindex, udatalib_getter_uint8, player_splitscreenindex_noset), - FIELD(player_t, fovadd, udatalib_getter_fixed, udatalib_setter_fixed), // Mmm yeah thats definitely synch safe - - // Same as player.name - { "sliproll", 0, player_sliproll_getter, player_sliproll_noset }, - { "viewrollangle", 0, player_viewrollangle_getter, player_viewrollangle_noset }, - { "ping", 0, player_ping_getter, player_ping_noset }, // Hmm originally setter doesn't exist so data is written as unreachable custom field... - { NULL, 0, NULL, NULL }, +enum player_e { +#define ENUMITEM(name) player_##name, + PLAYER_FIELDS(ENUMITEM) + NUMPLAYERFIELDS, +#undef ENUMITEM }; -#undef FIELD -// Implement udatalib simple getters -#define pushplayer(L, player) LUA_PushUserdata(L, player, META_PLAYER) -int udatalib_getter_player(lua_State *L) -UDATALIB_SIMPLE_GETTER(player_t*, pushplayer) -#undef pushplayer +static const char *player_opt[] = { +#define STRITEM(name) (#name), + PLAYER_FIELDS(STRITEM) + NULL, +}; -#define pushcmd(L, cmd) LUA_PushUserdata(L, &cmd, META_TICCMD) -int udatalib_getter_cmd(lua_State *L) -UDATALIB_SIMPLE_GETTER(ticcmd_t, pushcmd) -#undef pushcmd - -int udatalib_getter_playerstate(lua_State *L) -UDATALIB_SIMPLE_GETTER(playerstate_t, lua_pushinteger) - -int udatalib_setter_playerstate(lua_State *L) -UDATALIB_SIMPLE_SETTER(playerstate_t, luaL_checkinteger) - -#define pushpowers(L, powers) LUA_PushUserdata(L, &powers, META_POWERS) -int udatalib_getter_powers(lua_State *L) -UDATALIB_SIMPLE_GETTER(UINT16, pushpowers) -#undef pushpowers - -#define pushkartstuff(L, kartstuff) LUA_PushUserdata(L, &kartstuff, META_KARTSTUFF) -int udatalib_getter_kartstuff(lua_State *L) -UDATALIB_SIMPLE_GETTER(INT32, pushkartstuff) -#undef pushkartstuff - -int udatalib_getter_pflags(lua_State *L) -UDATALIB_SIMPLE_GETTER(pflags_t, lua_pushinteger) - -int udatalib_setter_pflags(lua_State *L) -UDATALIB_SIMPLE_SETTER(pflags_t, luaL_checkinteger) - -int udatalib_getter_panim(lua_State *L) -UDATALIB_SIMPLE_GETTER(panim_t, lua_pushinteger) - -int udatalib_setter_panim(lua_State *L) -UDATALIB_SIMPLE_SETTER(panim_t, luaL_checkinteger) -// Fields that cannot be set directly -#define NOSET(field) \ -int player_ ## field ## _noset(lua_State *L) \ -{ \ - return luaL_error(L, LUA_QL("player_t") " field " LUA_QS " should not be set directly.", #field); \ -} - -NOSET(name) -NOSET(cmd) -NOSET(powers) -NOSET(kartstuff) -NOSET(bot) -NOSET(splitscreenindex) -NOSET(ping) -NOSET(sliproll) -NOSET(viewrollangle) - -#undef NOSET - -// Rest of getters/setters with arbitary logic - - -#define GETPLAYER() (player_t*)lua_touserdata(L, 1) - -int player_name_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - lua_pushstring(L, player_names[plr-players]); - - return 1; -} - -int player_mo_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - if (plr->spectator) - lua_pushnil(L); - else - LUA_PushUserdata(L, plr->mo, META_MOBJ); - - return 1; -} - -int player_mo_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - plr->mo->player = NULL; // remove player pointer from old mobj - (newmo->player = plr)->mo = newmo; // set player pointer for new mobj, and set new mobj as the player's mobj - - return 0; -} - -int player_aiming_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - plr->aiming = luaL_checkangle(L, 2); - for (UINT8 i = 0; i <= splitscreen; i++) - { - if (plr == P_GetLocalPlayerForNum(i)) - { - localaiming[i] = plr->aiming; - break; - } - } - - return 0; -} - -int player_skincolor_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - UINT8 newcolor = (UINT8)luaL_checkinteger(L,2); - if (newcolor >= MAXSKINCOLORS) - return luaL_error(L, "player.skincolor %d out of range (0 - %d).", newcolor, MAXSKINCOLORS-1); - plr->skincolor = newcolor; - - return 0; -} - -int player_localskin_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - if (plr->localskin) - lua_pushstring(L, K_GetPlayerSkin(plr)->name); - else - lua_pushnil(L); - - return 1; -} - -int player_localskin_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - SetLocalPlayerSkin(plr - players, luaL_optstring(L, 2, "none"), NULL); - - return 0; -} - -int player_axis_setter(lua_State *L) -{ - mobj_t **axis; - - UDATALIB_GETFIELD(mobj_t*, axis); - - P_SetTarget(axis, *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ))); - - return 0; -} - -int player_capsule_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - mobj_t *mo = NULL; - if (!lua_isnil(L, 2)) - mo = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&plr->capsule, mo); - - return 0; -} - -// Probably can do same thing as with axis1 and axis2 -int player_awayviewmobj_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - mobj_t *mo = NULL; - if (!lua_isnil(L, 2)) - mo = *((mobj_t **)luaL_checkudata(L, 2, META_MOBJ)); - P_SetTarget(&plr->awayviewmobj, mo); - - return 0; -} - -int player_awayviewtics_setter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - plr->awayviewtics = (INT32)luaL_checkinteger(L, 2); - if (plr->awayviewtics && !plr->awayviewmobj) // awayviewtics must ALWAYS have an awayviewmobj set!! - P_SetTarget(&plr->awayviewmobj, plr->mo); // but since the script might set awayviewmobj immediately AFTER setting awayviewtics, use player mobj as filler for now. - - return 0; -} - -int player_ping_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - lua_pushinteger(L, playerpingtable[( plr - players )]); - - return 1; -} - -int player_sliproll_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - lua_pushangle(L, R_PlayerSliptideAngle(plr)); - - return 1; -} - -int player_viewrollangle_getter(lua_State *L) -{ - player_t *plr = GETPLAYER(); - - lua_pushangle(L, R_ViewRollAngle(plr)); - - return 1; -} +static int player_fields_ref = LUA_NOREF; static int lib_iteratePlayers(lua_State *L) { @@ -514,57 +276,380 @@ FUNCINLINE static ATTRINLINE int lib_lenDisplayplayers(lua_State *L) static int player_get(lua_State *L) { player_t *plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); - const char *field = luaL_checkstring(L, 2); // Still gotta have 2 strcmp's + enum player_e field = Lua_optoption(L, 2, -1, player_fields_ref); if (!plr) { - if (fastcmp(field,"valid")) { + if (field == player_valid) { lua_pushboolean(L, false); return 1; } return LUA_ErrInvalid(L, "player_t"); - } else if (fastcmp(field,"valid")) { + } else if (field == player_valid) { lua_pushboolean(L, true); return 1; } - lua_getmetatable(L, 1); - - lua_pushvalue(L, 2); // Push field name - lua_rawget(L, -2); // Get getter/setter table from metatable - - // If field exists, run getter for it - if (!lua_isnil(L, -1)) { - //CONS_Printf("Running getter for field %s\n", field); - lua_rawgeti(L, -1, UDATALIB_GETTER); - lua_pushlightuserdata(L, plr); - lua_call(L, 1, 1); - //CONS_Printf("Getter returned %s\n", lua_typename(L, lua_type(L, -1))); - return 1; - } - - lua_pop(L, 1); - - lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); - I_Assert(lua_istable(L, -1)); - lua_pushlightuserdata(L, plr); - lua_rawget(L, -2); - if (!lua_istable(L, -1)) { // no extra values table - CONS_Debug(DBG_LUA, M_GetText("'%s' has no extvars table or field named '%s'; returning nil.\n"), "player_t", field); - return 0; - } - lua_getfield(L, -1, field); - if (lua_isnil(L, -1)) // no value for this field + switch (field) { - CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; returning nil.\n"), "player_t", field); + case player_valid: + lua_pushboolean(L, true); + break; + case player_name: + lua_pushstring(L, player_names[plr-players]); + break; + case player_mo: + { + if (plr->spectator) + lua_pushnil(L); + else + LUA_PushUserdata(L, plr->mo, META_MOBJ); + } + break; + case player_cmd: + LUA_PushUserdata(L, &plr->cmd, META_TICCMD); + break; + case player_playerstate: + lua_pushinteger(L, plr->playerstate); + break; + case player_viewz: + lua_pushfixed(L, plr->viewz); + break; + case player_viewheight: + lua_pushfixed(L, plr->viewheight); + /* break; + case player_deltaviewheight: + lua_pushfixed(L, plr->deltaviewheight); + break; + case player_bob: + lua_pushfixed(L, plr->bob);*/ + break; + case player_aiming: + lua_pushangle(L, plr->aiming); + break; + case player_health: + lua_pushinteger(L, plr->health); + break; + case player_pity: + lua_pushinteger(L, plr->pity); + break; + case player_currentweapon: + lua_pushinteger(L, plr->currentweapon); + break; + case player_ringweapons: + lua_pushinteger(L, plr->ringweapons); + break; + case player_powers: + LUA_PushUserdata(L, plr->powers, META_POWERS); + break; + case player_kartstuff: + LUA_PushUserdata(L, plr->kartstuff, META_KARTSTUFF); + break; + case player_frameangle: + lua_pushangle(L, plr->frameangle); + break; + case player_pflags: + lua_pushinteger(L, plr->pflags); + break; + case player_panim: + lua_pushinteger(L, plr->panim); + break; + case player_flashcount: + lua_pushinteger(L, plr->flashcount); + break; + case player_flashpal: + lua_pushinteger(L, plr->flashpal); + break; + case player_skincolor: + lua_pushinteger(L, plr->skincolor); + break; + case player_score: + lua_pushinteger(L, plr->score); + break; + case player_dashspeed: + lua_pushfixed(L, plr->dashspeed); + break; + case player_dashtime: + lua_pushinteger(L, plr->dashtime); + // SRB2kart + break; + case player_kartspeed: + lua_pushinteger(L, plr->kartspeed); + break; + case player_kartweight: + lua_pushinteger(L, plr->kartweight); + // + break; + case player_charflags: + lua_pushinteger(L, plr->charflags); + break; + case player_lives: + lua_pushinteger(L, plr->lives); + break; + case player_continues: + lua_pushinteger(L, plr->continues); + break; + case player_xtralife: + lua_pushinteger(L, plr->xtralife); + break; + case player_gotcontinue: + lua_pushinteger(L, plr->gotcontinue); + break; + case player_speed: + lua_pushfixed(L, plr->speed); + break; + case player_jumping: + lua_pushboolean(L, plr->jumping); + break; + case player_secondjump: + lua_pushinteger(L, plr->secondjump); + break; + case player_fly1: + lua_pushinteger(L, plr->fly1); + break; + case player_scoreadd: + lua_pushinteger(L, plr->scoreadd); + break; + case player_glidetime: + lua_pushinteger(L, plr->glidetime); + break; + case player_climbing: + lua_pushinteger(L, plr->climbing); + break; + case player_deadtimer: + lua_pushinteger(L, plr->deadtimer); + break; + case player_exiting: + lua_pushinteger(L, plr->exiting); + break; + case player_homing: + lua_pushinteger(L, plr->homing); + break; + case player_skidtime: + lua_pushinteger(L, plr->skidtime); + break; + case player_cmomx: + lua_pushfixed(L, plr->cmomx); + break; + case player_cmomy: + lua_pushfixed(L, plr->cmomy); + break; + case player_rmomx: + lua_pushfixed(L, plr->rmomx); + break; + case player_rmomy: + lua_pushfixed(L, plr->rmomy); + break; + case player_numboxes: + lua_pushinteger(L, plr->numboxes); + break; + case player_totalring: + lua_pushinteger(L, plr->totalring); + break; + case player_realtime: + lua_pushinteger(L, plr->realtime); + break; + case player_laps: + lua_pushinteger(L, plr->laps); + break; + case player_ctfteam: + lua_pushinteger(L, plr->ctfteam); + break; + case player_gotflag: + lua_pushinteger(L, plr->gotflag); + break; + case player_weapondelay: + lua_pushinteger(L, plr->weapondelay); + break; + case player_tossdelay: + lua_pushinteger(L, plr->tossdelay); + break; + case player_starpostx: + lua_pushinteger(L, plr->starpostx); + break; + case player_starposty: + lua_pushinteger(L, plr->starposty); + break; + case player_starpostz: + lua_pushinteger(L, plr->starpostz); + break; + case player_starpostnum: + lua_pushinteger(L, plr->starpostnum); + break; + case player_starposttime: + lua_pushinteger(L, plr->starposttime); + break; + case player_starpostangle: + lua_pushangle(L, plr->starpostangle); + break; + case player_angle_pos: + lua_pushangle(L, plr->angle_pos); + break; + case player_old_angle_pos: + lua_pushangle(L, plr->old_angle_pos); + break; + case player_axis1: + LUA_PushUserdata(L, plr->axis1, META_MOBJ); + break; + case player_axis2: + LUA_PushUserdata(L, plr->axis2, META_MOBJ); + break; + case player_bumpertime: + lua_pushinteger(L, plr->bumpertime); + break; + case player_flyangle: + lua_pushinteger(L, plr->flyangle); + break; + case player_drilltimer: + lua_pushinteger(L, plr->drilltimer); + break; + case player_linkcount: + lua_pushinteger(L, plr->linkcount); + break; + case player_linktimer: + lua_pushinteger(L, plr->linktimer); + break; + case player_anotherflyangle: + lua_pushinteger(L, plr->anotherflyangle); + break; + case player_nightstime: + lua_pushinteger(L, plr->nightstime); + break; + case player_drillmeter: + lua_pushinteger(L, plr->drillmeter); + break; + case player_drilldelay: + lua_pushinteger(L, plr->drilldelay); + break; + case player_bonustime: + lua_pushboolean(L, plr->bonustime); + break; + case player_capsule: + LUA_PushUserdata(L, plr->capsule, META_MOBJ); + break; + case player_mare: + lua_pushinteger(L, plr->mare); + break; + case player_marebegunat: + lua_pushinteger(L, plr->marebegunat); + break; + case player_startedtime: + lua_pushinteger(L, plr->startedtime); + break; + case player_finishedtime: + lua_pushinteger(L, plr->finishedtime); + break; + case player_finishedrings: + lua_pushinteger(L, plr->finishedrings); + break; + case player_marescore: + lua_pushinteger(L, plr->marescore); + break; + case player_lastmarescore: + lua_pushinteger(L, plr->lastmarescore); + break; + case player_lastmare: + lua_pushinteger(L, plr->lastmare); + break; + case player_maxlink: + lua_pushinteger(L, plr->maxlink); + break; + case player_texttimer: + lua_pushinteger(L, plr->texttimer); + break; + case player_textvar: + lua_pushinteger(L, plr->textvar); + break; + case player_lastsidehit: + lua_pushinteger(L, plr->lastsidehit); + break; + case player_lastlinehit: + lua_pushinteger(L, plr->lastlinehit); + break; + case player_losstime: + lua_pushinteger(L, plr->losstime); + break; + case player_timeshit: + lua_pushinteger(L, plr->timeshit); + break; + case player_onconveyor: + lua_pushinteger(L, plr->onconveyor); + break; + case player_awayviewmobj: + LUA_PushUserdata(L, plr->awayviewmobj, META_MOBJ); + break; + case player_awayviewtics: + lua_pushinteger(L, plr->awayviewtics); + break; + case player_awayviewaiming: + lua_pushangle(L, plr->awayviewaiming); + break; + case player_spectator: + lua_pushboolean(L, plr->spectator); + break; + case player_bot: + lua_pushinteger(L, plr->bot); + break; + case player_jointime: + lua_pushinteger(L, plr->jointime); + break; + case player_spectatorreentry: + lua_pushinteger(L, plr->spectatorreentry); + break; + case player_grieftime: + lua_pushinteger(L, plr->grieftime); + break; + case player_griefstrikes: + lua_pushinteger(L, plr->griefstrikes); + break; + case player_splitscreenindex: + lua_pushinteger(L, plr->splitscreenindex); + break; +#ifdef HWRENDER + case player_fovadd: + lua_pushfixed(L, plr->fovadd); + break; +#endif + case player_ping: + lua_pushinteger(L, playerpingtable[( plr - players )]); + break; + // Saturn + case player_localskin: + if (plr->localskin) + lua_pushstring(L, K_GetPlayerSkin(plr)->name); + else + lua_pushnil(L); + break; + case player_sliproll: + lua_pushangle(L, R_PlayerSliptideAngle(plr)); + break; + case player_viewrollangle: + lua_pushangle(L, R_ViewRollAngle(plr)); + break; + default: + lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); + I_Assert(lua_istable(L, -1)); + lua_pushlightuserdata(L, plr); + lua_rawget(L, -2); + if (!lua_istable(L, -1)) { // no extra values table + CONS_Debug(DBG_LUA, M_GetText("'%s' has no extvars table or field named '%s'; returning nil.\n"), "player_t", lua_tostring(L, 2)); + return 0; + } + lua_pushvalue(L, 2); // field name + lua_gettable(L, -2); + if (lua_isnil(L, -1)) // no value for this field + CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; returning nil.\n"), "player_t", lua_tostring(L, 2)); + break; } return 1; } +#define NOSET luaL_error(L, LUA_QL("player_t") " field " LUA_QS " should not be set directly.", lua_tostring(L, 2)) static int player_set(lua_State *L) { player_t *plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); - const char *field = luaL_checkstring(L, 2); + enum player_e field = Lua_optoption(L, 2, player_cmd, player_fields_ref); + if (!plr) return LUA_ErrInvalid(L, "player_t"); @@ -574,39 +659,379 @@ static int player_set(lua_State *L) if (hook_cmd_running) return luaL_error(L, "Do not alter player_t in BuildCMD code!"); - lua_getmetatable(L, 1); // Push metatable - - lua_pushvalue(L, 2); // Push field name - lua_rawget(L, -2); // Get getter/setter table from metatable - - // If field exists, run setter for it - if (!lua_isnil(L, -1)) { - //CONS_Printf("Running setter for field %s\n", luaL_checkstring(L, 2)); - lua_rawgeti(L, -1, UDATALIB_SETTER); - lua_pushlightuserdata(L, plr); - lua_pushvalue(L, 3); - lua_call(L, 2, 0); - return 0; - } - - lua_pop(L, 1); - - lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); - I_Assert(lua_istable(L, -1)); - lua_pushlightuserdata(L, plr); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) { - // This index doesn't have a table for extra values yet, let's make one. - lua_pop(L, 1); - CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; adding it as Lua data.\n"), "player_t", field); - lua_newtable(L); - lua_pushlightuserdata(L, plr); - lua_pushvalue(L, -2); // ext value table - lua_rawset(L, -4); // LREG_EXTVARS table - } - lua_pushvalue(L, 3); // value to store - lua_setfield(L, -2, field); - lua_pop(L, 2); + switch (field) + { + case player_name: + case player_valid: + case player_ping: + //return NOSET; + return 0; // In vanilla those are not handled and are added into player fields table, but they aren't accessible anyway so we can just ignore them + break; + case player_mo: + { + mobj_t *newmo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + plr->mo->player = NULL; // remove player pointer from old mobj + (newmo->player = plr)->mo = newmo; // set player pointer for new mobj, and set new mobj as the player's mobj + } + break; + case player_cmd: + return NOSET; + break; + case player_playerstate: + plr->playerstate = luaL_checkinteger(L, 3); + break; + case player_viewz: + plr->viewz = luaL_checkfixed(L, 3); + break; + case player_viewheight: + plr->viewheight = luaL_checkfixed(L, 3); + /* break; + case player_deltaviewheight: + plr->deltaviewheight = luaL_checkfixed(L, 3); + break; + case player_bob: + plr->bob = luaL_checkfixed(L, 3);*/ + break; + case player_aiming: + { + plr->aiming = luaL_checkangle(L, 3); + for (UINT8 i = 0; i <= splitscreen; i++) + { + if (plr == &players[displayplayers[i]]) + { + localaiming[i] = plr->aiming; + } + } + } + break; + case player_health: + plr->health = (INT32)luaL_checkinteger(L, 3); + break; + case player_pity: + plr->pity = (SINT8)luaL_checkinteger(L, 3); + break; + case player_currentweapon: + plr->currentweapon = (INT32)luaL_checkinteger(L, 3); + break; + case player_ringweapons: + plr->ringweapons = (INT32)luaL_checkinteger(L, 3); + break; + case player_powers: + return NOSET; + break; + case player_pflags: + plr->pflags = luaL_checkinteger(L, 3); + break; + case player_panim: + plr->panim = luaL_checkinteger(L, 3); + break; + case player_flashcount: + plr->flashcount = (UINT16)luaL_checkinteger(L, 3); + break; + case player_flashpal: + plr->flashpal = (UINT16)luaL_checkinteger(L, 3); + break; + case player_skincolor: + { + UINT8 newcolor = (UINT8)luaL_checkinteger(L,3); + if (newcolor >= MAXSKINCOLORS) + return luaL_error(L, "player.skincolor %d out of range (0 - %d).", newcolor, MAXSKINCOLORS-1); + plr->skincolor = newcolor; + } + break; + case player_score: + plr->score = (UINT32)luaL_checkinteger(L, 3); + break; + case player_dashspeed: + plr->dashspeed = luaL_checkfixed(L, 3); + break; + case player_dashtime: + plr->dashtime = (INT32)luaL_checkinteger(L, 3); + // SRB2kart + break; + case player_kartstuff: + return NOSET; + break; + case player_frameangle: + plr->frameangle = luaL_checkangle(L, 3); + break; + case player_kartspeed: + plr->kartspeed = (UINT8)luaL_checkinteger(L, 3); + break; + case player_kartweight: + plr->kartweight = (UINT8)luaL_checkinteger(L, 3); + // + break; + case player_charflags: + plr->charflags = (UINT32)luaL_checkinteger(L, 3); + break; + case player_lives: + plr->lives = (SINT8)luaL_checkinteger(L, 3); + break; + case player_continues: + plr->continues = (SINT8)luaL_checkinteger(L, 3); + break; + case player_xtralife: + plr->xtralife = (SINT8)luaL_checkinteger(L, 3); + break; + case player_gotcontinue: + plr->gotcontinue = (UINT8)luaL_checkinteger(L, 3); + break; + case player_speed: + plr->speed = luaL_checkfixed(L, 3); + break; + case player_jumping: + plr->jumping = luaL_checkboolean(L, 3); + break; + case player_secondjump: + plr->secondjump = (UINT8)luaL_checkinteger(L, 3); + break; + case player_fly1: + plr->fly1 = (UINT8)luaL_checkinteger(L, 3); + break; + case player_scoreadd: + plr->scoreadd = (UINT8)luaL_checkinteger(L, 3); + break; + case player_glidetime: + plr->glidetime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_climbing: + plr->climbing = (INT32)luaL_checkinteger(L, 3); + break; + case player_deadtimer: + plr->deadtimer = (INT32)luaL_checkinteger(L, 3); + break; + case player_exiting: + plr->exiting = (tic_t)luaL_checkinteger(L, 3); + break; + case player_homing: + plr->homing = (UINT8)luaL_checkinteger(L, 3); + break; + case player_skidtime: + plr->skidtime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_cmomx: + plr->cmomx = luaL_checkfixed(L, 3); + break; + case player_cmomy: + plr->cmomy = luaL_checkfixed(L, 3); + break; + case player_rmomx: + plr->rmomx = luaL_checkfixed(L, 3); + break; + case player_rmomy: + plr->rmomy = luaL_checkfixed(L, 3); + break; + case player_numboxes: + plr->numboxes = (INT16)luaL_checkinteger(L, 3); + break; + case player_totalring: + plr->totalring = (INT16)luaL_checkinteger(L, 3); + break; + case player_realtime: + plr->realtime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_laps: + plr->laps = (UINT8)luaL_checkinteger(L, 3); + break; + case player_ctfteam: + plr->ctfteam = (INT32)luaL_checkinteger(L, 3); + break; + case player_gotflag: + plr->gotflag = (UINT16)luaL_checkinteger(L, 3); + break; + case player_weapondelay: + plr->weapondelay = (INT32)luaL_checkinteger(L, 3); + break; + case player_tossdelay: + plr->tossdelay = (INT32)luaL_checkinteger(L, 3); + break; + case player_starpostx: + plr->starpostx = (INT16)luaL_checkinteger(L, 3); + break; + case player_starposty: + plr->starposty = (INT16)luaL_checkinteger(L, 3); + break; + case player_starpostz: + plr->starpostz = (INT16)luaL_checkinteger(L, 3); + break; + case player_starpostnum: + plr->starpostnum = (INT32)luaL_checkinteger(L, 3); + break; + case player_starposttime: + plr->starposttime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_starpostangle: + plr->starpostangle = luaL_checkangle(L, 3); + break; + case player_angle_pos: + plr->angle_pos = luaL_checkangle(L, 3); + break; + case player_old_angle_pos: + plr->old_angle_pos = luaL_checkangle(L, 3); + break; + case player_axis1: + P_SetTarget(&plr->axis1, *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ))); + break; + case player_axis2: + P_SetTarget(&plr->axis2, *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ))); + break; + case player_bumpertime: + plr->bumpertime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_flyangle: + plr->flyangle = (INT32)luaL_checkinteger(L, 3); + break; + case player_drilltimer: + plr->drilltimer = (tic_t)luaL_checkinteger(L, 3); + break; + case player_linkcount: + plr->linkcount = (INT32)luaL_checkinteger(L, 3); + break; + case player_linktimer: + plr->linktimer = (tic_t)luaL_checkinteger(L, 3); + break; + case player_anotherflyangle: + plr->anotherflyangle = (INT32)luaL_checkinteger(L, 3); + break; + case player_nightstime: + plr->nightstime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_drillmeter: + plr->drillmeter = (INT32)luaL_checkinteger(L, 3); + break; + case player_drilldelay: + plr->drilldelay = (UINT8)luaL_checkinteger(L, 3); + break; + case player_bonustime: + plr->bonustime = luaL_checkboolean(L, 3); + break; + case player_capsule: + { + mobj_t *mo = NULL; + if (!lua_isnil(L, 3)) + mo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&plr->capsule, mo); + } + break; + case player_mare: + plr->mare = (UINT8)luaL_checkinteger(L, 3); + break; + case player_marebegunat: + plr->marebegunat = (tic_t)luaL_checkinteger(L, 3); + break; + case player_startedtime: + plr->startedtime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_finishedtime: + plr->finishedtime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_finishedrings: + plr->finishedrings = (INT16)luaL_checkinteger(L, 3); + break; + case player_marescore: + plr->marescore = (UINT32)luaL_checkinteger(L, 3); + break; + case player_lastmarescore: + plr->lastmarescore = (UINT32)luaL_checkinteger(L, 3); + break; + case player_lastmare: + plr->lastmare = (UINT8)luaL_checkinteger(L, 3); + break; + case player_maxlink: + plr->maxlink = (INT32)luaL_checkinteger(L, 3); + break; + case player_texttimer: + plr->texttimer = (UINT8)luaL_checkinteger(L, 3); + break; + case player_textvar: + plr->textvar = (UINT8)luaL_checkinteger(L, 3); + break; + case player_lastsidehit: + plr->lastsidehit = (INT16)luaL_checkinteger(L, 3); + break; + case player_lastlinehit: + plr->lastlinehit = (INT16)luaL_checkinteger(L, 3); + break; + case player_losstime: + plr->losstime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_timeshit: + plr->timeshit = (UINT8)luaL_checkinteger(L, 3); + break; + case player_onconveyor: + plr->onconveyor = (INT32)luaL_checkinteger(L, 3); + break; + case player_awayviewmobj: + { + mobj_t *mo = NULL; + if (!lua_isnil(L, 3)) + mo = *((mobj_t **)luaL_checkudata(L, 3, META_MOBJ)); + P_SetTarget(&plr->awayviewmobj, mo); + } + break; + case player_awayviewtics: + { + plr->awayviewtics = (INT32)luaL_checkinteger(L, 3); + if (plr->awayviewtics && !plr->awayviewmobj) // awayviewtics must ALWAYS have an awayviewmobj set!! + P_SetTarget(&plr->awayviewmobj, plr->mo); // but since the script might set awayviewmobj immediately AFTER setting awayviewtics, use player mobj as filler for now. + } + break; + case player_awayviewaiming: + plr->awayviewaiming = luaL_checkangle(L, 3); + break; + case player_spectator: + plr->spectator = lua_toboolean(L, 3); + break; + case player_bot: + return NOSET; + break; + case player_jointime: + plr->jointime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_spectatorreentry: + plr->spectatorreentry = (tic_t)luaL_checkinteger(L, 3); + break; + case player_grieftime: + plr->grieftime = (tic_t)luaL_checkinteger(L, 3); + break; + case player_griefstrikes: + plr->griefstrikes = (UINT8)luaL_checkinteger(L, 3); + break; + case player_splitscreenindex: + return NOSET; + break; +#ifdef HWRENDER + case player_fovadd: + plr->fovadd = luaL_checkfixed(L, 3); + break; +#endif + // Saturn + case player_localskin: + SetLocalPlayerSkin(plr - players, luaL_optstring(L, 2, "none"), NULL); + break; + case player_sliproll: + case player_viewrollangle: + return NOSET; + break; + default: + lua_getfield(L, LUA_REGISTRYINDEX, LREG_EXTVARS); + I_Assert(lua_istable(L, -1)); + lua_pushlightuserdata(L, plr); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) { + // This index doesn't have a table for extra values yet, let's make one. + lua_pop(L, 1); + CONS_Debug(DBG_LUA, M_GetText("'%s' has no field named '%s'; adding it as Lua data.\n"), "player_t", lua_tostring(L, 2)); + lua_newtable(L); + lua_pushlightuserdata(L, plr); + lua_pushvalue(L, -2); // ext value table + lua_rawset(L, -4); // LREG_EXTVARS table + } + lua_pushvalue(L, 3); // value to store + lua_setfield(L, -2, lua_tostring(L, 2)); + lua_pop(L, 2); + } return 0; } @@ -798,6 +1223,8 @@ static int ticcmd_set(lua_State *L) int LUA_PlayerLib(lua_State *L) { + player_fields_ref = Lua_CreateFieldTable(L, player_opt); + luaL_newmetatable(L, META_PLAYER); lua_pushcfunction(L, player_get); lua_setfield(L, -2, "__index"); @@ -807,8 +1234,6 @@ int LUA_PlayerLib(lua_State *L) lua_pushcfunction(L, player_num); lua_setfield(L, -2, "__len"); - - udatalib_addfields(L, -1, player_fields); lua_pop(L,1); luaL_newmetatable(L, META_POWERS); diff --git a/src/lua_udatalib.c b/src/lua_udatalib.c deleted file mode 100644 index 962fb0638..000000000 --- a/src/lua_udatalib.c +++ /dev/null @@ -1,137 +0,0 @@ -// SONIC ROBO BLAST 2 KART SATURN -//----------------------------------------------------------------------------- -// Copyright (C) 2025 by Indev. -// -// This program is free software distributed under the -// terms of the GNU General Public License, version 2. -// See the 'LICENSE' file for more details. -//----------------------------------------------------------------------------- -/// \file lua_udatalib.c -/// \brief Implementation of lua userdata object metatables based on getter/setter functions - -#include "doomtype.h" -#include "m_fixed.h" -#include "lua_udatalib.h" - -// Getters -int udatalib_getter_fixed(lua_State *L) -UDATALIB_SIMPLE_GETTER(fixed_t, lua_pushinteger) - -int udatalib_getter_angle(lua_State *L) -UDATALIB_SIMPLE_GETTER(angle_t, lua_pushangle) - -int udatalib_getter_uint8(lua_State *L) -UDATALIB_SIMPLE_GETTER(UINT8, lua_pushinteger) - -int udatalib_getter_sint8(lua_State *L) -UDATALIB_SIMPLE_GETTER(SINT8, lua_pushinteger) - -int udatalib_getter_uint16(lua_State *L) -UDATALIB_SIMPLE_GETTER(UINT16, lua_pushinteger) - -int udatalib_getter_int16(lua_State *L) -UDATALIB_SIMPLE_GETTER(INT16, lua_pushinteger) - -int udatalib_getter_uint32(lua_State *L) -UDATALIB_SIMPLE_GETTER(UINT32, lua_pushinteger) - -int udatalib_getter_int32(lua_State *L) -UDATALIB_SIMPLE_GETTER(INT32, lua_pushinteger) - -int udatalib_getter_uint64(lua_State *L) -UDATALIB_SIMPLE_GETTER(UINT64, lua_pushinteger) - -int udatalib_getter_int64(lua_State *L) -UDATALIB_SIMPLE_GETTER(INT64, lua_pushinteger) - -int udatalib_getter_string(lua_State *L) -UDATALIB_SIMPLE_GETTER(const char*, lua_pushstring) - -int udatalib_getter_boolean(lua_State *L) -UDATALIB_SIMPLE_GETTER(boolean, lua_pushboolean) - -int udatalib_getter_spritenum(lua_State *L) -UDATALIB_SIMPLE_GETTER(spritenum_t, lua_pushinteger) - -int udatalib_getter_tic(lua_State *L) -UDATALIB_SIMPLE_GETTER(tic_t, lua_pushinteger) - -// Setters -int udatalib_setter_fixed(lua_State *L) -UDATALIB_SIMPLE_SETTER(fixed_t, luaL_checkfixed) - -int udatalib_setter_angle(lua_State *L) -UDATALIB_SIMPLE_SETTER(angle_t, luaL_checkangle) - -int udatalib_setter_uint8(lua_State *L) -UDATALIB_SIMPLE_SETTER(UINT8, (UINT8)luaL_checkinteger) - -int udatalib_setter_sint8(lua_State *L) -UDATALIB_SIMPLE_SETTER(SINT8, luaL_checkinteger) - -int udatalib_setter_uint16(lua_State *L) -UDATALIB_SIMPLE_SETTER(UINT16, (UINT16)luaL_checkinteger) - -int udatalib_setter_int16(lua_State *L) -UDATALIB_SIMPLE_SETTER(INT16, luaL_checkinteger) - -int udatalib_setter_uint32(lua_State *L) -UDATALIB_SIMPLE_SETTER(UINT32, (UINT32)luaL_checkinteger) - -int udatalib_setter_int32(lua_State *L) -UDATALIB_SIMPLE_SETTER(INT32, luaL_checkinteger) - -int udatalib_setter_uint64(lua_State *L) -UDATALIB_SIMPLE_SETTER(UINT64, (UINT64)luaL_checkinteger) - -int udatalib_setter_int64(lua_State *L) -UDATALIB_SIMPLE_SETTER(INT64, luaL_checkinteger) - -int udatalib_setter_string(lua_State *L) -UDATALIB_SIMPLE_SETTER(const char*, luaL_checkstring) - -int udatalib_setter_boolean(lua_State *L) -UDATALIB_SIMPLE_SETTER(boolean, luaL_checkboolean) - -// ffs -int udatalib_setter_boolean_nocheck(lua_State *L) -UDATALIB_SIMPLE_SETTER(boolean, lua_toboolean) - -int udatalib_setter_spritenum(lua_State *L) -UDATALIB_SIMPLE_SETTER(spritenum_t, luaL_checkinteger) - -int udatalib_setter_tic(lua_State *L) -UDATALIB_SIMPLE_SETTER(tic_t, (tic_t)luaL_checkinteger) - - -void udatalib_addfield(lua_State *L, int mt, udata_field_t field) -{ - int idx = abs_index(L, mt); - - lua_createtable(L, 2, 0); // Push table - - lua_pushinteger(L, field.offset); // Push offset - lua_pushcclosure(L, field.getter, 1); // Pop offset, push closure - - lua_rawseti(L, -2, UDATALIB_GETTER); // Pop closure, add to table - - lua_pushinteger(L, field.offset); // Push offset - lua_pushcclosure(L, field.setter, 1); // Pop offset, push closure - - lua_rawseti(L, -2, UDATALIB_SETTER); // Pop closure, add to table - - lua_pushstring(L, field.name); // Push field name - lua_pushvalue(L, -2); // Copy table - lua_rawset(L, idx); // Pop field name and table, add to metatable - - lua_pop(L, 1); // Pop field table -} - -void udatalib_addfields(lua_State *L, int mt, const udata_field_t fields[]) -{ - for (unsigned i = 0; fields[i].name != NULL; ++i) - { - udatalib_addfield(L, mt, fields[i]); - //CONS_Printf("Add field name=%s, offset=%d, getter=%p, setter=%p\n", fields[i].name, fields[i].offset, fields[i].getter, fields[i].setter); - } -} diff --git a/src/lua_udatalib.h b/src/lua_udatalib.h deleted file mode 100644 index ad23bf898..000000000 --- a/src/lua_udatalib.h +++ /dev/null @@ -1,114 +0,0 @@ -// SONIC ROBO BLAST 2 KART SATURN -//----------------------------------------------------------------------------- -// Copyright (C) 2025 by Indev. -// -// This program is free software distributed under the -// terms of the GNU General Public License, version 2. -// See the 'LICENSE' file for more details. -//----------------------------------------------------------------------------- -/// \file lua_udatalib.h -/// \brief Implementation of lua userdata object metatables based on getter/setter functions - -#ifndef __LUA_UDATALIB_H__ -#define __LUA_UDATALIB_H__ - -#include "doomdef.h" -#include "lua_script.h" -#include "lua_libs.h" -#include "fastcmp.h" - -// Indices in getter/setter tables for fields -#define UDATALIB_GETTER 1 -#define UDATALIB_SETTER 2 - -typedef struct udata_field_s { - const char *name; - size_t offset; - lua_CFunction getter; - lua_CFunction setter; -} udata_field_t; - -// Simple setters and getters, for situations where getter is simply -// "return obj.field" and setter is "obj.field = value" -int udatalib_getter_fixed(lua_State *L); -int udatalib_getter_angle(lua_State *L); -int udatalib_getter_uint8(lua_State *L); -int udatalib_getter_sint8(lua_State *L); -int udatalib_getter_uint16(lua_State *L); -int udatalib_getter_int16(lua_State *L); -int udatalib_getter_uint32(lua_State *L); -int udatalib_getter_int32(lua_State *L); -int udatalib_getter_uint64(lua_State *L); -int udatalib_getter_int64(lua_State *L); -int udatalib_getter_string(lua_State *L); -int udatalib_getter_boolean(lua_State *L); -int udatalib_getter_spritenum(lua_State *L); -int udatalib_getter_mobj(lua_State *L); -int udatalib_getter_subsector(lua_State *L); -int udatalib_getter_mobjtype(lua_State *L); -int udatalib_getter_player(lua_State *L); -int udatalib_getter_mapthing(lua_State *L); -int udatalib_getter_slope(lua_State *L); -int udatalib_getter_cmd(lua_State *L); -int udatalib_getter_playerstate(lua_State *L); -int udatalib_getter_powers(lua_State *L); -int udatalib_getter_kartstuff(lua_State *L); -int udatalib_getter_pflags(lua_State *L); -int udatalib_getter_panim(lua_State *L); -int udatalib_getter_tic(lua_State *L); - -int udatalib_setter_fixed(lua_State *L); -int udatalib_setter_angle(lua_State *L); -int udatalib_setter_uint8(lua_State *L); -int udatalib_setter_sint8(lua_State *L); -int udatalib_setter_uint16(lua_State *L); -int udatalib_setter_int16(lua_State *L); -int udatalib_setter_uint32(lua_State *L); -int udatalib_setter_int32(lua_State *L); -int udatalib_setter_uint64(lua_State *L); -int udatalib_setter_int64(lua_State *L); -int udatalib_setter_string(lua_State *L); -int udatalib_setter_boolean(lua_State *L); -int udatalib_setter_boolean_nocheck(lua_State *L); -int udatalib_setter_spritenum(lua_State *L); -int udatalib_setter_mobj(lua_State *L); -int udatalib_setter_playerstate(lua_State *L); -int udatalib_setter_pflags(lua_State *L); -int udatalib_setter_panim(lua_State *L); -int udatalib_setter_tic(lua_State *L); - -// Adds setter and getter for field and stores it as field.name..".get" and -// field.name..".set" in table at index mt. (metatable, usually) -void udatalib_addfield(lua_State *L, int mt, udata_field_t field); - -// Convinience function to add array of fields at once. -void udatalib_addfields(lua_State *L, int mt, const udata_field_t fields[]); - -// I miss C++ template functons :( - -// Get pointer to field value within getter or setter -#define UDATALIB_GETFIELD(type, field) \ -{ \ - size_t offset = lua_tointeger(L, lua_upvalueindex(1)); \ - field = (type*)((uint8_t*)lua_touserdata(L, 1) + offset); \ -} - -// Creates implementation for simple getter and setter. Super lazy, yes. -// Assumes userdata is a pointer to actual data. Maybe not really safe. -#define UDATALIB_SIMPLE_GETTER(type, pushf) \ -{ \ - type *field; \ - UDATALIB_GETFIELD(type, field) \ - pushf(L, *field); \ - return 1; \ -} - -#define UDATALIB_SIMPLE_SETTER(type, checkf) \ -{ \ - type *field; \ - UDATALIB_GETFIELD(type, field) \ - *field = checkf(L, 2); \ - return 0; \ -} - -#endif diff --git a/src/m_argv.c b/src/m_argv.c index 6219b191d..01a2eef58 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -14,8 +14,7 @@ #include #ifdef HAVE_THREADS -#include -#define local_for_thread thread_local +#define local_for_thread _Thread_local #else #define local_for_thread #endif diff --git a/src/m_menu.c b/src/m_menu.c index 2c8a625ad..19c70a731 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -3989,6 +3989,10 @@ boolean replaynamesloaded = false; // If it's not equal, that means we started more than one thread (left and re-entered menu for example) size_t replaythreadnum = 0; +// for replay dates +#include +const char *demodateformat = "%d.%m.%Y"; + #ifdef HAVE_THREADS I_mutex replayquerymutex; @@ -4025,6 +4029,7 @@ static void ReplayNamesLoadThread(void* userdata) menudemo_t *demolist_all_local = (menudemo_t*)malloc(sizeof(menudemo_t)*demolist_all_size_local); memcpy(demolist_all_local, demolist_all, sizeof(menudemo_t)*demolist_all_size_local); size_t localreplaythreadnum = replaythreadnum; + const char *localdemodateformat = demodateformat; Unlock_search_state(); @@ -4032,13 +4037,7 @@ static void ReplayNamesLoadThread(void* userdata) { if (demolist_all_local[i].type != MD_SUBDIR) { - G_LoadDemoTitle(&demolist_all_local[i]); - - // this is fucking horrid, but i wanna be able to search for dates - /*if (demolist_all_local[i].date[0] != '\0') - { - snprintf(demolist_all_local[i].title, sizeof(demolist_all_local[i].title), "%s %s", demolist_all_local[i].title, demolist_all_local[i].date); - }*/ + G_LoadDemoTitle(&demolist_all_local[i], localdemodateformat); } } @@ -4061,6 +4060,18 @@ static void LoadReplayNames(void) Lock_search_state(); replaythreadnum++; replaynamesloaded = false; + if (cv_demodateformat.value == 2) + demodateformat = "%m.%d.%Y"; + else if (cv_demodateformat.value == 1) + demodateformat = "%d.%m.%Y"; + else + { + // get the date format here + // setlocale reads/writes global state + const char *locale = setlocale(LC_TIME, NULL); + // US ppl are special (: + demodateformat = (locale && strstr(locale, "en_US")) ? "%m.%d.%Y" : "%d.%m.%Y"; + } Unlock_search_state(); #ifdef HAVE_THREADS diff --git a/src/m_menudefs.c b/src/m_menudefs.c index 4a00f3020..bc52db949 100644 --- a/src/m_menudefs.c +++ b/src/m_menudefs.c @@ -1004,7 +1004,7 @@ static menuitem_t OP_ExpOptionsMenu[] = #ifdef HWRENDER {IT_STRING | IT_CVAR, NULL, "Screen Textures", &cv_glscreentextures, 95}, #ifdef USE_FBO_OGL - {IT_STRING | IT_CVAR, NULL, "FBO Downsampling support", &cv_glframebuffer, 100}, + {IT_STRING | IT_CVAR, NULL, "FBO support", &cv_glframebuffer, 100}, {IT_STRING | IT_CVAR, NULL, "Palette Depth", &cv_glpalettedepth, 110}, {IT_DISABLED, NULL, "", NULL, 120}, // dummy text #else @@ -1030,7 +1030,7 @@ static const char* OP_ExpTooltips[] = #ifdef HWRENDER "Disabling Screen Textures may result in a performance boost\nbut will break certain effects.\nScreen textures are required at resolutions lower than your desktop resolution!", #ifdef USE_FBO_OGL - "Allows the game to downsample from a higher resolution\nthan your display in OpenGL renderer mode.\nRequires a GPU with atleast OpenGL 2.1 support.", + "Allows the game to downsample from a higher resolution\nthan your display in OpenGL renderer mode.\nThis can be set to automatically enable if the game resolution\nis higher than the current Display Resolution\nor enabled at all times.\nThis may improve performance in some cases.\nRequires a GPU with atleast OpenGL 2.1 support.", #endif "Change the bit depth of the Lookup Palette in Palette rendering mode\n 16 bits is like software looks ingame\nwhile 24 bits is how software looks in screenshots.", #endif diff --git a/src/r_defs.h b/src/r_defs.h index d6c419c1b..1a3b88196 100644 --- a/src/r_defs.h +++ b/src/r_defs.h @@ -504,6 +504,9 @@ typedef struct subsector_s void *splats; // floorsplat_t list #endif size_t validcount; +#ifdef HWRENDER + boolean hashorizon; // not really needed for software, this stuff works differently there +#endif } subsector_t; // Sector list node showing all sectors an object appears in. diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index ae9b61446..b1276e7ad 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -97,11 +97,9 @@ static char vidModeName[33][32]; // allow 33 different modes rendermode_t rendermode = render_none; #ifdef HWRENDER -unsigned msaa = 0; - // Eee probably the way i organize it isn't best >< // Just don't know where do i put declaration and "implementation" -boolean a2c = false; +unsigned msaa = 0; #endif static void KeyboardLayout_OnChange(void) @@ -115,10 +113,6 @@ consvar_t cv_keyboardlayout = {"keyboardlayout", "Default US", CV_SAVE|CV_CALL, static void Impl_SetVsync(void); -static INT32 desktopwidth = 0, desktopheight = 0; - -static void I_CheckDesktopRes(void); - // synchronize page flipping with screen refresh consvar_t cv_vidwait = {"vid_wait", "Off", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, Impl_SetVsync, 0, NULL, NULL, 0, 0, NULL}; static consvar_t cv_stretch = {"stretch", "Off", CV_SAVE|CV_NOSHOWHELP, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; @@ -197,10 +191,6 @@ static INT32 custom_height = 0; static SDL_bool Impl_CreateWindow(SDL_bool fullscreen); static void Impl_SetWindowIcon(void); -#ifdef USE_FBO_OGL -boolean downsample = false; -#endif - static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code) { if (code >= SDL_SCANCODE_A && code <= SDL_SCANCODE_Z) @@ -518,11 +508,11 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) return; } - if (windowmoved && rendermode == render_opengl) + if (windowmoved) { - I_CheckDesktopRes(); #ifdef USE_FBO_OGL - I_DownSample(); + if (rendermode == render_opengl) + I_CheckDownSampleState(false); #endif windowmoved = SDL_FALSE; } @@ -1229,9 +1219,7 @@ static void VID_Command_ModeList_f(void) // List windowed modes INT32 i = 0; - CONS_Printf("NOTE: Under SDL2, all modes are supported on all platforms.\n" - "Under opengl, fullscreen only supports native desktop resolution.\n" - "Under software, the mode is stretched up to desktop resolution.\n"); + CONS_Printf("NOTE: Under SDL2, all modes are supported on all platforms.\n"); for (i = 0; i < MAXWINMODES; i++) { @@ -1257,78 +1245,41 @@ static void VID_Command_Mode_f (void) setmodeneeded = modenum+1; // request vid mode change } -// Get the desktop resolution from the current display the gamewindow resides on -static void I_CheckDesktopRes(void) -{ - int currentDisplayIndex = -1; - SDL_DisplayMode curmode; - - desktopwidth = 0; - desktopheight = 0; - - currentDisplayIndex = SDL_GetWindowDisplayIndex(window); - - // No valid index - if (currentDisplayIndex < 0) - { - return; - } - - if (SDL_GetDesktopDisplayMode(currentDisplayIndex, &curmode) != 0) - { - return; - } - - desktopwidth = curmode.w; - desktopheight = curmode.h; -} - -// Check if the game resolution matches the desktop resolution -boolean I_CheckNativeRes(void) -{ - return (vid.width == desktopwidth && vid.height == desktopheight); -} - #ifdef USE_FBO_OGL +static boolean vid_downsampling = false; -void RefreshOGLSDLSurface(void) +boolean I_IsDownsampling(void) { - if (rendermode == render_opengl) - OglSdlSurface(vid.width, vid.height); + return vid_downsampling; } -void I_DownSample(void) +void I_CheckDownSampleState(boolean forcerefresh) { - boolean needrefresh = false; - - if (!cv_glframebuffer.value || !supportFBO || (cv_glscreentextures.value == 0)) // no sense to do this crap if we cant benefit from it + // no fbo support so we dont care + if (!supportFBO) { - downsample = false; + vid_downsampling = false; return; } - if (I_CheckNativeRes() && (downsample == true)) + // no sense to do this crap if we cant benefit from it + boolean newdownsamplestate = false; + + if (cv_glframebuffer.value && (cv_glscreentextures.value == 2)) { - downsample = false; - RefreshOGLSDLSurface(); - return; + int sdlw, sdlh; + SDL_GetWindowSize(window, &sdlw, &sdlh); + if (vid.width > sdlw || vid.height > sdlh) + newdownsamplestate = true; } - if ((vid.width > desktopwidth) || (vid.height > desktopheight)) //check if current resolution is higher than current display resolution + // state changed or fbo was toggled + if (vid_downsampling != newdownsamplestate || forcerefresh) { - downsample = true; - needrefresh = true; - } - else if (downsample == true) - { - downsample = false; - needrefresh = true; - } - - if (needrefresh) - { - RefreshOGLSDLSurface(); - needrefresh = false; + //printf("downsample changed from %d to %d\n", vid_downsampling, newdownsamplestate); + // needs to be reset before refreshing the gl surface! + vid_downsampling = newdownsamplestate; + OglSdlSurface(vid.width, vid.height); } } #endif @@ -1360,9 +1311,10 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen) // Reposition window only in windowed mode SDL_SetWindowSize(window, width, height); + const int windowpos = SDL_GetWindowDisplayIndex(window); SDL_SetWindowPosition(window, - SDL_WINDOWPOS_CENTERED_DISPLAY(SDL_GetWindowDisplayIndex(window)), - SDL_WINDOWPOS_CENTERED_DISPLAY(SDL_GetWindowDisplayIndex(window)) + SDL_WINDOWPOS_CENTERED_DISPLAY(windowpos), + SDL_WINDOWPOS_CENTERED_DISPLAY(windowpos) ); } } @@ -1382,9 +1334,8 @@ static void SDLSetMode(INT32 width, INT32 height, SDL_bool fullscreen) #ifdef HWRENDER if (rendermode == render_opengl) { - I_CheckDesktopRes(); #ifdef USE_FBO_OGL - I_DownSample(); + I_CheckDownSampleState(false); #endif OglSdlSurface(vid.width, vid.height); } @@ -1580,7 +1531,7 @@ void VID_PrepareModeList(void) static UINT32 refresh_rate; static UINT32 VID_GetRefreshRate(void) { - int index = SDL_GetWindowDisplayIndex(window); + int index; SDL_DisplayMode m; if (SDL_WasInit(SDL_INIT_VIDEO) == 0) @@ -1589,6 +1540,8 @@ static UINT32 VID_GetRefreshRate(void) return 0; } + index = SDL_GetWindowDisplayIndex(window); + if (SDL_GetDesktopDisplayMode(index, &m) != 0) { // Error has occurred. @@ -1825,7 +1778,6 @@ void I_StartupGraphics(void) rendermode = render_opengl; msaa = 0; boolean msaa_set = false; - a2c = false; boolean a2c_set = false; // MSAA antialiasing if (M_CheckParm("-msaa") && M_IsNextParm()) @@ -1837,16 +1789,6 @@ void I_StartupGraphics(void) } } - if (M_CheckParm("-a2c")) - { - a2c = true; - a2c_set = true; - } - else if (M_CheckParm("-noa2c")) - { - a2c_set = true; - } - { char line[16]; char * word; @@ -1890,19 +1832,10 @@ void I_StartupGraphics(void) } else { - CONS_Printf("Using last know MSAA value: %u\n", msaa); + CONS_Printf("Using last known MSAA value: %u\n", msaa); } } } - - if (!a2c_set) - { - if (fasticmp(word, "a2c")) - { - a2c = true; - CONS_Printf("Using a2c because it was specified to be used earlier\n"); - } - } } fclose(file); @@ -1937,9 +1870,6 @@ void I_StartupGraphics(void) #ifdef HWRENDER fprintf(file, "msaa %u\n", msaa); - - if (a2c) - fputs("a2c\n", file); #endif fclose(file); } diff --git a/src/sdl/ogl_sdl.c b/src/sdl/ogl_sdl.c index f5e7f2aea..144c50f8d 100644 --- a/src/sdl/ogl_sdl.c +++ b/src/sdl/ogl_sdl.c @@ -65,7 +65,7 @@ PFNglGetString pglGetString; #ifdef USE_FBO_OGL boolean UseScreenFBO(void) { - return (supportFBO && cv_glframebuffer.value && downsample); + return (supportFBO && cv_glframebuffer.value && (cv_glscreentextures.value == 2) && (cv_glframebuffer.value == 2 || I_IsDownsampling())); } #endif @@ -203,9 +203,9 @@ boolean OglSdlSurface(INT32 w, INT32 h) #ifdef USE_FBO_OGL if (UseScreenFBO()) - GL_Framebuffer_Enable(); + GL_FBO_Enable(); else - GL_Framebuffer_Disable(); + GL_FBO_Disable(); #endif if (!first_init) @@ -225,7 +225,6 @@ boolean OglSdlSurface(INT32 w, INT32 h) void OglSdlFinishUpdate(SDL_Window *window) { int sdlw, sdlh; - #ifdef USE_FBO_OGL const boolean usefbo = UseScreenFBO(); #endif @@ -235,9 +234,7 @@ void OglSdlFinishUpdate(SDL_Window *window) #ifdef USE_FBO_OGL if (usefbo) - { - GL_Framebuffer_Unbind(); - } + GL_FBO_BindMainFrameBuffer(); #endif if (HWR_ShouldUsePaletteRendering()) @@ -255,9 +252,7 @@ void OglSdlFinishUpdate(SDL_Window *window) #ifdef USE_FBO_OGL if (usefbo) - { - GL_Framebuffer_Enable(); - } + GL_FBO_Enable(); #endif SDL_GL_SwapWindow(window); @@ -266,11 +261,10 @@ void OglSdlFinishUpdate(SDL_Window *window) // Sryder: We need to draw the final screen texture again into the other buffer in the original position so that // effects that want to take the old screen can do so after this - // well we dont need it on native res it seems #ifdef USE_FBO_OGL - if ((!I_CheckNativeRes() && !usefbo) || WipeInAction) + if (!usefbo && (realwidth != sdlw || realheight != sdlh)) // not needed if fbo is in use #else - if (!I_CheckNativeRes() || WipeInAction) + if (realwidth != sdlw || realheight != sdlh) // this is not needed if the render size matches window size #endif HWR_DrawScreenFinalTexture(realwidth, realheight); }