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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines