Skip to content

Commit c8561ea

Browse files
committed
Migrate to new gameinfo APIs
1 parent e303d45 commit c8561ea

7 files changed

+134
-45
lines changed

BaseMenu.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,11 @@ void UI_Init( void )
11641164
uiStatic.lowmemory = (int)EngFuncs::GetCvarFloat( "host_lowmemorymode" );
11651165

11661166
// setup game info
1167-
EngFuncs::GetGameInfo( &gMenu.m_gameinfo );
1167+
gameinfo2_t *gi = EngFuncs::GetGameInfo();
1168+
if( !gi )
1169+
Host_Error( "pfnGetGameInfo returned NULL!\n" );
1170+
1171+
gMenu.m_gameinfo = *gi;
11681172

11691173
uiStatic.renderPicbuttonText = gMenu.m_gameinfo.flags & GFL_RENDER_PICBUTTON_TEXT;
11701174

BaseMenu.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class CMenu
330330
{
331331
public:
332332
// Game information
333-
GAMEINFO m_gameinfo;
333+
gameinfo2_t m_gameinfo;
334334
};
335335

336336
typedef struct

Primitive.h

-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ inline bool isrange( T min, T value, T max )
3535
#define FBitSet( iBitVector, bit ) ((iBitVector) & (bit))
3636

3737
// engine constants
38-
enum
39-
{
40-
GAME_NORMAL = 0,
41-
GAME_SINGLEPLAYER_ONLY,
42-
GAME_MULTIPLAYER_ONLY
43-
};
4438

4539
enum
4640
{

Utils.cpp

+76-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ of the License, or (at your option) any later version.
88
99
This program is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
1313
See the GNU General Public License for more details.
1414
@@ -279,7 +279,7 @@ const char *Info_ValueForKey( const char *s, const char *key )
279279
}
280280

281281

