ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.54 by root, Fri May 19 03:49:47 2006 UTC vs.
Revision 1.70 by root, Fri May 26 18:28:23 2006 UTC

18 $VERSION = '0.1'; 18 $VERSION = '0.1';
19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23
24use utf8;
23 25
24use Carp (); 26use Carp ();
25use AnyEvent (); 27use AnyEvent ();
26use BerkeleyDB; 28use BerkeleyDB;
27
28use CFClient::OpenGL;
29
30our %GL_EXT;
31our $GL_VERSION;
32
33our $GL_NPOT;
34our $GL_DEBUG = 1;
35
36sub gl_init {
37 $GL_VERSION = gl_version * 1;
38 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
39
40 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
41
42 glDisable GL_COLOR_MATERIAL;
43 glShadeModel GL_FLAT;
44 glDisable GL_DITHER;
45 glDisable GL_DEPTH_TEST;
46 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
47
48 CFClient::Texture::restore_state ();
49}
50
51sub gl_check {
52 return unless $GL_DEBUG;
53
54 if (my $error = glGetError) {
55 Carp::cluck sprintf "opengl error %x while %s", $error, sprintf @_;
56 }
57}
58 29
59sub find_rcfile($) { 30sub find_rcfile($) {
60 my $path; 31 my $path;
61 32
62 for (grep !ref, @INC) { 33 for (grep !ref, @INC) {
105 -Home => "$Crossfire::VARDIR/pclient", 76 -Home => "$Crossfire::VARDIR/pclient",
106 -Cachesize => 1_000_000, 77 -Cachesize => 1_000_000,
107 -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt", 78 -ErrFile => "$Crossfire::VARDIR/pclient/errorlog.txt",
108# -ErrPrefix => "DATABASE", 79# -ErrPrefix => "DATABASE",
109 -Verbose => 1, 80 -Verbose => 1,
110 -Flags => DB_CREATE | DB_RECOVER_FATAL | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN, 81 -Flags => DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN,
111 or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error"; 82 or die "unable to create/open database home $Crossfire::VARDIR/pclient: $BerkeleyDB::Error";
112 83
113sub db_table($) { 84sub db_table($) {
114 my ($table) = @_; 85 my ($table) = @_;
115 86
129 my ($pom) = @_; 100 my ($pom) = @_;
130 101
131 $pom->present ("CFClient::PodToPango") 102 $pom->present ("CFClient::PodToPango")
132} 103}
133 104
105sub pod_to_pango_list($) {
106 my ($pom) = @_;
107
108 [
109 map s/^(\s*)// && [40 * length $1, length $_ ? $_ : " "],
110 split /\n/, $pom->present ("CFClient::PodToPango")
111 ]
112}
113
134package CFClient::PodToPango; 114package CFClient::PodToPango;
135 115
136use base Pod::POM::View::Text; 116use base Pod::POM::View::Text;
137 117
138our $indent = 0; 118our $indent = 0;
157 . $_[1]->content->present ($_[0]) 137 . $_[1]->content->present ($_[0])
158} 138}
159 139
160sub view_verbatim { 140sub view_verbatim {
161 (join "", 141 (join "",
162 map +("\t" x ($indent / 2)) . "$_\n", 142 map +("\t" x ($indent / 2)) . "<tt>$_</tt>\n",
163 split /\n/, CFClient::UI::Label::escape ($_[1])) 143 split /\n/, CFClient::UI::Label::escape ($_[1]))
164 . "\n" 144 . "\n"
165} 145}
166 146
167sub view_textblock { 147sub view_textblock {
168 ("\t" x ($indent / 2)) . "$_[1]\n\n" 148 ("\t" x ($indent / 2)) . "$_[1]\n\n"
169} 149}
170 150
151sub view_head1 {
152 "\n\n<span foreground='#ffff00' size='x-large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
153 . $_[1]->content->present ($_[0])
154};
155
171sub view_head2 { 156sub view_head2 {
157 "\n<span foreground='#ccccff' size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
158 . $_[1]->content->present ($_[0])
159};
160
161sub view_head3 {
172 "<big>" . $_[1]->title->present ($_[0]) . "</big>\n\n" 162 "\n<span size='large'>" . $_[1]->title->present ($_[0]) . "</span>\n\n"
173 . $_[1]->content->present ($_[0]) 163 . $_[1]->content->present ($_[0])
174}; 164};
175 165
176sub view_over { 166sub view_over {
177 local $indent = $indent + $_[1]->indent; 167 local $indent = $indent + $_[1]->indent;
198 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync }); 188 $DB_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
199 189
200 $db->db_put ($key => $data) 190 $db->db_put ($key => $data)
201} 191}
202 192
203package CFClient::Texture; 193package CFClient::Item;
204 194
205use strict; 195sub desc_string {
206
207use Scalar::Util;
208
209use CFClient::OpenGL;
210
211my %TEXTURES;
212
213sub new {
214 my ($class, %data) = @_;
215
216 my $self = bless {
217 internalformat => GL_RGBA,
218 format => GL_RGBA,
219 type => GL_UNSIGNED_BYTE,
220 %data,
221 }, $class;
222
223 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
224
225 $self->upload;
226
227 $self
228}
229
230sub new_from_image {
231 my ($class, $image, %arg) = @_;
232
233 $class->new (image => $image, %arg)
234}
235
236sub new_from_file {
237 my ($class, $path, %arg) = @_;
238
239 open my $fh, "<:raw", $path
240 or die "$path: $!";
241
242 local $/;
243 $class->new_from_image (<$fh>, %arg)
244}
245
246#sub new_from_surface {
247# my ($class, $surface) = @_;
248#
249# $surface->rgba;
250#
251# $class->new (
252# data => $surface->pixels,
253# w => $surface->width,
254# h => $surface->height,
255# )
256#}
257
258sub new_from_layout {
259 my ($class, $layout, %arg) = @_;
260
261 my ($w, $h, $data, $format, $internalformat) = $layout->render;
262
263 $class->new (
264 w => $w,
265 h => $h,
266 data => $data,
267 format => $format,
268 internalformat => $format,
269 type => GL_UNSIGNED_BYTE,
270 %arg,
271 )
272}
273
274sub new_from_opengl {
275 my ($class, $w, $h, $cb) = @_;
276
277 $class->new (w => $w || 1, h => $h || 1, render_cb => $cb)
278}
279
280sub topot {
281 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
282}
283
284sub upload {
285 my ($self) = @_; 196 my ($self) = @_;
286 197
287 return unless $GL_VERSION; 198 my $desc =
199 $self->{nrof} < 2
200 ? $self->{name}
201 : "$self->{nrof} × $self->{name_pl}";
288 202
289 my $data; 203 $self->{flags} & Crossfire::Protocol::F_OPEN
204 and $desc .= " (open)";
205 $self->{flags} & Crossfire::Protocol::F_APPLIED
206 and $desc .= " (applied)";
207 $self->{flags} & Crossfire::Protocol::F_UNPAID
208 and $desc .= " (unpaid)";
209 $self->{flags} & Crossfire::Protocol::F_MAGIC
210 and $desc .= " (magic)";
211 $self->{flags} & Crossfire::Protocol::F_CURSED
212 and $desc .= " (cursed)";
213 $self->{flags} & Crossfire::Protocol::F_DAMNED
214 and $desc .= " (damned)";
215 $self->{flags} & Crossfire::Protocol::F_LOCKED
216 and $desc .= " *";
290 217
291 if (exists $self->{data}) { 218 $desc
292 $data = $self->{data}; 219}
293 220
294 } elsif (exists $self->{render_cb}) { 221sub weight_string {
295 glViewport 0, 0, $self->{w}, $self->{h}; 222 my ($self) = @_;
296 glMatrixMode GL_PROJECTION;
297 glLoadIdentity;
298 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
299 glMatrixMode GL_MODELVIEW;
300 glLoadIdentity;
301 $self->{render_cb}->($self, $self->{w}, $self->{h});
302 223
303 } else { 224 my $weight = ($self->{nrof} || 1) * $self->{weight};
304 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
305 = CFClient::load_image_inline $self->{image};
306 }
307 225
308 my ($tw, $th) = @$self{qw(w h)}; 226 $weight < 0 ? "?" : $weight * 0.001
227}
309 228
310 unless ($tw > 0 && $th > 0) { 229sub update_widgets {
311 $tw = $th = 1; 230 my ($self) = @_;
312 $data = "\x00" x 64;
313 }
314 231
315 $self->{minified} = [CFClient::average $tw, $th, $data] 232 my $button_cb = sub {
316 if $self->{minify}; 233 my (undef, $ev, $x, $y) = @_;
317 234
318 unless ($GL_NPOT) { 235 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
319 # TODO: does not work for zero-sized textures 236 my $targ = $::CONN->{player}{tag};
320 $tw = topot $tw;
321 $th = topot $th;
322 237
323 if (($tw != $self->{w} || $th != $self->{h}) && defined $data) { 238 if ($self->{container} == $::CONN->{player}{tag}) {
324 my $bpp = (length $data) / ($self->{w} * $self->{h}); 239 $targ = $::CONN->{open_container};
325 $data = pack "(a" . ($tw * $bpp) . ")*", 240 }
326 unpack "(a" . ($self->{w} * $bpp) . ")*", $data; 241
327 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); 242 $::CONN->send ("move $targ $self->{tag} 0");
243 } elsif ($ev->{button} == 1) {
244 $::CONN->send ("examine $self->{tag}");
245 } elsif ($ev->{button} == 2) {
246 $::CONN->send ("apply $self->{tag}");
247 } elsif ($ev->{button} == 3) {
248 my @menu_items = (
249 ["examine", sub { $::CONN->send ("examine $self->{tag}") }],
250 ["mark", sub { $::CONN->send ("mark ". pack "N", $self->{tag}) }],
251 ["apply", sub { $::CONN->send ("apply $self->{tag}") }],
252 (
253 $self->{flags} & Crossfire::Protocol::F_LOCKED
254 ? (
255 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $self->{tag}) }],
256 )
257 : (
258 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $self->{tag}) }],
259 ["drop", sub { $::CONN->send ("move $::CONN->{open_container} $self->{tag} 0") }],
260 )
261 ),
262 );
263
264 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
328 } 265 }
266
267 1
329 } 268 };
330 269
331 $self->{s} = $self->{w} / $tw; 270 my $tooltip_std = "<small>"
332 $self->{t} = $self->{h} / $th; 271 . "Left click - examine item\n"
272 . "Shift-Left click - " . ($self->{container} ? "move or drop" : "take") . " item\n"
273 . "Middle click - apply\n"
274 . "Right click - further options"
275 . "</small>\n";
333 276
334 $self->{name} ||= glGenTexture; 277 $self->{face_widget} ||= new CFClient::UI::Face
335 278 can_events => 1,
336 glBindTexture GL_TEXTURE_2D, $self->{name}; 279 can_hover => 1,
337 280 anim => $self->{anim},
338 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 281 animspeed => $self->{animspeed}, # TODO# must be set at creation time
339 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 282 connect_button_down => $button_cb,
340
341 if ($::FAST) {
342 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
343 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;
344 } elsif ($self->{mipmap} && $GL_VERSION >= 1.4) {
345 # alternatively check for 0x8191
346 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, 1;
347 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
348 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
349 } else {
350 glTexParameter GL_TEXTURE_2D, GL_GENERATE_MIPMAP, $self->{mipmap};
351 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
352 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;
353 } 283 ;
284 $self->{face_widget}{face} = $self->{face};
285 $self->{face_widget}{anim} = $self->{anim};
286 $self->{face_widget}{animspeed} = $self->{animspeed};
287 $self->{face_widget}->set_tooltip (
288 "<b>Face/Animation.</b>\n"
289 . "Item uses face #$self->{face}. "
290 . ($self->{animspeed} ? "Item uses animation #$self->{anim} at " . (1 / $self->{animspeed}) . "fps. " : "Item is not animated. ")
291 . "\n\n$tooltip_std"
292 );
354 293
355 glGetError; 294 $self->{desc_widget} ||= new CFClient::UI::Label
356 295 can_events => 1,
357 if (defined $data) { 296 can_hover => 1,
358 glTexImage2D GL_TEXTURE_2D, 0, 297 ellipsise => 2,
359 $self->{internalformat}, 298 align => -1,
360 $tw, $th, 299 connect_button_down => $button_cb,
361 0,
362 $self->{format},
363 $self->{type},
364 $data;
365 CFClient::gl_check "uploading texture %dx%d if=%x f=%x t=%x",
366 $tw, $th, $self->{internalformat}, $self->{format}, $self->{type};
367 } else {
368 glCopyTexImage2D GL_TEXTURE_2D, 0,
369 $self->{internalformat},
370 0, 0,
371 $tw, $th,
372 0;
373 CFClient::gl_check "copying to texture %dx%d if=%x",
374 $tw, $th, $self->{internalformat};
375 } 300 ;
376} 301 my $desc = CFClient::Item::desc_string $self;
302 $self->{desc_widget}->set_text ($desc);
303 $self->{desc_widget}->set_tooltip ("<b>$desc</b>.\n$tooltip_std");
377 304
378sub DESTROY { 305 $self->{weight_widget} ||= new CFClient::UI::Label
379 my ($self) = @_; 306 can_events => 1,
307 can_hover => 1,
308 ellipsise => 0,
309 align => 0,
310 connect_button_down => $button_cb,
311 ;
312 $self->{weight_widget}->set_text (CFClient::Item::weight_string $self);
380 313
381 delete $TEXTURES{$self+0}; 314 $self->{weight_widget}->set_tooltip (
382 315 "<b>Weight</b>.\n"
383 glDeleteTexture delete $self->{name} 316 . ($self->{weight} >= 0 ? "One item weighs $self->{weight}g. " : "You have no idea how much this weighs. ")
384 if $self->{name}; 317 . ($self->{nrof} ? "You have $self->{nrof} of it. " : "Item cannot stack with others of it's kind. ")
385} 318 . "\n\n$tooltip_std"
386 319 );
387sub restore_state {
388 $_->upload
389 for values %TEXTURES;
390} 320}
391 321
3921; 3221;
393 323
394=back 324=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines