author | Wuzzy <Wuzzy@disroot.org> |
Thu, 25 May 2023 17:46:57 +0200 | |
changeset 15987 | e8d94f84d294 |
parent 15874 | c4561095666a |
child 16001 | cee831693af1 |
permissions | -rw-r--r-- |
4976 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
10107
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
4976 | 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); |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
27 |
|
4380 | 28 |
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
29 |
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline; |
7013 | 30 |
procedure copyToXYFromRect(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt); |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
31 |
|
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
32 |
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline; |
7013 | 33 |
procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt; frame: 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
|
34 |
procedure DrawLine2Surf(dest: PSDL_Surface; x0,y0,x1,y1:LongInt; r,g,b: byte); |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
35 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
36 |
|
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
37 |
function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture; |
7013 | 38 |
function RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture; |
4380 | 39 |
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture; |
40 |
||
14757
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
41 |
function IsTooDarkToRead(TextColor: Longword): boolean; inline; |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
42 |
|
4380 | 43 |
implementation |
11821 | 44 |
uses uVariables, uConsts, uTextures, SysUtils, uUtils, uDebug; |
4380 | 45 |
|
46 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
|
47 |
var r: TSDL_Rect; |
|
48 |
begin |
|
49 |
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
|
50 |
if Clear then |
14655
b055360684bd
Remove black pixels at corners of DrawRoundRect rectangles
Wuzzy <Wuzzy2@mail.ru>
parents:
13490
diff
changeset
|
51 |
SDL_FillRect(Surface, @r, SDL_MapRGBA(Surface^.format, 0, 0, 0, 0)); |
4380 | 52 |
|
53 |
BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF); |
|
54 |
FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF); |
|
55 |
||
11836 | 56 |
r.y:= rect^.y + cFontBorder div 2; |
57 |
r.h:= rect^.h - cFontBorder; |
|
4380 | 58 |
SDL_FillRect(Surface, @r, BorderColor); |
11836 | 59 |
r.x:= rect^.x + cFontBorder div 2; |
60 |
r.w:= rect^.w - cFontBorder; |
|
4380 | 61 |
r.y:= rect^.y; |
62 |
r.h:= rect^.h; |
|
63 |
SDL_FillRect(Surface, @r, BorderColor); |
|
11836 | 64 |
r.x:= rect^.x + cFontBorder; |
65 |
r.y:= rect^.y + cFontBorder div 2; |
|
66 |
r.w:= rect^.w - cFontBorder * 2; |
|
67 |
r.h:= rect^.h - cFontBorder; |
|
4380 | 68 |
SDL_FillRect(Surface, @r, FillColor); |
11836 | 69 |
r.x:= rect^.x + cFontBorder div 2; |
70 |
r.y:= rect^.y + cFontBorder; |
|
71 |
r.w:= rect^.w - cFontBorder; |
|
72 |
r.h:= rect^.h - cFontBorder * 2; |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
73 |
SDL_FillRect(Surface, @r, FillColor); |
4380 | 74 |
end; |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
75 |
(* |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
76 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring): TSDL_Rect; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
77 |
begin |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
78 |
WriteInRoundRect:= WriteInRoundRect(Surface, X, Y, Color, Font, s, 0); |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
79 |
end;*) |
4380 | 80 |
|
14766
7cc768094d66
Refactor IsTooDarkToRead to fix pas2c crash
Wuzzy <Wuzzy2@mail.ru>
parents:
14757
diff
changeset
|
81 |
function IsTooDarkToRead(TextColor: LongWord): boolean; inline; |
14757
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
82 |
var clr: TSDL_Color; |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
83 |
begin |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
84 |
clr.r:= (TextColor shr 16) and $FF; |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
85 |
clr.g:= (TextColor shr 8) and $FF; |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
86 |
clr.b:= TextColor and $FF; |
14766
7cc768094d66
Refactor IsTooDarkToRead to fix pas2c crash
Wuzzy <Wuzzy2@mail.ru>
parents:
14757
diff
changeset
|
87 |
IsTooDarkToRead:= not ((clr.r >= cInvertTextColorAt) or (clr.g >= cInvertTextColorAt) or (clr.b >= cInvertTextColorAt)); |
14757
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
88 |
end; |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
89 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
90 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect; |
10494 | 91 |
var w, h: Longword; |
4380 | 92 |
tmpsurf: PSDL_Surface; |
14757
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
93 |
finalRect, textRect: TSDL_Rect; |
4380 | 94 |
clr: TSDL_Color; |
95 |
begin |
|
10127 | 96 |
TTF_SizeUTF8(Fontz[Font].Handle, PChar(s), @w, @h); |
11836 | 97 |
if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor; |
4380 | 98 |
finalRect.x:= X; |
99 |
finalRect.y:= Y; |
|
11836 | 100 |
finalRect.w:= w + cFontBorder * 2 + cFontPadding * 2; |
6982 | 101 |
finalRect.h:= h + cFontBorder * 2; |
6750 | 102 |
textRect.x:= X; |
103 |
textRect.y:= Y; |
|
104 |
textRect.w:= w; |
|
105 |
textRect.h:= h; |
|
4380 | 106 |
clr.r:= (Color shr 16) and $FF; |
107 |
clr.g:= (Color shr 8) and $FF; |
|
108 |
clr.b:= Color and $FF; |
|
14768
7dfc6ed13337
Fix uninitialized alpha values of rendered text
Wuzzy <Wuzzy2@mail.ru>
parents:
14766
diff
changeset
|
109 |
clr.a:= $FF; |
14766
7cc768094d66
Refactor IsTooDarkToRead to fix pas2c crash
Wuzzy <Wuzzy2@mail.ru>
parents:
14757
diff
changeset
|
110 |
if (not IsTooDarkToRead(Color)) then |
14757
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
111 |
DrawRoundRect(@finalRect, cWhiteColor, cNearBlackColor, Surface, true) |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
112 |
else |
8563cc40fc1e
Invert colors if clan color is very dark
Wuzzy <Wuzzy2@mail.ru>
parents:
14655
diff
changeset
|
113 |
DrawRoundRect(@finalRect, cNearBlackColor, cWhiteColor, Surface, true); |
10127 | 114 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(s), clr); |
11836 | 115 |
finalRect.x:= X + cFontBorder + cFontPadding; |
6982 | 116 |
finalRect.y:= Y + cFontBorder; |
11507 | 117 |
if SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then |
15775 | 118 |
exit(finalRect); |
6750 | 119 |
SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect); |
4380 | 120 |
SDL_FreeSurface(tmpsurf); |
121 |
finalRect.x:= X; |
|
122 |
finalRect.y:= Y; |
|
11836 | 123 |
finalRect.w:= w + cFontBorder * 2 + cFontPadding * 2; |
6982 | 124 |
finalRect.h:= h + cFontBorder * 2; |
4380 | 125 |
WriteInRoundRect:= finalRect; |
126 |
end; |
|
127 |
||
128 |
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean); |
|
129 |
var y, x, i, j: LongInt; |
|
130 |
tmpPixel: Longword; |
|
131 |
pixels: PLongWordArray; |
|
132 |
begin |
|
11532 | 133 |
if checkFails(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true) then |
134 |
exit; |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
135 |
SDL_LockSurface(Surface); |
4380 | 136 |
pixels:= Surface^.pixels; |
137 |
if Vertical then |
|
138 |
for y := 0 to (Surface^.h div 2) - 1 do |
|
139 |
for x := 0 to Surface^.w - 1 do |
|
140 |
begin |
|
141 |
i:= y * Surface^.w + x; |
|
142 |
j:= (Surface^.h - y - 1) * Surface^.w + x; |
|
143 |
tmpPixel:= pixels^[i]; |
|
144 |
pixels^[i]:= pixels^[j]; |
|
145 |
pixels^[j]:= tmpPixel; |
|
146 |
end |
|
147 |
else |
|
148 |
for x := 0 to (Surface^.w div 2) - 1 do |
|
11532 | 149 |
for y := 0 to Surface^.h - 1 do |
4380 | 150 |
begin |
151 |
i:= y*Surface^.w + x; |
|
152 |
j:= y*Surface^.w + (Surface^.w - x - 1); |
|
153 |
tmpPixel:= pixels^[i]; |
|
154 |
pixels^[i]:= pixels^[j]; |
|
155 |
pixels^[j]:= tmpPixel; |
|
156 |
end; |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
157 |
SDL_UnlockSurface(Surface); |
4380 | 158 |
end; |
159 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
160 |
procedure copyToXY(src, dest: PSDL_Surface; destX, destY: LongInt); inline; |
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
|
161 |
begin |
12098
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
162 |
// copy from complete src |
7013 | 163 |
copyToXYFromRect(src, dest, 0, 0, src^.w, src^.h, destX, destY); |
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 |
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
|
165 |
|
7013 | 166 |
procedure copyToXYFromRect(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, destX, destY: LongInt); |
12099 | 167 |
var spi, dpi, iX, iY, dX, dY, lX, lY, aT: LongInt; |
4380 | 168 |
srcPixels, destPixels: PLongWordArray; |
12099 | 169 |
rD, gD, bD, aD, rT, gT, bT: Byte; |
4380 | 170 |
begin |
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
171 |
SDL_LockSurface(src); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
172 |
SDL_LockSurface(dest); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
173 |
|
4380 | 174 |
srcPixels:= src^.pixels; |
175 |
destPixels:= dest^.pixels; |
|
176 |
||
12098
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
177 |
// what's the offset between src and dest coords? |
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
178 |
dX:= destX - srcX; |
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
179 |
dY:= destY - srcY; |
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
180 |
|
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
181 |
// let's figure out where the rectangle we can actually copy ends |
12101
2e70ef81e281
copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents:
12099
diff
changeset
|
182 |
lX:= min(srcX + srcW, src^.w) - 1; |
2e70ef81e281
copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents:
12099
diff
changeset
|
183 |
if lX + dx >= dest^.w then lX:= dest^.w - dx - 1; |
2e70ef81e281
copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents:
12099
diff
changeset
|
184 |
lY:= min(srcY + srcH, src^.h) - 1; |
2e70ef81e281
copyToXYFromRect: simplify my math (so that it actually, you know, works...)
sheepluva
parents:
12099
diff
changeset
|
185 |
if lY + dy >= dest^.h then lY:= dest^.h - dy - 1; |
12098
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
186 |
|
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
187 |
for iX:= srcX to lX do |
966a9739812f
copyToXYFromRect: fix pixels overflowing pixel lines in dest
sheepluva
parents:
11836
diff
changeset
|
188 |
for iY:= srcY to lY do |
4380 | 189 |
begin |
12099 | 190 |
// src pixel index |
15874
c4561095666a
Fix texture copying routine not taking pitch into account
unc0rr
parents:
15775
diff
changeset
|
191 |
spi:= iY * src^.pitch div 4 + iX; |
12099 | 192 |
// dest pixel index |
15874
c4561095666a
Fix texture copying routine not taking pitch into account
unc0rr
parents:
15775
diff
changeset
|
193 |
dpi:= (iY + dY) * dest^.pitch div 4 + (iX + dX); |
12099 | 194 |
|
195 |
// get src alpha (and set it as target alpha for now) |
|
196 |
aT:= (srcPixels^[spi] and AMask) shr AShift; |
|
197 |
||
198 |
// src pixel opaque? |
|
199 |
if aT = 255 then |
|
4380 | 200 |
begin |
12099 | 201 |
// just copy full pixel |
202 |
destPixels^[dpi]:= srcPixels^[spi]; |
|
203 |
continue; |
|
4380 | 204 |
end; |
12099 | 205 |
|
206 |
// get dst alpha (without shift for now) |
|
207 |
aD:= (destPixels^[dpi] and AMask) shr AShift; |
|
208 |
||
209 |
// dest completely transparent? |
|
210 |
if aD = 0 then |
|
211 |
begin |
|
212 |
// just copy src pixel |
|
213 |
destPixels^[dpi]:= srcPixels^[spi]; |
|
214 |
continue; |
|
215 |
end; |
|
216 |
||
217 |
// looks like some blending is necessary |
|
218 |
||
219 |
// set color of target RGB to src for now |
|
220 |
SDL_GetRGB(srcPixels^[spi], src^.format, @rT, @gT, @bT); |
|
221 |
SDL_GetRGB(destPixels^[dpi], dest^.format, @rD, @gD, @bD); |
|
222 |
// note: this is not how to correctly blend RGB, just sayin' (R,G,B are not linear...) |
|
223 |
rT:= (rD * (255 - aT) + rT * aT) div 255; |
|
224 |
gT:= (gD * (255 - aT) + gT * aT) div 255; |
|
225 |
bT:= (bD * (255 - aT) + bT * aT) div 255; |
|
226 |
aT:= aD + ((255 - LongInt(aD)) * aT div 255); |
|
227 |
||
12102
51596d30a724
fix chat SDL surfaces being in wrong color format (didn't play well with copyToXY's new quick pixel copies)
sheepluva
parents:
12101
diff
changeset
|
228 |
destPixels^[dpi]:= SDL_MapRGBA(dest^.format, rT, gT, bT, Byte(aT)); |
51596d30a724
fix chat SDL surfaces being in wrong color format (didn't play well with copyToXY's new quick pixel copies)
sheepluva
parents:
12101
diff
changeset
|
229 |
|
4380 | 230 |
end; |
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
231 |
|
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
232 |
SDL_UnlockSurface(src); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
233 |
SDL_UnlockSurface(dest); |
4380 | 234 |
end; |
235 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
236 |
procedure DrawSprite2Surf(sprite: TSprite; dest: PSDL_Surface; x,y: LongInt); inline; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
237 |
begin |
12099 | 238 |
DrawSpriteFrame2Surf(sprite, dest, x, y, 0); |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
239 |
end; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
240 |
|
7013 | 241 |
procedure DrawSpriteFrame2Surf(sprite: TSprite; dest: PSDL_Surface; x,y,frame: 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
|
242 |
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
|
243 |
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
|
244 |
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
|
245 |
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
|
246 |
col:= Frame div numFramesFirstCol; |
10015 | 247 |
|
248 |
copyToXYFromRect(SpritesData[sprite].Surface, dest, |
|
249 |
col*SpritesData[sprite].Width, |
|
250 |
row*SpritesData[sprite].Height, |
|
251 |
SpritesData[sprite].Width, |
|
252 |
spritesData[sprite].Height, |
|
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
|
253 |
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
|
254 |
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
|
255 |
|
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
|
256 |
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
|
257 |
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
|
258 |
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
|
259 |
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
|
260 |
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
|
261 |
begin |
6992 | 262 |
//max:= (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
|
263 |
yMax:= dest^.pitch div 4; |
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
264 |
|
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
265 |
SDL_LockSurface(dest); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
266 |
|
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
|
267 |
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
|
268 |
|
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
|
269 |
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
|
270 |
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
|
271 |
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
|
272 |
if y0 < y1 then sy:= 1 else sy:= -1; |
10015 | 273 |
err:= dx-dy; |
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
|
274 |
|
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
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
|
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
|
279 |
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
|
280 |
|
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
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
|
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
|
288 |
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
|
289 |
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
|
290 |
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
|
291 |
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
|
292 |
end; |
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
293 |
end; |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
294 |
SDL_UnlockSurface(dest); |
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
|
295 |
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
|
296 |
|
4380 | 297 |
procedure copyRotatedSurface(src, dest: PSDL_Surface); // this is necessary since width/height are read only in SDL, apparently |
298 |
var y, x, i, j: LongInt; |
|
299 |
srcPixels, destPixels: PLongWordArray; |
|
300 |
begin |
|
11532 | 301 |
checkFails(src^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true); |
302 |
checkFails(dest^.format^.BytesPerPixel = 4, 'rotateSurface failed, expecting 32 bit surface', true); |
|
303 |
if not allOK then exit; |
|
4380 | 304 |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
305 |
SDL_LockSurface(src); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
306 |
SDL_LockSurface(dest); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
307 |
|
4380 | 308 |
srcPixels:= src^.pixels; |
309 |
destPixels:= dest^.pixels; |
|
310 |
||
311 |
j:= 0; |
|
312 |
for x := 0 to src^.w - 1 do |
|
313 |
for y := 0 to src^.h - 1 do |
|
314 |
begin |
|
315 |
i:= (src^.h - 1 - y) * (src^.pitch div 4) + x; |
|
316 |
destPixels^[j]:= srcPixels^[i]; |
|
317 |
inc(j) |
|
318 |
end; |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
319 |
|
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
320 |
SDL_UnlockSurface(src); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
321 |
SDL_UnlockSurface(dest); |
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
322 |
|
4380 | 323 |
end; |
324 |
||
15987
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
325 |
{$IFNDEF PAS2C} |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
326 |
// Wraps the text s by inserting breakStr as newlines with |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
327 |
// maximum column length maxCol. |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
328 |
// Same as Pascal's WrapText, but without the annoying |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
329 |
// behavior that text enclosed in " and ' disables word-wrapping |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
330 |
function SimpleWrapText(s, breakStr: string; maxCol: integer): string; |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
331 |
var |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
332 |
breakChars: set of char = [#9,' ','-']; |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
333 |
begin |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
334 |
// escape the " and ' characters before calling WrapText |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
335 |
// using ASCII ESC control character |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
336 |
s:= StringReplace(s, '"', #27+'Q', [rfReplaceAll]); |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
337 |
s:= StringReplace(s, '''', #27+'q', [rfReplaceAll]); |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
338 |
|
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
339 |
s:= WrapText(s, #1, breakChars, maxCol); |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
340 |
|
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
341 |
// Undo the escapes |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
342 |
s:= StringReplace(s, #27+'Q', '"', [rfReplaceAll]); |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
343 |
s:= StringReplace(s, #27+'q', '''', [rfReplaceAll]); |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
344 |
SimpleWrapText:= s; |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
345 |
end; |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
346 |
{$ENDIF} |
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
347 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
348 |
function RenderStringTex(s: ansistring; Color: Longword; font: THWFont): PTexture; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
349 |
begin |
7013 | 350 |
RenderStringTex:= RenderStringTexLim(s, Color, font, 0); |
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
351 |
end; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
352 |
|
7013 | 353 |
function RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture; |
10494 | 354 |
var w, h: Longword; |
4380 | 355 |
finalSurface: PSDL_Surface; |
356 |
begin |
|
10139 | 357 |
if cOnlyStats then |
358 |
begin |
|
359 |
RenderStringTexLim:= nil; |
|
360 |
end |
|
361 |
else |
|
362 |
begin |
|
363 |
if length(s) = 0 then s:= _S' '; |
|
364 |
font:= CheckCJKFont(s, font); |
|
365 |
w:= 0; h:= 0; // avoid compiler hints |
|
366 |
TTF_SizeUTF8(Fontz[font].Handle, PChar(s), @w, @h); |
|
11836 | 367 |
if (maxLength > 0) and (w > maxLength * HDPIScaleFactor) then w := maxLength * HDPIScaleFactor; |
4380 | 368 |
|
11836 | 369 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + cFontBorder*2 + cFontPadding*2, h + cFontBorder * 2, |
10139 | 370 |
32, RMask, GMask, BMask, AMask); |
4380 | 371 |
|
11532 | 372 |
if checkFails(finalSurface <> nil, 'RenderString: fail to create surface', true) then |
373 |
exit(nil); |
|
10139 | 374 |
|
375 |
WriteInRoundRect(finalSurface, 0, 0, Color, font, s, maxLength); |
|
4380 | 376 |
|
12591 | 377 |
checkFails(SDL_SetColorKey(finalSurface, SDL_TRUE, 0) = 0, errmsgTransparentSet, false); |
4380 | 378 |
|
10139 | 379 |
RenderStringTexLim:= Surface2Tex(finalSurface, false); |
4380 | 380 |
|
10139 | 381 |
SDL_FreeSurface(finalSurface); |
382 |
end; |
|
4380 | 383 |
end; |
384 |
||
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
385 |
function GetNextSpeechLine(s: ansistring; ldelim: char; var startFrom: LongInt; out substr: ansistring): boolean; |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
386 |
var p, l, m, r: Integer; |
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
387 |
newl, skip: boolean; |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
388 |
c : char; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
389 |
begin |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
390 |
m:= Length(s); |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
391 |
|
10690 | 392 |
substr:= ''; |
393 |
||
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
394 |
SetLengthA(substr, m); |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
395 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
396 |
// number of chars read |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
397 |
r:= 0; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
398 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
399 |
// number of chars to be written |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
400 |
l:= 0; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
401 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
402 |
newl:= true; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
403 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
404 |
for p:= max(1, startFrom) to m do |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
405 |
begin |
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
406 |
|
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
407 |
inc(r); |
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
408 |
// read char from source string |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
409 |
c:= s[p]; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
410 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
411 |
// strip empty lines, spaces and newlines on beginnings of line |
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
412 |
skip:= ((newl or (p = m)) and ((c = ' ') or (c = ldelim))); |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
413 |
|
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
414 |
if (not skip) then |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
415 |
begin |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
416 |
newl:= (c = ldelim); |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
417 |
// stop if we went past the end of the line |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
418 |
if newl then |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
419 |
break; |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
420 |
|
10691
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
421 |
// copy current char to output substring |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
422 |
inc(l); |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
423 |
substr[l]:= c; |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
424 |
end; |
97f45f1374be
change speechfix implementation (no "continue" anymore
sheepluva
parents:
10690
diff
changeset
|
425 |
|
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
426 |
end; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
427 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
428 |
inc(startFrom, r); |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
429 |
|
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
430 |
SetLengthA(substr, l); |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
431 |
|
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
432 |
GetNextSpeechLine:= (l > 0); |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
433 |
end; |
4380 | 434 |
|
435 |
function RenderSpeechBubbleTex(s: ansistring; SpeechType: Longword; font: THWFont): PTexture; |
|
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
436 |
var textWidth, textHeight, x, y, w, h, i, j, pos, line, numLines, edgeWidth, edgeHeight, cornerWidth, cornerHeight: LongInt; |
4380 | 437 |
finalSurface, tmpsurf, rotatedEdge: PSDL_Surface; |
438 |
rect: TSDL_Rect; |
|
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
439 |
substr: ansistring; |
4380 | 440 |
edge, corner, tail: TSPrite; |
441 |
begin |
|
10139 | 442 |
if cOnlyStats then exit(nil); |
443 |
||
4380 | 444 |
case SpeechType of |
10142 | 445 |
1: begin |
10139 | 446 |
edge:= sprSpeechEdge; |
447 |
corner:= sprSpeechCorner; |
|
448 |
tail:= sprSpeechTail; |
|
449 |
end; |
|
10142 | 450 |
2: begin |
10139 | 451 |
edge:= sprThoughtEdge; |
452 |
corner:= sprThoughtCorner; |
|
453 |
tail:= sprThoughtTail; |
|
454 |
end; |
|
10142 | 455 |
3: begin |
10139 | 456 |
edge:= sprShoutEdge; |
457 |
corner:= sprShoutCorner; |
|
458 |
tail:= sprShoutTail; |
|
10142 | 459 |
end |
460 |
else |
|
461 |
exit(nil) |
|
4380 | 462 |
end; |
463 |
edgeHeight:= SpritesData[edge].Height; |
|
464 |
edgeWidth:= SpritesData[edge].Width; |
|
465 |
cornerWidth:= SpritesData[corner].Width; |
|
466 |
cornerHeight:= SpritesData[corner].Height; |
|
467 |
||
468 |
numLines:= 0; |
|
469 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
470 |
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
|
471 |
s:= '...'; |
4380 | 472 |
font:= CheckCJKFont(s, font); |
473 |
w:= 0; h:= 0; // avoid compiler hints |
|
10127 | 474 |
TTF_SizeUTF8(Fontz[font].Handle, PChar(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
|
475 |
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
|
476 |
w:= 8; |
4380 | 477 |
j:= 0; |
478 |
if (length(s) > 20) then |
|
479 |
begin |
|
480 |
w:= 0; |
|
481 |
i:= round(Sqrt(length(s)) * 2); |
|
10127 | 482 |
{$IFNDEF PAS2C} |
15987
e8d94f84d294
Fix speech bubble not wrapping if contain quote marks (bug 753)
Wuzzy <Wuzzy@disroot.org>
parents:
15874
diff
changeset
|
483 |
s:= SimpleWrapText(s, #1, i); |
10127 | 484 |
{$ENDIF} |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
485 |
pos:= 1; line:= 0; |
4380 | 486 |
// Find the longest line for the purposes of centring the text. Font dependant. |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
487 |
while GetNextSpeechLine(s, #1, pos, substr) do |
4380 | 488 |
begin |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
489 |
inc(numLines); |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
490 |
i:= 0; j:= 0; |
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
491 |
TTF_SizeUTF8(Fontz[font].Handle, PChar(substr), @i, @j); |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
492 |
if i > w then |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
493 |
w:= i; |
4380 | 494 |
end; |
495 |
end |
|
496 |
else numLines := 1; |
|
497 |
||
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
498 |
if numLines < 1 then |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
499 |
begin |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
500 |
s:= '...'; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
501 |
numLines:= 1; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
502 |
end; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
503 |
|
4380 | 504 |
textWidth:=((w-(cornerWidth-edgeWidth)*2) div edgeWidth)*edgeWidth+edgeWidth; |
505 |
textHeight:=(((numlines * h + 2)-((cornerHeight-edgeWidth)*2)) div edgeWidth)*edgeWidth; |
|
506 |
||
507 |
textHeight:=max(textHeight,edgeWidth); |
|
508 |
//textWidth:=max(textWidth,SpritesData[tail].Width); |
|
509 |
rect.x:= 0; |
|
510 |
rect.y:= 0; |
|
511 |
rect.w:= textWidth + (cornerWidth * 2); |
|
512 |
rect.h:= textHeight + cornerHeight*2 - edgeHeight + SpritesData[tail].Height; |
|
513 |
//s:= inttostr(w) + ' ' + inttostr(numlines) + ' ' + inttostr(rect.x) + ' '+inttostr(rect.y) + ' ' + inttostr(rect.w) + ' ' + inttostr(rect.h); |
|
514 |
||
515 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, RMask, GMask, BMask, AMask); |
|
516 |
||
11532 | 517 |
if checkFails(finalSurface <> nil, 'RenderString: fail to create surface', true) then |
518 |
exit(nil); |
|
4380 | 519 |
|
520 |
//////////////////////////////// CORNERS /////////////////////////////// |
|
521 |
copyToXY(SpritesData[corner].Surface, finalSurface, 0, 0); /////////////////// NW |
|
522 |
||
523 |
flipSurface(SpritesData[corner].Surface, true); // store all 4 versions in memory to avoid repeated flips? |
|
524 |
x:= 0; |
|
525 |
y:= textHeight + cornerHeight -1; |
|
526 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SW |
|
527 |
||
528 |
flipSurface(SpritesData[corner].Surface, false); |
|
529 |
x:= rect.w-cornerWidth-1; |
|
530 |
y:= textHeight + cornerHeight -1; |
|
531 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SE |
|
532 |
||
533 |
flipSurface(SpritesData[corner].Surface, true); |
|
534 |
x:= rect.w-cornerWidth-1; |
|
535 |
y:= 0; |
|
536 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// NE |
|
537 |
flipSurface(SpritesData[corner].Surface, false); // restore original position |
|
538 |
//////////////////////////////// END CORNERS /////////////////////////////// |
|
539 |
||
540 |
//////////////////////////////// EDGES ////////////////////////////////////// |
|
541 |
x:= cornerWidth; |
|
542 |
y:= 0; |
|
543 |
while x < rect.w-cornerWidth-1 do |
|
544 |
begin |
|
545 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// top edge |
|
546 |
inc(x,edgeWidth); |
|
547 |
end; |
|
548 |
flipSurface(SpritesData[edge].Surface, true); |
|
549 |
x:= cornerWidth; |
|
550 |
y:= textHeight + cornerHeight*2 - edgeHeight-1; |
|
551 |
while x < rect.w-cornerWidth-1 do |
|
552 |
begin |
|
553 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// bottom edge |
|
554 |
inc(x,edgeWidth); |
|
555 |
end; |
|
556 |
flipSurface(SpritesData[edge].Surface, true); // restore original position |
|
557 |
||
558 |
rotatedEdge:= SDL_CreateRGBSurface(SDL_SWSURFACE, edgeHeight, edgeWidth, 32, RMask, GMask, BMask, AMask); |
|
559 |
x:= rect.w - edgeHeight - 1; |
|
560 |
y:= cornerHeight; |
|
561 |
//// initially was going to rotate in place, but the SDL spec claims width/height are read only |
|
562 |
copyRotatedSurface(SpritesData[edge].Surface,rotatedEdge); |
|
563 |
while y < textHeight + cornerHeight do |
|
564 |
begin |
|
565 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
566 |
inc(y,edgeWidth); |
|
567 |
end; |
|
568 |
flipSurface(rotatedEdge, false); // restore original position |
|
569 |
x:= 0; |
|
570 |
y:= cornerHeight; |
|
571 |
while y < textHeight + cornerHeight do |
|
572 |
begin |
|
573 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
574 |
inc(y,edgeWidth); |
|
575 |
end; |
|
576 |
//////////////////////////////// END EDGES ////////////////////////////////////// |
|
577 |
||
578 |
x:= cornerWidth; |
|
579 |
y:= textHeight + cornerHeight * 2 - edgeHeight - 1; |
|
580 |
copyToXY(SpritesData[tail].Surface, finalSurface, x, y); |
|
581 |
||
582 |
rect.x:= edgeHeight; |
|
583 |
rect.y:= edgeHeight; |
|
584 |
rect.w:= rect.w - edgeHeight * 2; |
|
585 |
rect.h:= textHeight + cornerHeight * 2 - edgeHeight * 2; |
|
586 |
i:= rect.w; |
|
587 |
j:= rect.h; |
|
13490
8935dcc0e130
Always use SDL_Map(A)RGB in SDL_FillRect for color
Wuzzy <Wuzzy2@mail.ru>
parents:
12591
diff
changeset
|
588 |
SDL_FillRect(finalSurface, @rect, SDL_MapRGB(finalSurface^.format, cWhiteColor shr 16, cWhiteColor shr 8, cWhiteColor and $FF)); |
4380 | 589 |
|
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
590 |
pos:= 1; line:= 0; |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
591 |
while GetNextSpeechLine(s, #1, pos, substr) do |
4380 | 592 |
begin |
10689
692649e341fc
change string types of speech bubbles fix to work with pas2c
sheepluva
parents:
10687
diff
changeset
|
593 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, PChar(substr), cNearBlackColorChannels); |
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
594 |
rect.x:= edgeHeight + 1 + ((i - w) div 2); |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
595 |
// trying to more evenly position the text, vertically |
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
596 |
rect.y:= edgeHeight + ((j-(numLines*h)) div 2) + line * h; |
11507 | 597 |
if not SDLCheck(tmpsurf <> nil, 'TTF_RenderUTF8_Blended', true) then |
598 |
begin |
|
599 |
SDL_UpperBlit(tmpsurf, nil, finalSurface, @rect); |
|
600 |
SDL_FreeSurface(tmpsurf); |
|
601 |
end; |
|
10687
2e921409b5b1
cleanup speech bubble code a little. this fixes issue 719
sheepluva
parents:
10494
diff
changeset
|
602 |
inc(line); |
4380 | 603 |
end; |
604 |
||
605 |
RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true); |
|
606 |
||
607 |
SDL_FreeSurface(rotatedEdge); |
|
608 |
SDL_FreeSurface(finalSurface); |
|
8026
4a4f21070479
merge xymeng's gsoc engine with a few updates (and further checks on symbol definitions)
koda
parents:
7546
diff
changeset
|
609 |
|
4380 | 610 |
end; |
611 |
||
4611 | 612 |
end. |