282-
/*
282+
/*
283283
===================
284284
Key_GetKey
285285
===================
@@ -554,6 +554,80 @@ bool UI::Names::CheckIsNameValid(const char *name)
554554
return true;
555555
}
556556

557+
char *Q_pretifymem( float value, int digitsafterdecimal )
558+
{
559+
static char output[8][32];
560+
static int current;
561+
const float onekb = 1024.0f;
562+
const float onemb = onekb * onekb;
563+
const char *suffix;
564+
char *out = output[current];
565+
char val[32], *i, *o, *dot;
566+
int pos;
567+
568+
current = ( current + 1 ) & ( 8 - 1 );
569+
570+
// first figure out which bin to use
571+
if( value > onemb )
572+
{
573+
value /= onemb;
574+
suffix = " Mb";
575+
}
576+
else if( value > onekb )
577+
{
578+
value /= onekb;
579+
suffix = " Kb";
580+
}
581+
else
582+
{
583+
suffix = " bytes";
584+
}
585+
586+
// clamp to >= 0
587+
digitsafterdecimal = Q_max( digitsafterdecimal, 0 );
588+
589+
// if it's basically integral, don't do any decimals
590+
if( fabs( value - (int)value ) < 0.00001f )
591+
{
592+
snprintf( val, sizeof( val ), "%i%s", (int)value, suffix );
593+
}
594+
else
595+
{
596+
char fmt[32];
597+
598+
// otherwise, create a format string for the decimals
599+
snprintf( fmt, sizeof( fmt ), "%%.%if%s", digitsafterdecimal, suffix );
600+
snprintf( val, sizeof( val ), fmt, (double)value );
601+
}
602+
603+
// copy from in to out
604+
i = val;
605+
o = out;
606+
607+
// search for decimal or if it was integral, find the space after the raw number
608+
dot = strchr( i, '.' );
609+
if( !dot ) dot = strchr( i, ' ' );
610+
611+
pos = dot - i; // compute position of dot
612+
pos -= 3; // don't put a comma if it's <= 3 long
613+
614+
while( *i )
615+
{
616+
// if pos is still valid then insert a comma every third digit, except if we would be
617+
// putting one in the first spot
618+
if( pos >= 0 && !( pos % 3 ))
619+
{
620+
// never in first spot
621+
if( o != out ) *o++ = ',';
622+
}
623+
624+
pos--; // count down comma position
625+
*o++ = *i++; // copy rest of data as normal
626+
}
627+
*o = 0; // terminate
628+
629+
return out;
630+
}
557631

558632
void Com_EscapeCommand( char *newCommand, const char *oldCommand, int len )
559633
{

Utils.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ inline unsigned int PackAlpha( const unsigned int ulRGB, const unsigned int ulAl
126126

127127
inline unsigned int UnpackAlpha( const unsigned int ulRGBA )
128128
{
129-
return ((ulRGBA & 0xFF000000) >> 24);
129+
return ((ulRGBA & 0xFF000000) >> 24);
130130
}
131131

132132
inline float InterpVal( const float from, const float to, const float frac )
@@ -410,4 +410,7 @@ int Con_UtfProcessChar(int in );
410410
int Con_UtfMoveLeft( const char *str, int pos );
411411
int Con_UtfMoveRight( const char *str, int pos, int length );
412412

413+
char *Q_pretifymem( float value, int digitsafterdecimal );
414+
#define Q_memprint( val ) Q_pretifymem( val, 2 )
415+
413416
#endif//UTILS_H

enginecallback_menu.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,14 @@ class EngFuncs
381381
}
382382

383383
// collect info from engine
384-
static inline int GetGameInfo( GAMEINFO *pgameinfo )
384+
static inline gameinfo2_t *GetGameInfo( void )
385385
{
386-
return engfuncs.pfnGetGameInfo( pgameinfo );
386+
return textfuncs.pfnGetGameInfo( GAMEINFO_VERSION );
387387
}
388388

389-
static inline GAMEINFO **GetGamesList( int *numGames ) // collect info about all mods
389+
static inline gameinfo2_t *GetModInfo( int i ) // collect info about all mods
390390
{
391-
return engfuncs.pfnGetGamesList( numGames );
391+
return textfuncs.pfnGetModInfo( GAMEINFO_VERSION, i );
392392
}
393393

394394
static inline char **GetFilesList( const char *pattern, int *numFiles, int gamedironly ) // find in files
@@ -506,7 +506,7 @@ class EngFuncs
506506
{
507507
return textfuncs.pfnGetRenderers( num, sz1, s1, sz2, s2 ) != 0;
508508
}
509-
509+
510510
static inline double DoubleTime()
511511
{
512512
return textfuncs.pfnDoubleTime();

menus/CustomGame.cpp

+43-29
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ of the License, or (at your option) any later version.
88
99
This program is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
1313
See the GNU General Public License for more details.
1414
@@ -46,6 +46,7 @@ struct mod_t
4646
char name[32];
4747
char ver[32];
4848
char size[32];
49+
uint64_t bytes;
4950

5051
int TypeCmp( const mod_t &other ) const
5152
{
@@ -57,6 +58,13 @@ struct mod_t
5758
return stricmp( name, other.name );
5859
}
5960

61+
int SizeCmp( const mod_t &other ) const
62+
{
63+
if( bytes > other.bytes ) return 1;
64+
else if( bytes < other.bytes ) return -1;
65+
return 0;
66+
}
67+
6068
#define GENERATE_COMPAR_FN( method ) \
6169
static int method ## Ascend( const void *a, const void *b ) \
6270
{\
@@ -69,6 +77,7 @@ struct mod_t
6977

7078
GENERATE_COMPAR_FN( TypeCmp )
7179
GENERATE_COMPAR_FN( NameCmp )
80+
GENERATE_COMPAR_FN( SizeCmp )
7281
#undef GENERATE_COMPAR_FN
7382
};
7483

@@ -160,35 +169,40 @@ CMenuModListModel::Update
160169
*/
161170
void CMenuModListModel::Update( void )
162171
{
163-
int numGames, i;
164-
GAMEINFO **games;
172+
int i;
165173

166-
games = EngFuncs::GetGamesList( &numGames );
167-
168-
for( i = 0; i < numGames; i++ )
174+
for( i = 0; ; i++ )
169175
{
170-
Q_strncpy( mods[i].dir, games[i]->gamefolder, sizeof( mods[i].dir ));
171-
Q_strncpy( mods[i].webSite, games[i]->game_url, sizeof( mods[i].webSite ));
172-
Q_strncpy( mods[i].type, games[i]->type, sizeof( mods[i].type ));
173-
Q_strncpy( mods[i].ver, games[i]->version, sizeof( mods[i].ver ));
176+
gameinfo2_t *gi = EngFuncs::GetModInfo( i );
174177

175-
if( ColorStrlen( games[i]->title ) > sizeof( mods[i].name ) - 1 ) // NAME_LENGTH
178+
if( !gi )
179+
break;
180+
181+
Q_strncpy( mods[i].dir, gi->gamefolder, sizeof( mods[i].dir ));
182+
Q_strncpy( mods[i].webSite, gi->game_url, sizeof( mods[i].webSite ));
183+
Q_strncpy( mods[i].type, gi->type, sizeof( mods[i].type ));
184+
Q_strncpy( mods[i].ver, gi->version, sizeof( mods[i].ver ));
185+
186+
if( ColorStrlen( gi->title ) > sizeof( mods[i].name ) - 1 ) // NAME_LENGTH
176187
{
177-
Q_strncpy( mods[i].name, games[i]->title, sizeof( mods[i].name ) - 4 );
178-
// I am lazy to put strncat here :(
179-
mods[i].name[28] = mods[i].name[29] = mods[i].name[30] = '.';
180-
mods[i].name[31] = 0;
188+
size_t s = sizeof( mods[i].name ) - 4;
189+
190+
Q_strncpy( mods[i].name, gi->title, s );
191+
192+
mods[i].name[s] = mods[i].name[s+1] = mods[i].name[s+2] = '.';
193+
mods[i].name[s+3] = 0;
181194
}
182-
else Q_strncpy( mods[i].name, games[i]->title, sizeof( mods[i].name ));
195+
else Q_strncpy( mods[i].name, gi->title, sizeof( mods[i].name ));
183196

184-
if( games[i]->size[0] && atoi( games[i]->size ) != 0 )
185-
Q_strncpy( mods[i].size, games[i]->size, sizeof( mods[i].size ));
197+
mods[i].bytes = gi->size;
198+
if( gi->size > 0 )
199+
Q_strncpy( mods[i].size, Q_memprint( gi->size ), sizeof( mods[i].size ));
186200
else Q_strncpy( mods[i].size, "0.0 Mb", sizeof( mods[i].size ));
187201
}
188202

189-
m_iNumItems = numGames;
203+
m_iNumItems = i;
190204

191-
if( numGames )
205+
if( i != 0 )
192206
{
193207
if( m_iSortingColumn != -1 )
194208
Sort( m_iSortingColumn, m_bAscend );
@@ -206,20 +220,20 @@ bool CMenuModListModel::Sort(int column, bool ascend)
206220
switch( column )
207221
{
208222
case COLUMN_TYPE:
209-
qsort( mods, m_iNumItems, sizeof(mod_t),
223+
qsort( mods, m_iNumItems, sizeof( mod_t ),
210224
ascend ? mod_t::TypeCmpAscend : mod_t::TypeCmpDescend );
211-
return true;
225+
return true;
212226
case COLUMN_NAME:
213-
qsort( mods, m_iNumItems, sizeof(mod_t),
227+
qsort( mods, m_iNumItems, sizeof( mod_t ),
214228
ascend ? mod_t::NameCmpAscend : mod_t::NameCmpDescend );
215-
return true;
216-
case COLUMN_VER:
217-
return false;
229+
return true;
218230
case COLUMN_SIZE:
219-
return false;
231+
qsort( mods, m_iNumItems, sizeof( mod_t ),
232+
ascend ? mod_t::SizeCmpAscend : mod_t::SizeCmpDescend );
233+
return true;
220234
}
221235

222-
return false;
236+
return false;
223237
}
224238

225239
/*
@@ -260,7 +274,7 @@ void CMenuCustomGame::_Init( void )
260274
if( !stricmp( modListModel.mods[i].dir, gMenu.m_gameinfo.gamefolder ) )
261275
{
262276
modList.SetCurrentIndex( i );
263-
if( modList.onChanged )
277+
if( modList.onChanged )
264278
modList.onChanged( &modList );
265279
break;
266280
}

0 commit comments

Comments
 (0)