author | unc0rr |
Tue, 01 May 2012 19:28:30 +0400 | |
changeset 6978 | 1dc33050149d |
parent 6750 | 5eb087708fbd |
child 6982 | 8d41d22a291d |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
4976 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
4380 | 19 |
{$INCLUDE "options.inc"} |
4976 | 20 |
|
4380 | 21 |
unit uRenderUtils; |
22 |
||
23 |
interface |
|
24 |
uses SDLh, uTypes; |
|
25 |
||
26 |
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean); |
|
27 |
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL |
|
28 |
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); |
|
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
29 |
procedure copyToXY(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
30 |
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: LongInt = 0); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
31 |
procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte); |
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
32 |
function RenderStringTex(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord = 0): PTexture; |
4380 | 33 |
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture; |
34 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
|
35 |
||
36 |
implementation |
|
4404 | 37 |
uses uUtils, uVariables, uConsts, uTextures, sysutils, uDebug; |
4380 | 38 |
|
39 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
|
40 |
var r: TSDL_Rect; |
|
41 |
begin |
|
42 |
r:= rect^; |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
43 |
if Clear then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
44 |
SDL_FillRect(Surface, @r, 0); |
4380 | 45 |
|
46 |
BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF); |
|
47 |
FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF); |
|
48 |
||
49 |
r.y:= rect^.y + 1; |
|
50 |
r.h:= rect^.h - 2; |
|
51 |
SDL_FillRect(Surface, @r, BorderColor); |
|
52 |
r.x:= rect^.x + 1; |
|
53 |
r.w:= rect^.w - 2; |
|
54 |
r.y:= rect^.y; |
|
55 |
r.h:= rect^.h; |
|
56 |
SDL_FillRect(Surface, @r, BorderColor); |
|
57 |
r.x:= rect^.x + 2; |
|
58 |
r.y:= rect^.y + 1; |
|
59 |
r.w:= rect^.w - 4; |
|
60 |
r.h:= rect^.h - 2; |
|
61 |
SDL_FillRect(Surface, @r, FillColor); |
|
62 |
r.x:= rect^.x + 1; |
|
63 |
r.y:= rect^.y + 2; |
|
64 |
r.w:= rect^.w - 2; |
|
65 |
r.h:= rect^.h - 4; |
|
66 |
SDL_FillRect(Surface, @r, FillColor) |
|
67 |
end; |
|
68 |
||
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
69 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord = 0): TSDL_Rect; |
4380 | 70 |
var w, h: LongInt; |
71 |
tmpsurf: PSDL_Surface; |
|
72 |
clr: TSDL_Color; |
|
6750 | 73 |
finalRect, textRect: TSDL_Rect; |
4380 | 74 |
begin |
6286
835392304f81
and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents:
4976
diff
changeset
|
75 |
TTF_SizeUTF8(Fontz[Font].Handle, Str2PChar(s), @w, @h); |
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
76 |
if (maxLength <> 0) and (w > maxLength) then w := maxLength; |
4380 | 77 |
finalRect.x:= X; |
78 |
finalRect.y:= Y; |
|
79 |
finalRect.w:= w + FontBorder * 2 + 4; |
|
80 |
finalRect.h:= h + FontBorder * 2; |
|
6750 | 81 |
textRect.x:= X; |
82 |
textRect.y:= Y; |
|
83 |
textRect.w:= w; |
|
84 |
textRect.h:= h; |
|
4380 | 85 |
DrawRoundRect(@finalRect, cWhiteColor, endian(cNearBlackColorChannels.value), Surface, true); |
86 |
clr.r:= (Color shr 16) and $FF; |
|
87 |
clr.g:= (Color shr 8) and $FF; |
|
88 |
clr.b:= Color and $FF; |
|
89 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr); |
|
90 |
finalRect.x:= X + FontBorder + 2; |
|
91 |
finalRect.y:= Y + FontBorder; |
|
92 |
SDLTry(tmpsurf <> nil, true); |
|
6750 | 93 |
SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect); |
4380 | 94 |
SDL_FreeSurface(tmpsurf); |
95 |
finalRect.x:= X; |
|
96 |
finalRect.y:= Y; |
|
97 |
finalRect.w:= w + FontBorder * 2 + 4; |
|
98 |
finalRect.h:= h + FontBorder * 2; |
|
99 |
WriteInRoundRect:= finalRect; |
|
100 |
end; |
|
101 |
||
102 |
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean); |
|
103 |
var y, x, i, j: LongInt; |
|
104 |
tmpPixel: Longword; |
|
105 |
pixels: PLongWordArray; |
|
106 |
begin |
|
107 |
TryDo(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true); |
|
108 |
pixels:= Surface^.pixels; |
|
109 |
if Vertical then |
|
110 |
for y := 0 to (Surface^.h div 2) - 1 do |
|
111 |
for x := 0 to Surface^.w - 1 do |
|
112 |
begin |
|
113 |
i:= y * Surface^.w + x; |
|
114 |
j:= (Surface^.h - y - 1) * Surface^.w + x; |
|
115 |
tmpPixel:= pixels^[i]; |
|
116 |
pixels^[i]:= pixels^[j]; |
|
117 |
pixels^[j]:= tmpPixel; |
|
118 |
end |
|
119 |
else |
|
120 |
for x := 0 to (Surface^.w div 2) - 1 do |
|
121 |
for y := 0 to Surface^.h -1 do |
|
122 |
begin |
|
123 |
i:= y*Surface^.w + x; |
|
124 |
j:= y*Surface^.w + (Surface^.w - x - 1); |
|
125 |
tmpPixel:= pixels^[i]; |
|
126 |
pixels^[i]:= pixels^[j]; |
|
127 |
pixels^[j]:= tmpPixel; |
|
128 |
end; |
|
129 |
end; |
|
130 |
||
131 |
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); |
|
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
132 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
133 |
copyToXY(src, dest, 0,0,src^.w, src^.h, destX, destY); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
134 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
135 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
136 |
procedure copyToXY(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
137 |
var i, j, maxDest, maxSrc, iX, iY: LongInt; |
4380 | 138 |
srcPixels, destPixels: PLongWordArray; |
139 |
r0, g0, b0, a0, r1, g1, b1, a1: Byte; |
|
140 |
begin |
|
141 |
maxDest:= (dest^.pitch div 4) * dest^.h; |
|
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
142 |
maxSrc:= (src^.pitch div 4) * src^.h; |
4380 | 143 |
srcPixels:= src^.pixels; |
144 |
destPixels:= dest^.pixels; |
|
145 |
||
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
146 |
for iX:= 0 to srcW - 1 do |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
147 |
for iY:= 0 to srcH - 1 do |
4380 | 148 |
begin |
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
149 |
i:= (destY + iY) * (dest^.pitch div 4) + (destX + iX); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
150 |
j:= (srcY + iY) * (src^.pitch div 4) + (srcX + iX); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
151 |
if (i < maxDest) and (j < maxSrc) and (srcPixels^[j] and AMask <> 0) then |
4380 | 152 |
begin |
153 |
SDL_GetRGBA(destPixels^[i], dest^.format, @r0, @g0, @b0, @a0); |
|
154 |
SDL_GetRGBA(srcPixels^[j], src^.format, @r1, @g1, @b1, @a1); |
|
155 |
r0:= (r0 * (255 - LongInt(a1)) + r1 * LongInt(a1)) div 255; |
|
156 |
g0:= (g0 * (255 - LongInt(a1)) + g1 * LongInt(a1)) div 255; |
|
157 |
b0:= (b0 * (255 - LongInt(a1)) + b1 * LongInt(a1)) div 255; |
|
158 |
a0:= (a0 * (255 - LongInt(a1)) + a1 * LongInt(a1)) div 255; |
|
159 |
destPixels^[i]:= SDL_MapRGBA(dest^.format, r0, g0, b0, a0); |
|
160 |
end; |
|
161 |
end; |
|
162 |
end; |
|
163 |
||
6620
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
164 |
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y,frame: LongInt); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
165 |
var numFramesFirstCol, row, col: LongInt; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
166 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
167 |
numFramesFirstCol:= SpritesData[sprite].imageHeight div SpritesData[sprite].Height; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
168 |
row:= Frame mod numFramesFirstCol; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
169 |
col:= Frame div numFramesFirstCol; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
170 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
171 |
copyToXY(SpritesData[sprite].Surface, dest, |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
172 |
col*SpritesData[sprite].Width, |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
173 |
row*SpritesData[sprite].Height, |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
174 |
SpritesData[sprite].Width, |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
175 |
spritesData[sprite].Height, |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
176 |
x,y); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
177 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
178 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
179 |
procedure DrawLine2Surf(dest: PSDL_Surface; x0, y0,x1,y1: LongInt; r,g,b: byte); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
180 |
var |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
181 |
max: LongInt; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
182 |
dx,dy,err,e2,sx,sy: LongInt; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
183 |
yMax: LongInt; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
184 |
destPixels: PLongwordArray; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
185 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
186 |
max:= (dest^.pitch div 4) * dest^.h; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
187 |
yMax:= dest^.pitch div 4; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
188 |
destPixels:= dest^.pixels; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
189 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
190 |
dx:= abs(x1-x0); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
191 |
dy:= abs(y1-y0); |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
192 |
if x0 < x1 then sx:= 1 else sx:= -1; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
193 |
if y0 < y1 then sy:= 1 else sy:= -1; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
194 |
err:= dx-dy; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
195 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
196 |
while(true) do |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
197 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
198 |
destPixels^[(y0 * yMax) + x0]:= SDL_MapRGB(dest^.format, r,g,b); //But will it blend? no |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
199 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
200 |
if (x0 = x1) and (y0 = y1) then break; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
201 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
202 |
e2:= 2*err; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
203 |
if e2 > -dy then |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
204 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
205 |
err:= err - dy; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
206 |
x0 := x0 + sx; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
207 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
208 |
|
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
209 |
if e2 < dx then |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
210 |
begin |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
211 |
err:= err + dx; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
212 |
y0:=y0+sy |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
213 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
214 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
215 |
end; |
b211d0b690de
Expanded copyToXY, it doesn't copy the whole src sprite, srcX/Y to srcW/h, added DrawSprite2Surf and DrawLine2Surf
Xeli
parents:
6580
diff
changeset
|
216 |
|
4380 | 217 |
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL, apparently |
218 |
var y, x, i, j: LongInt; |
|
219 |
srcPixels, destPixels: PLongWordArray; |
|
220 |
begin |
|
221 |
TryDo(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true); |
|
222 |
TryDo(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true); |
|
223 |
||
224 |
srcPixels:= src^.pixels; |
|
225 |
destPixels:= dest^.pixels; |
|
226 |
||
227 |
j:= 0; |
|
228 |
for x := 0 to src^.w - 1 do |
|
229 |
for y := 0 to src^.h - 1 do |
|
230 |
begin |
|
231 |
i:= (src^.h - 1 - y) * (src^.pitch div 4) + x; |
|
232 |
destPixels^[j]:= srcPixels^[i]; |
|
233 |
inc(j) |
|
234 |
end; |
|
235 |
end; |
|
236 |
||
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
237 |
function RenderStringTex(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord = 0): PTexture; |
4380 | 238 |
var w, h: LongInt; |
239 |
finalSurface: PSDL_Surface; |
|
240 |
begin |
|
241 |
if length(s) = 0 then s:= ' '; |
|
242 |
font:= CheckCJKFont(s, font); |
|
243 |
w:= 0; h:= 0; // avoid compiler hints |
|
6286
835392304f81
and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents:
4976
diff
changeset
|
244 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h); |
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
245 |
if (maxLength <> 0) and (w > maxLength) then w := maxLength; |
4380 | 246 |
|
247 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + FontBorder * 2 + 4, h + FontBorder * 2, |
|
248 |
32, RMask, GMask, BMask, AMask); |
|
249 |
||
250 |
TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true); |
|
251 |
||
6749
6bb0cea803bd
Clamp name/team name textures to avoid long team / hog name abuse. Limit width in px instead of chars to avoid unicode issues.
nemo
parents:
6700
diff
changeset
|
252 |
WriteInRoundRect(finalSurface, 0, 0, Color, font, s, maxLength); |
4380 | 253 |
|
254 |
TryDo(SDL_SetColorKey(finalSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
|
255 |
||
256 |
RenderStringTex:= Surface2Tex(finalSurface, false); |
|
257 |
||
258 |
SDL_FreeSurface(finalSurface); |
|
259 |
end; |
|
260 |
||
261 |
||
262 |
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture; |
|
263 |
var textWidth, textHeight, x, y, w, h, i, j, pos, prevpos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt; |
|
264 |
finalSurface, tmpsurf, rotatedEdge: PSDL_Surface; |
|
265 |
rect: TSDL_Rect; |
|
4611 | 266 |
chars: set of char = [#9,' ',';',':','?','!',',']; |
4380 | 267 |
substr: shortstring; |
268 |
edge, corner, tail: TSPrite; |
|
269 |
begin |
|
270 |
case SpeechType of |
|
271 |
1: begin; |
|
272 |
edge:= sprSpeechEdge; |
|
273 |
corner:= sprSpeechCorner; |
|
274 |
tail:= sprSpeechTail; |
|
275 |
end; |
|
276 |
2: begin; |
|
277 |
edge:= sprThoughtEdge; |
|
278 |
corner:= sprThoughtCorner; |
|
279 |
tail:= sprThoughtTail; |
|
280 |
end; |
|
281 |
3: begin; |
|
282 |
edge:= sprShoutEdge; |
|
283 |
corner:= sprShoutCorner; |
|
284 |
tail:= sprShoutTail; |
|
285 |
end; |
|
286 |
end; |
|
287 |
edgeHeight:= SpritesData[edge].Height; |
|
288 |
edgeWidth:= SpritesData[edge].Width; |
|
289 |
cornerWidth:= SpritesData[corner].Width; |
|
290 |
cornerHeight:= SpritesData[corner].Height; |
|
291 |
// This one screws up WrapText |
|
292 |
//s:= 'This is the song that never ends. ''cause it goes on and on my friends. Some people, started singing it not knowing what it was. And they''ll just go on singing it forever just because... This is the song that never ends...'; |
|
293 |
// This one does not |
|
294 |
//s:= 'This is the song that never ends. cause it goes on and on my friends. Some people, started singing it not knowing what it was. And they will go on singing it forever just because... This is the song that never ends... '; |
|
295 |
||
296 |
numLines:= 0; |
|
297 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
298 |
if length(s) = 0 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
299 |
s:= '...'; |
4380 | 300 |
font:= CheckCJKFont(s, font); |
301 |
w:= 0; h:= 0; // avoid compiler hints |
|
6286
835392304f81
and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents:
4976
diff
changeset
|
302 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(s), @w, @h); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
303 |
if w<8 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
304 |
w:= 8; |
4380 | 305 |
j:= 0; |
306 |
if (length(s) > 20) then |
|
307 |
begin |
|
308 |
w:= 0; |
|
309 |
i:= round(Sqrt(length(s)) * 2); |
|
310 |
s:= WrapText(s, #1, chars, i); |
|
311 |
pos:= 1; prevpos:= 0; line:= 0; |
|
312 |
// Find the longest line for the purposes of centring the text. Font dependant. |
|
313 |
while pos <= length(s) do |
|
314 |
begin |
|
315 |
if (s[pos] = #1) or (pos = length(s)) then |
|
316 |
begin |
|
317 |
inc(numlines); |
|
318 |
if s[pos] <> #1 then inc(pos); |
|
319 |
while s[prevpos+1] = ' ' do inc(prevpos); |
|
320 |
substr:= copy(s, prevpos+1, pos-prevpos-1); |
|
321 |
i:= 0; j:= 0; |
|
6286
835392304f81
and while we are giving SDLh.pas all this love, let's fix the signature of one SDL_ttf calls
koda
parents:
4976
diff
changeset
|
322 |
TTF_SizeUTF8(Fontz[font].Handle, Str2PChar(substr), @i, @j); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
323 |
if i > w then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
324 |
w:= i; |
4380 | 325 |
prevpos:= pos; |
326 |
end; |
|
327 |
inc(pos); |
|
328 |
end; |
|
329 |
end |
|
330 |
else numLines := 1; |
|
331 |
||
332 |
textWidth:=((w-(cornerWidth-edgeWidth)*2) div edgeWidth)*edgeWidth+edgeWidth; |
|
333 |
textHeight:=(((numlines * h + 2)-((cornerHeight-edgeWidth)*2)) div edgeWidth)*edgeWidth; |
|
334 |
||
335 |
textHeight:=max(textHeight,edgeWidth); |
|
336 |
//textWidth:=max(textWidth,SpritesData[tail].Width); |
|
337 |
rect.x:= 0; |
|
338 |
rect.y:= 0; |
|
339 |
rect.w:= textWidth + (cornerWidth * 2); |
|
340 |
rect.h:= textHeight + cornerHeight*2 - edgeHeight + SpritesData[tail].Height; |
|
341 |
//s:= inttostr(w) + ' ' + inttostr(numlines) + ' ' + inttostr(rect.x) + ' '+inttostr(rect.y) + ' ' + inttostr(rect.w) + ' ' + inttostr(rect.h); |
|
342 |
||
343 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, RMask, GMask, BMask, AMask); |
|
344 |
||
345 |
TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true); |
|
346 |
||
347 |
//////////////////////////////// CORNERS /////////////////////////////// |
|
348 |
copyToXY(SpritesData[corner].Surface, finalSurface, 0, 0); /////////////////// NW |
|
349 |
||
350 |
flipSurface(SpritesData[corner].Surface, true); // store all 4 versions in memory to avoid repeated flips? |
|
351 |
x:= 0; |
|
352 |
y:= textHeight + cornerHeight -1; |
|
353 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SW |
|
354 |
||
355 |
flipSurface(SpritesData[corner].Surface, false); |
|
356 |
x:= rect.w-cornerWidth-1; |
|
357 |
y:= textHeight + cornerHeight -1; |
|
358 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SE |
|
359 |
||
360 |
flipSurface(SpritesData[corner].Surface, true); |
|
361 |
x:= rect.w-cornerWidth-1; |
|
362 |
y:= 0; |
|
363 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// NE |
|
364 |
flipSurface(SpritesData[corner].Surface, false); // restore original position |
|
365 |
//////////////////////////////// END CORNERS /////////////////////////////// |
|
366 |
||
367 |
//////////////////////////////// EDGES ////////////////////////////////////// |
|
368 |
x:= cornerWidth; |
|
369 |
y:= 0; |
|
370 |
while x < rect.w-cornerWidth-1 do |
|
371 |
begin |
|
372 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// top edge |
|
373 |
inc(x,edgeWidth); |
|
374 |
end; |
|
375 |
flipSurface(SpritesData[edge].Surface, true); |
|
376 |
x:= cornerWidth; |
|
377 |
y:= textHeight + cornerHeight*2 - edgeHeight-1; |
|
378 |
while x < rect.w-cornerWidth-1 do |
|
379 |
begin |
|
380 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// bottom edge |
|
381 |
inc(x,edgeWidth); |
|
382 |
end; |
|
383 |
flipSurface(SpritesData[edge].Surface, true); // restore original position |
|
384 |
||
385 |
rotatedEdge:= SDL_CreateRGBSurface(SDL_SWSURFACE, edgeHeight, edgeWidth, 32, RMask, GMask, BMask, AMask); |
|
386 |
x:= rect.w - edgeHeight - 1; |
|
387 |
y:= cornerHeight; |
|
388 |
//// initially was going to rotate in place, but the SDL spec claims width/height are read only |
|
389 |
copyRotatedSurface(SpritesData[edge].Surface,rotatedEdge); |
|
390 |
while y < textHeight + cornerHeight do |
|
391 |
begin |
|
392 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
393 |
inc(y,edgeWidth); |
|
394 |
end; |
|
395 |
flipSurface(rotatedEdge, false); // restore original position |
|
396 |
x:= 0; |
|
397 |
y:= cornerHeight; |
|
398 |
while y < textHeight + cornerHeight do |
|
399 |
begin |
|
400 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
401 |
inc(y,edgeWidth); |
|
402 |
end; |
|
403 |
//////////////////////////////// END EDGES ////////////////////////////////////// |
|
404 |
||
405 |
x:= cornerWidth; |
|
406 |
y:= textHeight + cornerHeight * 2 - edgeHeight - 1; |
|
407 |
copyToXY(SpritesData[tail].Surface, finalSurface, x, y); |
|
408 |
||
409 |
rect.x:= edgeHeight; |
|
410 |
rect.y:= edgeHeight; |
|
411 |
rect.w:= rect.w - edgeHeight * 2; |
|
412 |
rect.h:= textHeight + cornerHeight * 2 - edgeHeight * 2; |
|
413 |
i:= rect.w; |
|
414 |
j:= rect.h; |
|
415 |
SDL_FillRect(finalSurface, @rect, cWhiteColor); |
|
416 |
||
417 |
pos:= 1; prevpos:= 0; line:= 0; |
|
418 |
while pos <= length(s) do |
|
419 |
begin |
|
420 |
if (s[pos] = #1) or (pos = length(s)) then |
|
421 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
422 |
if s[pos] <> #1 then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
423 |
inc(pos); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
424 |
while s[prevpos+1] = ' 'do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
425 |
inc(prevpos); |
4380 | 426 |
substr:= copy(s, prevpos+1, pos-prevpos-1); |
427 |
if Length(substr) <> 0 then |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
428 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
429 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(substr), cNearBlackColorChannels); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
430 |
rect.x:= edgeHeight + 1 + ((i - w) div 2); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
431 |
// trying to more evenly position the text, vertically |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
432 |
rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
433 |
SDLTry(tmpsurf <> nil, true); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
434 |
SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
435 |
SDL_FreeSurface(tmpsurf); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
436 |
inc(line); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
437 |
prevpos:= pos; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
438 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
439 |
end; |
4380 | 440 |
inc(pos); |
441 |
end; |
|
442 |
||
443 |
RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true); |
|
444 |
||
445 |
SDL_FreeSurface(rotatedEdge); |
|
446 |
SDL_FreeSurface(finalSurface); |
|
447 |
end; |
|
448 |
||
4611 | 449 |
end. |