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