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.19 by root, Mon Apr 10 19:34:03 2006 UTC vs.
Revision 1.196 by root, Sun Nov 9 13:33:42 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3Crossfire::Client - undocumented utility garbage for our crossfire client 3DC - undocumented utility garbage for our deliantra client
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Crossfire::Client; 7 use DC;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package Crossfire::Client; 15package DC;
16
17use Carp ();
18
19our $VERSION;
16 20
17BEGIN { 21BEGIN {
18 $VERSION = '0.1'; 22 $VERSION = '0.9978';
19 23
20 use XSLoader; 24 use XSLoader;
21 XSLoader::load "Crossfire::Client", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
22} 26}
23 27
24our %GL_EXT; 28use utf8;
25our $GL_VERSION; 29use strict qw(vars subs);
26 30
27our $GL_NPOT; 31use AnyEvent ();
32use Pod::POM ();
33use File::Path ();
34use Storable (); # finally
35use Fcntl ();
36use JSON::XS qw(encode_json decode_json);
28 37
29sub gl_init { 38=item guard { BLOCK }
30 $GL_VERSION = gl_version * 1;
31 %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; 40Returns an object that executes the given block as soon as it is destroyed.
34 41
35 Crossfire::Client::Texture::restore_state (); 42=cut
36 43
37# $GL_NPOT=0;#d# 44sub guard(&) {
45 bless \(my $cb = $_[0]), "DC::Guard"
46}
47
48sub DC::Guard::DESTROY {
49 ${$_[0]}->()
50}
51
52=item shorten $string[, $maxlength]
53
54=cut
55
56sub shorten($;$) {
57 my ($str, $len) = @_;
58 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
59 $str
60}
61
62sub asxml($) {
63 local $_ = $_[0];
64
65 s/&/&amp;/g;
66 s/>/&gt;/g;
67 s/</&lt;/g;
68
69 $_
70}
71
72sub socketpipe() {
73 socketpair my $fh1, my $fh2, &Socket::AF_UNIX, &Socket::SOCK_STREAM, &Socket::PF_UNSPEC
74 or die "cannot establish bidirectional pipe: $!\n";
75
76 ($fh1, $fh2)
77}
78
79sub background(&;&) {
80 my ($bg, $cb) = @_;
81
82 my ($fh_r, $fh_w) = DC::socketpipe;
83
84 my $pid = fork;
85
86 if (defined $pid && !$pid) {
87 local $SIG{__DIE__};
88
89 open STDOUT, ">&", $fh_w;
90 open STDERR, ">&", $fh_w;
91 close $fh_r;
92 close $fh_w;
93
94 $| = 1;
95
96 eval { $bg->() };
97
98 if ($@) {
99 my $msg = $@;
100 $msg =~ s/\n+/\n/;
101 warn "FATAL: $msg";
102 DC::_exit 1;
103 }
104
105 # win32 is fucked up, of course. exit will clean stuff up,
106 # which destroys our database etc. _exit will exit ALL
107 # forked processes, because of the dreaded fork emulation.
108 DC::_exit 0;
109 }
110
111 close $fh_w;
112
113 my $buffer;
114
115 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
116 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
117 undef $w;
118 $cb->();
119 return;
120 }
121
122 while ($buffer =~ s/^(.*)\n//) {
123 my $line = $1;
124 $line =~ s/\s+$//;
125 utf8::decode $line;
126 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
127 $cb->(JSON::XS->new->allow_nonref->decode ($1));
128 } else {
129 ::message ({
130 markup => "background($pid): " . DC::asxml $line,
131 });
132 }
133 }
134 });
135}
136
137sub background_msg {
138 my ($msg) = @_;
139
140 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
141 $msg =~ s/\n//g;
142 utf8::encode $msg;
143 print $msg, "\n";
144}
145
146package DC;
147
148our $RC_THEME;
149our %THEME;
150our @RC_PATH;
151our $RC_BASE;
152
153for (grep !ref, @INC) {
154 $RC_BASE = "$_/Deliantra/Client/private/resources";
155 last if -d $RC_BASE;
38} 156}
39 157
40sub find_rcfile($) { 158sub find_rcfile($) {
41 my $path; 159 my $path;
42 160
43 for (@INC) { 161 for (@RC_PATH, "") {
44 $path = "$_/Crossfire/resources/$_[0]"; 162 $path = "$RC_BASE/$_/$_[0]";
45 return $path if -r $path; 163 return $path if -r $path;
46 } 164 }
47 165
48 die "FATAL: can't find required file $_[0]\n"; 166 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
167}
168
169sub load_json($) {
170 my ($file) = @_;
171
172 open my $fh, $file
173 or return;
174
175 local $/;
176 JSON::XS->new->utf8->relaxed->decode (<$fh>)
177}
178
179sub set_theme($) {
180 return if $RC_THEME eq $_[0];
181 $RC_THEME = $_[0];
182
183 # kind of hacky, find the main theme file, then load all theme files and merge them
184
185 %THEME = ();
186 @RC_PATH = "theme-$RC_THEME";
187
188 my $theme = load_json find_rcfile "theme.json"
189 or die "FATAL: theme resource file not found";
190
191 @RC_PATH = @{ $theme->{path} } if $theme->{path};
192
193 for (@RC_PATH, "") {
194 my $theme = load_json "$RC_BASE/$_/theme.json"
195 or next;
196
197 %THEME = ( %$theme, %THEME );
198 }
49} 199}
50 200
51sub read_cfg { 201sub read_cfg {
52 my ($file) = @_; 202 my ($file) = @_;
53 203
54 open CFG, $file 204 $::CFG = load_json $file;
205}
206
207sub write_cfg {
208 my $file = "$Deliantra::VARDIR/client.cf";
209
210 $::CFG->{VERSION} = $::VERSION;
211
212 open my $fh, ">:utf8", $file
55 or return; 213 or return;
56 214 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
57 my $CFG;
58
59 local $/;
60 $CFG = eval <CFG>;
61
62 $::CFG = $CFG;
63
64 close CFG;
65} 215}
66 216
67sub write_cfg { 217sub http_proxy {
68 my ($file) = @_; 218 my @proxy = win32_proxy_info;
69 219
70 open CFG, ">$file" 220 if (@proxy) {
221 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
222 } elsif (exists $ENV{http_proxy}) {
223 $ENV{http_proxy}
224 } else {
225 ()
226 }
227}
228
229sub set_proxy {
230 my $proxy = http_proxy
71 or return; 231 or return;
72 232
233 $ENV{http_proxy} = $proxy;
234}
235
236sub lwp_useragent {
237 require LWP::UserAgent;
73 { 238
74 require Data::Dumper; 239 DC::set_proxy;
75 local $Data::Dumper::Purity = 1;
76 $::CFG->{VERSION} = $::VERSION;
77 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
78 }
79 240
80 close CFG; 241 my $ua = LWP::UserAgent->new (
81} 242 agent => "deliantra $VERSION",
82 243 keep_alive => 1,
83package Crossfire::Client::Texture; 244 env_proxy => 1,
84 245 timeout => 30,
85use Scalar::Util;
86
87use SDL::OpenGL;
88
89my @textures;
90
91sub new {
92 my ($class, %data) = @_;
93
94 my $self = bless {
95 internalformat => GL_RGBA,
96 format => GL_RGBA,
97 %data,
98 }, $class;
99
100 push @textures, $self;
101 Scalar::Util::weaken $textures[-1];
102
103 $self->upload;
104
105 $self
106}
107
108sub new_from_image {
109 my ($class, $image) = @_;
110
111 $class->new (image => $image)
112}
113
114sub new_from_file {
115 my ($class, $path) = @_;
116
117 open my $fh, "<:raw", $path
118 or die "$path: $!";
119
120 local $/;
121 $class->new_from_image (<$fh>)
122}
123
124#sub new_from_surface {
125# my ($class, $surface) = @_;
126#
127# $surface->rgba;
128#
129# $class->new (
130# data => $surface->pixels,
131# width => $surface->width,
132# height => $surface->height,
133# )
134#}
135
136sub new_from_text {
137 my ($class, $text, $height) = @_;
138
139 my ($w, $h, $data) = Crossfire::Client::font_render $text, $height;
140
141 $class->new (
142 width => $w,
143 height => $h,
144 data => $data,
145 internalformat => GL_ALPHA4,
146 format => GL_ALPHA,
147 ) 246 );
148} 247}
149 248
150sub new_from_opengl { 249sub lwp_check($) {
151 my ($class, $w, $h, $cb) = @_;
152
153 $class->new (width => $w, height => $h, rendercb => $cb)
154}
155
156sub topot {
157 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
158}
159
160sub upload {
161 my ($self) = @_; 250 my ($res) = @_;
162 251
163 return unless $SDL::App::USING_OPENGL; 252 $res->is_error
253 and die $res->status_line;
164 254
165 my $data; 255 $res
256}
166 257
167 if (exists $self->{data}) { 258sub fh_nonblocking($$) {
168 $data = $self->{data}; 259 my ($fh, $nb) = @_;
169 } elsif (exists $self->{rendercb}) {
170 glViewport 0, 0, $self->{width}, $self->{height};
171 glMatrixMode GL_PROJECTION;
172 glLoadIdentity;
173 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
174 glMatrixMode GL_MODELVIEW;
175 glPushmatrix;
176 glLoadIdentity;
177 glClear GL_COLOR_BUFFER_BIT;
178 260
179 $self->{rendercb}->($self, $self->{width}, $self->{height}); 261 if ($^O eq "MSWin32") {
262 $nb = (! ! $nb) + 0;
263 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
180 } else { 264 } else {
181 my $pb = new Gtk2::Gdk::PixbufLoader; 265 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
182 $pb->write ($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 }
193
194 my ($tw, $th) = @$self{qw(width height)};
195
196 unless ($NPOT) {
197 $tw = topot $tw;
198 $th = topot $th;
199
200 if (defined $data) {
201 $data = pack "(a$tw)*", (unpack "(a$self->{width})*", $data);
202 $data .= ("\x00" x $tw) x ($th - $self->{height});
203 }
204 }
205
206 $self->{u} = $self->{width} / $tw;
207 $self->{v} = $self->{height} / $th;
208
209 $self->{name} ||= (glGenTextures 1)->[0];
210
211 glBindTexture GL_TEXTURE_2D, $self->{name};
212
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;
216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
217 266 }
218 glGetError;
219 if (defined $data) {
220 glTexImage2D GL_TEXTURE_2D, 0,
221 $self->{internalformat},
222 $tw, $th, # need to pad texture first
223 0,
224 $self->{format},
225 GL_UNSIGNED_BYTE,
226 $data;
227 glGetError and die;
228 } else {
229 glCopyTexImage2D GL_TEXTURE_2D, 0,
230 $self->{internalformat},
231 0, 0,
232 $tw, $th,
233 0;
234 glPopmatrix;
235 }
236} 267}
237 268
238sub DESTROY { 269package DC::Layout;
239 my ($self) = @_;
240 270
241 return unless exists $self->{name}; 271$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {
272 glyph_cache_restore;
273};
242 274
243 glDeleteTextures delete $self->{name}; 275$DC::OpenGL::SHUTDOWN_HOOK{"DC::Layout"} = sub {
244} 276 glyph_cache_backup;
245
246sub restore_state{
247 $_->upload
248 for grep $_, @textures;
249}; 277};
250 278
2511; 2791;
252 280
253=back 281=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines