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