ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.60 by root, Mon Apr 10 11:56:28 2006 UTC vs.
Revision 1.156 by elmex, Fri Apr 21 12:27:20 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8; 4use utf8;
5 5
6use Glib; 6use Time::HiRes 'time';
7use Gtk2 -init;
8
9use SDL;
10use SDL::App;
11use SDL::Event; 7use Event;
12use SDL::Surface;
13use SDL::OpenGL;
14use SDL::OpenGL::Constants;
15 8
16use Crossfire; 9use Crossfire;
17use Crossfire::Protocol; 10use Crossfire::Protocol;
18 11
19use Crossfire::Client; 12use Compress::LZF;
20use Crossfire::Client::Widget;
21 13
22our $FACECACHE; 14use CFClient;
15use CFClient::UI;
16use CFClient::MapWidget;
23 17
24our $VERSION = '0.1'; 18our $VERSION = '0.1';
25 19
26our %GL_EXT; 20my $MAX_FPS = 60;
21my $MIN_FPS = 5; # unused as of yet
22
23our $META_SERVER = "crossfire.real-time.com:13326";
24
25our $FACEMAP;
26our $TILECACHE;
27our $MAPCACHE;
28
29our $LAST_REFRESH;
30our $NOW;
27 31
28our $CFG; 32our $CFG;
29our $CONN; 33our $CONN;
34our $FAST; # fast, low-quality mode, possibly useful for software-rendering
30 35
36our @SDL_MODES;
31our $WIDTH; 37our $WIDTH;
32our $HEIGHT; 38our $HEIGHT;
33our $FULLSCREEN; 39our $FULLSCREEN;
34
35our $FONTSIZE; 40our $FONTSIZE;
36 41
42our $MAP;
43our $MAPWIDGET;
44our $BUTTONBAR;
45our $LOGVIEW;
46our $CONSOLE;
47our $METASERVER;
48
49our $GAUGES;
50our $STATWIDS;
51
37our $SDL_TIMER; 52our $SDL_ACTIVE;
38our $SDL_APP;
39our $SDL_EV;
40our %SDL_CB; 53our %SDL_CB;
41 54
42our @GL_INIT; # hooks called on every gl init 55our $SDL_MIXER;
56our @SOUNDS; # event => file mapping
57our %AUDIO_CHUNKS; # audio files
43 58
44our $ALT_ENTER_MESSAGE; 59our $ALT_ENTER_MESSAGE;
45our $STATUS_LINE; 60our $STATUS_LINE;
61our $DEBUG_STATUS;
46 62
47my $last_refresh; 63sub status {
48my %ANIMATE; 64 $STATUS_LINE->set_text ($_[0]);
49my $refresh_handler; 65 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
66}
50 67
51our ($tw, $te); # Test widget #d# 68sub debug {
69 $DEBUG_STATUS->set_text ($_[0]);
70 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
71}
52 72
53sub init_screen { 73sub start_game {
54 $SDL_APP = new SDL::App 74 status "logging in...";
55 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
56 -title => "Crossfire+ Client",
57 -width => $WIDTH,
58 -height => $HEIGHT,
59 -opengl => 1,
60 -red_size => 5,
61 -green_size => 5,
62 -blue_size => 5,
63 -alpha_size => 0,
64 -double_buffer => 1,
65 -fullscreen => $FULLSCREEN,
66 -resizeable => 0;
67 75
68 $SDL_EV = new SDL::Event; 76 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
69 $SDL_EV->set_unicode (1);
70 77
71 $last_refresh = SDL::GetTicks; 78 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
72 79
73 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; 80 $MAP = new CFClient::Map $mapsize, $mapsize;
74 81
75 $GL_EXT{GL_ARB_texture_non_power_of_two} 82 my ($host, $port) = split /:/, $CFG->{host};
76 or warn "WARNING: non-power-of-two opengl extension required";
77
78 $FONTSIZE = int $HEIGHT / 50;
79
80 #############################################################################
81
82 glClearColor 0.5, 0.5, 0.5, 1;
83
84 glEnable GL_TEXTURE_2D;
85 glEnable GL_COLOR_MATERIAL;
86 glShadeModel GL_FLAT;
87 glDisable GL_DEPTH_TEST;
88 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89
90 $_->() for @GL_INIT;
91
92 #############################################################################
93 83
94 $STATUS_LINE = new Crossfire::Client::Widget::Label
95 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
96 "";
97 $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE);
98
99 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
100 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
101 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
102 $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
103
104 # Test code #d#
105 unless ($tw) { # haha...
106 $te = new Crossfire::Client::Widget::FancyFrame;
107 $te->add (new Crossfire::Client::Widget::Entry);
108 $te->move (300, 0, 2);
109 $Crossfire::Client::Widget::TOPLEVEL->add ($te);
110
111 $tw = new Crossfire::Client::Widget::Animator;
112 my $lbl1 = new Crossfire::Client::Widget::Label
113 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
114 my $lbl2 = new Crossfire::Client::Widget::Label
115 0, 0, 10, $FONTSIZE, "LBL2";
116
117 my $vb = new Crossfire::Client::Widget::VBox;
118 my $f = new Crossfire::Client::Widget::FancyFrame;
119 my $f2 = new Crossfire::Client::Widget::FancyFrame;
120 $f->add ($lbl1);
121 $f2->add ($lbl2);
122 $vb->add ($f);
123 $vb->add ($f2, 1);
124
125 $tw->add ($vb);
126 $tw->w (400);
127 $tw->h (300);
128 $tw->move ($WIDTH - 200, 0);
129 $tw->moveto (0, 0);
130 $Crossfire::Client::Widget::TOPLEVEL->add ($tw);
131
132# $f->move ($WIDTH - 200, 0);
133# $Crossfire::Client::Widget::TOPLEVEL->add ($f);
134 }
135}
136
137sub start_game {
138 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
139 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
140 while $SDL_EV->poll;
141
142 1
143 };
144
145 $WIDTH = $CFG->{width};
146 $HEIGHT = $CFG->{height};
147 $FULLSCREEN = 0;
148
149 init_screen;
150
151 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
152
153 $CONN = new conn 84 $CONN = new conn
154 host => $CFG->{host}, 85 host => $host,
155 port => $CFG->{port}, 86 port => $port || 13327,
156 user => $CFG->{user}, 87 user => $CFG->{user},
157 pass => $CFG->{password}, 88 pass => $CFG->{password},
158 mapw => $mapsize, 89 mapw => $mapsize,
159 maph => $mapsize, 90 maph => $mapsize,
160 ; 91 ;
161 92
93 status "login successful";
94
162 Crossfire::Client::lowdelay fileno $CONN->{fh}; 95 CFClient::lowdelay fileno $CONN->{fh};
163} 96}
164 97
165sub stop_game { 98sub stop_game {
166 remove Glib::Source $SDL_TIMER;
167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
169
170 undef $SDL_APP;
171 undef $CONN; 99 undef $CONN;
172 SDL::Quit;
173} 100}
174 101
175sub force_refresh { 102sub client_setup {
176 glViewport 0, 0, $WIDTH, $HEIGHT; 103 my $dialog = new CFClient::UI::FancyFrame
104 title => "Client Setup",
105 child => (my $vbox = new CFClient::UI::VBox);
106 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
177 107
178 glMatrixMode GL_PROJECTION; 108 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
179 glLoadIdentity; 109 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
180 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
181 glMatrixMode GL_MODELVIEW;
182 110
183 glClear GL_COLOR_BUFFER_BIT; 111 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
112 $hbox->add (my $mode_label = new CFClient::UI::Label align => 0, valign => 0, height => 0.8, template => "9999x9999");
184 113
185 $Crossfire::Client::Widget::TOPLEVEL->draw; 114 $mode_slider->connect (changed => sub {
115 my ($self, $value) = @_;
186 116
187 SDL::GLSwapBuffers; 117 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
188} 118 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
119 });
120 $mode_slider->emit (changed => $mode_slider->{range}[0]);
189 121
190sub refresh { 122 $table->add (0, 1, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
191 $refresh_handler ||= add Glib::Idle sub { 123 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
192 if ($SDL_APP) { 124 my ($self, $value) = @_;
193 my $next_refresh = SDL::GetTicks; 125 $CFG->{fullscreen} = $value;
194 my $interval = ($next_refresh - $last_refresh) * 0.001; 126 });
195 $last_refresh = $next_refresh;
196 127
197 force_refresh; 128 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
198 $_->animate ($interval) for grep $_, values %ANIMATE; 129 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
130 my ($self, $value) = @_;
131 $CFG->{fast} = $value;
132 });
199 133
200 if (%ANIMATE) { 134 $table->add (0, 3, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
135 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub {
136 my ($self, $value) = @_;
137 $CFG->{fow_enable} = $value;
138 });
139
140 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
141 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
142 my ($self, $value) = @_;
143 $CFG->{fow_intensity} = $value;
144 });
145
146 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
147 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
148 my ($self, $value) = @_;
149 $CFG->{fow_smooth} = $value;
150 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
151 });
152
153 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "GUI Fontsize");
154 $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{gui_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
155 $CFG->{gui_fontsize} = 0.1 * int $_[1] * 10;
156# $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
157 });
158
159 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Server Log Fontsize");
160 $table->add (1, 7, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
161 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
162 });
163
164 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
165 video_shutdown ();
166 video_init ();
167 });
168
169 $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
170 $table->add (1, 9, new CFClient::UI::CheckBox state => $CFG->{audio_enable}, connect_changed => sub {
171 $CFG->{audio_enable} = $_[1];
172 });
173# $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Effects Volume");
174# $table->add (1, 8, new CFClient::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], connect_changed => sub {
175# $CFG->{effects_volume} = $_[1];
176# });
177 $table->add (0, 10, new CFClient::UI::Label valign => 0, align => 1, text => "Background Music");
178 $table->add (1, 10, my $hbox = new CFClient::UI::HBox);
179 $hbox->add (new CFClient::UI::CheckBox expand => 1, state => $CFG->{bgm_enable}, connect_changed => sub {
180 $CFG->{bgm_enable} = $_[1];
181 });
182 $hbox->add (new CFClient::UI::Slider expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0.1], connect_changed => sub {
183 $CFG->{bgm_volume} = $_[1];
184 CFClient::MixMusic::volume $_[1] * 128;
185 });
186
187 $table->add (1, 11, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
188 audio_shutdown ();
189 audio_init ();
190 });
191
192 $dialog
193}
194
195sub make_stats_window {
196 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH * 2/5, y => 0, title => "Stats");
197
198 $tgw->add (my $vb = new CFClient::UI::VBox);
199 $vb->add ($STATWIDS->{title} = new CFClient::UI::Label valign => 0, align => -1, text => "Title:");
200 $vb->add (my $lhb = new CFClient::UI::HBox);
201 $lhb->add ($STATWIDS->{exp} = new CFClient::UI::Label valign => 0, align => -1, text => "Exp:", expand => 1);
202 $lhb->add ($STATWIDS->{lvl} = new CFClient::UI::Label valign => 0, align => -1, text => "Level:", expand => 1);
203
204 $vb->add (my $hb = new CFClient::UI::HBox expand => 1);
205 $hb->add (my $hg = new CFClient::UI::Gauge type => 'hp', expand => 1);
206 $hb->add (my $mg = new CFClient::UI::Gauge type => 'mana', expand => 1);
207 $hb->add (my $gg = new CFClient::UI::Gauge type => 'grace', expand => 1);
208 $hb->add (my $fg = new CFClient::UI::Gauge type => 'food', expand => 1);
209 $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
210
211 $hb->add (my $tbl = new CFClient::UI::Table expand => 1);
212
213 if (0) { # this code can vanish, just wanted to preserver it for a checkin
214 $tbl->add (0, 0, $STATWIDS->{st_str} = new CFClient::UI::Label valign => 0, align => -1, text => "S");
215 $tbl->add (0, 1, $STATWIDS->{st_dex} = new CFClient::UI::Label valign => 0, align => -1, text => "D");
216 $tbl->add (0, 2, $STATWIDS->{st_con} = new CFClient::UI::Label valign => 0, align => -1, text => "Co");
217 $tbl->add (0, 3, $STATWIDS->{st_int} = new CFClient::UI::Label valign => 0, align => -1, text => "I");
218 $tbl->add (0, 4, $STATWIDS->{st_wis} = new CFClient::UI::Label valign => 0, align => -1, text => "W");
219 $tbl->add (0, 5, $STATWIDS->{st_pow} = new CFClient::UI::Label valign => 0, align => -1, text => "P");
220 $tbl->add (0, 6, $STATWIDS->{st_cha} = new CFClient::UI::Label valign => 0, align => -1, text => "Ch");
221
222 $tbl->add (1, 0, $STATWIDS->{st_wc} = new CFClient::UI::Label valign => 0, align => -1, text => "Wc");
223 $tbl->add (1, 1, $STATWIDS->{st_ac} = new CFClient::UI::Label valign => 0, align => -1, text => "Ac");
224 $tbl->add (1, 2, $STATWIDS->{st_dam} = new CFClient::UI::Label valign => 0, align => -1, text => "Dam");
225 $tbl->add (1, 3, $STATWIDS->{st_arm} = new CFClient::UI::Label valign => 0, align => -1, text => "Arm");
226 $tbl->add (1, 4, $STATWIDS->{st_spd} = new CFClient::UI::Label valign => 0, align => -1, text => "Sp");
227 $tbl->add (1, 5, $STATWIDS->{st_wspd} = new CFClient::UI::Label valign => 0, align => -1, text => "WSp");
228 } else {
229 $tbl->add (0, 0, new CFClient::UI::Label valign => 0, align => +1, text => "S");
230 $tbl->add (0, 1, new CFClient::UI::Label valign => 0, align => +1, text => "D");
231 $tbl->add (0, 2, new CFClient::UI::Label valign => 0, align => +1, text => "Co");
232 $tbl->add (0, 3, new CFClient::UI::Label valign => 0, align => +1, text => "I");
233 $tbl->add (0, 4, new CFClient::UI::Label valign => 0, align => +1, text => "W");
234 $tbl->add (0, 5, new CFClient::UI::Label valign => 0, align => +1, text => "P");
235 $tbl->add (0, 6, new CFClient::UI::Label valign => 0, align => +1, text => "Ch");
236
237 $tbl->add (1, 0, $STATWIDS->{st_str} = new CFClient::UI::Label valign => 0, align => -1, text => "");
238 $tbl->add (1, 1, $STATWIDS->{st_dex} = new CFClient::UI::Label valign => 0, align => -1, text => "");
239 $tbl->add (1, 2, $STATWIDS->{st_con} = new CFClient::UI::Label valign => 0, align => -1, text => "");
240 $tbl->add (1, 3, $STATWIDS->{st_int} = new CFClient::UI::Label valign => 0, align => -1, text => "");
241 $tbl->add (1, 4, $STATWIDS->{st_wis} = new CFClient::UI::Label valign => 0, align => -1, text => "");
242 $tbl->add (1, 5, $STATWIDS->{st_pow} = new CFClient::UI::Label valign => 0, align => -1, text => "");
243 $tbl->add (1, 6, $STATWIDS->{st_cha} = new CFClient::UI::Label valign => 0, align => -1, text => "");
244
245 $tbl->add (2, 0, new CFClient::UI::Label valign => 0, align => +1, text => "Wc");
246 $tbl->add (2, 1, new CFClient::UI::Label valign => 0, align => +1, text => "Ac");
247 $tbl->add (2, 2, new CFClient::UI::Label valign => 0, align => +1, text => "Dam");
248 $tbl->add (2, 3, new CFClient::UI::Label valign => 0, align => +1, text => "Arm");
249 $tbl->add (2, 4, new CFClient::UI::Label valign => 0, align => +1, text => "Sp");
250 $tbl->add (2, 5, new CFClient::UI::Label valign => 0, align => +1, text => "WSp");
251
252 $tbl->add (3, 0, $STATWIDS->{st_wc} = new CFClient::UI::Label valign => 0, align => -1, text => "");
253 $tbl->add (3, 1, $STATWIDS->{st_ac} = new CFClient::UI::Label valign => 0, align => -1, text => "");
254 $tbl->add (3, 2, $STATWIDS->{st_dam} = new CFClient::UI::Label valign => 0, align => -1, text => "");
255 $tbl->add (3, 3, $STATWIDS->{st_arm} = new CFClient::UI::Label valign => 0, align => -1, text => "");
256 $tbl->add (3, 4, $STATWIDS->{st_spd} = new CFClient::UI::Label valign => 0, align => -1, text => "");
257 $tbl->add (3, 5, $STATWIDS->{st_wspd} = new CFClient::UI::Label valign => 0, align => -1, text => "");
258 }
259
260 $hb->add (my $tbl2 = new CFClient::UI::Table);
261
262 my $row = 0;
263 my $col = 0;
264
265 for (qw/slow holyw conf fire depl magic
266 drain acid pois para deat phys
267 blind fear tund elec cold ghit/)
268 {
269 $tbl2->add ($col, $row, new CFClient::UI::Image image => "ui/resist/resist_$_.png");
270 $tbl2->add ($col + 1, $row,
271 $STATWIDS->{"res_$_"} =
272 new CFClient::UI::Label text => "0", align => -1, valign => 0
273 );
274
275 $row++;
276 if ($row % 6 == 0) {
277 $col += 2;
278 $row = 0;
279 }
280 }
281
282 update_stats_window ({});
283
284 $tgw
285}
286
287sub update_stats_window {
288 my ($stats) = @_;
289
290 # i love text protocols!!!
291 my $hp = $stats->{1};
292 my $hp_m = $stats->{2};
293 my $sp = $stats->{3};
294 my $sp_m = $stats->{4};
295 my $fo = $stats->{18};
296 my $fo_m = 999;
297 my $gr = $stats->{23};
298 my $gr_m = $stats->{24};
299
300 $GAUGES->{hp} ->set_value ($hp, $hp_m);
301 $GAUGES->{mana} ->set_value ($sp, $sp_m);
302 $GAUGES->{food} ->set_value ($fo, $fo_m);
303 $GAUGES->{grace} ->set_value ($gr, $gr_m);
304 $STATWIDS->{title} ->set_text ("Title: " . $stats->{21});
305 $STATWIDS->{exp} ->set_text ("Exp.: " . ($stats->{11} || $stats->{28}));
306 $STATWIDS->{lvl} ->set_text ("Level: " . $stats->{12});
307
308 if (0) { # this code can vanish, just wanted to preserver it for a checkin
309 $STATWIDS->{st_str} ->set_text (sprintf "S%d", $stats->{5});
310 $STATWIDS->{st_dex} ->set_text (sprintf "D%d", $stats->{8});
311 $STATWIDS->{st_con} ->set_text (sprintf "Co%d", $stats->{9});
312 $STATWIDS->{st_int} ->set_text (sprintf "I%d", $stats->{6});
313 $STATWIDS->{st_wis} ->set_text (sprintf "W%d", $stats->{7});
314 $STATWIDS->{st_pow} ->set_text (sprintf "P%d", $stats->{22});
315 $STATWIDS->{st_cha} ->set_text (sprintf "Ch%d", $stats->{10});
316 $STATWIDS->{st_wc} ->set_text (sprintf "Wc%d", $stats->{13});
317 $STATWIDS->{st_ac} ->set_text (sprintf "Ac%d", $stats->{14});
318 $STATWIDS->{st_dam} ->set_text (sprintf "Dam%d", $stats->{15});
319 $STATWIDS->{st_arm} ->set_text (sprintf "Arm%d", $stats->{16});
320 $STATWIDS->{st_spd} ->set_text (sprintf "Sp%.1f", $stats->{17});
321 $STATWIDS->{st_wspd}->set_text (sprintf "WSp%.1f", $stats->{19});
322 } else {
323 $STATWIDS->{st_str} ->set_text (sprintf "%d", $stats->{5});
324 $STATWIDS->{st_dex} ->set_text (sprintf "%d", $stats->{8});
325 $STATWIDS->{st_con} ->set_text (sprintf "%d", $stats->{9});
326 $STATWIDS->{st_int} ->set_text (sprintf "%d", $stats->{6});
327 $STATWIDS->{st_wis} ->set_text (sprintf "%d", $stats->{7});
328 $STATWIDS->{st_pow} ->set_text (sprintf "%d", $stats->{22});
329 $STATWIDS->{st_cha} ->set_text (sprintf "%d", $stats->{10});
330 $STATWIDS->{st_wc} ->set_text (sprintf "%d", $stats->{13});
331 $STATWIDS->{st_ac} ->set_text (sprintf "%d", $stats->{14});
332 $STATWIDS->{st_dam} ->set_text (sprintf "%d", $stats->{15});
333 $STATWIDS->{st_arm} ->set_text (sprintf "%d", $stats->{16});
334 $STATWIDS->{st_spd} ->set_text (sprintf "%.1f", $stats->{17});
335 $STATWIDS->{st_wspd}->set_text (sprintf "%.1f", $stats->{19});
336 }
337
338 my %tbl = (
339 phys => 100,
340 magic => 101,
341 fire => 102,
342 elec => 103,
343 cold => 104,
344 conf => 105,
345 acid => 106,
346 drain => 107,
347 ghit => 108,
348 pois => 109,
349 slow => 110,
350 para => 111,
351 tund => 112,
352 fear => 113,
353 deat => 115,
354 holyw => 116,
355 blind => 117
356 );
357
358 for (keys %tbl) {
359 $STATWIDS->{"res_$_"}->set_text (sprintf "%d%", $stats->{$tbl{$_}});
360 }
361
362}
363
364sub metaserver_dialog {
365 my $dialog = new CFClient::UI::FancyFrame
366 title => "Metaserver",
367 child => (my $vbox = new CFClient::UI::VBox);
368
369 $vbox->add ($dialog->{table} = new CFClient::UI::Table);
370
371 $dialog
372}
373
374sub update_metaserver {
375 my ($HOST) = @_;
376
377 status "fetching metaserver list...";
378
379 my $buf;
380
381 my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
382
383 Event->io (fd => $fh, poll => 'r', cb => sub {
384 my $res = sysread $fh, $buf, 8192, length $buf;
385
386 if (!defined $res) {
387 $_[0]->w->cancel;
388 status "metaserver: $!";
389 } elsif ($res == 0) {
390 $_[0]->w->cancel;
391 status "server list retrieved";
392
393 my $table = $METASERVER->{table};
394
395 $table->clear;
396
397 my @col = qw(Use #Users Host Uptime Version Description);
398 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
399 for 0 .. $#col;
400
401 my @align = qw(1 0 1 1 -1);
402
403 my $y = 0;
404 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
405 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
406
407 for ($desc) {
408 s/<br>/\n/gi;
409 s/<li>/\n· /gi;
410 s/<.*?>//sgi;
411 s/&/&amp;/g;
412 s/</&lt;/g;
413 s/>/&gt;/g;
414 }
415
416 $uptime = sprintf "%dd %02d:%02d:%02d",
417 (int $m->[8] / 86400),
418 (int $m->[8] / 3600) % 24,
419 (int $m->[8] / 60) % 60,
420 $m->[8] % 60;
421
422 $m = [$users, $host, $uptime, $version, $desc];
423
424 $y++;
425
426 $table->add (0, $y, new CFClient::UI::VBox children => [
427 (new CFClient::UI::Button text => " ", connect_activate => sub {
428 $HOST->set_text ($CFG->{host} = $host);
429 }),
430 (new CFClient::UI::Empty expand => 1),
431 ]);
201 1 432
433 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
434 for 0 .. $#$m;
435 }
436 }
437 });
438}
439
440sub server_setup {
441 my $dialog = new CFClient::UI::FancyFrame
442 title => "Server Setup",
443 child => (my $vbox = new CFClient::UI::VBox);
444
445 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
446 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
447
448 {
449 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
450
451 $vbox->add (my $HOST = new CFClient::UI::Entry expand => 1, text => $CFG->{host}, connect_changed => sub {
452 my ($self, $value) = @_;
453 $CFG->{host} = $value;
454 });
455
456 $METASERVER = metaserver_dialog;
457
458 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
459 update_metaserver $HOST;
460 });
461 }
462
463 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
464 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
465 my ($self, $value) = @_;
466 $CFG->{user} = $value;
467 });
468
469 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
470 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
471 my ($self, $value) = @_;
472 $CFG->{password} = $value;
473 });
474
475 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
476 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub {
477 my ($self, $value) = @_;
478 $CFG->{say_command} = $value;
479 });
480
481 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
482 $table->add (1, 7, new CFClient::UI::Slider
483 req_w => 100,
484 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
485 connect_changed => sub {
486 my ($self, $value) = @_;
487
488 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
489 },
490 );
491
492 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
493 start_game;
494 });
495
496 $dialog
497}
498
499sub message_window {
500 my $window = new CFClient::UI::FancyFrame
501 title => "Messages",
502 border_bg => [1, 1, 1, 0.5],
503 bg => [0.3, 0.3, 0.3, 0.8],
504 user_w => int $::WIDTH / 3,
505 user_h => int $::HEIGHT / 5,
506 child => (my $vbox = new CFClient::UI::VBox);
507
508 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
509 expand => 1,
510 fontsize => $::CFG->{log_fontsize},
511 );
512
513 $vbox->add (my $input = new CFClient::UI::Entry
514 connect_focus_in => sub {
515 my ($input, $prev_focus) = @_;
516
517 delete $input->{refocus_map};
518
519 if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
520 $input->{refocus_map} = 1;
521 }
522 delete $input->{auto_activated};
523 },
524 connect_activate => sub {
525 my ($input, $text) = @_;
526 $input->set_text ('');
527
528 if ($text =~ /^\/(.*)/) {
529 $::CONN->user_send ($1);
202 } else { 530 } else {
203 undef $refresh_handler; 531 my $say_cmd = $::CFG->{say_command} || 'say';
532 $::CONN->user_send ("$say_cmd $text");
533 }
534 if ($input->{refocus_map}) {
535 delete $input->{refocus_map};
536 $MAPWIDGET->focus_in
537 }
538 },
539 connect_escape => sub {
540 $MAPWIDGET->focus_in
541 },
542 );
543
544 $CONSOLE = {
545 window => $window,
546 input => $input
547 };
548
549 $window
550}
551
552sub sdl_init {
553 CFClient::SDL_Init
554 and die "SDL::Init failed!\n";
555}
556
557sub video_init {
558 sdl_init;
559
560 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
561 $FULLSCREEN = $CFG->{fullscreen};
562 $FAST = $CFG->{fast};
563
564 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
565 or die "SDL_SetVideoMode failed!\n";
566
567 $SDL_ACTIVE = 1;
568
569 $LAST_REFRESH = time - 0.01;
570
571 CFClient::gl_init;
572
573 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
574
575 #############################################################################
576
577 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
578 $DEBUG_STATUS->show;
579
580 $STATUS_LINE = new CFClient::UI::Label
581 padding => 0,
582 y => $HEIGHT - $FONTSIZE * 1.8;
583 $STATUS_LINE->show;
584
585 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
586 padding => 0,
587 fontsize => 0.8,
588 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
589 $ALT_ENTER_MESSAGE->show;
590 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
591
592 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
593 $MAPWIDGET->focus_in;
594 $MAPWIDGET->connect (activate_console => sub {
595 my ($mapwidget, $preset) = @_;
596
597 if ($CONSOLE) {
598 $CONSOLE->{input}->{auto_activated} = 1;
599 $CONSOLE->{input}->focus_in;
600
601 if ($preset && $CONSOLE->{input}->get_text eq '') {
602 $CONSOLE->{input}->set_text ($preset);
603 }
604 }
605 });
606
607 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
608
609 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
610 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
611 $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
612
613 $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
614 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
615 status "Configuration Saved";
616 });
617
618 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
619
620
621 $CFClient::UI::ROOT->add (make_stats_window);
622}
623
624sub video_shutdown {
625 $CFClient::UI::ROOT->{children} = [];
626 undef $SDL_ACTIVE;
627}
628
629my @bgmusic = qw(game1.ogg game2.ogg game3.ogg game5.ogg game6.ogg ross1.ogg ross2.ogg ross3.ogg ross4.ogg ross5.ogg); #d#
630my $bgmusic;#TODO#hack#d#
631
632sub audio_music_finished {
633 return unless $CFG->{bgm_enable};
634
635 # TODO: hack, do play loop and mood music
636 $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/$bgmusic[0]";
637 $bgmusic->play (0);
638
639 push @bgmusic, shift @bgmusic;
640}
641
642sub audio_init {
643 if ($CFG->{audio_enable}) {
644 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
645 $SDL_MIXER = !CFClient::Mix_OpenAudio;
646 CFClient::Mix_AllocateChannels 8;
647 CFClient::MixMusic::volume $CFG->{bgm_volume} * 128;
648
649 audio_music_finished;
650
651 while (<$fh>) {
652 next if /^\s*#/;
653 next if /^\s*$/;
654
655 my ($file, $volume, $event) = split /\s+/, $_, 3;
656
657 push @SOUNDS, "$volume,$file";
658
659 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
660 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
661 $chunk->volume ($volume * 128 / 100);
662 $chunk
204 0 663 };
205 } 664 }
206 } else { 665 } else {
207 undef $refresh_handler; 666 status "unable to open sound config: $!";
208 0
209 } 667 }
210 }; 668 }
669}
670
671sub audio_shutdown {
672 CFClient::Mix_CloseAudio if $SDL_MIXER;
673 undef $SDL_MIXER;
674 @SOUNDS = ();
675 %AUDIO_CHUNKS = ();
676}
677
678my %animate_object;
679my $animate_timer;
680
681my $want_refresh;
682my $can_refresh;
683
684my $fps = 9;
685
686sub force_refresh {
687 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
688 debug sprintf "%3.2f", $fps;
689
690 $want_refresh = 0;
691 $can_refresh = 0;
692
693 $CFClient::UI::ROOT->draw;
694
695 CFClient::SDL_GL_SwapBuffers;
696
697 $LAST_REFRESH = $NOW;
698}
699
700my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
701 $NOW = time;
702
703 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
704 for CFClient::SDL_PollEvent;
705
706 if (%animate_object) {
707 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
708 $want_refresh++;
709 }
710
711 if ($want_refresh) {
712 force_refresh;
713 } else {
714 $can_refresh = 1;
715 }
716});
717
718sub refresh {
719 $want_refresh++;
211} 720}
212 721
213sub animation_start { 722sub animation_start {
214 my ($widget) = @_; 723 my ($widget) = @_;
215 $ANIMATE{$widget} = $widget; 724 $animate_object{$widget} = $widget;
216 Scalar::Util::weaken $ANIMATE{$widget};
217
218 refresh;
219} 725}
220 726
221sub animation_stop { 727sub animation_stop {
222 my ($widget) = @_; 728 my ($widget) = @_;
223 delete $ANIMATE{$widget}; 729 delete $animate_object{$widget};
730}
731
732@conn::ISA = Crossfire::Protocol::;
733
734sub conn::stats_update {
735 my ($self, $stats) = @_;
736
737 update_stats_window ($stats);
738}
739
740sub conn::user_send {
741 my ($self, $command) = @_;
742
743 $self->send_command ($command);
744 status $command;
745}
746
747sub conn::map_scroll {
748 my ($self, $dx, $dy) = @_;
749
750 $MAP->scroll ($dx, $dy);
751}
752
753sub conn::feed_map1a {
754 my ($self, $data) = @_;
755
756# $self->Crossfire::Protocol::feed_map1a ($data);
757
758 $MAP->map1a_update ($data);
759 $MAPWIDGET->update;
760}
761
762sub conn::flush_map {
763 my ($self) = @_;
764
765 my $map_info = delete $self->{map_info}
766 or return;
767
768 my ($hash, $x, $y, $w, $h) = @$map_info;
769
770 my $data = $MAP->get_rect ($x, $y, $w, $h);
771 $MAPCACHE->put ($hash => Compress::LZF::compress $data);
772 #warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
773}
774
775sub conn::map_clear {
776 my ($self) = @_;
777
778 $self->flush_map;
779 delete $self->{neigh_map};
780
781 $MAP->clear;
782}
783
784
785sub conn::load_map($$$) {
786 my ($self, $hash, $x, $y) = @_;
787
788 if (defined (my $data = $MAPCACHE->get ($hash))) {
789 $data = Compress::LZF::decompress $data;
790 #warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
791 for my $id ($MAP->set_rect ($x, $y, $data)) {
792 my $data = $TILECACHE->get ($id)
793 or next;
794
795 $self->set_texture ($id => $data);
796 }
797 }
798}
799
800# this method does a "flood fill" into every tile direction
801# it assumes that tiles are arranged in a rectangular grid,
802# i.e. a map is the same as the left of the right map etc.
803# failure to comply are harmless and result in display errors
804# at worst.
805sub conn::flood_fill {
806 my ($self, $gx, $gy, $path, $hash, $flags) = @_;
807
808 # the server does not allow map paths > 6
809 return if 6 <= length $path;
810
811 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
812
813 for (
814 [1, 0, -1],
815 [2, 1, 0],
816 [3, 0, 1],
817 [4, -1, 0],
818 ) {
819 my ($tile, $dx, $dy) = @$_;
820
821 my $gx = $gx + $dx;
822 my $gy = $gy + $dy;
823
824 next unless $flags & (1 << ($tile - 1));
825 next if $self->{neigh_grid}{$gx, $gy}++;
826
827 my $neigh = $self->{neigh_map}{$hash} ||= [];
828 if (my $info = $neigh->[$tile]) {
829 my ($flags, $x, $y, $w, $h, $hash) = @$info;
830
831 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
832 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
833
834 } else {
835 $self->send_mapinfo ("spatial $path$tile", sub {
836 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
837
838 return if $mode ne "spatial";
839
840 $x += $MAP->ox;
841 $y += $MAP->oy;
842
843 $self->load_map ($hash, $x, $y)
844 unless $self->{neigh_map}{$hash}[5]++;#d#
845
846 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
847
848 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
849 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
850 });
851 }
852 }
853}
854
855sub conn::map_change {
856 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
857
858 $self->flush_map;
859
860 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
861
862 my $mapmapw = 250;
863 my $mapmaph = 250;
864
865 $self->{neigh_rect} = [
866 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
867 $ox + $mapmapw * 0.5 + $w, $oy + $mapmapw * 0.5 + $h,
868 ];
869
870 delete $self->{neigh_grid};
871 $self->flood_fill (0, 0, "", $hash, $flags);
872
873 $x += $ox;
874 $y += $oy;
875
876 $self->{map_info} = [$hash, $x, $y, $w, $h];
877
878 $self->load_map ($hash, $x, $y);
879}
880
881sub conn::face_find {
882 my ($self, $facenum, $face) = @_;
883
884 my $hash = "$face->{chksum},$face->{name}";
885
886 my $id = $FACEMAP->get ($hash);
887
888 unless ($id) {
889 # create new id for face
890 # i love transactions
891 for (1..100) {
892 my $txn = $CFClient::DB_ENV->txn_begin;
893 my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
894 if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
895 $id++;
896 if ($FACEMAP->put (id => $id) == 0
897 && $FACEMAP->put ($hash => $id) == 0) {
898 $txn->txn_commit;
899
900 goto gotid;
901 }
902 }
903 $txn->abort;
904 }
905
906 CFClient::fatal "maximum number of transaction retries reached - database problems?";
907 }
908
909gotid:
910 $face->{id} = $id;
911 $MAP->set_face ($facenum => $id);
912 $TILECACHE->get ($id)
913}
914
915sub conn::face_update {
916 my ($self, $facenum, $face) = @_;
917
918 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
919
920 $self->set_texture ($face->{id} => delete $face->{image});
921}
922
923sub conn::set_texture {
924 my ($self, $id, $data) = @_;
925
926 $self->{texture}[$id] ||= do {
927 my $tex =
928 new_from_image CFClient::Texture
929 $data, minify => 1;
930
931 $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
932 $MAPWIDGET->update;
933
934 $tex
935 };
936}
937
938sub conn::sound_play {
939 my ($self, $x, $y, $soundnum, $type) = @_;
940
941 $SDL_MIXER
942 or return;
943
944 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
945 or return;
946
947 $chunk->play;
948# warn "sound $x,$y,$soundnum,$type\n";#d#
949}
950
951sub conn::query {
952 my ($self, $flags, $prompt) = @_;
953
954 #TODO, display dialog with relevant information
955 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
956}
957
958sub conn::drawinfo {
959 my ($self, $color, $text) = @_;
960
961 my @color = (
962 [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
963 [1.00, 1.00, 1.00],
964 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
965 [1.00, 0.00, 0.00],
966 [1.00, 0.54, 0.00],
967 [0.11, 0.56, 1.00],
968 [0.93, 0.46, 0.00],
969 [0.18, 0.54, 0.34],
970 [0.56, 0.73, 0.56],
971 [0.80, 0.80, 0.80],
972 [0.55, 0.41, 0.13],
973 [0.99, 0.77, 0.26],
974 [0.74, 0.65, 0.41],
975 );
976
977 $LOGVIEW->add_paragraph ($color[$color], $text);
978}
979
980sub conn::spell_add {
981 my ($self, $spell) = @_;
982
983 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message}, sub {
984 });
985 $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message}, sub {
986 });
987}
988
989sub conn::spell_delete {
990 my ($self, $spell) = @_;
991}
992
993sub conn::addme_success {
994 my ($self) = @_;
995
996 for my $skill (values %{$self->{skill_info}}) {
997 $MAPWIDGET->add_command ("ready_skill $skill", "", sub {
998 });
999 $MAPWIDGET->add_command ("use_skill $skill", "", sub {
1000 });
1001 }
224} 1002}
225 1003
226%SDL_CB = ( 1004%SDL_CB = (
227 SDL_QUIT() => sub { 1005 CFClient::SDL_QUIT => sub {
228 main_quit Gtk2; 1006 Event::unloop -1;
229 }, 1007 },
230 SDL_VIDEORESIZE() => sub { 1008 CFClient::SDL_VIDEORESIZE => sub {
231 }, 1009 },
232 SDL_VIDEOEXPOSE() => sub { 1010 CFClient::SDL_VIDEOEXPOSE => \&refresh,
233 refresh; 1011 CFClient::SDL_ACTIVEEVENT => sub {
1012# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
234 }, 1013 },
235 SDL_KEYDOWN() => sub { 1014 CFClient::SDL_KEYDOWN => sub {
236 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 1015 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
237 # alt-enter 1016 # alt-enter
238 $FULLSCREEN = !$FULLSCREEN; 1017 video_shutdown;
239 init_screen; 1018 $CFG->{fullscreen} = !$CFG->{fullscreen};
1019 video_init;
240 } else { 1020 } else {
241 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 1021 CFClient::UI::feed_sdl_key_down_event ($_[0]);
242 } 1022 }
243 }, 1023 },
244 SDL_KEYUP() => sub { 1024 CFClient::SDL_KEYUP => \&CFClient::UI::feed_sdl_key_up_event,
245 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 1025 CFClient::SDL_MOUSEMOTION => \&CFClient::UI::feed_sdl_motion_event,
246 }, 1026 CFClient::SDL_MOUSEBUTTONDOWN => \&CFClient::UI::feed_sdl_button_down_event,
247 SDL_MOUSEMOTION() => sub { 1027 CFClient::SDL_MOUSEBUTTONUP => \&CFClient::UI::feed_sdl_button_up_event,
248 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); 1028 CFClient::SDL_USEREVENT => \&audio_music_finished,
249 },
250 SDL_MOUSEBUTTONDOWN() => sub {
251 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
252 },
253 SDL_MOUSEBUTTONUP() => sub {
254 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
255 },
256 SDL_ACTIVEEVENT() => sub {
257 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
258 },
259); 1029);
260 1030
261@conn::ISA = Crossfire::Protocol::;
262
263sub conn::map_update {
264 my ($self, $dirty) = @_;
265
266 refresh;
267}
268
269sub conn::map_scroll {
270 my ($self, $dx, $dy) = @_;
271
272# refresh;
273}
274
275sub conn::map_clear {
276 my ($self) = @_;
277
278# refresh;
279}
280
281sub conn::face_find {
282 my ($self, $face) = @_;
283
284 $FACECACHE->{"$face->{chksum},$face->{name}"}
285}
286
287sub conn::face_update {
288 my ($self, $face) = @_;
289
290 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
291
292 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
293}
294
295sub conn::query {
296 my ($self, $flags, $prompt) = @_;
297
298 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
299}
300
301sub gtk_add_cfg_field {
302 my ($tbl, $cfg, $klbl, $key, $value) = @_;
303 my $i = $cfg->{_i}++;
304 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
305 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
306 if ($key eq 'password') {
307 $ent->set_invisible_char ("*");
308 $ent->set (visibility => 0)
309 }
310 $ent->set_text ($value);
311 $ent->signal_connect (changed => sub {
312 my ($ent) = @_;
313 $cfg->{$key} = $ent->get_text;
314 # TODO: Mapsize should be a slider in the game gui
315 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
316 $cfg->{$key} = 100;
317 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
318 $cfg->{$key} = 50;
319 }
320 });
321}
322
323sub run_config_dialog {
324 my (%events) = @_;
325
326 my $w = Gtk2::Window->new;
327
328 my @cfg = (
329 [qw/Host host/],
330 [qw/Port port/],
331 [qw/Mapsize% mapsize/],
332 [qw/Username user/],
333 [qw/Password password/],
334 );
335
336 my $cfg = {};
337
338 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
339 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
340
341 $w->add (my $vb = Gtk2::VBox->new);
342 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
343 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
344 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
345 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
346 my $i = 0;
347 my $act = 0;
348 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
349 if ($_ eq $selmode) { $act = $i }
350 $cb->append_text ($_);
351 $i++;
352 }
353 $cb->set_active ($act);
354 $cb->signal_connect (changed => sub {
355 my ($cb) = @_;
356 my $txt = $cb->get_active_text;
357 if ($txt =~ m/(\d+)x(\d+)/) {
358 $::CFG->{width} = $1;
359 $::CFG->{height} = $2;
360 }
361 });
362
363 $cfg->{_i} = 1;
364 for (@cfg) {
365 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
366 }
367
368 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
369 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
370 $cb->signal_connect (clicked => sub {
371 for (keys %$cfg) {
372 $::CFG->{$_} = $cfg->{$_}
373 if $_ ne '_i';
374 }
375 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
376 });
377 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
378 $cb->signal_connect (clicked => sub {
379 for (keys %$cfg) {
380 $::CFG->{$_} = $cfg->{$_}
381 if $_ ne '_i';
382 }
383 my $cb = $events{login} || sub {};
384 $cb->($::CFG->{user}, $::CFG->{password});
385 });
386 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
387 $cb->signal_connect (clicked => sub {
388 my $cb = $events{logout} || sub {};
389 $cb->();
390 });
391 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
392 $cb->signal_connect (clicked => sub { $w->destroy });
393
394 $w->show_all;
395
396 $w->signal_connect (destroy => sub { Gtk2->main_quit });
397}
398
399
400############################################################################# 1031#############################################################################
401 1032
402SDL::Init SDL_INIT_EVERYTHING; 1033$SIG{INT} = $SIG{TERM} = sub { exit };
403 1034
404my $mapwidget = Crossfire::Client::Widget::MapWidget->new; 1035$TILECACHE = CFClient::db_table "tilecache";
1036$FACEMAP = CFClient::db_table "facemap";
405 1037
406$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
407$mapwidget->focus_in;
408
409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 1038CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
410 1039
411$CFG ||= { 1040my %DEF_CFG = (
1041 sdl_mode => 0,
412 width => 640, 1042 width => 640,
413 height => 480, 1043 height => 480,
1044 fullscreen => 0,
1045 fast => 0,
1046 fow_enable => 1,
1047 fow_intensity => 0.45,
1048 fow_smooth => 0,
1049 gui_fontsize => 1,
1050 log_fontsize => 14,
414 mapsize => 100, 1051 mapsize => 100,
415 fullscreen => 0,
416 host => "crossfire.schmorp.de", 1052 host => "crossfire.schmorp.de",
417 port => 13327, 1053 say_command => 'say',
418}; 1054 audio_enable => 1,
1055 bgm_enable => 1,
1056 bgm_volume => 0.25,
1057);
419 1058
420Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 1059while (my ($k, $v) = each %DEF_CFG) {
1060 $CFG->{$k} = $v unless exists $CFG->{$k};
1061}
421 1062
422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 1063sdl_init;
423 1064
424run_config_dialog 1065@SDL_MODES = reverse
425 login => sub { start_game }, 1066 grep $_->[0] >= 640 && $_->[1] >= 480,
426 logout => sub { stop_game }; 1067 CFClient::SDL_ListModes;
427 1068
428main Gtk2; 1069@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
429 1070
430Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 1071$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
1072
1073{
1074 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
1075
1076 CFClient::add_font $_ for @fonts;
1077 CFClient::set_font $fonts[0];
1078}
1079
1080video_init;
1081audio_init;
1082
1083Event::loop;
1084
1085END { CFClient::SDL_Quit }
1086
1087

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines