… | |
… | |
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 | |
|
|
66 | package Crossfire::Client::Texture; |
|
|
67 | |
|
|
68 | use Scalar::Util; |
|
|
69 | |
|
|
70 | use SDL::OpenGL; |
|
|
71 | |
|
|
72 | my @textures; |
|
|
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 | |
|
|
93 | sub new_from_file { |
|
|
94 | my ($class, $path) = @_; |
|
|
95 | |
|
|
96 | open my $fh, "<:raw", $path |
|
|
97 | or die "$path: $!"; |
|
|
98 | |
|
|
99 | local $/; |
|
|
100 | $class->new_from_image (<$fh>) |
|
|
101 | } |
|
|
102 | |
|
|
103 | sub new_from_surface { |
|
|
104 | my ($class, $surface) = @_; |
|
|
105 | |
|
|
106 | $surface->rgba; |
|
|
107 | |
|
|
108 | $class->_new ( |
|
|
109 | data => $surface->pixels, |
|
|
110 | width => $surface->width, |
|
|
111 | height => $surface->height, |
|
|
112 | ) |
|
|
113 | } |
|
|
114 | |
|
|
115 | sub new_from_ttf { |
|
|
116 | my ($class, $ttf, $text) = @_; |
|
|
117 | |
|
|
118 | utf8::upgrade $text; |
|
|
119 | |
|
|
120 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
|
|
121 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
|
|
122 | |
|
|
123 | $class->new_from_surface ($surface) |
|
|
124 | } |
|
|
125 | |
|
|
126 | sub upload { |
|
|
127 | my ($self) = @_; |
|
|
128 | |
|
|
129 | return unless $SDL::App::USING_OPENGL; |
|
|
130 | |
|
|
131 | my $data; |
|
|
132 | |
|
|
133 | if (exists $self->{data}) { |
|
|
134 | $data = $self->{data}; |
|
|
135 | } else { |
|
|
136 | my $pb = new Gtk2::Gdk::PixbufLoader; |
|
|
137 | $pb->write ($self->{image}); |
|
|
138 | $pb->close; |
|
|
139 | |
|
|
140 | $pb = $pb->get_pixbuf; |
|
|
141 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
142 | |
|
|
143 | $self->{width} = $pb->get_width; |
|
|
144 | $self->{height} = $pb->get_height; |
|
|
145 | |
|
|
146 | $data = $pb->get_pixels; |
|
|
147 | } |
|
|
148 | |
|
|
149 | ($self->{name}) = @{glGenTextures 1}; |
|
|
150 | |
|
|
151 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
|
|
152 | |
|
|
153 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
|
|
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
|
|
156 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
157 | |
|
|
158 | glTexImage2D GL_TEXTURE_2D, 0, |
|
|
159 | GL_RGBA8, |
|
|
160 | $self->{width}, $self->{height}, |
|
|
161 | 0, |
|
|
162 | GL_RGBA, |
|
|
163 | GL_UNSIGNED_BYTE, |
|
|
164 | $data; |
|
|
165 | } |
|
|
166 | |
|
|
167 | sub DESTROY { |
|
|
168 | my ($self) = @_; |
|
|
169 | |
|
|
170 | return unless exists $self->{name}; |
|
|
171 | |
|
|
172 | glDeleteTextures delete $self->{name}; |
|
|
173 | } |
|
|
174 | |
|
|
175 | push @::GLINIT, sub { |
|
|
176 | $_->upload |
|
|
177 | for grep $_, @textures; |
|
|
178 | }; |
|
|
179 | |
24 | 1; |
180 | 1; |
25 | |
181 | |
26 | =back |
182 | =back |
27 | |
183 | |
28 | =head1 AUTHOR |
184 | =head1 AUTHOR |