author | unc0rr |
Sun, 30 Nov 2008 16:32:53 +0000 | |
changeset 1523 | be52ace20c1a |
parent 1485 | 51c11e77408a |
child 1819 | 17dac76954d1 |
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; |
946 | 45 |
|
46 |
InputStr: TChatLine; |
|
47 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 48 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
49 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1118 | 50 |
var strSurface, resSurface: PSDL_Surface; |
1431 | 51 |
w, h: LongInt; |
942 | 52 |
begin |
1118 | 53 |
if cl.Tex <> nil then |
54 |
FreeTexture(cl.Tex); |
|
942 | 55 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
56 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
57 |
|
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
58 |
if isInput then str:= UserNick + '> ' + str + '_'; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
59 |
|
1118 | 60 |
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
|
61 |
|
1118 | 62 |
resSurface:= SDL_CreateRGBSurface(0, |
1431 | 63 |
toPowerOf2(w), |
64 |
toPowerOf2(h), |
|
1118 | 65 |
32, |
66 |
RMask, GMask, BMask, AMask); |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
67 |
|
1118 | 68 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF); |
1431 | 69 |
cl.Width:= w + 4; |
1118 | 70 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
71 |
SDL_FreeSurface(strSurface); |
|
72 |
||
73 |
cl.Time:= RealTicks + 12500; |
|
74 |
cl.Tex:= Surface2Tex(resSurface); |
|
1431 | 75 |
|
1118 | 76 |
SDL_FreeSurface(resSurface) |
946 | 77 |
end; |
78 |
||
79 |
procedure AddChatString(s: shortstring); |
|
80 |
begin |
|
81 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
82 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
83 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
84 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
85 |
inc(visibleCount) |
942 | 86 |
end; |
87 |
||
88 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
89 |
var i, t, cnt: Longword; |
1431 | 90 |
r: TSDL_Rect; |
942 | 91 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
92 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
93 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
94 |
i:= lastStr; |
1431 | 95 |
|
96 |
r.x:= 6; |
|
97 |
r.y:= (visibleCount - t) * 16 + 10; |
|
98 |
r.h:= 16; |
|
99 |
||
100 |
if (GameState = gsChat) |
|
101 |
and (InputStr.Tex <> nil) then |
|
102 |
begin |
|
103 |
r.w:= InputStr.Width; |
|
104 |
DrawFillRect(r); |
|
105 |
DrawTexture(8, visibleCount * 16 + 10, InputStr.Tex); |
|
106 |
end; |
|
107 |
||
108 |
dec(r.y, 16); |
|
109 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
110 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
111 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
112 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
113 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
114 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
115 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
116 |
and |
1118 | 117 |
(Strs[i].Tex <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
118 |
begin |
1431 | 119 |
r.w:= Strs[i].Width; |
120 |
DrawFillRect(r); |
|
1118 | 121 |
DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
1431 | 122 |
dec(r.y, 16); |
123 |
||
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
124 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
125 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
126 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
127 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
128 |
|
947 | 129 |
visibleCount:= cnt; |
942 | 130 |
end; |
131 |
||
1033 | 132 |
procedure AcceptChatString(s: shortstring); |
1035 | 133 |
var i: TWave; |
1033 | 134 |
begin |
1378 | 135 |
if (s[1] = '/') and (copy(s, 1, 4) <> '/me ') then |
1035 | 136 |
begin |
137 |
if CurrentTeam^.ExtDriven then exit; |
|
138 |
||
139 |
for i:= Low(TWave) to High(TWave) do |
|
140 |
if (s = Wavez[i].cmd) then |
|
141 |
begin |
|
142 |
ParseCommand('/taunt ' + char(i), true); |
|
143 |
exit |
|
144 |
end; |
|
145 |
end |
|
146 |
else |
|
147 |
ParseCommand('/say ' + s, true); |
|
1033 | 148 |
end; |
149 |
||
946 | 150 |
procedure KeyPressChat(Key: Longword); |
151 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
152 |
var i, btw: integer; |
|
1001 | 153 |
utf8: shortstring; |
946 | 154 |
begin |
155 |
if Key <> 0 then |
|
156 |
case Key of |
|
157 |
8: if Length(InputStr.s) > 0 then |
|
158 |
begin |
|
159 |
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
|
160 |
SetLine(InputStr, InputStr.s, true) |
946 | 161 |
end; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
162 |
27: SetLine(InputStr, '', true); |
948 | 163 |
13, 271: begin |
947 | 164 |
if Length(InputStr.s) > 0 then |
165 |
begin |
|
1033 | 166 |
AcceptChatString(InputStr.s); |
1118 | 167 |
SetLine(InputStr, '', false) |
947 | 168 |
end; |
948 | 169 |
FreezeEnterKey; |
946 | 170 |
GameState:= gsGame |
171 |
end |
|
172 |
else |
|
173 |
if (Key < $80) then btw:= 1 |
|
174 |
else if (Key < $800) then btw:= 2 |
|
175 |
else if (Key < $10000) then btw:= 3 |
|
176 |
else btw:= 4; |
|
177 |
||
178 |
utf8:= ''; |
|
179 |
||
180 |
for i:= btw downto 2 do |
|
181 |
begin |
|
182 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
183 |
Key:= Key shr 6 |
|
184 |
end; |
|
185 |
||
186 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
|
187 |
||
1485
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
188 |
if byte(InputStr.s[0]) + btw > 240 then exit; |
51c11e77408a
Fix chat bugs leading to serialized data corruption
unc0rr
parents:
1431
diff
changeset
|
189 |
|
946 | 190 |
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
|
191 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 192 |
end |
193 |
end; |
|
194 |
||
195 |
||
942 | 196 |
end. |