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