hedgewars/uRenderUtils.pas
changeset 10116 dd27562b6f21
parent 10108 c68cf030eded
child 10127 7f29a65aa1e4
equal deleted inserted replaced
10115:794af9339726 10116:dd27562b6f21
    33 procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: LongInt);
    33 procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: LongInt);
    34 procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte);
    34 procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte);
    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  RenderStringTexPChar(s: PChar; Color: Longword; font: THWFont): PTexture;
    38 function  RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
    39 function  RenderStringTexLim(s: PChar; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
       
    40 function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
    39 function  RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
    41 
    40 
    42 implementation
    41 implementation
    43 uses uUtils, uVariables, uConsts, uTextures, SysUtils, uDebug;
    42 uses uUtils, uVariables, uConsts, uTextures, SysUtils, uDebug;
    44 
    43 
    75 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    74 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect;
    76 begin
    75 begin
    77     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
    76     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
    78 end;*)
    77 end;*)
    79 
    78 
    80 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: PChar; maxLength: LongWord): TSDL_Rect;
    79 function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect;
    81 var w, h: LongInt;
    80 var w, h: LongInt;
    82     tmpsurf: PSDL_Surface;
    81     tmpsurf: PSDL_Surface;
    83     clr: TSDL_Color;
    82     clr: TSDL_Color;
    84     finalRect, textRect: TSDL_Rect;
    83     finalRect, textRect: TSDL_Rect;
    85 begin
    84 begin
    86     TTF_SizeUTF8(Fontz[Font].Handle, s, @w, @h);
    85     TTF_SizeUTF8(Fontz[Font].Handle, Str2PChar(s), @w, @h);
    87     if (maxLength <> 0) and (w > maxLength) then w := maxLength;
    86     if (maxLength <> 0) and (w > maxLength) then w := maxLength;
    88     finalRect.x:= X;
    87     finalRect.x:= X;
    89     finalRect.y:= Y;
    88     finalRect.y:= Y;
    90     finalRect.w:= w + cFontBorder * 2 + 4;
    89     finalRect.w:= w + cFontBorder * 2 + 4;
    91     finalRect.h:= h + cFontBorder * 2;
    90     finalRect.h:= h + cFontBorder * 2;
    95     textRect.h:= h;
    94     textRect.h:= h;
    96     DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true);
    95     DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true);
    97     clr.r:= (Color shr 16) and $FF;
    96     clr.r:= (Color shr 16) and $FF;
    98     clr.g:= (Color shr 8) and $FF;
    97     clr.g:= (Color shr 8) and $FF;
    99     clr.b:= Color and $FF;
    98     clr.b:= Color and $FF;
   100     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, s, clr);
    99     tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr);
   101     finalRect.x:= X + cFontBorder + 2;
   100     finalRect.x:= X + cFontBorder + 2;
   102     finalRect.y:= Y + cFontBorder;
   101     finalRect.y:= Y + cFontBorder;
   103     SDLTry(tmpsurf <> nil, true);
   102     SDLTry(tmpsurf <> nil, true);
   104     SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect);
   103     SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect);
   105     SDL_FreeSurface(tmpsurf);
   104     SDL_FreeSurface(tmpsurf);
   269 
   268 
   270 end;
   269 end;
   271 
   270 
   272 function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
   271 function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
   273 begin
   272 begin
   274     RenderStringTex:= RenderStringTexLim(Str2PChar(s), Color, font, 0);
   273     RenderStringTex:= RenderStringTexLim(s, Color, font, 0);
   275 end;
   274 end;
   276 
   275 
   277 function RenderStringTexPChar(s: PChar; Color: Longword; font: THWFont): PTexture;
   276 function RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
   278 begin
       
   279     RenderStringTexPChar:= RenderStringTexLim(s, Color, font, 0);
       
   280 end;
       
   281 
       
   282 function RenderStringTexLim(s: PChar; Color: Longword; font: THWFont; maxLength: LongWord): PTexture;
       
   283 var w, h: LongInt;
   277 var w, h: LongInt;
   284     finalSurface: PSDL_Surface;
   278     finalSurface: PSDL_Surface;
   285 begin
   279 begin
   286     if s[0] = #0 then s:= Str2PChar(' '); // conversion because pas2c ain't smart enough yet;
   280     if length(s) = 0 then s:= _S' ';
   287     font:= CheckCJKFont(s, font);
   281     font:= CheckCJKFont(s, font);
   288     w:= 0; h:= 0; // avoid compiler hints
   282     w:= 0; h:= 0; // avoid compiler hints
   289     TTF_SizeUTF8(Fontz[font].Handle, s, @w, @h);
   283     TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h);
   290     if (maxLength <> 0) and (w > maxLength) then w := maxLength;
   284     if (maxLength <> 0) and (w > maxLength) then w := maxLength;
   291 
   285 
   292     finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + cFontBorder * 2 + 4, h + cFontBorder * 2,
   286     finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + cFontBorder * 2 + 4, h + cFontBorder * 2,
   293             32, RMask, GMask, BMask, AMask);
   287             32, RMask, GMask, BMask, AMask);
   294 
   288 
   340 
   334 
   341     numLines:= 0;
   335     numLines:= 0;
   342 
   336 
   343     if length(s) = 0 then
   337     if length(s) = 0 then
   344         s:= '...';
   338         s:= '...';
   345     font:= CheckCJKFont(Str2PChar(s), font);
   339     font:= CheckCJKFont(s, font);
   346     w:= 0; h:= 0; // avoid compiler hints
   340     w:= 0; h:= 0; // avoid compiler hints
   347     TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h);
   341     TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h);
   348     if w<8 then
   342     if w<8 then
   349         w:= 8;
   343         w:= 8;
   350     j:= 0;
   344     j:= 0;