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 | } |
22 | } |
|
|
23 | |
|
|
24 | use Carp (); |
|
|
25 | use AnyEvent (); |
|
|
26 | use BerkeleyDB; |
|
|
27 | |
|
|
28 | use CFClient::OpenGL; |
23 | |
29 | |
24 | our %GL_EXT; |
30 | our %GL_EXT; |
25 | our $GL_VERSION; |
31 | our $GL_VERSION; |
26 | |
32 | |
27 | our $GL_NPOT; |
33 | our $GL_NPOT; |
|
|
34 | our $GL_DEBUG = 1; |
28 | |
35 | |
29 | sub gl_init { |
36 | sub gl_init { |
30 | $GL_VERSION = gl_version * 1; |
37 | $GL_VERSION = gl_version * 1; |
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
38 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
32 | |
39 | |
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
40 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
|
|
41 | $GL_NPOT = 0 if gl_vendor =~ /ATI Technologies/; # ATI doesn't get it right... |
34 | |
42 | |
|
|
43 | glDisable GL_COLOR_MATERIAL; |
|
|
44 | glShadeModel GL_FLAT; |
|
|
45 | glDisable GL_DITHER; |
|
|
46 | glDisable GL_DEPTH_TEST; |
|
|
47 | glDepthMask 0; |
|
|
48 | glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; |
|
|
49 | |
35 | Crossfire::Client::Texture::restore_state (); |
50 | CFClient::Texture::restore_state (); |
|
|
51 | } |
36 | |
52 | |
37 | # $GL_NPOT=0;#d# |
53 | sub gl_check { |
|
|
54 | return unless $GL_DEBUG; |
|
|
55 | |
|
|
56 | if (my $error = glGetError) { |
|
|
57 | Carp::cluck sprintf "opengl error %x while %s", $error, &sprintf(@_); |
|
|
58 | } |
38 | } |
59 | } |
39 | |
60 | |
40 | sub find_rcfile($) { |
61 | sub find_rcfile($) { |
41 | my $path; |
62 | my $path; |
42 | |
63 | |
43 | for (@INC) { |
64 | for (grep !ref, @INC) { |
44 | $path = "$_/Crossfire/resources/$_[0]"; |
65 | $path = "$_/CFClient/resources/$_[0]"; |
45 | return $path if -r $path; |
66 | return $path if -r $path; |
46 | } |
67 | } |
47 | |
68 | |
48 | die "FATAL: can't find required file $_[0]\n"; |
69 | die "FATAL: can't find required file $_[0]\n"; |
49 | } |
70 | } |
… | |
… | |
78 | } |
99 | } |
79 | |
100 | |
80 | close CFG; |
101 | close CFG; |
81 | } |
102 | } |
82 | |
103 | |
|
|
104 | mkdir "$Crossfire::VARDIR/pclient", 0777; |
|
|
105 | |
|
|
106 | our $DB_ENV = new BerkeleyDB::Env |
|
|
107 | -Home => "$Crossfire::VARDIR/pclient", |
|
|
108 | -Cachesize => 1_000_000, |
|
|
109 | -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt", |
|
|
110 | # -ErrPrefix => "DATABASE", |
|
|
111 | -Verbose => 1, |
|
|
112 | -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN, |
|
|
113 | or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error"; |
|
|
114 | |
|
|
115 | sub db_table($) { |
|
|
116 | my ($table) = @_; |
|
|
117 | |
|
|
118 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
119 | |
|
|
120 | new CFClient::Database |
|
|
121 | -Env => $DB_ENV, |
|
|
122 | -Filename => $table, |
|
|
123 | # -Filename => "database", |
|
|
124 | # -Subname => $table, |
|
|
125 | -Property => DB_CHKSUM, |
|
|
126 | -Flags => DB_CREATE | DB_UPGRADE, |
|
|
127 | or die "unable to create/open database table $_[0]: $BerkeleyDB::Error"; |
|
|
128 | } |
|
|
129 | |
|
|
130 | sub pod_to_pango($) { |
|
|
131 | my ($pom) = @_; |
|
|
132 | |
|
|
133 | $pom->present ("CFClient::PodToPango") |
|
|
134 | } |
|
|
135 | |
|
|
136 | package CFClient::PodToPango; |
|
|
137 | |
|
|
138 | use base Pod::POM::View::Text; |
|
|
139 | |
|
|
140 | our $indent = 0; |
|
|
141 | |
|
|
142 | *view_seq_code = |
|
|
143 | *view_seq_bold = sub { "<b>$_[1]</b>" }; |
|
|
144 | *view_seq_italic = sub { "<i>$_[1]</i>" }; |
|
|
145 | *view_seq_space = |
|
|
146 | *view_seq_link = |
|
|
147 | *view_seq_index = sub { CFClient::UI::Label::escape ($_[1]) }; |
|
|
148 | |
|
|
149 | sub view_seq_text { |
|
|
150 | my $text = $_[1]; |
|
|
151 | $text =~ s/\s+/ /g; |
|
|
152 | CFClient::UI::Label::escape ($text) |
|
|
153 | } |
|
|
154 | |
|
|
155 | sub view_item { |
|
|
156 | ("\t" x ($indent / 4)) |
|
|
157 | . $_[1]->title->present ($_[0]) |
|
|
158 | . "\n" |
|
|
159 | . $_[1]->content->present ($_[0]) |
|
|
160 | } |
|
|
161 | |
|
|
162 | sub view_verbatim { |
|
|
163 | (join "", |
|
|
164 | map +("\t" x ($indent / 2)) . "$_\n", |
|
|
165 | split /\n/, CFClient::UI::Label::escape ($_[1])) |
|
|
166 | . "\n" |
|
|
167 | } |
|
|
168 | |
|
|
169 | sub view_textblock { |
|
|
170 | ("\t" x ($indent / 2)) . "$_[1]\n\n" |
|
|
171 | } |
|
|
172 | |
|
|
173 | sub view_head2 { |
|
|
174 | "<big>" . $_[1]->title->present ($_[0]) . "</big>\n\n" |
|
|
175 | . $_[1]->content->present ($_[0]) |
|
|
176 | }; |
|
|
177 | |
|
|
178 | sub view_over { |
|
|
179 | local $indent = $indent + $_[1]->indent; |
|
|
180 | $_[1]->content->present ($_[0]) |
|
|
181 | } |
|
|
182 | |
|
|
183 | package CFClient::Database; |
|
|
184 | |
|
|
185 | our @ISA = BerkeleyDB::Btree::; |
|
|
186 | |
|
|
187 | sub get($$) { |
|
|
188 | my $data; |
|
|
189 | |
|
|
190 | $_[0]->db_get ($_[1], $data) == 0 |
|
|
191 | ? $data |
|
|
192 | : () |
|
|
193 | } |
|
|
194 | |
|
|
195 | my %DB_SYNC; |
|
|
196 | |
|
|
197 | sub put($$$) { |
|
|
198 | my ($db, $key, $data) = @_; |
|
|
199 | |
|
|
200 | $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); |
|
|
201 | |
|
|
202 | $db->db_put ($key => $data) |
|
|
203 | } |
|
|
204 | |
83 | package Crossfire::Client::Texture; |
205 | package CFClient::Texture; |
|
|
206 | |
|
|
207 | use strict; |
84 | |
208 | |
85 | use Scalar::Util; |
209 | use Scalar::Util; |
86 | |
210 | |
87 | use SDL::OpenGL; |
211 | use CFClient::OpenGL; |
88 | |
212 | |
89 | my @textures; |
213 | my %TEXTURES; |
90 | |
214 | |
91 | sub new { |
215 | sub new { |
92 | my ($class, %data) = @_; |
216 | my ($class, %data) = @_; |
93 | |
217 | |
94 | my $self = bless { |
218 | my $self = bless { |
95 | internalformat => GL_RGBA, |
219 | internalformat => GL_RGBA, |
96 | format => GL_RGBA, |
220 | format => GL_RGBA, |
|
|
221 | type => GL_UNSIGNED_BYTE, |
97 | %data, |
222 | %data, |
98 | }, $class; |
223 | }, $class; |
99 | |
224 | |
100 | push @textures, $self; |
225 | Scalar::Util::weaken ($TEXTURES{$self+0} = $self); |
101 | Scalar::Util::weaken $textures[-1]; |
|
|
102 | |
226 | |
103 | $self->upload; |
227 | $self->upload; |
104 | |
228 | |
105 | $self |
229 | $self |
106 | } |
230 | } |
107 | |
231 | |
108 | sub new_from_image { |
232 | sub new_from_image { |
109 | my ($class, $image) = @_; |
233 | my ($class, $image, %arg) = @_; |
110 | |
234 | |
111 | $class->new (image => $image) |
235 | $class->new (image => $image, %arg) |
112 | } |
236 | } |
113 | |
237 | |
114 | sub new_from_file { |
238 | sub new_from_file { |
115 | my ($class, $path) = @_; |
239 | my ($class, $path, %arg) = @_; |
116 | |
240 | |
117 | open my $fh, "<:raw", $path |
241 | open my $fh, "<:raw", $path |
118 | or die "$path: $!"; |
242 | or die "$path: $!"; |
119 | |
243 | |
120 | local $/; |
244 | local $/; |
121 | $class->new_from_image (<$fh>) |
245 | $class->new_from_image (<$fh>, %arg) |
122 | } |
246 | } |
123 | |
247 | |
124 | #sub new_from_surface { |
248 | #sub new_from_surface { |
125 | # my ($class, $surface) = @_; |
249 | # my ($class, $surface) = @_; |
126 | # |
250 | # |
127 | # $surface->rgba; |
251 | # $surface->rgba; |
128 | # |
252 | # |
129 | # $class->new ( |
253 | # $class->new ( |
130 | # data => $surface->pixels, |
254 | # data => $surface->pixels, |
131 | # width => $surface->width, |
255 | # w => $surface->width, |
132 | # height => $surface->height, |
256 | # h => $surface->height, |
133 | # ) |
257 | # ) |
134 | #} |
258 | #} |
135 | |
259 | |
136 | sub new_from_text { |
260 | sub new_from_layout { |
137 | my ($class, $text, $height) = @_; |
261 | my ($class, $layout, %arg) = @_; |
138 | |
262 | |
139 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
263 | my ($w, $h, $data, $format, $internalformat) = $layout->render; |
140 | |
264 | |
141 | $class->new ( |
265 | $class->new ( |
142 | width => $w, |
266 | w => $w, |
143 | height => $h, |
267 | h => $h, |
144 | data => $data, |
268 | data => $data, |
145 | internalformat => GL_ALPHA4, |
|
|
146 | format => GL_ALPHA, |
269 | format => $format, |
|
|
270 | internalformat => $format, |
|
|
271 | type => GL_UNSIGNED_BYTE, |
|
|
272 | %arg, |
147 | ) |
273 | ) |
148 | } |
274 | } |
149 | |
275 | |
150 | sub new_from_opengl { |
276 | sub new_from_opengl { |
151 | my ($class, $w, $h, $cb) = @_; |
277 | my ($class, $w, $h, $cb) = @_; |
152 | |
278 | |
153 | $class->new (width => $w, height => $h, rendercb => $cb) |
279 | $class->new (w => $w || 1, h => $h || 1, render_cb => $cb) |
154 | } |
280 | } |
155 | |
281 | |
156 | sub topot { |
282 | sub topot { |
157 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
283 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
158 | } |
284 | } |
159 | |
285 | |
160 | sub upload { |
286 | sub upload { |
161 | my ($self) = @_; |
287 | my ($self) = @_; |
162 | |
288 | |
163 | return unless $SDL::App::USING_OPENGL; |
289 | return unless $GL_VERSION; |
164 | |
290 | |
165 | my $data; |
291 | my $data; |
166 | |
292 | |
167 | if (exists $self->{data}) { |
293 | if (exists $self->{data}) { |
168 | $data = $self->{data}; |
294 | $data = $self->{data}; |
|
|
295 | |
169 | } elsif (exists $self->{rendercb}) { |
296 | } elsif (exists $self->{render_cb}) { |
170 | glViewport 0, 0, $self->{width}, $self->{height}; |
297 | glViewport 0, 0, $self->{w}, $self->{h}; |
171 | glMatrixMode GL_PROJECTION; |
298 | glMatrixMode GL_PROJECTION; |
172 | glLoadIdentity; |
299 | glLoadIdentity; |
173 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
300 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
174 | glMatrixMode GL_MODELVIEW; |
301 | glMatrixMode GL_MODELVIEW; |
175 | glPushmatrix; |
|
|
176 | glLoadIdentity; |
302 | glLoadIdentity; |
177 | glClear GL_COLOR_BUFFER_BIT; |
|
|
178 | |
|
|
179 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
303 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
304 | |
180 | } else { |
305 | } else { |
181 | my $pb = new Gtk2::Gdk::PixbufLoader; |
306 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
182 | $pb->write ($self->{image}); |
307 | = CFClient::load_image_inline $self->{image}; |
183 | $pb->close; |
|
|
184 | |
|
|
185 | $pb = $pb->get_pixbuf; |
|
|
186 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
187 | |
|
|
188 | $self->{width} = $pb->get_width; |
|
|
189 | $self->{height} = $pb->get_height; |
|
|
190 | |
|
|
191 | $data = $pb->get_pixels; |
|
|
192 | } |
308 | } |
193 | |
309 | |
194 | my ($tw, $th) = @$self{qw(width height)}; |
310 | my ($tw, $th) = @$self{qw(w h)}; |
195 | |
311 | |
|
|
312 | unless ($tw > 0 && $th > 0) { |
|
|
313 | $tw = $th = 1; |
|
|
314 | $data = "\x00" x 64; |
|
|
315 | } |
|
|
316 | |
|
|
317 | $self->{minified} = [CFClient::average $tw, $th, $data] |
|
|
318 | if $self->{minify}; |
|
|
319 | |
196 | unless ($NPOT) { |
320 | unless ($GL_NPOT) { |
|
|
321 | # TODO: does not work for zero-sized textures |
197 | $tw = topot $tw; |
322 | $tw = topot $tw; |
198 | $th = topot $th; |
323 | $th = topot $th; |
199 | |
324 | |
200 | if (defined $data) { |
325 | if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { |
201 | $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data); |
326 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
327 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
328 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
202 | $data .= ("\x00" x $tw) x ($th - $self->{height}); |
329 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
203 | } |
330 | } |
204 | } |
331 | } |
205 | |
332 | |
206 | $self->{u} = $self->{width} / $tw; |
333 | $self->{s} = $self->{w} / $tw; |
207 | $self->{v} = $self->{height} / $th; |
334 | $self->{t} = $self->{h} / $th; |
208 | |
335 | |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
336 | $self->{name} ||= glGenTexture; |
210 | |
337 | |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
338 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
212 | |
339 | |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
|
|
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
340 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
341 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
|
|
342 | |
|
|
343 | if ($::FAST) { |
|
|
344 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
|
|
345 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST; |
|
|
346 | } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) { |
|
|
347 | # alternatively check for 0x8191 |
|
|
348 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1; |
|
|
349 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
350 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
351 | } else { |
|
|
352 | glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap}; |
|
|
353 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
|
|
354 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR; |
|
|
355 | } |
217 | |
356 | |
218 | glGetError; |
357 | glGetError; |
|
|
358 | |
219 | if (defined $data) { |
359 | if (defined $data) { |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
360 | glTexImage2D GL_TEXTURE_2D, 0, |
221 | $self->{internalformat}, |
361 | $self->{internalformat}, |
222 | $tw, $th, # need to pad texture first |
362 | $tw, $th, |
223 | 0, |
363 | 0, |
224 | $self->{format}, |
364 | $self->{format}, |
225 | GL_UNSIGNED_BYTE, |
365 | $self->{type}, |
226 | $data; |
366 | $data; |
227 | glGetError and die; |
367 | CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x", |
|
|
368 | $tw, $th, $self->{internalformat}, $self->{format}, $self->{type}; |
228 | } else { |
369 | } else { |
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
370 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
230 | $self->{internalformat}, |
371 | $self->{internalformat}, |
231 | 0, 0, |
372 | 0, 0, |
232 | $tw, $th, |
373 | $tw, $th, |
233 | 0; |
374 | 0; |
234 | glPopmatrix; |
375 | CFClient::gl_check "copying to texture %dx%d if=%x", |
|
|
376 | $tw, $th, $self->{internalformat}; |
235 | } |
377 | } |
|
|
378 | |
|
|
379 | glBindTexture GL_TEXTURE_2D, 0; # just to be on the safe side |
236 | } |
380 | } |
237 | |
381 | |
238 | sub DESTROY { |
382 | sub DESTROY { |
239 | my ($self) = @_; |
383 | my ($self) = @_; |
240 | |
384 | |
241 | return unless exists $self->{name}; |
385 | delete $TEXTURES{$self+0}; |
242 | |
386 | |
243 | glDeleteTextures delete $self->{name}; |
387 | glDeleteTexture delete $self->{name} |
|
|
388 | if $self->{name}; |
244 | } |
389 | } |
245 | |
390 | |
246 | sub restore_state{ |
391 | sub restore_state { |
247 | $_->upload |
392 | $_->upload |
248 | for grep $_, @textures; |
393 | for values %TEXTURES; |
249 | }; |
394 | } |
250 | |
395 | |
251 | 1; |
396 | 1; |
252 | |
397 | |
253 | =back |
398 | =back |
254 | |
399 | |