author | Stepan777 <stepik-777@mail.ru> |
Thu, 09 Aug 2012 19:52:21 +0400 | |
changeset 7534 | ccb9c2906160 |
parent 7069 | bcf9d8e64e92 |
child 7291 | ad4b6c2b09e8 |
child 7546 | b50556f2a0e8 |
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); |
|
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 |
||
41 |
implementation |
|
7063 | 42 |
uses uUtils, uVariables, uConsts, uTextures, SysUtils, uDebug; |
4380 | 43 |
|
44 |
procedure DrawRoundRect(rect: PSDL_Rect; BorderColor, FillColor: Longword; Surface: PSDL_Surface; Clear: boolean); |
|
45 |
var r: TSDL_Rect; |
|
46 |
begin |
|
47 |
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
|
48 |
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
|
49 |
SDL_FillRect(Surface, @r, 0); |
4380 | 50 |
|
51 |
BorderColor:= SDL_MapRGB(Surface^.format, BorderColor shr 16, BorderColor shr 8, BorderColor and $FF); |
|
52 |
FillColor:= SDL_MapRGB(Surface^.format, FillColor shr 16, FillColor shr 8, FillColor and $FF); |
|
53 |
||
54 |
r.y:= rect^.y + 1; |
|
55 |
r.h:= rect^.h - 2; |
|
56 |
SDL_FillRect(Surface, @r, BorderColor); |
|
57 |
r.x:= rect^.x + 1; |
|
58 |
r.w:= rect^.w - 2; |
|
59 |
r.y:= rect^.y; |
|
60 |
r.h:= rect^.h; |
|
61 |
SDL_FillRect(Surface, @r, BorderColor); |
|
62 |
r.x:= rect^.x + 2; |
|
63 |
r.y:= rect^.y + 1; |
|
64 |
r.w:= rect^.w - 4; |
|
65 |
r.h:= rect^.h - 2; |
|
66 |
SDL_FillRect(Surface, @r, FillColor); |
|
67 |
r.x:= rect^.x + 1; |
|
68 |
r.y:= rect^.y + 2; |
|
69 |
r.w:= rect^.w - 2; |
|
70 |
r.h:= rect^.h - 4; |
|
71 |
SDL_FillRect(Surface, @r, FillColor) |
|
72 |
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
|
73 |
(* |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
74 |
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
|
75 |
begin |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
76 |
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
|
77 |
end;*) |
4380 | 78 |
|
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
79 |
function WriteInRoundRect(Surface: PSDL_Surface; X, Y: LongInt; Color: LongWord; Font: THWFont; s: ansistring; maxLength: LongWord): TSDL_Rect; |
4380 | 80 |
var w, h: LongInt; |
81 |
tmpsurf: PSDL_Surface; |
|
82 |
clr: TSDL_Color; |
|
6750 | 83 |
finalRect, textRect: TSDL_Rect; |
4380 | 84 |
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
|
85 |
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
|
86 |
if (maxLength <> 0) and (w > maxLength) then w := maxLength; |
4380 | 87 |
finalRect.x:= X; |
88 |
finalRect.y:= Y; |
|
6982 | 89 |
finalRect.w:= w + cFontBorder * 2 + 4; |
90 |
finalRect.h:= h + cFontBorder * 2; |
|
6750 | 91 |
textRect.x:= X; |
92 |
textRect.y:= Y; |
|
93 |
textRect.w:= w; |
|
94 |
textRect.h:= h; |
|
4380 | 95 |
DrawRoundRect(@finalRect, cWhiteColor, endian(cNearBlackColorChannels.value), Surface, true); |
96 |
clr.r:= (Color shr 16) and $FF; |
|
97 |
clr.g:= (Color shr 8) and $FF; |
|
98 |
clr.b:= Color and $FF; |
|
99 |
tmpsurf:= TTF_RenderUTF8_Blended(Fontz[Font].Handle, Str2PChar(s), clr); |
|
6982 | 100 |
finalRect.x:= X + cFontBorder + 2; |
101 |
finalRect.y:= Y + cFontBorder; |
|
4380 | 102 |
SDLTry(tmpsurf <> nil, true); |
6750 | 103 |
SDL_UpperBlit(tmpsurf, @textRect, Surface, @finalRect); |
4380 | 104 |
SDL_FreeSurface(tmpsurf); |
105 |
finalRect.x:= X; |
|
106 |
finalRect.y:= Y; |
|
6982 | 107 |
finalRect.w:= w + cFontBorder * 2 + 4; |
108 |
finalRect.h:= h + cFontBorder * 2; |
|
4380 | 109 |
WriteInRoundRect:= finalRect; |
110 |
end; |
|
111 |
||
112 |
procedure flipSurface(Surface: PSDL_Surface; Vertical: Boolean); |
|
113 |
var y, x, i, j: LongInt; |
|
114 |
tmpPixel: Longword; |
|
115 |
pixels: PLongWordArray; |
|
116 |
begin |
|
117 |
TryDo(Surface^.format^.BytesPerPixel = 4, 'flipSurface failed, expecting 32 bit surface', true); |
|
118 |
pixels:= Surface^.pixels; |
|
119 |
if Vertical then |
|
120 |
for y := 0 to (Surface^.h div 2) - 1 do |
|
121 |
for x := 0 to Surface^.w - 1 do |
|
122 |
begin |
|
123 |
i:= y * Surface^.w + x; |
|
124 |
j:= (Surface^.h - y - 1) * Surface^.w + x; |
|
125 |
tmpPixel:= pixels^[i]; |
|
126 |
pixels^[i]:= pixels^[j]; |
|
127 |
pixels^[j]:= tmpPixel; |
|
128 |
end |
|
129 |
else |
|
130 |
for x := 0 to (Surface^.w div 2) - 1 do |
|
131 |
for y := 0 to Surface^.h -1 do |
|
132 |
begin |
|
133 |
i:= y*Surface^.w + x; |
|
134 |
j:= y*Surface^.w + (Surface^.w - x - 1); |
|
135 |
tmpPixel:= pixels^[i]; |
|
136 |
pixels^[i]:= pixels^[j]; |
|
137 |
pixels^[j]:= tmpPixel; |
|
138 |
end; |
|
139 |
end; |
|
140 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
141 |
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
|
142 |
begin |
7013 | 143 |
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
|
144 |
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
|
145 |
|
7013 | 146 |
procedure copyToXYFromRect(src, dest: PSDL_Surface; srcX, srcY, srcW, srcH, 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
|
147 |
var i, j, maxDest, maxSrc, iX, iY: LongInt; |
4380 | 148 |
srcPixels, destPixels: PLongWordArray; |
149 |
r0, g0, b0, a0, r1, g1, b1, a1: Byte; |
|
150 |
begin |
|
151 |
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
|
152 |
maxSrc:= (src^.pitch div 4) * src^.h; |
4380 | 153 |
srcPixels:= src^.pixels; |
154 |
destPixels:= dest^.pixels; |
|
155 |
||
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
|
156 |
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
|
157 |
for iY:= 0 to srcH - 1 do |
4380 | 158 |
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
|
159 |
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
|
160 |
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
|
161 |
if (i < maxDest) and (j < maxSrc) and (srcPixels^[j] and AMask <> 0) then |
4380 | 162 |
begin |
163 |
SDL_GetRGBA(destPixels^[i], dest^.format, @r0, @g0, @b0, @a0); |
|
164 |
SDL_GetRGBA(srcPixels^[j], src^.format, @r1, @g1, @b1, @a1); |
|
165 |
r0:= (r0 * (255 - LongInt(a1)) + r1 * LongInt(a1)) div 255; |
|
166 |
g0:= (g0 * (255 - LongInt(a1)) + g1 * LongInt(a1)) div 255; |
|
167 |
b0:= (b0 * (255 - LongInt(a1)) + b1 * LongInt(a1)) div 255; |
|
168 |
a0:= (a0 * (255 - LongInt(a1)) + a1 * LongInt(a1)) div 255; |
|
169 |
destPixels^[i]:= SDL_MapRGBA(dest^.format, r0, g0, b0, a0); |
|
170 |
end; |
|
171 |
end; |
|
172 |
end; |
|
173 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
174 |
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
|
175 |
begin |
7013 | 176 |
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
|
177 |
end; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
178 |
|
7013 | 179 |
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
|
180 |
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
|
181 |
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
|
182 |
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
|
183 |
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
|
184 |
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
|
185 |
|
7013 | 186 |
copyToXYFromRect(SpritesData[sprite].Surface, 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
|
187 |
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
|
188 |
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
|
189 |
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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
|
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 |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
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
|
199 |
begin |
6992 | 200 |
//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
|
201 |
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
|
202 |
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
|
203 |
|
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 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
|
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 |
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
|
211 |
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
|
212 |
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
|
213 |
|
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 |
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
|
215 |
|
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 |
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
|
217 |
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
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
|
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
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
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
|
230 |
|
4380 | 231 |
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; |
|
233 |
srcPixels, destPixels: PLongWordArray; |
|
234 |
begin |
|
235 |
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); |
|
237 |
||
238 |
srcPixels:= src^.pixels; |
|
239 |
destPixels:= dest^.pixels; |
|
240 |
||
241 |
j:= 0; |
|
242 |
for x := 0 to src^.w - 1 do |
|
243 |
for y := 0 to src^.h - 1 do |
|
244 |
begin |
|
245 |
i:= (src^.h - 1 - y) * (src^.pitch div 4) + x; |
|
246 |
destPixels^[j]:= srcPixels^[i]; |
|
247 |
inc(j) |
|
248 |
end; |
|
249 |
end; |
|
250 |
||
6986
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
251 |
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
|
252 |
begin |
7013 | 253 |
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
|
254 |
end; |
409dd3851309
add support for default pascal mode by removing default arguments value (maybe this also helps the parser)
koda
parents:
6982
diff
changeset
|
255 |
|
7013 | 256 |
function RenderStringTexLim(s: ansistring; Color: Longword; font: THWFont; maxLength: LongWord): PTexture; |
4380 | 257 |
var w, h: LongInt; |
258 |
finalSurface: PSDL_Surface; |
|
259 |
begin |
|
7069 | 260 |
if length(s) = 0 then s:= _S' '; |
4380 | 261 |
font:= CheckCJKFont(s, font); |
262 |
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
|
263 |
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
|
264 |
if (maxLength <> 0) and (w > maxLength) then w := maxLength; |
4380 | 265 |
|
6982 | 266 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, w + cFontBorder * 2 + 4, h + cFontBorder * 2, |
4380 | 267 |
32, RMask, GMask, BMask, AMask); |
268 |
||
269 |
TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true); |
|
270 |
||
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
|
271 |
WriteInRoundRect(finalSurface, 0, 0, Color, font, s, maxLength); |
4380 | 272 |
|
273 |
TryDo(SDL_SetColorKey(finalSurface, SDL_SRCCOLORKEY, 0) = 0, errmsgTransparentSet, true); |
|
274 |
||
7013 | 275 |
RenderStringTexLim:= Surface2Tex(finalSurface, false); |
4380 | 276 |
|
277 |
SDL_FreeSurface(finalSurface); |
|
278 |
end; |
|
279 |
||
280 |
||
281 |
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; |
|
283 |
finalSurface, tmpsurf, rotatedEdge: PSDL_Surface; |
|
284 |
rect: TSDL_Rect; |
|
4611 | 285 |
chars: set of char = [#9,' ',';',':','?','!',',']; |
4380 | 286 |
substr: shortstring; |
287 |
edge, corner, tail: TSPrite; |
|
288 |
begin |
|
289 |
case SpeechType of |
|
290 |
1: begin; |
|
291 |
edge:= sprSpeechEdge; |
|
292 |
corner:= sprSpeechCorner; |
|
293 |
tail:= sprSpeechTail; |
|
294 |
end; |
|
295 |
2: begin; |
|
296 |
edge:= sprThoughtEdge; |
|
297 |
corner:= sprThoughtCorner; |
|
298 |
tail:= sprThoughtTail; |
|
299 |
end; |
|
300 |
3: begin; |
|
301 |
edge:= sprShoutEdge; |
|
302 |
corner:= sprShoutCorner; |
|
303 |
tail:= sprShoutTail; |
|
304 |
end; |
|
305 |
end; |
|
306 |
edgeHeight:= SpritesData[edge].Height; |
|
307 |
edgeWidth:= SpritesData[edge].Width; |
|
308 |
cornerWidth:= SpritesData[corner].Width; |
|
309 |
cornerHeight:= SpritesData[corner].Height; |
|
310 |
// This one screws up WrapText |
|
311 |
//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...'; |
|
312 |
// This one does not |
|
313 |
//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... '; |
|
314 |
||
315 |
numLines:= 0; |
|
316 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
317 |
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
|
318 |
s:= '...'; |
4380 | 319 |
font:= CheckCJKFont(s, font); |
320 |
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
|
321 |
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
|
322 |
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
|
323 |
w:= 8; |
4380 | 324 |
j:= 0; |
325 |
if (length(s) > 20) then |
|
326 |
begin |
|
327 |
w:= 0; |
|
328 |
i:= round(Sqrt(length(s)) * 2); |
|
329 |
s:= WrapText(s, #1, chars, i); |
|
330 |
pos:= 1; prevpos:= 0; line:= 0; |
|
331 |
// Find the longest line for the purposes of centring the text. Font dependant. |
|
332 |
while pos <= length(s) do |
|
333 |
begin |
|
334 |
if (s[pos] = #1) or (pos = length(s)) then |
|
335 |
begin |
|
336 |
inc(numlines); |
|
337 |
if s[pos] <> #1 then inc(pos); |
|
338 |
while s[prevpos+1] = ' ' do inc(prevpos); |
|
339 |
substr:= copy(s, prevpos+1, pos-prevpos-1); |
|
340 |
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
|
341 |
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
|
342 |
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
|
343 |
w:= i; |
4380 | 344 |
prevpos:= pos; |
345 |
end; |
|
346 |
inc(pos); |
|
347 |
end; |
|
348 |
end |
|
349 |
else numLines := 1; |
|
350 |
||
351 |
textWidth:=((w-(cornerWidth-edgeWidth)*2) div edgeWidth)*edgeWidth+edgeWidth; |
|
352 |
textHeight:=(((numlines * h + 2)-((cornerHeight-edgeWidth)*2)) div edgeWidth)*edgeWidth; |
|
353 |
||
354 |
textHeight:=max(textHeight,edgeWidth); |
|
355 |
//textWidth:=max(textWidth,SpritesData[tail].Width); |
|
356 |
rect.x:= 0; |
|
357 |
rect.y:= 0; |
|
358 |
rect.w:= textWidth + (cornerWidth * 2); |
|
359 |
rect.h:= textHeight + cornerHeight*2 - edgeHeight + SpritesData[tail].Height; |
|
360 |
//s:= inttostr(w) + ' ' + inttostr(numlines) + ' ' + inttostr(rect.x) + ' '+inttostr(rect.y) + ' ' + inttostr(rect.w) + ' ' + inttostr(rect.h); |
|
361 |
||
362 |
finalSurface:= SDL_CreateRGBSurface(SDL_SWSURFACE, rect.w, rect.h, 32, RMask, GMask, BMask, AMask); |
|
363 |
||
364 |
TryDo(finalSurface <> nil, 'RenderString: fail to create surface', true); |
|
365 |
||
366 |
//////////////////////////////// CORNERS /////////////////////////////// |
|
367 |
copyToXY(SpritesData[corner].Surface, finalSurface, 0, 0); /////////////////// NW |
|
368 |
||
369 |
flipSurface(SpritesData[corner].Surface, true); // store all 4 versions in memory to avoid repeated flips? |
|
370 |
x:= 0; |
|
371 |
y:= textHeight + cornerHeight -1; |
|
372 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SW |
|
373 |
||
374 |
flipSurface(SpritesData[corner].Surface, false); |
|
375 |
x:= rect.w-cornerWidth-1; |
|
376 |
y:= textHeight + cornerHeight -1; |
|
377 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// SE |
|
378 |
||
379 |
flipSurface(SpritesData[corner].Surface, true); |
|
380 |
x:= rect.w-cornerWidth-1; |
|
381 |
y:= 0; |
|
382 |
copyToXY(SpritesData[corner].Surface, finalSurface, x, y); /////////////////// NE |
|
383 |
flipSurface(SpritesData[corner].Surface, false); // restore original position |
|
384 |
//////////////////////////////// END CORNERS /////////////////////////////// |
|
385 |
||
386 |
//////////////////////////////// EDGES ////////////////////////////////////// |
|
387 |
x:= cornerWidth; |
|
388 |
y:= 0; |
|
389 |
while x < rect.w-cornerWidth-1 do |
|
390 |
begin |
|
391 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// top edge |
|
392 |
inc(x,edgeWidth); |
|
393 |
end; |
|
394 |
flipSurface(SpritesData[edge].Surface, true); |
|
395 |
x:= cornerWidth; |
|
396 |
y:= textHeight + cornerHeight*2 - edgeHeight-1; |
|
397 |
while x < rect.w-cornerWidth-1 do |
|
398 |
begin |
|
399 |
copyToXY(SpritesData[edge].Surface, finalSurface, x, y); ///////////////// bottom edge |
|
400 |
inc(x,edgeWidth); |
|
401 |
end; |
|
402 |
flipSurface(SpritesData[edge].Surface, true); // restore original position |
|
403 |
||
404 |
rotatedEdge:= SDL_CreateRGBSurface(SDL_SWSURFACE, edgeHeight, edgeWidth, 32, RMask, GMask, BMask, AMask); |
|
405 |
x:= rect.w - edgeHeight - 1; |
|
406 |
y:= cornerHeight; |
|
407 |
//// initially was going to rotate in place, but the SDL spec claims width/height are read only |
|
408 |
copyRotatedSurface(SpritesData[edge].Surface,rotatedEdge); |
|
409 |
while y < textHeight + cornerHeight do |
|
410 |
begin |
|
411 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
412 |
inc(y,edgeWidth); |
|
413 |
end; |
|
414 |
flipSurface(rotatedEdge, false); // restore original position |
|
415 |
x:= 0; |
|
416 |
y:= cornerHeight; |
|
417 |
while y < textHeight + cornerHeight do |
|
418 |
begin |
|
419 |
copyToXY(rotatedEdge, finalSurface, x, y); |
|
420 |
inc(y,edgeWidth); |
|
421 |
end; |
|
422 |
//////////////////////////////// END EDGES ////////////////////////////////////// |
|
423 |
||
424 |
x:= cornerWidth; |
|
425 |
y:= textHeight + cornerHeight * 2 - edgeHeight - 1; |
|
426 |
copyToXY(SpritesData[tail].Surface, finalSurface, x, y); |
|
427 |
||
428 |
rect.x:= edgeHeight; |
|
429 |
rect.y:= edgeHeight; |
|
430 |
rect.w:= rect.w - edgeHeight * 2; |
|
431 |
rect.h:= textHeight + cornerHeight * 2 - edgeHeight * 2; |
|
432 |
i:= rect.w; |
|
433 |
j:= rect.h; |
|
434 |
SDL_FillRect(finalSurface, @rect, cWhiteColor); |
|
435 |
||
436 |
pos:= 1; prevpos:= 0; line:= 0; |
|
437 |
while pos <= length(s) do |
|
438 |
begin |
|
439 |
if (s[pos] = #1) or (pos = length(s)) then |
|
440 |
begin |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
441 |
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
|
442 |
inc(pos); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
443 |
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
|
444 |
inc(prevpos); |
4380 | 445 |
substr:= copy(s, prevpos+1, pos-prevpos-1); |
446 |
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
|
447 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
448 |
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
|
449 |
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
|
450 |
// 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
|
451 |
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
|
452 |
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
|
453 |
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
|
454 |
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
|
455 |
inc(line); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
456 |
prevpos:= pos; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
457 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
6286
diff
changeset
|
458 |
end; |
4380 | 459 |
inc(pos); |
460 |
end; |
|
461 |
||
462 |
RenderSpeechBubbleTex:= Surface2Tex(finalSurface, true); |
|
463 |
||
464 |
SDL_FreeSurface(rotatedEdge); |
|
465 |
SDL_FreeSurface(finalSurface); |
|
466 |
end; |
|
467 |
||
4611 | 468 |
end. |