diff options
author | M. Kristall <mkpdev@gmail.com> | 2009-10-19 07:09:55 +0000 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-01-03 00:16:56 +0000 |
commit | 4bede371a0f04d5edf3bd28f503ccf11cc9c6f47 (patch) | |
tree | d2d2dfa738432e3f13893902d5392903cbd13a13 /src/ui | |
parent | be79439b479902f5b464511e95c4c5455071e8fa (diff) |
* Use binary searching instead of linear searching for many static arrays
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/ui_atoms.c | 27 | ||||
-rw-r--r-- | src/ui/ui_shared.c | 52 |
2 files changed, 36 insertions, 43 deletions
diff --git a/src/ui/ui_atoms.c b/src/ui/ui_atoms.c index 9f0f3a9a..8f31225f 100644 --- a/src/ui/ui_atoms.c +++ b/src/ui/ui_atoms.c @@ -153,18 +153,18 @@ static void UI_MessageMode_f( void ) Menus_ActivateByName( "say" ); } -struct +struct uicmd { char *cmd; void ( *function )( void ); } commands[ ] = { - { "ui_load", UI_Load }, - { "ui_report", UI_Report }, - { "ui_cache", UI_Cache_f }, + { "closemenus", UI_CloseMenus_f }, + { "menu", UI_Menu_f }, { "messagemode", UI_MessageMode_f }, { "messagemode2", UI_MessageMode_f }, - { "menu", UI_Menu_f }, - { "closemenus", UI_CloseMenus_f } + { "ui_cache", UI_Cache_f }, + { "ui_load", UI_Load }, + { "ui_report", UI_Report } }; /* @@ -174,20 +174,17 @@ UI_ConsoleCommand */ qboolean UI_ConsoleCommand( int realTime ) { - char *cmd; - int i; + struct uicmd *cmd = bsearch( UI_Argv( 0 ), commands, + sizeof( commands ) / sizeof( commands[ 0 ] ), sizeof( commands[ 0 ] ), + cmdcmp ); uiInfo.uiDC.frameTime = realTime - uiInfo.uiDC.realTime; uiInfo.uiDC.realTime = realTime; - cmd = UI_Argv( 0 ); - for( i = 0; i < sizeof( commands ) / sizeof( commands[ 0 ] ); i++ ) + if( cmd ) { - if( Q_stricmp( commands[ i ].cmd, cmd ) == 0 ) - { - commands[ i ].function( ); - return qtrue; - } + cmd->function( ); + return qtrue; } return qfalse; diff --git a/src/ui/ui_shared.c b/src/ui/ui_shared.c index d38a0243..d5ed79dc 100644 --- a/src/ui/ui_shared.c +++ b/src/ui/ui_shared.c @@ -2238,37 +2238,41 @@ void UI_Text_PaintWithCursor( float x, float y, float scale, vec4_t color, const commandDef_t commandList[] = { + {"close", &Script_Close}, // menu + {"conditionalopen", &Script_ConditionalOpen}, // menu + {"exec", &Script_Exec}, // group/name {"fadein", &Script_FadeIn}, // group/name {"fadeout", &Script_FadeOut}, // group/name - {"show", &Script_Show}, // group/name {"hide", &Script_Hide}, // group/name - {"setcolor", &Script_SetColor}, // works on this {"open", &Script_Open}, // menu - {"conditionalopen", &Script_ConditionalOpen}, // menu - {"close", &Script_Close}, // menu + {"orbit", &Script_Orbit}, // group/name + {"play", &Script_Play}, // group/name + {"playlooped", &Script_playLooped}, // group/name + {"reset", &Script_Reset}, // resets the state of the item argument {"setasset", &Script_SetAsset}, // works on this {"setbackground", &Script_SetBackground}, // works on this - {"setitemcolor", &Script_SetItemColor}, // group/name + {"setcolor", &Script_SetColor}, // works on this + {"setcvar", &Script_SetCvar}, // group/name {"setfocus", &Script_SetFocus}, // sets this background color to team color - {"reset", &Script_Reset}, // resets the state of the item argument - {"setplayermodel", &Script_SetPlayerModel}, // sets this background color to team color + {"setitemcolor", &Script_SetItemColor}, // group/name {"setplayerhead", &Script_SetPlayerHead}, // sets this background color to team color + {"setplayermodel", &Script_SetPlayerModel}, // sets this background color to team color + {"show", &Script_Show}, // group/name {"transition", &Script_Transition}, // group/name - {"setcvar", &Script_SetCvar}, // group/name - {"exec", &Script_Exec}, // group/name - {"play", &Script_Play}, // group/name - {"playlooped", &Script_playLooped}, // group/name - {"orbit", &Script_Orbit} // group/name }; -int scriptCommandCount = sizeof( commandList ) / sizeof( commandDef_t ); +static size_t scriptCommandCount = sizeof( commandList ) / sizeof( commandDef_t ); +// despite what lcc thinks, we do not get cmdcmp here +static int commandComp( const void *a, const void *b ) +{ + return Q_stricmp( (const char *)a, ((commandDef_t *)b)->name ); +} void Item_RunScript( itemDef_t *item, const char *s ) { char script[1024], *p; - int i; - qboolean bRan; + commandDef_t *cmd; memset( script, 0, sizeof( script ) ); if( item && s && s[0] ) @@ -2287,20 +2291,12 @@ void Item_RunScript( itemDef_t *item, const char *s ) if( command[0] == ';' && command[1] == '\0' ) continue; - bRan = qfalse; - - for( i = 0; i < scriptCommandCount; i++ ) - { - if( Q_stricmp( command, commandList[i].name ) == 0 ) - { - ( commandList[i].handler( item, &p ) ); - bRan = qtrue; - break; - } - } - + cmd = bsearch( command, commandList, scriptCommandCount, + sizeof( commandDef_t ), commandComp ); + if( cmd ) + cmd->handler( item, &p ); + else // not in our auto list, pass to handler - if( !bRan ) DC->runScript( &p ); } } |