hedgewars/uRenderUtils.pas
changeset 10015 4feced261c68
parent 9998 736015b847e3
parent 9127 e350500c4edb
child 10040 4ac87acbaed9
equal deleted inserted replaced
10014:56d2f2d5aad8 10015:4feced261c68
    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);
   180 var numFramesFirstCol, row, col: LongInt;
   189 var numFramesFirstCol, row, col: LongInt;
   181 begin
   190 begin
   182     numFramesFirstCol:= SpritesData[sprite].imageHeight div SpritesData[sprite].Height;
   191     numFramesFirstCol:= SpritesData[sprite].imageHeight div SpritesData[sprite].Height;
   183     row:= Frame mod numFramesFirstCol;
   192     row:= Frame mod numFramesFirstCol;
   184     col:= Frame div numFramesFirstCol;
   193     col:= Frame div numFramesFirstCol;
   185     
   194 
   186     copyToXYFromRect(SpritesData[sprite].Surface, dest, 
   195     copyToXYFromRect(SpritesData[sprite].Surface, dest,
   187              col*SpritesData[sprite].Width, 
   196              col*SpritesData[sprite].Width,
   188              row*SpritesData[sprite].Height, 
   197              row*SpritesData[sprite].Height,
   189              SpritesData[sprite].Width, 
   198              SpritesData[sprite].Width,
   190              spritesData[sprite].Height, 
   199              spritesData[sprite].Height,
   191              x,y);
   200              x,y);
   192 end;
   201 end;
   193 
   202 
   194 procedure DrawLine2Surf(dest: PSDL_Surface; x0, y0,x1,y1: LongInt; r,g,b: byte);
   203 procedure DrawLine2Surf(dest: PSDL_Surface; x0, y0,x1,y1: LongInt; r,g,b: byte);
   195 var
   204 var
   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;
   207     if y0 < y1 then sy:= 1 else sy:= -1;
   219     if y0 < y1 then sy:= 1 else sy:= -1;
   208     err:= dx-dy; 
   220     err:= dx-dy;
   209 
   221 
   210     while(true) do
   222     while(true) do
   211         begin
   223         begin
   212         destPixels^[(y0 * yMax) + x0]:= SDL_MapRGB(dest^.format, r,g,b); //But will it blend? no
   224         destPixels^[(y0 * yMax) + x0]:= SDL_MapRGB(dest^.format, r,g,b); //But will it blend? no
   213 
   225 
   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;
   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.