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.137 by elmex, Tue Apr 18 21:31:48 2006 UTC vs.
Revision 1.150 by root, Thu Apr 20 07:12:58 2006 UTC

4use utf8; 4use utf8;
5 5
6use Time::HiRes 'time'; 6use Time::HiRes 'time';
7use Event; 7use Event;
8 8
9use SDL;
10use SDL::App;
11use SDL::Event;
12use SDL::Surface;
13
14use SDL::Mixer;
15use SDL::Sound;
16use SDL::Music;
17
18use SDL::OpenGL;
19
20use Crossfire; 9use Crossfire;
21use Crossfire::Protocol; 10use Crossfire::Protocol;
22 11
23use Compress::LZF; 12use Compress::LZF;
24 13
25use CFClient; 14use CFClient;
26use CFClient::UI; 15use CFClient::UI;
16use CFClient::MapWidget;
27 17
28our $VERSION = '0.1'; 18our $VERSION = '0.1';
29 19
30my $MAX_FPS = 60; 20my $MAX_FPS = 60;
31my $MIN_FPS = 5; # unused as of yet 21my $MIN_FPS = 5; # unused as of yet
57our $METASERVER; 47our $METASERVER;
58 48
59our $GAUGES; 49our $GAUGES;
60 50
61our $SDL_ACTIVE; 51our $SDL_ACTIVE;
62our $SDL_EV;
63our %SDL_CB; 52our %SDL_CB;
64 53
65our $SDL_MIXER; 54our $SDL_MIXER;
66our @SOUNDS; # event => file mapping 55our @SOUNDS; # event => file mapping
67our %AUDIO_CHUNKS; # audio files 56our %AUDIO_CHUNKS; # audio files
109 undef $CONN; 98 undef $CONN;
110} 99}
111 100
112sub client_setup { 101sub client_setup {
113 my $dialog = new CFClient::UI::FancyFrame 102 my $dialog = new CFClient::UI::FancyFrame
103 title => "Client Setup",
114 child => (my $vbox = new CFClient::UI::VBox); 104 child => (my $vbox = new CFClient::UI::VBox);
115 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
116 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); 105 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
117 106
118 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); 107 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
119 $table->add (1, 0, my $hbox = new CFClient::UI::HBox); 108 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
120 109
121 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); 110 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
122 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); 111 $hbox->add (my $mode_label = new CFClient::UI::Label align => 0, valign => 0, height => 0.8, template => "9999x9999");
123 112
124 $mode_slider->connect (changed => sub { 113 $mode_slider->connect (changed => sub {
125 my ($self, $value) = @_; 114 my ($self, $value) = @_;
126 115
127 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; 116 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
128 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); 117 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
129 }); 118 });
130 $mode_slider->emit (changed => $mode_slider->{range}[0]); 119 $mode_slider->emit (changed => $mode_slider->{range}[0]);
131 120
132 $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen"); 121 $table->add (0, 1, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
133 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub { 122 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
134 my ($self, $value) = @_; 123 my ($self, $value) = @_;
135 $CFG->{fullscreen} = $value; 124 $CFG->{fullscreen} = $value;
136 }); 125 });
137 126
138 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast & Ugly"); 127 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
139 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub { 128 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
140 my ($self, $value) = @_; 129 my ($self, $value) = @_;
141 $CFG->{fast} = $value; 130 $CFG->{fast} = $value;
142 }); 131 });
143 132
144 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Fog of War"); 133 $table->add (0, 3, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
145 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub { 134 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub {
146 my ($self, $value) = @_; 135 my ($self, $value) = @_;
147 $CFG->{fow_enable} = $value; 136 $CFG->{fow_enable} = $value;
148 }); 137 });
149 138
150 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "FoW Intensity"); 139 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
151 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub { 140 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
152 my ($self, $value) = @_; 141 my ($self, $value) = @_;
153 $CFG->{fow_intensity} = $value; 142 $CFG->{fow_intensity} = $value;
154 }); 143 });
155 144
156 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "FoW Smooth"); 145 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
157 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub { 146 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
158 my ($self, $value) = @_; 147 my ($self, $value) = @_;
159 $CFG->{fow_smooth} = $value; 148 $CFG->{fow_smooth} = $value;
160 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2; 149 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
161 }); 150 });
162 151
163 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Log Fontsize"); 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");
164 $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 8, 30, 1], connect_changed => sub { 159 $table->add (1, 7, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
165 my ($self, $value) = @_;
166 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = int $value); 160 $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
167 }); 161 });
168 162
169 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { 163 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
170 video_shutdown (); 164 video_shutdown ();
171 video_init (); 165 video_init ();
172 }); 166 });
173 167
174 $table->add (0, 8, new CFClient::UI::Label align => 1, text => "Sound"); 168 $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
175 $table->add (1, 8, new CFClient::UI::CheckBox state => $CFG->{sound}, connect_changed => sub {
176 my ($self, $value) = @_;
177 $CFG->{sound} = $value;
178 });
179 $table->add (0, 9, new CFClient::UI::Label align => 1, text => "Bg. Music");
180 $table->add (1, 9, new CFClient::UI::CheckBox state => $CFG->{bg_music_enable}, connect_changed => sub { 169 $table->add (1, 9, new CFClient::UI::CheckBox state => $CFG->{audio_enable}, connect_changed => sub {
181 my ($self, $value) = @_;
182 $CFG->{bg_music_enable} = $value; 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];
183 }); 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
184 $table->add (1, 10, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { 186 $table->add (1, 11, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
185 audio_shutdown (); 187 audio_shutdown ();
186 audio_init (); 188 audio_init ();
187 }); 189 });
188 190
189 $dialog 191 $dialog
190} 192}
191 193
192sub metaserver_dialog { 194sub metaserver_dialog {
193 my $dialog = new CFClient::UI::FancyFrame 195 my $dialog = new CFClient::UI::FancyFrame
196 title => "Metaserver",
194 child => (my $vbox = new CFClient::UI::VBox); 197 child => (my $vbox = new CFClient::UI::VBox);
195 198
196 $vbox->add ($dialog->{table} = new CFClient::UI::Table); 199 $vbox->add ($dialog->{table} = new CFClient::UI::Table);
197 200
198 $dialog 201 $dialog
255 $HOST->set_text ($CFG->{host} = $host); 258 $HOST->set_text ($CFG->{host} = $host);
256 }), 259 }),
257 (new CFClient::UI::Empty expand => 1), 260 (new CFClient::UI::Empty expand => 1),
258 ]); 261 ]);
259 262
260 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => $FONTSIZE * 0.8) 263 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
261 for 0 .. $#$m; 264 for 0 .. $#$m;
262 } 265 }
263 } 266 }
264 }); 267 });
265} 268}
266 269
267sub server_setup { 270sub server_setup {
268 my $dialog = new CFClient::UI::FancyFrame 271 my $dialog = new CFClient::UI::FancyFrame
272 title => "Server Setup",
269 child => (my $vbox = new CFClient::UI::VBox); 273 child => (my $vbox = new CFClient::UI::VBox);
270 274
271 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup");
272 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); 275 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
273 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host:Port"); 276 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
274 277
275 { 278 {
276 $table->add (1, 2, my $vbox = new CFClient::UI::VBox); 279 $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
277 280
278 $vbox->add (my $HOST = new CFClient::UI::Entry text => $CFG->{host}, connect_changed => sub { 281 $vbox->add (my $HOST = new CFClient::UI::Entry expand => 1, text => $CFG->{host}, connect_changed => sub {
279 my ($self, $value) = @_; 282 my ($self, $value) = @_;
280 $CFG->{host} = $value; 283 $CFG->{host} = $value;
281 }); 284 });
282 285
283 $METASERVER = metaserver_dialog; 286 $METASERVER = metaserver_dialog;
284 287
285 $vbox->add (new CFClient::UI::Flopper text => "Metaserver", other => $METASERVER, connect_open => sub { 288 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
286 update_metaserver $HOST; 289 update_metaserver $HOST;
287 }); 290 });
288 } 291 }
289 292
290 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username"); 293 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
291 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub { 294 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
292 my ($self, $value) = @_; 295 my ($self, $value) = @_;
293 $CFG->{user} = $value; 296 $CFG->{user} = $value;
294 }); 297 });
295 298
296 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password"); 299 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
297 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub { 300 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
298 my ($self, $value) = @_; 301 my ($self, $value) = @_;
299 $CFG->{password} = $value; 302 $CFG->{password} = $value;
300 }); 303 });
301 304
302 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Def. say cmd"); 305 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
303 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub { 306 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub {
304 my ($self, $value) = @_; 307 my ($self, $value) = @_;
305 $CFG->{say_command} = $value; 308 $CFG->{say_command} = $value;
306 }); 309 });
307 310
308 $table->add (0, 7, new CFClient::UI::Label align => 1, text => "Map Size"); 311 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
309 $table->add (1, 7, new CFClient::UI::Slider 312 $table->add (1, 7, new CFClient::UI::Slider
310 req_w => 100, 313 req_w => 100,
311 range => [$CFG->{mapsize}, 10, 100 + 1, 1], 314 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
312 connect_changed => sub { 315 connect_changed => sub {
313 my ($self, $value) = @_; 316 my ($self, $value) = @_;
323 $dialog 326 $dialog
324} 327}
325 328
326sub message_window { 329sub message_window {
327 my $window = new CFClient::UI::FancyFrame 330 my $window = new CFClient::UI::FancyFrame
331 title => "Messages",
328 border_bg => [1, 1, 1, 0.5], 332 border_bg => [1, 1, 1, 0.5],
329 bg => [0.3, 0.3, 0.3, 0.8], 333 bg => [0.3, 0.3, 0.3, 0.8],
330 user_w => int $::WIDTH / 3, 334 user_w => int $::WIDTH / 3,
331 user_h => int $::HEIGHT / 5, 335 user_h => int $::HEIGHT / 5,
332 child => (my $vbox = new CFClient::UI::VBox); 336 child => (my $vbox = new CFClient::UI::VBox);
374 378
375 $window 379 $window
376} 380}
377 381
378sub sdl_init { 382sub sdl_init {
379 #SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE 383 CFClient::SDL_Init
380 SDL::Init SDL_INIT_AUDIO | SDL_INIT_VIDEO
381 and die "SDL::Init failed!\n"; 384 and die "SDL::Init failed!\n";
382} 385}
383 386
384sub video_init { 387sub video_init {
385 sdl_init; 388 sdl_init;
386 389
387 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 390 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
388 $FULLSCREEN = $CFG->{fullscreen}; 391 $FULLSCREEN = $CFG->{fullscreen};
389 $FAST = $CFG->{fast}; 392 $FAST = $CFG->{fast};
390 393
391 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5; 394 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
392 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
393 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
394 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 1;
395
396 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
397 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
398 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
399 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
400
401 SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1;
402 SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15;
403 SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0;
404
405 SDL::SetVideoMode $WIDTH, $HEIGHT, 0,
406 SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF
407 | ($FULLSCREEN ? SDL_FULLSCREEN : 0)
408 or die "SDL::SetVideoMode failed!\n"; 395 or die "SDL_SetVideoMode failed!\n";
409
410 SDL::WMSetCaption "Crossfire+ Client", "Crossfire+";
411
412 $SDL_EV = new SDL::Event;
413 $SDL_EV->set_unicode (1);
414 396
415 $SDL_ACTIVE = 1; 397 $SDL_ACTIVE = 1;
416 398
417 $LAST_REFRESH = time - 0.01; 399 $LAST_REFRESH = time - 0.01;
418 400
419 CFClient::gl_init; 401 CFClient::gl_init;
420 402
421 $FONTSIZE = int $HEIGHT / 40; 403 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
422 404
423 ############################################################################# 405 #############################################################################
424 406
425 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100; 407 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
426 $CFClient::UI::ROOT->add ($DEBUG_STATUS); 408 $DEBUG_STATUS->show;
427 409
428 $STATUS_LINE = new CFClient::UI::Label 410 $STATUS_LINE = new CFClient::UI::Label
429 padding => 0, 411 padding => 0,
430 y => $HEIGHT * 44 / 45 - $FONTSIZE; 412 y => $HEIGHT - $FONTSIZE * 1.8;
431 $CFClient::UI::ROOT->add ($STATUS_LINE); 413 $STATUS_LINE->show;
432 414
433 $ALT_ENTER_MESSAGE = new CFClient::UI::Label 415 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
434 padding => 0, 416 padding => 0,
435 y => $HEIGHT * 44 / 45,
436 fontsize => $HEIGHT / 45, 417 fontsize => 0.8,
437 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 418 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
438 $CFClient::UI::ROOT->add ($ALT_ENTER_MESSAGE); 419 $ALT_ENTER_MESSAGE->show;
420 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
439 421
440 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::UI::MapWidget); 422 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
441 $MAPWIDGET->focus_in; 423 $MAPWIDGET->focus_in;
442 $MAPWIDGET->connect (activate_console => sub { 424 $MAPWIDGET->connect (activate_console => sub {
443 my ($mapwidget, $preset) = @_; 425 my ($mapwidget, $preset) = @_;
444 426
445 if ($CONSOLE) { 427 if ($CONSOLE) {
463 status "Configuration Saved"; 445 status "Configuration Saved";
464 }); 446 });
465 447
466 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup 448 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
467 449
468 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 300, y => 0); 450 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 300, y => 0, title => "Stats");
469 $tgw->add (my $hbox = new CFClient::UI::HBox ()); 451 $tgw->add (my $hbox = new CFClient::UI::HBox ());
470 452
471 $hbox->add (my $hg = new CFClient::UI::VGauge (gauge => 'hp')); 453 $hbox->add (my $hg = new CFClient::UI::Gauge (type => 'hp'));
472 $hbox->add (my $mg = new CFClient::UI::VGauge (gauge => 'mana')); 454 $hbox->add (my $mg = new CFClient::UI::Gauge (type => 'mana'));
473 $hbox->add (my $gg = new CFClient::UI::VGauge (gauge => 'grace')); 455 $hbox->add (my $gg = new CFClient::UI::Gauge (type => 'grace'));
474 $hbox->add (my $fg = new CFClient::UI::VGauge (gauge => 'food')); 456 $hbox->add (my $fg = new CFClient::UI::Gauge (type => 'food'));
475 457
476 $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg }; 458 $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
477 $CFClient::UI::ROOT->add ($tgw); 459 $CFClient::UI::ROOT->add ($tgw);
478} 460}
479 461
480sub video_shutdown { 462sub video_shutdown {
481 $CFClient::UI::ROOT->{children} = []; 463 $CFClient::UI::ROOT->{children} = [];
482 undef $SDL_ACTIVE; 464 undef $SDL_ACTIVE;
483 undef $SDL_EV;
484} 465}
485 466
486my $bgmusic;#TODO#hack#d# 467my $bgmusic;#TODO#hack#d#
487 468
488sub audio_init { 469sub audio_init {
489 if ($CFG->{sound} || 1) { 470 if ($CFG->{audio_enable}) {
490 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") { 471 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
491 $SDL_MIXER = new SDL::Mixer; 472
492 $SDL_MIXER->allocate_channels (8); 473 $SDL_MIXER = !CFClient::Mix_OpenAudio;
474 CFClient::Mix_AllocateChannels 8;
475 CFClient::MixMusic::volume $CFG->{bgm_volume} * 128;
493 476
477 # TODO: hack, do play loop and mood music
494 if ($CFG->{bg_music_enable}) { 478 if ($CFG->{bgm_enable}) {
495 $bgmusic = new SDL::Music CFClient::find_rcfile "music/game3.ogg"; 479 $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/game3.ogg";
496 $SDL_MIXER->play_music ($bgmusic, -1); 480 $bgmusic->play;
497 } 481 }
498 482
499 while (<$fh>) { 483 while (<$fh>) {
500 next if /^\s*#/; 484 next if /^\s*#/;
501 next if /^\s*$/; 485 next if /^\s*$/;
503 my ($file, $volume, $event) = split /\s+/, $_, 3; 487 my ($file, $volume, $event) = split /\s+/, $_, 3;
504 488
505 push @SOUNDS, "$volume,$file"; 489 push @SOUNDS, "$volume,$file";
506 490
507 $AUDIO_CHUNKS{"$volume,$file"} ||= do { 491 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
508 my $chunk = new SDL::Sound CFClient::find_rcfile "sounds/$file"; 492 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
509 $chunk->volume ($volume * 128 / 100); 493 $chunk->volume ($volume * 128 / 100);
510 $chunk 494 $chunk
511 }; 495 };
512 } 496 }
513 } else { 497 } else {
515 } 499 }
516 } 500 }
517} 501}
518 502
519sub audio_shutdown { 503sub audio_shutdown {
504 CFClient::Mix_CloseAudio if $SDL_MIXER;
520 undef $SDL_MIXER; 505 undef $SDL_MIXER;
521 @SOUNDS = (); 506 @SOUNDS = ();
522 %AUDIO_CHUNKS = (); 507 %AUDIO_CHUNKS = ();
523} 508}
524 509
537 $want_refresh = 0; 522 $want_refresh = 0;
538 $can_refresh = 0; 523 $can_refresh = 0;
539 524
540 $CFClient::UI::ROOT->draw; 525 $CFClient::UI::ROOT->draw;
541 526
542 SDL::GLSwapBuffers; 527 CFClient::SDL_GL_SwapBuffers;
543 528
544 $LAST_REFRESH = $NOW; 529 $LAST_REFRESH = $NOW;
545} 530}
546 531
547my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub { 532my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
548 $NOW = time; 533 $NOW = time;
549 534
550 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() 535 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
551 while $SDL_EV->poll; 536 for CFClient::SDL_PollEvent;
552 537
553 if (%animate_object) { 538 if (%animate_object) {
554 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; 539 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
555 $want_refresh++; 540 $want_refresh++;
556 } 541 }
586 my $hp = $stats->{1}; 571 my $hp = $stats->{1};
587 my $hp_m = $stats->{2}; 572 my $hp_m = $stats->{2};
588 my $sp = $stats->{3}; 573 my $sp = $stats->{3};
589 my $sp_m = $stats->{4}; 574 my $sp_m = $stats->{4};
590 my $fo = $stats->{18}; 575 my $fo = $stats->{18};
591 my $fo_m = 1000; 576 my $fo_m = 999;
592 my $gr = $stats->{23}; 577 my $gr = $stats->{23};
593 my $gr_m = $stats->{24}; 578 my $gr_m = $stats->{24};
594 579
595 #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n"; 580 #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
596 $GAUGES->{hp}->set_value ($hp, $hp_m); 581 $GAUGES->{hp} ->set_value ($hp, $hp_m);
597 $GAUGES->{mana}->set_value ($sp, $sp_m); 582 $GAUGES->{mana} ->set_value ($sp, $sp_m);
598 $GAUGES->{food}->set_value ($fo, $fo_m); 583 $GAUGES->{food} ->set_value ($fo, $fo_m);
599 $GAUGES->{grace}->set_value ($gr, $gr_m); 584 $GAUGES->{grace}->set_value ($gr, $gr_m);
600} 585}
601 586
602sub conn::user_send { 587sub conn::user_send {
603 my ($self, $command) = @_; 588 my ($self, $command) = @_;
638 623
639sub conn::map_clear { 624sub conn::map_clear {
640 my ($self) = @_; 625 my ($self) = @_;
641 626
642 $self->flush_map; 627 $self->flush_map;
643 delete $self->{neigh}; 628 delete $self->{neigh_map};
644 629
645 $MAP->clear; 630 $MAP->clear;
646} 631}
647 632
648 633
660 } 645 }
661 } 646 }
662} 647}
663 648
664sub conn::flood_fill { 649sub conn::flood_fill {
665 my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_; 650 my ($self, $gx, $gy, $path, $hash, $flags) = @_;
666 651
667 # the server does not allow map paths > 6 652 # the server does not allow map paths > 6
668 return if 6 <= length $path; 653 return if 6 <= length $path;
669 654
670 for my $tile (1..4) { 655 my ($x0, $y0, $x1, $y1) = @{$self->{neigh_rect}};
671 next if $self->{neigh}{$hash}[$tile]; 656
657 for (
658 [1, 0, -1],
659 [2, 1, 0],
660 [3, 0, 1],
661 [4, -1, 0],
662 ) {
663 my ($tile, $dx, $dy) = @$_;
664
665 my $gx = $gx + $dx;
666 my $gy = $gy + $dy;
667
672 next unless $flags & (1 << ($tile - 1)); 668 next unless $flags & (1 << ($tile - 1));
669 next if $self->{neigh_grid}{$gx, $gy}++;
673 670
674 my $neigh = $self->{neigh}{$hash} ||= []; 671 my $neigh = $self->{neigh_map}{$hash} ||= [];
675 672 if (my $info = $neigh->[$tile]) {
676 $self->send_mapinfo ("spatial $path$tile", sub {
677 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_; 673 my ($flags, $x, $y, $w, $h, $hash) = @$info;
678 674
679 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
680 return if $mode ne "spatial";
681
682 $x += $MAP->ox;
683 $y += $MAP->oy;
684
685 $self->load_map ($hash, $x, $y)
686 unless $self->{neigh}{$hash}[5]++;#d#
687
688 $neigh->[$tile] = [$x, $y, $w, $h];
689
690 $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1) 675 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
691 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1; 676 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
677
678 } else {
679 $self->send_mapinfo ("spatial $path$tile", sub {
680 my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
681
682 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
683 return if $mode ne "spatial";
684
685 $x += $MAP->ox;
686 $y += $MAP->oy;
687
688 $self->load_map ($hash, $x, $y)
689 unless $self->{neigh_map}{$hash}[5]++;#d#
690
691 $neigh->[$tile] = [$flags, $x, $y, $w, $h, $hash];
692
693 $self->flood_fill ($gx, $gy, "$path$tile", $hash, $flags)
694 if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
695 });
692 }); 696 }
693 } 697 }
694} 698}
695 699
696sub conn::map_change { 700sub conn::map_change {
697 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_; 701 my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
700 704
701 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy); 705 my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
702 706
703 my $mapmapw = 250; 707 my $mapmapw = 250;
704 my $mapmaph = 250; 708 my $mapmaph = 250;
709
710 $self->{neigh_rect} = [
711 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
712 $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5,
713 ];
705 714
715 delete $self->{neigh_grid};
706 $self->flood_fill ("", $hash, $flags, 716 $self->flood_fill (0, 0, "", $hash, $flags);
707 $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
708 $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5);
709 717
710 $x += $ox; 718 $x += $ox;
711 $y += $oy; 719 $y += $oy;
712 720
713 $self->{map_info} = [$hash, $x, $y, $w, $h]; 721 $self->{map_info} = [$hash, $x, $y, $w, $h];
773} 781}
774 782
775sub conn::sound_play { 783sub conn::sound_play {
776 my ($self, $x, $y, $soundnum, $type) = @_; 784 my ($self, $x, $y, $soundnum, $type) = @_;
777 785
786 $SDL_MIXER
787 or return;
788
778 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]} 789 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
779 or return; 790 or return;
780 791
781 $SDL_MIXER->play_channel (-1, $chunk); 792 $chunk->play;
782 warn "sound $x,$y,$soundnum,$type\n";#d# 793# warn "sound $x,$y,$soundnum,$type\n";#d#
783} 794}
784 795
785sub conn::query { 796sub conn::query {
786 my ($self, $flags, $prompt) = @_; 797 my ($self, $flags, $prompt) = @_;
787 798
788 #TODO 799 #TODO, display dialog with relevant information
789 warn "<<<<QUERY:$flags:$prompt>>>\n";#d# 800 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
790} 801}
791 802
792sub conn::drawinfo { 803sub conn::drawinfo {
793 my ($self, $color, $text) = @_; 804 my ($self, $color, $text) = @_;
809 ); 820 );
810 821
811 $LOGVIEW->add_paragraph ($color[$color], $text); 822 $LOGVIEW->add_paragraph ($color[$color], $text);
812} 823}
813 824
825sub conn::spell_add {
826 my ($self, $spell) = @_;
827
828 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message}, sub {
829 });
830 $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message}, sub {
831 });
832}
833
834sub conn::spell_delete {
835 my ($self, $spell) = @_;
836}
837
838sub conn::addme_success {
839 my ($self) = @_;
840
841 for my $skill (values %{$self->{skill_info}}) {
842 $MAPWIDGET->add_command ("ready_skill $skill", "", sub {
843 });
844 $MAPWIDGET->add_command ("use_skill $skill", "", sub {
845 });
846 }
847}
848
814%SDL_CB = ( 849%SDL_CB = (
815 SDL_QUIT() => sub { 850 CFClient::SDL_QUIT => sub {
816 Event::unloop -1; 851 Event::unloop -1;
817 }, 852 },
818 SDL_VIDEORESIZE() => sub { 853 CFClient::SDL_VIDEORESIZE => sub {
819 }, 854 },
820 SDL_VIDEOEXPOSE() => sub { 855 CFClient::SDL_VIDEOEXPOSE => sub {
821 refresh; 856 refresh;
822 }, 857 },
823 SDL_KEYDOWN() => sub { 858 CFClient::SDL_KEYDOWN => sub {
824 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 859 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
825 # alt-enter 860 # alt-enter
826 video_shutdown; 861 video_shutdown;
827 $CFG->{fullscreen} = !$CFG->{fullscreen}; 862 $CFG->{fullscreen} = !$CFG->{fullscreen};
828 video_init; 863 video_init;
829 } else { 864 } else {
830 CFClient::UI::feed_sdl_key_down_event ($SDL_EV); 865 CFClient::UI::feed_sdl_key_down_event ($_[0]);
831 } 866 }
832 }, 867 },
833 SDL_KEYUP() => sub { 868 CFClient::SDL_KEYUP => sub {
834 CFClient::UI::feed_sdl_key_up_event ($SDL_EV); 869 CFClient::UI::feed_sdl_key_up_event ($_[0]);
835 }, 870 },
836 SDL_MOUSEMOTION() => sub { 871 CFClient::SDL_MOUSEMOTION => sub {
837 CFClient::UI::feed_sdl_motion_event ($SDL_EV); 872 CFClient::UI::feed_sdl_motion_event ($_[0]);
838 }, 873 },
839 SDL_MOUSEBUTTONDOWN() => sub { 874 CFClient::SDL_MOUSEBUTTONDOWN => sub {
840 CFClient::UI::feed_sdl_button_down_event ($SDL_EV); 875 CFClient::UI::feed_sdl_button_down_event ($_[0]);
841 }, 876 },
842 SDL_MOUSEBUTTONUP() => sub { 877 CFClient::SDL_MOUSEBUTTONUP => sub {
843 CFClient::UI::feed_sdl_button_up_event ($SDL_EV); 878 CFClient::UI::feed_sdl_button_up_event ($_[0]);
844 }, 879 },
845 SDL_ACTIVEEVENT() => sub { 880 CFClient::SDL_ACTIVEEVENT => sub {
846# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 881# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
847 }, 882 },
848); 883);
849 884
850############################################################################# 885#############################################################################
863 fullscreen => 0, 898 fullscreen => 0,
864 fast => 0, 899 fast => 0,
865 fow_enable => 1, 900 fow_enable => 1,
866 fow_intensity => 0.45, 901 fow_intensity => 0.45,
867 fow_smooth => 0, 902 fow_smooth => 0,
903 gui_fontsize => 1,
868 log_fontsize => 14, 904 log_fontsize => 14,
869 mapsize => 100, 905 mapsize => 100,
870 host => "crossfire.schmorp.de", 906 host => "crossfire.schmorp.de",
871 say_command => 'say', 907 say_command => 'say',
908 audio_enable => 1,
909 bgm_enable => 1,
910 bgm_volume => 0.25,
872); 911);
873 912
874while (my ($k, $v) = each %DEF_CFG) { 913while (my ($k, $v) = each %DEF_CFG) {
875 $CFG->{$k} = $v unless exists $CFG->{$k}; 914 $CFG->{$k} = $v unless exists $CFG->{$k};
876} 915}
877 916
878sdl_init; 917sdl_init;
879 918
880@SDL_MODES = reverse 919@SDL_MODES = reverse
881 grep $_->[0] >= 640 && $_->[1] >= 480, 920 grep $_->[0] >= 640 && $_->[1] >= 480,
882 map [SDL::RectW ($_), SDL::RectH ($_)], 921 CFClient::SDL_ListModes;
883 @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL };
884 922
885@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)"; 923@SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
886 924
887$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES; 925$CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
888 926
896video_init; 934video_init;
897audio_init; 935audio_init;
898 936
899Event::loop; 937Event::loop;
900 938
901END { SDL::Quit } 939END { CFClient::SDL_Quit }
902 940
903 941

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines