… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | sub find_rcfile($) { |
|
|
25 | my $path; |
|
|
26 | |
|
|
27 | for (@INC) { |
|
|
28 | $path = "$_/Crossfire/resources/$_[0]"; |
|
|
29 | return $path if -r $path; |
|
|
30 | } |
|
|
31 | |
|
|
32 | die "FATAL: can't find required file $_[0]\n"; |
|
|
33 | } |
|
|
34 | |
|
|
35 | sub read_cfg { |
|
|
36 | my ($file) = @_; |
|
|
37 | |
|
|
38 | open CFG, $file |
|
|
39 | or return; |
|
|
40 | |
|
|
41 | my $CFG; |
|
|
42 | |
|
|
43 | local $/; |
|
|
44 | $CFG = eval <CFG>; |
|
|
45 | |
|
|
46 | $::CFG = $CFG; |
|
|
47 | |
|
|
48 | close CFG; |
|
|
49 | } |
|
|
50 | |
|
|
51 | sub write_cfg { |
|
|
52 | my ($file) = @_; |
|
|
53 | |
|
|
54 | open CFG, ">$file" |
|
|
55 | or return; |
|
|
56 | |
|
|
57 | { |
|
|
58 | local $Data::Dumper::Purity = 1; |
|
|
59 | $::CFG->{VERSION} = $::VERSION; |
|
|
60 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
|
|
61 | } |
|
|
62 | |
|
|
63 | close CFG; |
|
|
64 | } |
|
|
65 | |
24 | package Crossfire::Client::Texture; |
66 | package Crossfire::Client::Texture; |
25 | |
67 | |
26 | use Scalar::Util; |
68 | use Scalar::Util; |
27 | |
69 | |
28 | use SDL::OpenGL; |
70 | use SDL::OpenGL; |
29 | |
71 | |
30 | my @textures; |
72 | my @textures; |
31 | |
73 | |
|
|
74 | sub _new { |
|
|
75 | my ($class, %data) = @_; |
|
|
76 | |
|
|
77 | my $self = bless \%data, $class; |
|
|
78 | |
|
|
79 | push @textures, $self; |
|
|
80 | Scalar::Util::weaken $textures[-1]; |
|
|
81 | |
|
|
82 | $self->upload; |
|
|
83 | |
|
|
84 | $self |
|
|
85 | } |
|
|
86 | |
|
|
87 | sub new_from_image { |
|
|
88 | my ($class, $image) = @_; |
|
|
89 | |
|
|
90 | $class->_new (image => $image) |
|
|
91 | } |
|
|
92 | |
32 | sub new_from_file { |
93 | sub new_from_file { |
33 | my ($class, $path) = @_; |
94 | my ($class, $path) = @_; |
34 | |
95 | |
35 | open my $fh, "<:raw", $path |
96 | open my $fh, "<:raw", $path |
36 | or die "$path: $!"; |
97 | or die "$path: $!"; |
37 | |
98 | |
38 | local $/; |
99 | local $/; |
39 | $class->new_from_data (<$fh>) |
100 | $class->new_from_image (<$fh>) |
40 | } |
101 | } |
41 | |
102 | |
42 | sub new_from_image { |
103 | sub new_from_surface { |
43 | my ($class, $image) = @_; |
104 | my ($class, $surface) = @_; |
44 | |
105 | |
45 | my $self = bless { |
106 | $surface->rgba; |
46 | image => $data, |
|
|
47 | }; |
|
|
48 | |
107 | |
49 | push @textures, $self; |
108 | $class->_new ( |
50 | Scalar::Util::weaken $textures[-1]; |
109 | data => $surface->pixels, |
51 | |
110 | width => $surface->width, |
52 | $self->upload; |
111 | height => $surface->height, |
53 | |
112 | ) |
54 | $self |
|
|
55 | } |
113 | } |
56 | |
114 | |
57 | sub upload { |
115 | sub upload { |
58 | my ($self) = @_; |
116 | my ($self) = @_; |
59 | |
117 | |
… | |
… | |
63 | |
121 | |
64 | if (exists $self->{data}) { |
122 | if (exists $self->{data}) { |
65 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
123 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
66 | } else { |
124 | } else { |
67 | my $pb = new Gtk2::Gdk::PixbufLoader; |
125 | my $pb = new Gtk2::Gdk::PixbufLoader; |
68 | $pb->write ($self->{data}); |
126 | $pb->write ($self->{image}); |
69 | $pb->close; |
127 | $pb->close; |
70 | |
128 | |
71 | $pb = $pb->get_pixbuf; |
129 | $pb = $pb->get_pixbuf; |
72 | $pb = $pb->add_alpha (0, 0, 0, 0); |
130 | $pb = $pb->add_alpha (0, 0, 0, 0); |
73 | |
131 | |