From c0fa91bec9f6b6523cc5e9d546a297abce43f139 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 4 Feb 2023 21:24:19 +0300 Subject: [PATCH] engine: client: consolidate client and menu scissor functions --- engine/client/cl_game.c | 149 +++++++++++++++++++++++--------------- engine/client/cl_gameui.c | 64 +--------------- engine/client/client.h | 32 ++++---- 3 files changed, 111 insertions(+), 134 deletions(-) diff --git a/engine/client/cl_game.c b/engine/client/cl_game.c index e305f2ba..26b6c91d 100644 --- a/engine/client/cl_game.c +++ b/engine/client/cl_game.c @@ -371,55 +371,6 @@ void SPR_AdjustTexCoords( float width, float height, float *s1, float *t1, float *t2 /= height; } -static qboolean SPR_Scissor( float *x, float *y, float *width, float *height, float *u0, float *v0, float *u1, float *v1 ) -{ - float dudx, dvdy; - - // clip sub rect to sprite - if(( width == 0 ) || ( height == 0 )) - return false; - - if( *x + *width <= clgame.ds.scissor_x ) - return false; - if( *x >= clgame.ds.scissor_x + clgame.ds.scissor_width ) - return false; - if( *y + *height <= clgame.ds.scissor_y ) - return false; - if( *y >= clgame.ds.scissor_y + clgame.ds.scissor_height ) - return false; - - dudx = (*u1 - *u0) / *width; - dvdy = (*v1 - *v0) / *height; - - if( *x < clgame.ds.scissor_x ) - { - *u0 += (clgame.ds.scissor_x - *x) * dudx; - *width -= clgame.ds.scissor_x - *x; - *x = clgame.ds.scissor_x; - } - - if( *x + *width > clgame.ds.scissor_x + clgame.ds.scissor_width ) - { - *u1 -= (*x + *width - (clgame.ds.scissor_x + clgame.ds.scissor_width)) * dudx; - *width = clgame.ds.scissor_x + clgame.ds.scissor_width - *x; - } - - if( *y < clgame.ds.scissor_y ) - { - *v0 += (clgame.ds.scissor_y - *y) * dvdy; - *height -= clgame.ds.scissor_y - *y; - *y = clgame.ds.scissor_y; - } - - if( *y + *height > clgame.ds.scissor_y + clgame.ds.scissor_height ) - { - *v1 -= (*y + *height - (clgame.ds.scissor_y + clgame.ds.scissor_height)) * dvdy; - *height = clgame.ds.scissor_y + clgame.ds.scissor_height - *y; - } - - return true; -} - /* ==================== SPR_DrawGeneric @@ -470,7 +421,7 @@ static void SPR_DrawGeneric( int frame, float x, float y, float width, float hei } // pass scissor test if supposed - if( clgame.ds.scissor_test && !SPR_Scissor( &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) + if( !CL_Scissor( &clgame.ds.scissor, &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) return; // scale for screen sizes @@ -825,6 +776,92 @@ const char *CL_SoundFromIndex( int index ) return sfx->name; } +/* +================ +CL_EnableScissor + +enable scissor test +================ +*/ +void CL_EnableScissor( scissor_state_t *scissor, int x, int y, int width, int height ) +{ + scissor->x = x; + scissor->y = y; + scissor->width = width; + scissor->height = height; + scissor->test = true; +} + +/* +================ +CL_DisableScissor + +disable scissor test +================ +*/ +void CL_DisableScissor( scissor_state_t *scissor ) +{ + scissor->test = false; +} + +/* +================ +CL_Scissor + +perform common scissor test +================ +*/ +qboolean CL_Scissor( const scissor_state_t *scissor, float *x, float *y, float *width, float *height, float *u0, float *v0, float *u1, float *v1 ) +{ + float dudx, dvdy; + + if( !scissor->test ) + return true; + + // clip sub rect to sprite + if( *width == 0 || *height == 0 ) + return false; + + if( *x + *width <= scissor->x ) + return false; + if( *x >= scissor->x + scissor->width ) + return false; + if( *y + *height <= scissor->y ) + return false; + if( *y >= scissor->y + scissor->height ) + return false; + + dudx = (*u1 - *u0) / *width; + dvdy = (*v1 - *v0) / *height; + + if( *x < scissor->x ) + { + *u0 += (scissor->x - *x) * dudx; + *width -= scissor->x - *x; + *x = scissor->x; + } + + if( *x + *width > scissor->x + scissor->width ) + { + *u1 -= (*x + *width - (scissor->x + scissor->width)) * dudx; + *width = scissor->x + scissor->width - *x; + } + + if( *y < scissor->y ) + { + *v0 += (scissor->y - *y) * dvdy; + *height -= scissor->y - *y; + *y = scissor->y; + } + + if( *y + *height > scissor->y + scissor->height ) + { + *v1 -= (*y + *height - (scissor->y + scissor->height)) * dvdy; + *height = scissor->y + scissor->height - *y; + } + return true; +} + /* ========= SPR_EnableScissor @@ -839,11 +876,7 @@ static void GAME_EXPORT SPR_EnableScissor( int x, int y, int width, int height ) width = bound( 0, width, clgame.scrInfo.iWidth - x ); height = bound( 0, height, clgame.scrInfo.iHeight - y ); - clgame.ds.scissor_x = x; - clgame.ds.scissor_width = width; - clgame.ds.scissor_y = y; - clgame.ds.scissor_height = height; - clgame.ds.scissor_test = true; + CL_EnableScissor( &clgame.ds.scissor, x, y, width, height ); } /* @@ -854,11 +887,7 @@ SPR_DisableScissor */ static void GAME_EXPORT SPR_DisableScissor( void ) { - clgame.ds.scissor_x = 0; - clgame.ds.scissor_width = 0; - clgame.ds.scissor_y = 0; - clgame.ds.scissor_height = 0; - clgame.ds.scissor_test = false; + CL_DisableScissor( &clgame.ds.scissor ); } /* diff --git a/engine/client/cl_gameui.c b/engine/client/cl_gameui.c index e641cbbd..d19087c5 100644 --- a/engine/client/cl_gameui.c +++ b/engine/client/cl_gameui.c @@ -421,54 +421,6 @@ static void UI_ConvertGameInfo( GAMEINFO *out, gameinfo_t *in ) out->flags |= GFL_RENDER_PICBUTTON_TEXT; } -static qboolean PIC_Scissor( float *x, float *y, float *width, float *height, float *u0, float *v0, float *u1, float *v1 ) -{ - float dudx, dvdy; - - // clip sub rect to sprite - if(( width == 0 ) || ( height == 0 )) - return false; - - if( *x + *width <= gameui.ds.scissor_x ) - return false; - if( *x >= gameui.ds.scissor_x + gameui.ds.scissor_width ) - return false; - if( *y + *height <= gameui.ds.scissor_y ) - return false; - if( *y >= gameui.ds.scissor_y + gameui.ds.scissor_height ) - return false; - - dudx = (*u1 - *u0) / *width; - dvdy = (*v1 - *v0) / *height; - - if( *x < gameui.ds.scissor_x ) - { - *u0 += (gameui.ds.scissor_x - *x) * dudx; - *width -= gameui.ds.scissor_x - *x; - *x = gameui.ds.scissor_x; - } - - if( *x + *width > gameui.ds.scissor_x + gameui.ds.scissor_width ) - { - *u1 -= (*x + *width - (gameui.ds.scissor_x + gameui.ds.scissor_width)) * dudx; - *width = gameui.ds.scissor_x + gameui.ds.scissor_width - *x; - } - - if( *y < gameui.ds.scissor_y ) - { - *v0 += (gameui.ds.scissor_y - *y) * dvdy; - *height -= gameui.ds.scissor_y - *y; - *y = gameui.ds.scissor_y; - } - - if( *y + *height > gameui.ds.scissor_y + gameui.ds.scissor_height ) - { - *v1 -= (*y + *height - (gameui.ds.scissor_y + gameui.ds.scissor_height)) * dvdy; - *height = gameui.ds.scissor_y + gameui.ds.scissor_height - *y; - } - return true; -} - /* ==================== PIC_DrawGeneric @@ -511,7 +463,7 @@ static void PIC_DrawGeneric( float x, float y, float width, float height, const } // pass scissor test if supposed - if( gameui.ds.scissor_test && !PIC_Scissor( &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) + if( !CL_Scissor( &gameui.ds.scissor, &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) return; ref.dllFuncs.R_DrawStretchPic( x, y, width, height, s1, t1, s2, t2, gameui.ds.gl_texturenum ); @@ -658,11 +610,7 @@ static void GAME_EXPORT pfnPIC_EnableScissor( int x, int y, int width, int heigh width = bound( 0, width, gameui.globals->scrWidth - x ); height = bound( 0, height, gameui.globals->scrHeight - y ); - gameui.ds.scissor_x = x; - gameui.ds.scissor_width = width; - gameui.ds.scissor_y = y; - gameui.ds.scissor_height = height; - gameui.ds.scissor_test = true; + CL_EnableScissor( &gameui.ds.scissor, x, y, width, height ); } /* @@ -673,11 +621,7 @@ pfnPIC_DisableScissor */ static void GAME_EXPORT pfnPIC_DisableScissor( void ) { - gameui.ds.scissor_x = 0; - gameui.ds.scissor_width = 0; - gameui.ds.scissor_y = 0; - gameui.ds.scissor_height = 0; - gameui.ds.scissor_test = false; + CL_DisableScissor( &gameui.ds.scissor ); } /* @@ -765,7 +709,7 @@ static void GAME_EXPORT pfnDrawCharacter( int ix, int iy, int iwidth, int iheigh t2 = t1 + size; // pass scissor test if supposed - if( gameui.ds.scissor_test && !PIC_Scissor( &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) + if( !CL_Scissor( &gameui.ds.scissor, &x, &y, &width, &height, &s1, &t1, &s2, &t2 )) return; ref.dllFuncs.GL_SetRenderMode( kRenderTransTexture ); diff --git a/engine/client/client.h b/engine/client/client.h index b61848d0..8c324c1c 100644 --- a/engine/client/client.h +++ b/engine/client/client.h @@ -340,18 +340,23 @@ typedef struct qboolean valid; // all rectangles are valid } cl_font_t; +typedef struct scissor_state_s +{ + int x; + int y; + int width; + int height; + qboolean test; +} scissor_state_t; + typedef struct { + // scissor test + scissor_state_t scissor; + // temp handle const model_t *pSprite; // pointer to current SpriteTexture - // scissor test - int scissor_x; - int scissor_y; - int scissor_width; - int scissor_height; - qboolean scissor_test; - int renderMode; // override kRenderMode from TriAPI TRICULLSTYLE cullMode; // override CULL FACE from TriAPI @@ -378,14 +383,10 @@ typedef struct cl_predicted_player_s typedef struct { - int gl_texturenum; // this is a real texnum - // scissor test - int scissor_x; - int scissor_y; - int scissor_width; - int scissor_height; - qboolean scissor_test; + scissor_state_t scissor; + + int gl_texturenum; // this is a real texnum // holds text color rgba_t textColor; @@ -862,6 +863,9 @@ void pfnGetScreenFade( struct screenfade_s *fade ); physent_t *pfnGetPhysent( int idx ); struct msurface_s *pfnTraceSurface( int ground, float *vstart, float *vend ); movevars_t *pfnGetMoveVars( void ); +void CL_EnableScissor( scissor_state_t *scissor, int x, int y, int width, int height ); +void CL_DisableScissor( scissor_state_t *scissor ); +qboolean CL_Scissor( const scissor_state_t *scissor, float *x, float *y, float *width, float *height, float *u0, float *v0, float *u1, float *v1 ); _inline cl_entity_t *CL_EDICT_NUM( int n ) {