1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Crossfire::Client - undocumented utility garbage for our crossfire client |
3 | CFClient - undocumented utility garbage for our crossfire client |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use Crossfire::Client; |
7 | use CFClient; |
8 | |
8 | |
9 | =head1 DESCRIPTION |
9 | =head1 DESCRIPTION |
10 | |
10 | |
11 | =over 4 |
11 | =over 4 |
12 | |
12 | |
13 | =cut |
13 | =cut |
14 | |
14 | |
15 | package Crossfire::Client; |
15 | package CFClient; |
16 | |
16 | |
17 | BEGIN { |
17 | BEGIN { |
18 | $VERSION = '0.1'; |
18 | $VERSION = '0.1'; |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
21 | XSLoader::load "CFClient", $VERSION; |
|
|
22 | } |
|
|
23 | |
|
|
24 | our %GL_EXT; |
|
|
25 | our $GL_VERSION; |
|
|
26 | |
|
|
27 | our $GL_NPOT; |
|
|
28 | |
|
|
29 | sub gl_init { |
|
|
30 | $GL_VERSION = gl_version * 1; |
|
|
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
|
|
32 | |
|
|
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
|
|
34 | |
|
|
35 | CFClient::Texture::restore_state (); |
22 | } |
36 | } |
23 | |
37 | |
24 | sub find_rcfile($) { |
38 | sub find_rcfile($) { |
25 | my $path; |
39 | my $path; |
26 | |
40 | |
27 | for (@INC) { |
41 | for (@INC) { |
28 | $path = "$_/Crossfire/resources/$_[0]"; |
42 | $path = "$_/CFClient/resources/$_[0]"; |
29 | return $path if -r $path; |
43 | return $path if -r $path; |
30 | } |
44 | } |
31 | |
45 | |
32 | die "FATAL: can't find required file $_[0]\n"; |
46 | die "FATAL: can't find required file $_[0]\n"; |
33 | } |
47 | } |
… | |
… | |
53 | |
67 | |
54 | open CFG, ">$file" |
68 | open CFG, ">$file" |
55 | or return; |
69 | or return; |
56 | |
70 | |
57 | { |
71 | { |
|
|
72 | require Data::Dumper; |
58 | local $Data::Dumper::Purity = 1; |
73 | local $Data::Dumper::Purity = 1; |
59 | $::CFG->{VERSION} = $::VERSION; |
74 | $::CFG->{VERSION} = $::VERSION; |
60 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
75 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
61 | } |
76 | } |
62 | |
77 | |
63 | close CFG; |
78 | close CFG; |
64 | } |
79 | } |
65 | |
80 | |
66 | package Crossfire::Client::Texture; |
81 | package CFClient::Texture; |
67 | |
82 | |
68 | use Scalar::Util; |
83 | use Scalar::Util; |
69 | |
84 | |
70 | use SDL::OpenGL; |
85 | use SDL::OpenGL; |
71 | |
86 | |
72 | my @textures; |
87 | my @textures; |
73 | |
88 | |
74 | sub _new { |
89 | sub new { |
75 | my ($class, %data) = @_; |
90 | my ($class, %data) = @_; |
76 | |
91 | |
77 | my $self = bless \%data, $class; |
92 | my $self = bless { |
|
|
93 | internalformat => GL_RGBA, |
|
|
94 | format => GL_RGBA, |
|
|
95 | %data, |
|
|
96 | }, $class; |
78 | |
97 | |
79 | push @textures, $self; |
98 | push @textures, $self; |
80 | Scalar::Util::weaken $textures[-1]; |
99 | Scalar::Util::weaken $textures[-1]; |
81 | |
100 | |
82 | $self->upload; |
101 | $self->upload; |
… | |
… | |
85 | } |
104 | } |
86 | |
105 | |
87 | sub new_from_image { |
106 | sub new_from_image { |
88 | my ($class, $image) = @_; |
107 | my ($class, $image) = @_; |
89 | |
108 | |
90 | $class->_new (image => $image) |
109 | $class->new (image => $image) |
91 | } |
110 | } |
92 | |
111 | |
93 | sub new_from_file { |
112 | sub new_from_file { |
94 | my ($class, $path) = @_; |
113 | my ($class, $path) = @_; |
95 | |
114 | |
… | |
… | |
98 | |
117 | |
99 | local $/; |
118 | local $/; |
100 | $class->new_from_image (<$fh>) |
119 | $class->new_from_image (<$fh>) |
101 | } |
120 | } |
102 | |
121 | |
103 | sub new_from_surface { |
122 | #sub new_from_surface { |
104 | my ($class, $surface) = @_; |
123 | # my ($class, $surface) = @_; |
105 | |
124 | # |
106 | $surface->rgba; |
125 | # $surface->rgba; |
107 | |
126 | # |
108 | $class->_new ( |
127 | # $class->new ( |
109 | data => $surface->pixels, |
128 | # data => $surface->pixels, |
110 | width => $surface->width, |
129 | # w => $surface->width, |
111 | height => $surface->height, |
130 | # h => $surface->height, |
|
|
131 | # ) |
|
|
132 | #} |
|
|
133 | |
|
|
134 | sub new_from_layout { |
|
|
135 | my ($class, $layout) = @_; |
|
|
136 | |
|
|
137 | my ($w, $h, $data) = $layout->render; |
|
|
138 | |
|
|
139 | $class->new ( |
|
|
140 | w => $w, |
|
|
141 | h => $h, |
|
|
142 | data => $data, |
|
|
143 | internalformat => GL_ALPHA4, |
|
|
144 | format => GL_ALPHA, |
112 | ) |
145 | ) |
113 | } |
146 | } |
114 | |
147 | |
115 | sub new_from_ttf { |
148 | sub new_from_opengl { |
116 | my ($class, $ttf, $text) = @_; |
149 | my ($class, $w, $h, $cb) = @_; |
117 | |
150 | |
118 | utf8::upgrade $text; |
151 | $class->new (w => $w, h => $h, render_cb => $cb) |
|
|
152 | } |
119 | |
153 | |
120 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
154 | sub topot { |
121 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
122 | |
|
|
123 | $class->new_from_surface ($surface) |
|
|
124 | } |
156 | } |
125 | |
157 | |
126 | sub upload { |
158 | sub upload { |
127 | my ($self) = @_; |
159 | my ($self) = @_; |
128 | |
160 | |
… | |
… | |
130 | |
162 | |
131 | my $data; |
163 | my $data; |
132 | |
164 | |
133 | if (exists $self->{data}) { |
165 | if (exists $self->{data}) { |
134 | $data = $self->{data}; |
166 | $data = $self->{data}; |
|
|
167 | } elsif (exists $self->{render_cb}) { |
|
|
168 | glViewport 0, 0, $self->{w}, $self->{h}; |
|
|
169 | glMatrixMode GL_PROJECTION; |
|
|
170 | glLoadIdentity; |
|
|
171 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
|
|
172 | glMatrixMode GL_MODELVIEW; |
|
|
173 | glPushmatrix; |
|
|
174 | glLoadIdentity; |
|
|
175 | glClear GL_COLOR_BUFFER_BIT; |
|
|
176 | |
|
|
177 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
135 | } else { |
178 | } else { |
136 | my $pb = new Gtk2::Gdk::PixbufLoader; |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
137 | $pb->write ($self->{image}); |
180 | $pb->write ($self->{image}); |
138 | $pb->close; |
181 | $pb->close; |
139 | |
182 | |
140 | $pb = $pb->get_pixbuf; |
183 | $pb = $pb->get_pixbuf; |
141 | $pb = $pb->add_alpha (0, 0, 0, 0); |
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
142 | |
185 | |
143 | $self->{width} = $pb->get_width; |
186 | $self->{w} = $pb->get_width; |
144 | $self->{height} = $pb->get_height; |
187 | $self->{h} = $pb->get_height; |
145 | |
188 | |
146 | $data = $pb->get_pixels; |
189 | $data = $pb->get_pixels; |
147 | } |
190 | } |
148 | |
191 | |
|
|
192 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
193 | |
|
|
194 | unless ($tw || $th) { |
|
|
195 | $tw = $th = 1; |
|
|
196 | $data = "\x00" x 64; |
|
|
197 | } |
|
|
198 | |
|
|
199 | unless ($GL_NPOT) { |
|
|
200 | # TODO: does not work for zero-sized textures |
|
|
201 | $tw = topot $tw; |
|
|
202 | $th = topot $th; |
|
|
203 | |
|
|
204 | if (defined $data) { |
|
|
205 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
206 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
207 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
208 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | $self->{s} = $self->{w} / $tw; |
|
|
213 | $self->{t} = $self->{h} / $th; |
|
|
214 | |
149 | ($self->{name}) = @{glGenTextures 1}; |
215 | $self->{name} ||= (glGenTextures 1)->[0]; |
150 | |
216 | |
151 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
217 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
152 | |
218 | |
153 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
219 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
220 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
221 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
156 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
157 | |
223 | |
|
|
224 | glGetError; |
|
|
225 | if (defined $data) { |
158 | glTexImage2D GL_TEXTURE_2D, 0, |
226 | glTexImage2D GL_TEXTURE_2D, 0, |
159 | GL_RGBA8, |
227 | $self->{internalformat}, |
160 | $self->{width}, $self->{height}, |
228 | $tw, $th, # need to pad texture first |
161 | 0, |
229 | 0, |
162 | GL_RGBA, |
230 | $self->{format}, |
163 | GL_UNSIGNED_BYTE, |
231 | GL_UNSIGNED_BYTE, |
164 | $data; |
232 | $data; |
|
|
233 | glGetError and die; |
|
|
234 | } else { |
|
|
235 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
236 | $self->{internalformat}, |
|
|
237 | 0, 0, |
|
|
238 | $tw, $th, |
|
|
239 | 0; |
|
|
240 | glPopmatrix; |
|
|
241 | } |
165 | } |
242 | } |
166 | |
243 | |
167 | sub DESTROY { |
244 | sub DESTROY { |
168 | my ($self) = @_; |
245 | my ($self) = @_; |
169 | |
246 | |
170 | return unless exists $self->{name}; |
247 | return unless exists $self->{name}; |
171 | |
248 | |
172 | glDeleteTextures delete $self->{name}; |
249 | glDeleteTextures delete $self->{name}; |
173 | } |
250 | } |
174 | |
251 | |
175 | push @::GLINIT, sub { |
252 | sub restore_state{ |
176 | $_->upload |
253 | $_->upload |
177 | for grep $_, @textures; |
254 | for grep $_, @textures; |
178 | }; |
255 | }; |
179 | |
256 | |
180 | 1; |
257 | 1; |