author | unc0rr |
Wed, 11 Jun 2008 20:27:09 +0000 | |
changeset 988 | cdff3f6f9b38 |
parent 984 | dd5f16f69926 |
child 990 | dfa6a6fe1542 |
permissions | -rw-r--r-- |
942 | 1 |
(* |
2 |
* Hedgewars, a worms-like game |
|
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 = ''; |
988 | 28 |
ChatVisibleCount: Longword = 7; |
949 | 29 |
|
942 | 30 |
implementation |
948 | 31 |
uses uMisc, uStore, uConsts, SDLh, uConsole, uKeys; |
942 | 32 |
|
988 | 33 |
const MaxStrIndex = 30; |
942 | 34 |
|
946 | 35 |
type TChatLine = record |
36 |
s: shortstring; |
|
942 | 37 |
Time: Longword; |
38 |
Tex: PTexture; |
|
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 |
|
946 | 48 |
procedure SetLine(var cl: TChatLine; str: shortstring); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
49 |
var strSurface, resSurface: PSDL_Surface; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
50 |
r: TSDL_Rect; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
51 |
w, h: LongInt; |
942 | 52 |
begin |
946 | 53 |
if cl.Tex <> nil then |
54 |
FreeTexture(cl.Tex); |
|
942 | 55 |
|
946 | 56 |
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
|
57 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
58 |
resSurface:= SDL_CreateRGBSurface(0, |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
59 |
toPowerOf2(w + 2), |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
60 |
toPowerOf2(h + 2), |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
61 |
32, |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
62 |
RMask, GMask, BMask, AMask); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
63 |
|
946 | 64 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $202020); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
65 |
r.x:= 1; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
66 |
r.y:= 1; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
67 |
SDL_UpperBlit(strSurface, nil, resSurface, @r); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
68 |
|
946 | 69 |
strSurface:= TTF_RenderUTF8_Solid(Fontz[fnt16].Handle, Str2PChar(str), $FFFFFF); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
70 |
SDL_UpperBlit(strSurface, nil, resSurface, nil); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
71 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
72 |
SDL_FreeSurface(strSurface); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
73 |
|
947 | 74 |
cl.s:= str; |
984 | 75 |
cl.Time:= RealTicks + 12500; |
946 | 76 |
cl.Tex:= Surface2Tex(resSurface); |
77 |
SDL_FreeSurface(resSurface) |
|
78 |
end; |
|
79 |
||
80 |
procedure AddChatString(s: shortstring); |
|
81 |
begin |
|
82 |
lastStr:= (lastStr + 1) mod (MaxStrIndex + 1); |
|
83 |
||
84 |
SetLine(Strs[lastStr], s); |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
85 |
|
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
86 |
inc(visibleCount) |
942 | 87 |
end; |
88 |
||
89 |
procedure DrawChat; |
|
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
90 |
var i, t, cnt: Longword; |
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; |
988 | 95 |
while (t < ChatVisibleCount) |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
96 |
and (Strs[i].Tex <> nil) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
97 |
and (Strs[i].Time > RealTicks) do |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
98 |
begin |
988 | 99 |
DrawTexture(8, (visibleCount - t) * 16 - 6, Strs[i].Tex); |
945
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
100 |
if i = 0 then i:= MaxStrIndex else dec(i); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
101 |
inc(cnt); |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
102 |
inc(t) |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
103 |
end; |
4ead9cde4e14
- Start chat implementation: chat strings are on the screen
unc0rr
parents:
942
diff
changeset
|
104 |
|
947 | 105 |
visibleCount:= cnt; |
106 |
||
107 |
if (GameState = gsChat) |
|
108 |
and (InputStr.Tex <> nil) then |
|
988 | 109 |
DrawTexture(11, visibleCount * 16 + 10, InputStr.Tex); |
942 | 110 |
end; |
111 |
||
946 | 112 |
procedure KeyPressChat(Key: Longword); |
113 |
const firstByteMark: array[1..4] of byte = (0, $C0, $E0, $F0); |
|
114 |
var i, btw: integer; |
|
949 | 115 |
utf8, s: shortstring; |
946 | 116 |
begin |
117 |
if Key <> 0 then |
|
118 |
case Key of |
|
119 |
8: if Length(InputStr.s) > 0 then |
|
120 |
begin |
|
121 |
InputStr.s[0]:= InputStrL[byte(InputStr.s[0])]; |
|
122 |
SetLine(InputStr, InputStr.s) |
|
123 |
end; |
|
948 | 124 |
13, 271: begin |
947 | 125 |
if Length(InputStr.s) > 0 then |
126 |
begin |
|
950 | 127 |
ParseCommand('/say ' + InputStr.s, true); |
948 | 128 |
SetLine(InputStr, '') |
947 | 129 |
end; |
948 | 130 |
FreezeEnterKey; |
946 | 131 |
GameState:= gsGame |
132 |
end |
|
133 |
else |
|
134 |
if (Key < $80) then btw:= 1 |
|
135 |
else if (Key < $800) then btw:= 2 |
|
136 |
else if (Key < $10000) then btw:= 3 |
|
137 |
else btw:= 4; |
|
138 |
||
139 |
utf8:= ''; |
|
140 |
||
141 |
for i:= btw downto 2 do |
|
142 |
begin |
|
143 |
utf8:= char((Key or $80) and $BF) + utf8; |
|
144 |
Key:= Key shr 6 |
|
145 |
end; |
|
146 |
||
147 |
utf8:= char(Key or firstByteMark[btw]) + utf8; |
|
148 |
||
149 |
InputStrL[byte(InputStr.s[0]) + btw]:= InputStr.s[0]; |
|
150 |
SetLine(InputStr, InputStr.s + utf8) |
|
151 |
end |
|
152 |
end; |
|
153 |
||
154 |
||
942 | 155 |
end. |