… | |
… | |
21 | |
21 | |
22 | my $self = $class->SUPER::new ( |
22 | my $self = $class->SUPER::new ( |
23 | z => -1, |
23 | z => -1, |
24 | can_focus => 1, |
24 | can_focus => 1, |
25 | list => glGenList, |
25 | list => glGenList, |
26 | |
26 | tilesize => 32, |
27 | smooth_matrix => [ |
|
|
28 | 0.05, 0.13, 0.05, |
|
|
29 | 0.13, 0.30, 0.13, |
|
|
30 | 0.05, 0.13, 0.05, |
|
|
31 | ], |
|
|
32 | |
|
|
33 | @_ |
27 | @_ |
34 | ); |
28 | ); |
35 | |
29 | |
36 | $self->{completer} = new CFPlus::MapWidget::Command:: |
30 | $self->{completer} = new CFPlus::MapWidget::Command:: |
37 | command => $self->{command}, |
31 | command => $self->{command}, |
38 | tooltip => "#completer_help", |
32 | tooltip => "#completer_help", |
39 | ; |
33 | ; |
40 | |
34 | |
41 | $self |
35 | $self |
|
|
36 | } |
|
|
37 | |
|
|
38 | sub set_tilesize { |
|
|
39 | my ($self, $tilesize) = @_; |
|
|
40 | |
|
|
41 | $self->{tilesize} = $tilesize; |
42 | } |
42 | } |
43 | |
43 | |
44 | sub add_command { |
44 | sub add_command { |
45 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
45 | my ($self, $command, $tooltip, $widget, $cb) = @_; |
46 | |
46 | |
… | |
… | |
177 | |
177 | |
178 | if ($ev->{button} == 1) { |
178 | if ($ev->{button} == 1) { |
179 | $self->grab_focus; |
179 | $self->grab_focus; |
180 | return unless $::CONN; |
180 | return unless $::CONN; |
181 | |
181 | |
182 | my $x = $self->{dx} + CFPlus::floor +($ev->{x} - $self->{sx0}) / $self->{tilesize}; |
182 | my $x = $self->{dx} + CFPlus::floor +($ev->{x} - $self->{sx0}) / $self->{ctilesize}; |
183 | my $y = $self->{dy} + CFPlus::floor +($ev->{y} - $self->{sy0}) / $self->{tilesize}; |
183 | my $y = $self->{dy} + CFPlus::floor +($ev->{y} - $self->{sy0}) / $self->{ctilesize}; |
184 | |
184 | |
185 | $x -= CFPlus::floor $::MAP->w * 0.5; |
185 | $x -= CFPlus::floor $::MAP->w * 0.5; |
186 | $y -= CFPlus::floor $::MAP->h * 0.5; |
186 | $y -= CFPlus::floor $::MAP->h * 0.5; |
187 | |
187 | |
188 | if ($::CONN) { |
188 | if ($::CONN) { |
… | |
… | |
285 | |
285 | |
286 | 1 |
286 | 1 |
287 | } |
287 | } |
288 | |
288 | |
289 | sub size_request { |
289 | sub size_request { |
|
|
290 | my ($self) = @_; |
|
|
291 | |
290 | ( |
292 | ( |
291 | 32 * CFPlus::ceil $::WIDTH / 32, |
293 | $self->{tilesize} * CFPlus::ceil $::WIDTH / $self->{tilesize}, |
292 | 32 * CFPlus::ceil $::HEIGHT / 32, |
294 | $self->{tilesize} * CFPlus::ceil $::HEIGHT / $self->{tilesize}, |
293 | ) |
295 | ) |
294 | } |
296 | } |
295 | |
297 | |
296 | sub update { |
298 | sub update { |
297 | my ($self) = @_; |
299 | my ($self) = @_; |
… | |
… | |
299 | $self->{need_update} = 1; |
301 | $self->{need_update} = 1; |
300 | $self->SUPER::update; |
302 | $self->SUPER::update; |
301 | } |
303 | } |
302 | |
304 | |
303 | my %DIR = ( |
305 | my %DIR = ( |
|
|
306 | CFPlus::SDLK_KP5, [0, "stay fire"], |
304 | CFPlus::SDLK_KP8, [1, "north"], |
307 | CFPlus::SDLK_KP8, [1, "north"], |
305 | CFPlus::SDLK_KP9, [2, "northeast"], |
308 | CFPlus::SDLK_KP9, [2, "northeast"], |
306 | CFPlus::SDLK_KP6, [3, "east"], |
309 | CFPlus::SDLK_KP6, [3, "east"], |
307 | CFPlus::SDLK_KP3, [4, "southeast"], |
310 | CFPlus::SDLK_KP3, [4, "southeast"], |
308 | CFPlus::SDLK_KP2, [5, "south"], |
311 | CFPlus::SDLK_KP2, [5, "south"], |
… | |
… | |
340 | } elsif ($sym == CFPlus::SDLK_F9 && !$mod) { |
343 | } elsif ($sym == CFPlus::SDLK_F9 && !$mod) { |
341 | $::SETUP_DIALOG->toggle_visibility; |
344 | $::SETUP_DIALOG->toggle_visibility; |
342 | } elsif (!$::CONN) { |
345 | } elsif (!$::CONN) { |
343 | return 0; # bindings further down need a valid connection |
346 | return 0; # bindings further down need a valid connection |
344 | |
347 | |
345 | } elsif ($sym == CFPlus::SDLK_KP5 && !$mod) { |
|
|
346 | $::CONN->user_send ("stay fire"); |
|
|
347 | } elsif ($uni == ord ",") { |
348 | } elsif ($uni == ord ",") { |
348 | $::CONN->user_send ("take"); |
349 | $::CONN->user_send ("take"); |
349 | } elsif ($uni == ord " ") { |
350 | } elsif ($uni == ord " ") { |
350 | $::CONN->user_send ("apply"); |
351 | $::CONN->user_send ("apply"); |
351 | } elsif ($uni == 13) { |
352 | } elsif ($uni == 13) { |
… | |
… | |
422 | } |
423 | } |
423 | |
424 | |
424 | $res |
425 | $res |
425 | } |
426 | } |
426 | |
427 | |
|
|
428 | sub invoke_visibility_change { |
|
|
429 | my ($self) = @_; |
|
|
430 | |
|
|
431 | $self->refresh_hook; |
|
|
432 | |
|
|
433 | 0 |
|
|
434 | } |
|
|
435 | |
427 | sub set_magicmap { |
436 | sub set_magicmap { |
428 | my ($self, $w, $h, $x, $y, $data) = @_; |
437 | my ($self, $w, $h, $x, $y, $data) = @_; |
429 | |
438 | |
430 | $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; |
439 | $x -= $::MAP->ox + 1 + int 0.5 * $::MAP->w; |
431 | $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; |
440 | $y -= $::MAP->oy + 1 + int 0.5 * $::MAP->h; |
… | |
… | |
433 | $self->{magicmap} = [$x, $y, $w, $h, $data]; |
442 | $self->{magicmap} = [$x, $y, $w, $h, $data]; |
434 | |
443 | |
435 | $self->update; |
444 | $self->update; |
436 | } |
445 | } |
437 | |
446 | |
438 | sub draw { |
447 | sub refresh_hook { |
439 | my ($self) = @_; |
448 | my ($self) = @_; |
440 | |
449 | |
441 | return unless $::MAP; |
450 | return unless $::MAP; |
442 | |
451 | |
443 | my $focused = $CFPlus::UI::FOCUS == $self |
|
|
444 | || $CFPlus::UI::FOCUS == $self->{completer}{entry}; |
|
|
445 | |
|
|
446 | return |
|
|
447 | unless $focused || !$::FAST; |
|
|
448 | |
|
|
449 | if (delete $self->{need_update}) { |
452 | if (delete $self->{need_update}) { |
450 | my $tilesize = $self->{tilesize} = int 32 * $::CFG->{map_scale}; |
453 | my $tilesize = $self->{ctilesize} = (int $self->{tilesize} * $::CFG->{map_scale}) || 1; |
451 | |
454 | |
452 | my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize; |
455 | my $sw = $self->{sw} = 1 + CFPlus::ceil $self->{w} / $tilesize; |
453 | my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize; |
456 | my $sh = $self->{sh} = 1 + CFPlus::ceil $self->{h} / $tilesize; |
454 | |
457 | |
455 | my $sx = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize; |
458 | my $sx = CFPlus::ceil $::CFG->{map_shift_x} / $tilesize; |
… | |
… | |
461 | my $dx = $self->{dx} = CFPlus::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
464 | my $dx = $self->{dx} = CFPlus::ceil 0.5 * ($::MAP->w - $sw) - $sx; |
462 | my $dy = $self->{dy} = CFPlus::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
465 | my $dy = $self->{dy} = CFPlus::ceil 0.5 * ($::MAP->h - $sh) - $sy; |
463 | |
466 | |
464 | if ($::CFG->{fow_enable}) { |
467 | if ($::CFG->{fow_enable}) { |
465 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
468 | my ($w, $h, $data) = $::MAP->fow_texture ($dx, $dy, $sw, $sh); |
466 | |
|
|
467 | if ($::CFG->{fow_smooth} && $CFPlus::OpenGL::GL_VERSION >= 1.2) { # smooth fog of war |
|
|
468 | glConvolutionParameter (GL_CONVOLUTION_2D, GL_CONVOLUTION_BORDER_MODE, GL_CONSTANT_BORDER); |
|
|
469 | glConvolutionFilter2D ( |
|
|
470 | GL_CONVOLUTION_2D, |
|
|
471 | GL_ALPHA, |
|
|
472 | 3, 3, |
|
|
473 | GL_ALPHA, GL_FLOAT, |
|
|
474 | (pack "f*", @{ $self->{smooth_matrix} }), |
|
|
475 | ); |
|
|
476 | glEnable GL_CONVOLUTION_2D; |
|
|
477 | } |
|
|
478 | |
469 | |
479 | $self->{fow_texture} = new CFPlus::Texture |
470 | $self->{fow_texture} = new CFPlus::Texture |
480 | w => $w, |
471 | w => $w, |
481 | h => $h, |
472 | h => $h, |
482 | data => $data, |
473 | data => $data, |
483 | internalformat => GL_ALPHA, |
474 | internalformat => GL_ALPHA, |
484 | format => GL_ALPHA; |
475 | format => GL_ALPHA; |
485 | |
|
|
486 | glDisable GL_CONVOLUTION_2D if $::CFG->{fow_smooth}; |
|
|
487 | } else { |
476 | } else { |
488 | delete $self->{fow_texture}; |
477 | delete $self->{fow_texture}; |
489 | } |
478 | } |
490 | |
479 | |
491 | glNewList $self->{list}; |
480 | glNewList $self->{list}; |
492 | |
481 | |
493 | glPushMatrix; |
482 | glPushMatrix; |
494 | glTranslate $sx0, $sy0; |
483 | glTranslate $sx0, $sy0; |
495 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
484 | glScale $::CFG->{map_scale}, $::CFG->{map_scale}; |
496 | |
485 | |
497 | $::MAP->draw ($dx, $dy, $sw, $sh); |
486 | $::MAP->draw ($dx, $dy, $sw, $sh, $self->{tilesize}); |
498 | |
487 | |
499 | glScale 32, 32; |
488 | glScale $self->{tilesize}, $self->{tilesize}; |
500 | |
489 | |
501 | if (my $tex = $self->{fow_texture}) { |
490 | if (my $tex = $self->{fow_texture}) { |
|
|
491 | glPushMatrix; |
|
|
492 | glScale 1/3, 1/3; |
502 | glEnable GL_TEXTURE_2D; |
493 | glEnable GL_TEXTURE_2D; |
503 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
494 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
504 | |
495 | |
505 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
496 | glColor +($::CFG->{fow_intensity}) x 3, 0.9; |
506 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
497 | $self->{fow_texture}->draw_quad_alpha (0, 0); |
507 | |
498 | |
508 | glDisable GL_TEXTURE_2D; |
499 | glDisable GL_TEXTURE_2D; |
|
|
500 | glPopMatrix; |
509 | } |
501 | } |
510 | |
502 | |
511 | if ($self->{magicmap}) { |
503 | if ($self->{magicmap}) { |
512 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
504 | my ($x, $y, $w, $h, $data) = @{ $self->{magicmap} }; |
513 | |
505 | |
… | |
… | |
520 | } |
512 | } |
521 | |
513 | |
522 | glPopMatrix; |
514 | glPopMatrix; |
523 | glEndList; |
515 | glEndList; |
524 | } |
516 | } |
|
|
517 | |
|
|
518 | } |
|
|
519 | |
|
|
520 | sub draw { |
|
|
521 | my ($self) = @_; |
|
|
522 | |
|
|
523 | $self->{root}->on_post_alloc (prepare => sub { $self->refresh_hook }); |
|
|
524 | |
|
|
525 | return unless $self->{list}; |
|
|
526 | |
|
|
527 | my $focused = $CFPlus::UI::FOCUS == $self |
|
|
528 | || $CFPlus::UI::FOCUS == $self->{completer}{entry}; |
|
|
529 | |
|
|
530 | return |
|
|
531 | unless $focused || !$::FAST; |
525 | |
532 | |
526 | glCallList $self->{list}; |
533 | glCallList $self->{list}; |
527 | |
534 | |
528 | # TNT2 emulates logops in software (or worse :) |
535 | # TNT2 emulates logops in software (or worse :) |
529 | unless ($focused) { |
536 | unless ($focused) { |
… | |
… | |
548 | $self->SUPER::DESTROY; |
555 | $self->SUPER::DESTROY; |
549 | } |
556 | } |
550 | |
557 | |
551 | package CFPlus::MapWidget::MapMap; |
558 | package CFPlus::MapWidget::MapMap; |
552 | |
559 | |
|
|
560 | use strict; |
|
|
561 | use utf8; |
|
|
562 | |
553 | our @ISA = CFPlus::UI::Base::; |
563 | our @ISA = CFPlus::UI::Base::; |
554 | |
564 | |
555 | use Time::HiRes qw(time); |
565 | use Time::HiRes qw(time); |
556 | use CFPlus::OpenGL; |
566 | use CFPlus::OpenGL; |
557 | |
567 | |
558 | sub size_request { |
568 | sub size_request { |
559 | ($::HEIGHT * 0.25, $::HEIGHT * 0.25) |
569 | ($::HEIGHT * 0.2, $::HEIGHT * 0.2) |
560 | } |
570 | } |
561 | |
571 | |
562 | sub invoke_size_allocate { |
572 | sub refresh_hook { |
563 | my ($self, $w, $h) = @_; |
573 | my ($self) = @_; |
564 | |
574 | |
565 | $self->update; |
575 | if ($::MAP && $self->{texture_atime} < time) { |
566 | |
|
|
567 | 1 |
|
|
568 | } |
|
|
569 | |
|
|
570 | sub update { |
|
|
571 | my ($self) = @_; |
|
|
572 | |
|
|
573 | delete $self->{texture_atime}; |
|
|
574 | $self->SUPER::update; |
|
|
575 | } |
|
|
576 | |
|
|
577 | sub _draw { |
|
|
578 | my ($self) = @_; |
|
|
579 | |
|
|
580 | $::MAP or return; |
|
|
581 | |
|
|
582 | my ($w, $h) = @$self{qw(w h)}; |
576 | my ($w, $h) = @$self{qw(w h)}; |
583 | |
577 | |
584 | my $sw = int $::WIDTH / (32 * $::CFG->{map_scale}) + 0.99; |
578 | my $sw = int $::WIDTH / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
585 | my $sh = int $::HEIGHT / (32 * $::CFG->{map_scale}) + 0.99; |
579 | my $sh = int $::HEIGHT / ($::MAPWIDGET->{tilesize} * $::CFG->{map_scale}) + 0.99; |
586 | |
580 | |
587 | my $sx = int $::CFG->{map_shift_x} / 32; |
|
|
588 | my $sy = int $::CFG->{map_shift_y} / 32; |
|
|
589 | |
|
|
590 | my $ox = 0.5 * ($w - $sw); |
581 | my $ox = 0.5 * ($w - $sw); |
591 | my $oy = 0.5 * ($h - $sh); |
582 | my $oy = 0.5 * ($h - $sh); |
592 | |
583 | |
593 | glEnable GL_BLEND; |
584 | my $sx = int $::CFG->{map_shift_x} / $::MAPWIDGET->{tilesize}; |
594 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
585 | my $sy = int $::CFG->{map_shift_y} / $::MAPWIDGET->{tilesize}; |
595 | glEnable GL_TEXTURE_2D; |
|
|
596 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
|
|
597 | |
586 | |
598 | if ($self->{texture_atime} < time) { |
587 | #TODO: map scale is completely borked |
|
|
588 | |
|
|
589 | my $x0 = int $ox - $sx + 0.5; |
|
|
590 | my $y0 = int $oy - $sy + 0.5; |
|
|
591 | |
|
|
592 | $self->{sw} = $sw; |
|
|
593 | $self->{sh} = $sh; |
|
|
594 | |
|
|
595 | $self->{x0} = $x0; |
|
|
596 | $self->{y0} = $y0; |
|
|
597 | |
599 | $self->{texture_atime} = time + 1/3; |
598 | $self->{texture_atime} = time + 1/3; |
600 | |
599 | |
601 | $self->{texture} = |
600 | $self->{texture} = |
602 | new CFPlus::Texture |
601 | new CFPlus::Texture |
603 | w => $w, |
602 | w => $w, |
604 | h => $h, |
603 | h => $h, |
605 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
604 | data => $::MAP->mapmap (-$ox, -$oy, $w, $h), |
606 | type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
605 | type => $CFPlus::GL_VERSION >= 1.2 ? GL_UNSIGNED_INT_8_8_8_8_REV : GL_UNSIGNED_BYTE; |
607 | } |
606 | } |
|
|
607 | } |
|
|
608 | |
|
|
609 | sub invoke_visibility_change { |
|
|
610 | my ($self) = @_; |
|
|
611 | |
|
|
612 | $self->refresh_hook; |
|
|
613 | |
|
|
614 | 0 |
|
|
615 | } |
|
|
616 | |
|
|
617 | sub invoke_size_allocate { |
|
|
618 | my ($self, $w, $h) = @_; |
|
|
619 | |
|
|
620 | $self->update; |
|
|
621 | |
|
|
622 | 1 |
|
|
623 | } |
|
|
624 | |
|
|
625 | sub update { |
|
|
626 | my ($self) = @_; |
|
|
627 | |
|
|
628 | delete $self->{texture_atime}; |
|
|
629 | $self->SUPER::update; |
|
|
630 | } |
|
|
631 | |
|
|
632 | sub _draw { |
|
|
633 | my ($self) = @_; |
|
|
634 | |
|
|
635 | $self->{root}->on_post_alloc (texture => sub { $self->refresh_hook }); |
|
|
636 | |
|
|
637 | $self->{texture} or return; |
|
|
638 | |
|
|
639 | glEnable GL_BLEND; |
|
|
640 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
641 | glEnable GL_TEXTURE_2D; |
|
|
642 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
608 | |
643 | |
609 | $self->{texture}->draw_quad (0, 0); |
644 | $self->{texture}->draw_quad (0, 0); |
610 | |
645 | |
611 | glDisable GL_TEXTURE_2D; |
646 | glDisable GL_TEXTURE_2D; |
612 | |
647 | |
613 | glTranslate 0.375, 0.375; |
648 | glTranslate 0.375, 0.375; |
614 | |
|
|
615 | #TODO: map scale is completely borked |
|
|
616 | |
|
|
617 | my $x0 = int $ox - $sx + 0.5; |
|
|
618 | my $y0 = int $oy - $sy + 0.5; |
|
|
619 | |
649 | |
620 | glColor 1, 1, 0, 1; |
650 | glColor 1, 1, 0, 1; |
621 | glBegin GL_LINE_LOOP; |
651 | glBegin GL_LINE_LOOP; |
622 | glVertex $x0 , $y0 ; |
652 | glVertex $self->{x0} , $self->{y0} ; |
623 | glVertex $x0 , $y0 + $sh; |
653 | glVertex $self->{x0} , $self->{y0} + $self->{sh}; |
624 | glVertex $x0 + $sw, $y0 + $sh; |
654 | glVertex $self->{x0} + $self->{sw}, $self->{y0} + $self->{sh}; |
625 | glVertex $x0 + $sw, $y0 ; |
655 | glVertex $self->{x0} + $self->{sw}, $self->{y0} ; |
626 | glEnd; |
656 | glEnd; |
627 | |
657 | |
628 | glDisable GL_BLEND; |
658 | glDisable GL_BLEND; |
629 | } |
659 | } |
630 | |
660 | |