hedgewars/uRenderUtils.pas
changeset 14741 8563cc40fc1e
parent 14639 b055360684bd
child 14750 7cc768094d66
equal deleted inserted replaced
14740:2d859d34e6f6 14741:8563cc40fc1e
    35 procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
    35 procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
    36 
    36 
    37 function  RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
    37 function  RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
    38 function  RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
    38 function  RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
    39 function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
    39 function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
       
    40 
       
    41 function IsTooDarkToRead(TextColor: Longword): boolean; inline;
    40 
    42 
    41 implementation
    43 implementation
    42 uses uVariables, uConsts, uTextures, SysUtils, uUtils, uDebug;
    44 uses uVariables, uConsts, uTextures, SysUtils, uUtils, uDebug;
    43 
    45 
    44 procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
    46 procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean);
    74 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    76 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    75 begin
    77 begin
    76     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
    78     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
    77 end;*)
    79 end;*)
    78 
    80 
       
    81 function IsTooDarkToRead(TextColor: LongWord): boolean;
       
    82 var clr: TSDL_Color;
       
    83 begin
       
    84     clr.r:= (TextColor shr 16) and $FF;
       
    85     clr.g:= (TextColor shr 8) and $FF;
       
    86     clr.b:= TextColor and $FF;
       
    87     IsTooDarkToRead:= ((clr.r >= cInvertTextColorAt) or (clr.g >= cInvertTextColorAt) or (clr.b >= cInvertTextColorAt));
       
    88 end;
       
    89 
       
    90 function IsTooDarkToRead(TextColor: TSDL_COLOR): boolean;
       
    91 begin
       
    92     IsTooDarkToRead:= (not ((TextColor.r >= cInvertTextColorAt) or (TextColor.g >= cInvertTextColorAt) or (TextColor.b >= cInvertTextColorAt)));
       
    93 end;
       
    94 
    79 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect;
    95 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect;
    80 var w, h: Longword;
    96 var w, h: Longword;
    81     tmpsurf: PSDL_Surface;
    97     tmpsurf: PSDL_Surface;
       
    98     finalRect, textRect: TSDL_Rect;
    82     clr: TSDL_Color;
    99     clr: TSDL_Color;
    83     finalRect, textRect: TSDL_Rect;
       
    84 begin
   100 begin
    85     TTF_SizeUTF8(Fontz[Font].Handle, PChar(s), @w, @h);
   101     TTF_SizeUTF8(Fontz[Font].Handle, PChar(s), @w, @h);
    86     if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor;
   102     if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor;
    87     finalRect.x:= X;
   103     finalRect.x:= X;
    88     finalRect.y:= Y;
   104     finalRect.y:= Y;
    90     finalRect.h:= h + cFontBorder * 2;
   106     finalRect.h:= h + cFontBorder * 2;
    91     textRect.x:= X;
   107     textRect.x:= X;
    92     textRect.y:= Y;
   108     textRect.y:= Y;
    93     textRect.w:= w;
   109     textRect.w:= w;
    94     textRect.h:= h;
   110     textRect.h:= h;
    95     DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true);
       
    96     clr.r:= (Color shr 16) and $FF;
   111     clr.r:= (Color shr 16) and $FF;
    97     clr.g:= (Color shr 8) and $FF;
   112     clr.g:= (Color shr 8) and $FF;
    98     clr.b:= Color and $FF;
   113     clr.b:= Color and $FF;
       
   114     if (not IsTooDarkToRead(clr)) then
       
   115         DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true)
       
   116     else
       
   117         DrawRoundRect(@finalRect, cNearBlackColor, cWhiteColor, Surface, true);
    99     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr);
   118     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr);
   100     finalRect.x:= X + cFontBorder + cFontPadding;
   119     finalRect.x:= X + cFontBorder + cFontPadding;
   101     finalRect.y:= Y + cFontBorder;
   120     finalRect.y:= Y + cFontBorder;
   102     if SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
   121     if SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then
   103         exit;
   122         exit;