author | unc0rr |
Sun, 27 Jul 2008 16:03:45 +0000 | |
changeset 1116 | dd6db8e09f9e |
parent 1066 | 1f1b3686a2b0 |
child 1118 | caf47265d03f |
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 |
36 |
s: shortstring; |
|
942 | 37 |
Time: Longword; |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
38 |
Texb, Texf: PTexture; |
942 | 39 |
end; |
40 |
||
946 | 41 |
var Strs: array[0 .. MaxStrIndex] of TChatLine; |
942 | 42 |
lastStr: Longword = 0; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
43 |
visibleCount: Longword = 0; |
946 | 44 |
|
45 |
InputStr: TChatLine; |
|
46 |
InputStrL: array[0..260] of char; // for full str + 4-byte utf-8 char |
|
942 | 47 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
48 |
procedure SetLine(var cl: TChatLine; str: shortstring; isInput: boolean); |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
49 |
var surf: PSDL_Surface; |
942 | 50 |
begin |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
51 |
if cl.Texb <> nil then |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
52 |
begin |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
53 |
FreeTexture(cl.Texb); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
54 |
FreeTexture(cl.Texf) |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
55 |
end; |
942 | 56 |
|
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
57 |
cl.s:= str; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
58 |
|
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
59 |
if isInput then str:= UserNick + '> ' + str + '_'; |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
60 |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
61 |
cl.Time:= RealTicks + 12500; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
62 |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
63 |
TryDo(str <> '', 'Error: null chat string', true); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
64 |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
65 |
surf:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $202020); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
66 |
surf:= SDL_DisplayFormatAlpha(surf); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
67 |
TryDo(surf <> nil, 'Chat: fail to render string', true); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
68 |
cl.Texb:= Surface2Tex(surf); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
69 |
SDL_FreeSurface(surf); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
70 |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
71 |
surf:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
72 |
surf:= SDL_DisplayFormatAlpha(surf); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
73 |
TryDo(surf <> nil, 'Chat: fail to render string', true); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
74 |
cl.Texf:= Surface2Tex(surf); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
75 |
SDL_FreeSurface(surf) |
946 | 76 |
end; |
77 |
||
78 |
procedure AddChatString(s: shortstring); |
|
79 |
begin |
|
80 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
81 |
||
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
82 |
SetLine(Strs[lastStr], s, false); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
83 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
84 |
inc(visibleCount) |
942 | 85 |
end; |
86 |
||
87 |
procedure DrawChat; |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
88 |
const shift = 2; |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
89 |
var i, t, cnt: Longword; |
942 | 90 |
begin |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
91 |
cnt:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
92 |
t:= 0; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
93 |
i:= lastStr; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
94 |
while |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
95 |
( |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
96 |
((t < 7) and (Strs[i].Time > RealTicks)) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
97 |
or |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
98 |
((t < MaxStrIndex) and showAll) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
99 |
) |
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
100 |
and |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
101 |
(Strs[i].Texb <> nil) do |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
102 |
begin |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
103 |
DrawTexture(8 + shift, (visibleCount - t) * 16 - 6 + shift, Strs[i].Texb); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
104 |
DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Texf); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
105 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
106 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
107 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
108 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
109 |
|
947 | 110 |
visibleCount:= cnt; |
111 |
||
112 |
if (GameState = gsChat) |
|
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
113 |
and (InputStr.Texb <> nil) then |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
114 |
begin |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
115 |
DrawTexture(8 + shift, visibleCount * 16 + 10 + shift, InputStr.Texb); |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
116 |
DrawTexture(8, visibleCount * 16 + 10, InputStr.Texf) |
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
117 |
end |
942 | 118 |
end; |
119 |
||
1033 | 120 |
procedure AcceptChatString(s: shortstring); |
1035 | 121 |
var i: TWave; |
1033 | 122 |
begin |
1035 | 123 |
if s[1] = '/' then |
124 |
begin |
|
125 |
if CurrentTeam^.ExtDriven then exit; |
|
126 |
||
127 |
for i:= Low(TWave) to High(TWave) do |
|
128 |
if (s = Wavez[i].cmd) then |
|
129 |
begin |
|
130 |
ParseCommand('/taunt ' + char(i), true); |
|
131 |
exit |
|
132 |
end; |
|
133 |
end |
|
134 |
else |
|
135 |
ParseCommand('/say ' + s, true); |
|
1033 | 136 |
end; |
137 |
||
946 | 138 |
procedure KeyPressChat(Key: Longword); |
139 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
140 |
var i, btw: integer; |
|
1001 | 141 |
utf8: shortstring; |
946 | 142 |
begin |
143 |
if Key <> 0 then |
|
144 |
case Key of |
|
145 |
8: if Length(InputStr.s) > 0 then |
|
146 |
begin |
|
147 |
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
|
148 |
SetLine(InputStr, InputStr.s, true) |
946 | 149 |
end; |
990
dfa6a6fe1542
Implement history for chat (27 entries), no key binding yet
unc0rr
parents:
988
diff
changeset
|
150 |
27: SetLine(InputStr, '', true); |
948 | 151 |
13, 271: begin |
947 | 152 |
if Length(InputStr.s) > 0 then |
153 |
begin |
|
1033 | 154 |
AcceptChatString(InputStr.s); |
1116
dd6db8e09f9e
Try another appproach to render chat, should fix windows issues
unc0rr
parents:
1066
diff
changeset
|
155 |
SetLine(InputStr, '', true) |
947 | 156 |
end; |
948 | 157 |
FreezeEnterKey; |
946 | 158 |
GameState:= gsGame |
159 |
end |
|
160 |
else |
|
161 |
if (Key < $80) then btw:= 1 |
|
162 |
else if (Key < $800) then btw:= 2 |
|
163 |
else if (Key < $10000) then btw:= 3 |
|
164 |
else btw:= 4; |
|
165 |
||
166 |
utf8:= ''; |
|
167 |
||
168 |
for i:= btw downto 2 do |
|
169 |
begin |
|
170 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
171 |
Key:= Key shr 6 |
|
172 |
end; |
|
173 |
||
174 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
|
175 |
||
176 |
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
|
177 |
SetLine(InputStr, InputStr.s + utf8, true) |
946 | 178 |
end |
179 |
end; |
|
180 |
||
181 |
||
942 | 182 |
end. |