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