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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines