author | koda |
Sun, 15 Nov 2009 19:42:28 +0000 | |
changeset 2622 | 39932161194e |
parent 2619 | bc2786a00fb8 |
child 2630 | 079ef82eac75 |
permissions | -rw-r--r-- |
942 | 1 |
(* |
1066 | 2 |
* Hedgewars, a free turn based strategy game |
942 | 3 |
* Copyright (c) 2008 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 |
||
19 |
unit uChat; |
|
20 |
||
21 |
interface |
|
22 |
||
23 |
procedure AddChatString(s: shortstring); |
|
24 |
procedure DrawChat; |
|
946 | 25 |
procedure KeyPressChat(Key: Longword); |
942 | 26 |
|
949 | 27 |
var UserNick: shortstring = ''; |
993 | 28 |
showAll: boolean = false; |
949 | 29 |
|
942 | 30 |
implementation |
1035 | 31 |
uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys, uTeams; |
942 | 32 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
33 |
const MaxStrIndex = 27; |
942 | 34 |
|
946 | 35 |
type TChatLine = record |
1431 | 36 |
Tex: PTexture; |
942 | 37 |
Time: Longword; |
1431 | 38 |
Width: LongInt; |
39 |
s: shortstring; |
|
942 | 40 |
end; |
41 |
||
946 | 42 |
var Strs: array[0 .. MaxStrIndex] of TChatLine; |
942 | 43 |
lastStr: Longword = 0; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
44 |
visibleCount: Longword = 0; |
2376 | 45 |
|
946 | 46 |
InputStr: TChatLine; |
47 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 48 |
|
2396 | 49 |
const colors: array[#1..#4] of Longword = ( |
2622 | 50 |
{$IFDEF ENDIAN_LITTLE} |
51 |
// ABGR |
|
52 |
$FFFFFFFF, // chat message [White] |
|
53 |
$FFFF00FF, // action message [Purple] |
|
54 |
$FF90FF90, // join/leave message [Lime] |
|
55 |
$FFA0FFFF // team message [Light Yellow] |
|
56 |
{$ELSE} |
|
57 |
// RGBA |
|
58 |
$FFFFFFFF, // chat message [White] |
|
59 |
$FF00FFFF, // action message [Purple] |
|
60 |
$90FF90FF, // join/leave message [Lime] |
|
61 |
$FFFFA0FF // team message [Light Yellow] |
|
62 |
{$ENDIF} |
|
2396 | 63 |
); |
64 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
65 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1118 | 66 |
var strSurface, resSurface: PSDL_Surface; |
1431 | 67 |
w, h: LongInt; |
2396 | 68 |
color: Longword; |
942 | 69 |
begin |
1118 | 70 |
if cl.Tex <> nil then |
71 |
FreeTexture(cl.Tex); |
|
942 | 72 |
|
2396 | 73 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
74 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
75 |
|
2396 | 76 |
if isInput then |
77 |
begin |
|
2622 | 78 |
color:= |
79 |
{$IFDEF ENDIAN_LITTLE} |
|
80 |
$FFFFFF00; // [Yellow abgr] |
|
81 |
{$ELSE} |
|
82 |
$00FFFFFF; // [Yellow rgba] |
|
83 |
{$ENDIF} |
|
2396 | 84 |
str:= UserNick + '> ' + str + '_' |
85 |
end |
|
86 |
else begin |
|
87 |
color:= colors[str[1]]; |
|
88 |
delete(str, 1, 1) |
|
89 |
end; |
|
90 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
91 |
|
1118 | 92 |
TTF_SizeUTF8(Fontz[fnt16].Handle, Str2PChar(str), w, h); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
93 |
|
2622 | 94 |
resSurface:= SDL_CreateRGBSurface(0, toPowerOf2(w), toPowerOf2(h), 32, RMask, GMask, BMask, AMask); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
95 |
|
2396 | 96 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), color); |
1431 | 97 |
cl.Width:= w + 4; |
1118 | 98 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
99 |
SDL_FreeSurface(strSurface); |
|
100 |
||
101 |
cl.Time:= RealTicks + 12500; |
|
2290
bf87ca44782e
Selectively enable clamping - seeing if this helps avoid weird flake problems while still fixing vertical lines in waves and sky
nemo
parents:
2161
diff
changeset
|
102 |
cl.Tex:= Surface2Tex(resSurface, false); |
1431 | 103 |
|
1118 | 104 |
SDL_FreeSurface(resSurface) |
946 | 105 |
end; |
106 |
||
107 |
procedure AddChatString(s: shortstring); |
|
108 |
begin |
|
109 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
110 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
111 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
112 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
113 |
inc(visibleCount) |
942 | 114 |
end; |
115 |
||
116 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
117 |
var i, t, cnt: Longword; |
1431 | 118 |
r: TSDL_Rect; |
942 | 119 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
120 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
121 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
122 |
i:= lastStr; |
1431 | 123 |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
124 |
r.x:= 6 - cScreenWidth div 2; |
1431 | 125 |
r.y:= (visibleCount - t) * 16 + 10; |
126 |
r.h:= 16; |
|
127 |
||
128 |
if (GameState = gsChat) |
|
129 |
and (InputStr.Tex <> nil) then |
|
130 |
begin |
|
131 |
r.w:= InputStr.Width; |
|
132 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
133 |
DrawTexture(8 - cScreenWidth div 2, visibleCount * 16 + 10, InputStr.Tex); |
1431 | 134 |
end; |
135 |
||
136 |
dec(r.y, 16); |
|
137 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
138 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
139 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
140 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
141 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
142 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
143 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
144 |
and |
1118 | 145 |
(Strs[i].Tex <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
146 |
begin |
1431 | 147 |
r.w:= Strs[i].Width; |
148 |
DrawFillRect(r); |
|
2161
0c8634241fa4
Some work on zooming. Hedgewars are now unplayable.
unc0rr
parents:
2131
diff
changeset
|
149 |
DrawTexture(8 - cScreenWidth div 2, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
1431 | 150 |
dec(r.y, 16); |
2376 | 151 |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
152 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
153 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
154 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
155 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
156 |
|
947 | 157 |
visibleCount:= cnt; |
942 | 158 |
end; |
159 |
||
1033 | 160 |
procedure AcceptChatString(s: shortstring); |
1035 | 161 |
var i: TWave; |
2124 | 162 |
|
1033 | 163 |
begin |
2017 | 164 |
// "Make hedgehog say something" |
2376 | 165 |
if (s[1] = '"') and (s[Length(s)] = '"') then |
2017 | 166 |
begin |
2111 | 167 |
if CurrentTeam^.ExtDriven then |
168 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
169 |
else |
|
170 |
ParseCommand('/hogsay '#1 + copy(s, 2, Length(s)-2), true); |
|
2017 | 171 |
exit |
172 |
end; |
|
173 |
// 'Make hedgehog think something' |
|
2376 | 174 |
if (s[1] = '''') and (s[Length(s)] = '''') then |
2017 | 175 |
begin |
2111 | 176 |
if CurrentTeam^.ExtDriven then |
177 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
178 |
else |
|
179 |
ParseCommand('/hogsay '#2 + copy(s, 2, Length(s)-2), true); |
|
2017 | 180 |
exit |
181 |
end; |
|
182 |
// -Make hedgehog yell something- |
|
2376 | 183 |
if (s[1] = '-') and (s[Length(s)] = '-') then |
2017 | 184 |
begin |
2111 | 185 |
if CurrentTeam^.ExtDriven then |
186 |
ParseCommand('/say ' + copy(s, 2, Length(s)-2), true) |
|
187 |
else |
|
188 |
ParseCommand('/hogsay '#3 + copy(s, 2, Length(s)-2), true); |
|
2017 | 189 |
exit |
190 |
end; |
|
191 |
// These 3 are same as above, only are to make the hedgehog say it on next attack |
|
192 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hsa ') then |
|
193 |
begin |
|
2111 | 194 |
if CurrentTeam^.ExtDriven then |
2112 | 195 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 196 |
else |
2112 | 197 |
ParseCommand('/hogsay '#4 + copy(s, 6, Length(s)-5), true); |
2017 | 198 |
exit |
199 |
end; |
|
200 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hta ') then |
|
201 |
begin |
|
2111 | 202 |
if CurrentTeam^.ExtDriven then |
2112 | 203 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 204 |
else |
2112 | 205 |
ParseCommand('/hogsay '#5 + copy(s, 6, Length(s)-5), true); |
2017 | 206 |
exit |
207 |
end; |
|
208 |
if (s[1] = '/') and (copy(s, 1, 5) = '/hya ') then |
|
209 |
begin |
|
2111 | 210 |
if CurrentTeam^.ExtDriven then |
2112 | 211 |
ParseCommand('/say ' + copy(s, 6, Length(s)-5), true) |
2111 | 212 |
else |
2112 | 213 |
ParseCommand('/hogsay '#6 + copy(s, 6, Length(s)-5), true); |
2017 | 214 |
exit |
215 |
end; |
|
2111 | 216 |
|
2518 | 217 |
if (copy(s, 1, 6) = '/team ') and (length(s) > 6) then |
2124 | 218 |
begin |
2403 | 219 |
ParseCommand(s, true); |
2124 | 220 |
exit |
221 |
end; |
|
1378 | 222 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 223 |
begin |
224 |
if CurrentTeam^.ExtDriven then exit; |
|
2376 | 225 |
|
1035 | 226 |
for i:= Low(TWave) to High(TWave) do |
227 |
if (s = Wavez[i].cmd) then |
|
228 |
begin |
|
229 |
ParseCommand('/taunt ' + char(i), true); |
|
230 |
exit |
|
231 |
end; |
|
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
232 |
if (s = '/newgrave') then |
2017 | 233 |
begin |
1821
6b6cf3389f92
Hedgehog drops a grave on "/newgrave" command. Patch by nemo
unc0rr
parents:
1819
diff
changeset
|
234 |
ParseCommand('/newgrave', true); |
2017 | 235 |
exit |
236 |
end; |
|
237 |
end |
|
1035 | 238 |
else |
239 |
ParseCommand('/say ' + s, true); |
|
1033 | 240 |
end; |
241 |
||
946 | 242 |
procedure KeyPressChat(Key: Longword); |
243 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
244 |
var i, btw: integer; |
|
1001 | 245 |
utf8: shortstring; |
946 | 246 |
begin |
1819 | 247 |
|
946 | 248 |
if Key <> 0 then |
249 |
case Key of |
|
1819 | 250 |
{Backspace} |
251 |
8, 127: if Length(InputStr.s) > 0 then |
|
946 | 252 |
begin |
253 |
InputStr.s[0]:= InputStrL[byte(InputStr.s[0])]; |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
254 |
SetLine(InputStr, InputStr.s, true) |
946 | 255 |
end; |
1819 | 256 |
{Esc} |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
257 |
27: SetLine(InputStr, '', true); |
1819 | 258 |
{Return} |
2003 | 259 |
3, 13, 271: begin |
947 | 260 |
if Length(InputStr.s) > 0 then |
261 |
begin |
|
1033 | 262 |
AcceptChatString(InputStr.s); |
1118 | 263 |
SetLine(InputStr, '', false) |
947 | 264 |
end; |
948 | 265 |
FreezeEnterKey; |
946 | 266 |
GameState:= gsGame |
1819 | 267 |
end; |
946 | 268 |
else |
269 |
if (Key < $80) then btw:= 1 |
|
270 |
else if (Key < $800) then btw:= 2 |
|
271 |
else if (Key < $10000) then btw:= 3 |
|
272 |
else btw:= 4; |
|
2376 | 273 |
|
946 | 274 |
utf8:= ''; |
275 |
||
276 |
for i:= btw downto 2 do |
|
277 |
begin |
|
278 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
279 |
Key:= Key shr 6 |
|
280 |
end; |
|
2376 | 281 |
|
946 | 282 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
283 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
284 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
285 |
|
946 | 286 |
InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0]; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
287 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 288 |
end |
289 |
end; |
|
290 |
||
291 |
||
942 | 292 |
end. |