hedgewars/uRenderUtils.pas
branchwebgl
changeset 8026 4a4f21070479
parent 7546 b50556f2a0e8
child 9127 e350500c4edb
equal deleted inserted replaced
8023:7de85783b823 8026:4a4f21070479
    66     SDL_FillRect(Surface, @r, FillColor);
    66     SDL_FillRect(Surface, @r, FillColor);
    67     r.x:= rect^.x + 1;
    67     r.x:= rect^.x + 1;
    68     r.y:= rect^.y + 2;
    68     r.y:= rect^.y + 2;
    69     r.w:= rect^.w - 2;
    69     r.w:= rect^.w - 2;
    70     r.h:= rect^.h - 4;
    70     r.h:= rect^.h - 4;
    71     SDL_FillRect(Surface, @r, FillColor)
    71     SDL_FillRect(Surface, @r, FillColor);
    72 end;
    72 end;
    73 (*
    73 (*
    74 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;
    75 begin
    75 begin
    76     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
    76     WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0);
   113 var y, x, i, j: LongInt;
   113 var y, x, i, j: LongInt;
   114     tmpPixel: Longword;
   114     tmpPixel: Longword;
   115     pixels: PLongWordArray;
   115     pixels: PLongWordArray;
   116 begin
   116 begin
   117     TryDo(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true);
   117     TryDo(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true);
       
   118     SDL_LockSurface(Surface);
   118     pixels:= Surface^.pixels;
   119     pixels:= Surface^.pixels;
   119     if Vertical then
   120     if Vertical then
   120     for y := 0 to (Surface^.h div 2) - 1 do
   121     for y := 0 to (Surface^.h div 2) - 1 do
   121         for x := 0 to Surface^.w - 1 do
   122         for x := 0 to Surface^.w - 1 do
   122             begin
   123             begin
   134             j:= y*Surface^.w + (Surface^.w - x - 1);
   135             j:= y*Surface^.w + (Surface^.w - x - 1);
   135             tmpPixel:= pixels^[i];
   136             tmpPixel:= pixels^[i];
   136             pixels^[i]:= pixels^[j];
   137             pixels^[i]:= pixels^[j];
   137             pixels^[j]:= tmpPixel;
   138             pixels^[j]:= tmpPixel;
   138             end;
   139             end;
       
   140     SDL_UnlockSurface(Surface);
   139 end;
   141 end;
   140 
   142 
   141 procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline;
   143 procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline;
   142 begin
   144 begin
   143     copyToXYFromRect(src, dest, 0, 0, src^.w, src^.h, destX, destY);
   145     copyToXYFromRect(src, dest, 0, 0, src^.w, src^.h, destX, destY);
   148     srcPixels, destPixels: PLongWordArray;
   150     srcPixels, destPixels: PLongWordArray;
   149     r0, g0, b0, a0, r1, g1, b1, a1: Byte;
   151     r0, g0, b0, a0, r1, g1, b1, a1: Byte;
   150 begin
   152 begin
   151     maxDest:= (dest^.pitch div 4) * dest^.h;
   153     maxDest:= (dest^.pitch div 4) * dest^.h;
   152     maxSrc:= (src^.pitch div 4) * src^.h;
   154     maxSrc:= (src^.pitch div 4) * src^.h;
       
   155 
       
   156     SDL_LockSurface(src);
       
   157     SDL_LockSurface(dest);
       
   158 
   153     srcPixels:= src^.pixels;
   159     srcPixels:= src^.pixels;
   154     destPixels:= dest^.pixels;
   160     destPixels:= dest^.pixels;
   155 
   161 
   156     for iX:= 0 to srcW - 1 do
   162     for iX:= 0 to srcW - 1 do
   157     for iY:= 0 to srcH - 1 do
   163     for iY:= 0 to srcH - 1 do
   167             b0:= (b0 * (255 - LongInt(a1)) + b1 * LongInt(a1)) div 255;
   173             b0:= (b0 * (255 - LongInt(a1)) + b1 * LongInt(a1)) div 255;
   168             a0:= (a0 * (255 - LongInt(a1)) + a1 * LongInt(a1)) div 255;
   174             a0:= (a0 * (255 - LongInt(a1)) + a1 * LongInt(a1)) div 255;
   169             destPixels^[i]:= SDL_MapRGBA(dest^.format, r0, g0, b0, a0);
   175             destPixels^[i]:= SDL_MapRGBA(dest^.format, r0, g0, b0, a0);
   170             end;
   176             end;
   171         end;
   177         end;
       
   178 
       
   179     SDL_UnlockSurface(src);
       
   180     SDL_UnlockSurface(dest);
   172 end;
   181 end;
   173 
   182 
   174 procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline;
   183 procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline;
   175 begin
   184 begin
   176     DrawSpriteFrame2Surf(sprite, dest, x, y, 0);
   185     DrawSpriteFrame2Surf(sprite, dest, x, y, 0);
   197     yMax: LongInt;
   206     yMax: LongInt;
   198     destPixels: PLongwordArray;
   207     destPixels: PLongwordArray;
   199 begin
   208 begin
   200     //max:= (dest^.pitch div 4) * dest^.h;
   209     //max:= (dest^.pitch div 4) * dest^.h;
   201     yMax:= dest^.pitch div 4;
   210     yMax:= dest^.pitch div 4;
       
   211 
       
   212     SDL_LockSurface(dest);
       
   213 
   202     destPixels:= dest^.pixels;
   214     destPixels:= dest^.pixels;
   203 
   215 
   204     dx:= abs(x1-x0);
   216     dx:= abs(x1-x0);
   205     dy:= abs(y1-y0);
   217     dy:= abs(y1-y0);
   206     if x0 < x1 then sx:= 1 else sx:= -1;
   218     if x0 < x1 then sx:= 1 else sx:= -1;
   223         if e2 < dx then
   235         if e2 < dx then
   224             begin
   236             begin
   225             err:= err + dx;
   237             err:= err + dx;
   226             y0:=y0+sy
   238             y0:=y0+sy
   227             end;
   239             end;
   228         end; 
   240         end;
       
   241     SDL_UnlockSurface(dest);
   229 end;
   242 end;
   230 
   243 
   231 procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL, apparently
   244 procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL, apparently
   232 var y, x, i, j: LongInt;
   245 var y, x, i, j: LongInt;
   233     srcPixels, destPixels: PLongWordArray;
   246     srcPixels, destPixels: PLongWordArray;
   234 begin
   247 begin
   235     TryDo(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
   248     TryDo(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
   236     TryDo(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
   249     TryDo(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true);
   237 
   250 
       
   251     SDL_LockSurface(src);
       
   252     SDL_LockSurface(dest);
       
   253 
   238     srcPixels:= src^.pixels;
   254     srcPixels:= src^.pixels;
   239     destPixels:= dest^.pixels;
   255     destPixels:= dest^.pixels;
   240 
   256 
   241     j:= 0;
   257     j:= 0;
   242     for x := 0 to src^.w - 1 do
   258     for x := 0 to src^.w - 1 do
   244             begin
   260             begin
   245             i:= (src^.h - 1 - y) * (src^.pitch div 4) + x;
   261             i:= (src^.h - 1 - y) * (src^.pitch div 4) + x;
   246             destPixels^[j]:= srcPixels^[i];
   262             destPixels^[j]:= srcPixels^[i];
   247             inc(j)
   263             inc(j)
   248             end;
   264             end;
       
   265 
       
   266     SDL_UnlockSurface(src);
       
   267     SDL_UnlockSurface(dest);
       
   268 
   249 end;
   269 end;
   250 
   270 
   251 function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
   271 function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture;
   252 begin
   272 begin
   253     RenderStringTex:= RenderStringTexLim(s, Color, font, 0);
   273     RenderStringTex:= RenderStringTexLim(s, Color, font, 0);
   280 
   300 
   281 function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
   301 function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture;
   282 var textWidth, textHeight, x, y, w, h, i, j, pos, prevpos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt;
   302 var textWidth, textHeight, x, y, w, h, i, j, pos, prevpos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt;
   283     finalSurface, tmpsurf, rotatedEdge: PSDL_Surface;
   303     finalSurface, tmpsurf, rotatedEdge: PSDL_Surface;
   284     rect: TSDL_Rect;
   304     rect: TSDL_Rect;
   285     chars: set of char = [#9,' ',';',':','?','!',','];
   305     //chars: set of char = [#9,' ',';',':','?','!',','];
   286     substr: shortstring;
   306     substr: shortstring;
   287     edge, corner, tail: TSPrite;
   307     edge, corner, tail: TSPrite;
   288 begin
   308 begin
   289     case SpeechType of
   309       case SpeechType of
   290         1: begin;
   310         1: begin;
   291         edge:= sprSpeechEdge;
   311         edge:= sprSpeechEdge;
   292         corner:= sprSpeechCorner;
   312         corner:= sprSpeechCorner;
   293         tail:= sprSpeechTail;
   313         tail:= sprSpeechTail;
   294         end;
   314         end;
   324     j:= 0;
   344     j:= 0;
   325     if (length(s) > 20) then
   345     if (length(s) > 20) then
   326         begin
   346         begin
   327         w:= 0;
   347         w:= 0;
   328         i:= round(Sqrt(length(s)) * 2);
   348         i:= round(Sqrt(length(s)) * 2);
   329         s:= WrapText(s, #1, chars, i);
   349        // s:= WrapText(s, #1, chars, i);
   330         pos:= 1; prevpos:= 0; line:= 0;
   350         pos:= 1; prevpos:= 0; line:= 0;
   331     // Find the longest line for the purposes of centring the text.  Font dependant.
   351     // Find the longest line for the purposes of centring the text.  Font dependant.
   332         while pos <= length(s) do
   352         while pos <= length(s) do
   333             begin
   353             begin
   334             if (s[pos] = #1) or (pos = length(s)) then
   354             if (s[pos] = #1) or (pos = length(s)) then
   461 
   481 
   462     RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true);
   482     RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true);
   463 
   483 
   464     SDL_FreeSurface(rotatedEdge);
   484     SDL_FreeSurface(rotatedEdge);
   465     SDL_FreeSurface(finalSurface);
   485     SDL_FreeSurface(finalSurface);
       
   486 
   466 end;
   487 end;
   467 
   488 
   468 end.
   489 end.