Skip to content

Commit

Permalink
Override video mode list in Team Arena UI
Browse files Browse the repository at this point in the history
Override the video mode list in the Team Arena data files with detected
modes from SDL like in ioquake3's Q3 UI. Add the aspect ratio to the
end of the video resolution (i.e., "640x480 (4:3)"). Add the current
(custom) video mode to the list.

Before when using a custom resolution in the menu you could not change
the video mode using the mouse because the resolution text was blank.
Now custom video resolution is displayed and can be clicked.
  • Loading branch information
zturtleman committed Nov 19, 2021
1 parent e27b4e6 commit 278b7de
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
10 changes: 10 additions & 0 deletions code/ui/ui_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,7 @@ static void UI_Update(const char *name) {
trap_Cvar_SetValue( "r_stencilbits", 8 );
trap_Cvar_SetValue( "r_picmip", 0 );
trap_Cvar_SetValue( "r_mode", 4 );
trap_Cvar_Set( "ui_videomode", "800x600" );
trap_Cvar_SetValue( "r_texturebits", 32 );
trap_Cvar_SetValue( "r_fastSky", 0 );
trap_Cvar_SetValue( "r_inGameVideo", 1 );
Expand All @@ -2761,6 +2762,7 @@ static void UI_Update(const char *name) {
trap_Cvar_Reset( "r_stencilbits" );
trap_Cvar_SetValue( "r_picmip", 1 );
trap_Cvar_SetValue( "r_mode", 3 );
trap_Cvar_Set( "ui_videomode", "640x480" );
trap_Cvar_SetValue( "r_texturebits", 0 );
trap_Cvar_SetValue( "r_fastSky", 0 );
trap_Cvar_SetValue( "r_inGameVideo", 1 );
Expand All @@ -2778,6 +2780,7 @@ static void UI_Update(const char *name) {
trap_Cvar_Reset( "r_stencilbits" );
trap_Cvar_SetValue( "r_picmip", 1 );
trap_Cvar_SetValue( "r_mode", 3 );
trap_Cvar_Set( "ui_videomode", "640x480" );
trap_Cvar_SetValue( "r_texturebits", 0 );
trap_Cvar_SetValue( "cg_shadows", 0 );
trap_Cvar_SetValue( "r_fastSky", 1 );
Expand All @@ -2794,6 +2797,7 @@ static void UI_Update(const char *name) {
trap_Cvar_SetValue( "r_depthbits", 16 );
trap_Cvar_SetValue( "r_stencilbits", 0 );
trap_Cvar_SetValue( "r_mode", 3 );
trap_Cvar_Set( "ui_videomode", "640x480" );
trap_Cvar_SetValue( "r_picmip", 2 );
trap_Cvar_SetValue( "r_texturebits", 16 );
trap_Cvar_SetValue( "cg_shadows", 0 );
Expand Down Expand Up @@ -4718,6 +4722,11 @@ void UI_Init( qboolean inGameLoad, int maxSplitView ) {
UI_RegisterCvars();
UI_InitMemory();

trap_Cvar_Set("ui_videomode", va( "%dx%d", cgs.glconfig.vidWidth, cgs.glconfig.vidHeight ) );

// ### FIXME: Don't commit this.
Com_Printf( "DEBUG: ui_videomode default: '%s'\n", va( "%dx%d", cgs.glconfig.vidWidth, cgs.glconfig.vidHeight ) );

//UI_Load();
uiInfo.uiDC.registerShaderNoMip = &trap_R_RegisterShaderNoMip;
uiInfo.uiDC.setColor = &trap_R_SetColor;
Expand Down Expand Up @@ -5459,6 +5468,7 @@ static cvarTable_t cvarTable[] = {
{ NULL, "fraglimit", "20", CVAR_SERVERINFO | CVAR_ARCHIVE | CVAR_NORESTART},
{ NULL, "capturelimit", "8", CVAR_SERVERINFO | CVAR_ARCHIVE | CVAR_NORESTART},
{ &ui_serverStatusTimeOut, "ui_serverStatusTimeOut", "7000", CVAR_ARCHIVE},
{ NULL, "ui_videomode", "", CVAR_ROM },
{ &ui_defaultMaleTeamModel, "default_male_team_model", DEFAULT_TEAM_MODEL_MALE, CVAR_ARCHIVE},
{ &ui_defaultFemaleTeamModel, "default_female_team_model", DEFAULT_TEAM_MODEL_FEMALE, CVAR_ARCHIVE},
{ &ui_menuFont, "ui_menuFont", "fonts/LiberationSans-Bold.ttf", CVAR_ARCHIVE | CVAR_LATCH},
Expand Down
160 changes: 160 additions & 0 deletions code/ui/ui_shared.c
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,26 @@ qboolean Item_Multi_HandleKey(itemDef_t *item, int key) {
} else if ( current >= max ) {
current = 0;
}

if (multiPtr->videoMode) {
if (multiPtr->cvarValue[current] != -1) {
DC->setCVar("r_mode", va("%i", (int) multiPtr->cvarValue[current] ));
} else {
int w, h;
char *x;
char str[8];

x = strchr( multiPtr->cvarStr[current], 'x' ) + 1;
Q_strncpyz( str, multiPtr->cvarStr[current], MIN( x-multiPtr->cvarStr[current], sizeof( str ) ) );
w = atoi( str );
h = atoi( x );

DC->setCVar("r_mode", "-1");
DC->setCVar("r_customwidth", va("%i", w));
DC->setCVar("r_customheight", va("%i", h));
}
}

if (multiPtr->strDef) {
DC->setCVar(item->cvar, multiPtr->cvarStr[current]);
} else {
Expand Down Expand Up @@ -5019,6 +5039,7 @@ qboolean ItemParse_cvarStrList( itemDef_t *item, int handle ) {
multiPtr = (multiDef_t*)item->typeData;
multiPtr->count = 0;
multiPtr->strDef = qtrue;
multiPtr->videoMode = qfalse;

if (!trap_PC_ReadToken(handle, &token))
return qfalse;
Expand Down Expand Up @@ -5067,6 +5088,7 @@ qboolean ItemParse_cvarFloatList( itemDef_t *item, int handle ) {
multiPtr = (multiDef_t*)item->typeData;
multiPtr->count = 0;
multiPtr->strDef = qfalse;
multiPtr->videoMode = qfalse;

if (!trap_PC_ReadToken(handle, &token))
return qfalse;
Expand Down Expand Up @@ -5243,6 +5265,61 @@ void Item_SetupKeywordHash(void) {
}
}

static const char *builtinResolutions[ ] =
{
"320x240",
"400x300",
"512x384",
"640x480",
"800x600",
"960x720",
"1024x768",
"1152x864",
"1280x1024",
"1600x1200",
"2048x1536",
"856x480",
NULL
};

static const char *knownRatios[ ][2] =
{
{ "1.25:1", "5:4" },
{ "1.33:1", "4:3" },
{ "1.50:1", "3:2" },
{ "1.56:1", "14:9" },
{ "1.60:1", "16:10" },
{ "1.67:1", "5:3" },
{ "1.78:1", "16:9" },
{ NULL , NULL }
};

/*
===============
UI_ResolutionToAspect
===============
*/
static void UI_ResolutionToAspect( const char *resolution, char *aspect, size_t aspectLength ) {
int i, w, h;
char *x;
char str[8];

// calculate resolution's aspect ratio
x = strchr( resolution, 'x' ) + 1;
Q_strncpyz( str, resolution, MIN( x-resolution, sizeof( str ) ) );
w = atoi( str );
h = atoi( x );
Com_sprintf( aspect, aspectLength, "%.2f:1", (float)w / (float)h );

// rename common ratios ("1.33:1" -> "4:3")
for( i = 0; knownRatios[i][0]; i++ ) {
if( !Q_stricmp( aspect, knownRatios[i][0] ) ) {
Q_strncpyz( aspect, knownRatios[i][1], aspectLength );
break;
}
}
}

/*
===============
Item_ApplyHacks
Expand Down Expand Up @@ -5277,6 +5354,89 @@ static void Item_ApplyHacks( itemDef_t *item ) {
}
}

// Replace mode list and use a temporary ui_videomode cvar for handling custom modes
if ( item->type == ITEM_TYPE_MULTI && item->cvar && !Q_stricmp( item->cvar, "r_mode" ) ) {
multiDef_t *multiPtr = (multiDef_t*)item->typeData;
int i, oldCount;
char resbuf[MAX_STRING_CHARS];
char modeName[32], aspect[8];

item->cvar = "ui_videomode";
multiPtr->strDef = qtrue;
multiPtr->videoMode = qtrue;

oldCount = multiPtr->count;
multiPtr->count = 0;

DC->getCVarString( "r_availableModes", resbuf, sizeof( resbuf ) );

if ( *resbuf ) {
char *s = resbuf, *mode;

while ( s && multiPtr->count < MAX_MULTI_CVARS ) {
mode = s;

s = strchr(s, ' ');
if( s )
*s++ = '\0';

UI_ResolutionToAspect( mode, aspect, sizeof( aspect ) );
Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", mode, aspect );

multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );

for ( i = 0; builtinResolutions[i]; i++ ) {
if( !Q_stricmp( builtinResolutions[i], mode ) ) {
multiPtr->cvarStr[multiPtr->count] = builtinResolutions[i];
multiPtr->cvarValue[multiPtr->count] = i;
break;
}
}

if ( builtinResolutions[i] == NULL ) {
multiPtr->cvarStr[multiPtr->count] = String_Alloc( mode );
multiPtr->cvarValue[multiPtr->count] = -1;
}

multiPtr->count++;
}
} else {
for ( i = 0; builtinResolutions[i] && multiPtr->count < MAX_MULTI_CVARS; i++ ) {
UI_ResolutionToAspect( builtinResolutions[i], aspect, sizeof( aspect ) );
Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", builtinResolutions[i], aspect );

multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );
multiPtr->cvarStr[multiPtr->count] = builtinResolutions[i];
multiPtr->cvarValue[multiPtr->count] = i;
multiPtr->count++;
}
}

// Add custom resolution if not in mode list
if ( multiPtr->count < MAX_MULTI_CVARS ) {
char currentResolution[20];

Com_sprintf( currentResolution, sizeof ( currentResolution ), "%dx%d", cgs.glconfig.vidWidth, cgs.glconfig.vidHeight );
for ( i = 0; i < multiPtr->count; i++ ) {
if ( !Q_stricmp( multiPtr->cvarStr[i], currentResolution ) ) {
break;
}
}

if ( i == multiPtr->count ) {
UI_ResolutionToAspect( currentResolution, aspect, sizeof( aspect ) );
Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", currentResolution, aspect );

multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );
multiPtr->cvarStr[multiPtr->count] = String_Alloc( currentResolution );
multiPtr->cvarValue[multiPtr->count] = -1;
multiPtr->count++;
}
}

Com_Printf( "Found video mode list with %d modes, replaced list with %d modes\n", oldCount, multiPtr->count );
}

// Show selected score on the scoreboard
if ( item->type == ITEM_TYPE_LISTBOX && item->window.outlineColor[3] == 0 && ( item->special == FEEDER_SCOREBOARD
|| item->special == FEEDER_REDTEAM_LIST || item->special == FEEDER_BLUETEAM_LIST ) ) {
Expand Down
1 change: 1 addition & 0 deletions code/ui/ui_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ typedef struct multiDef_s {
float cvarValue[MAX_MULTI_CVARS];
int count;
qboolean strDef;
qboolean videoMode;
} multiDef_t;

typedef struct modelDef_s {
Expand Down

0 comments on commit 278b7de

Please sign in to comment.