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