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.31 by root, Fri Apr 14 14:55:27 2006 UTC vs.
Revision 1.140 by root, Fri Apr 6 07:45:33 2007 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFClient - undocumented utility garbage for our crossfire client 3CFPlus - undocumented utility garbage for our crossfire client
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFClient; 7 use CFPlus;
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 CFClient; 15package CFPlus;
16
17use Carp ();
16 18
17BEGIN { 19BEGIN {
18 $VERSION = '0.1'; 20 $VERSION = '0.97';
19 21
20 use XSLoader; 22 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 23 XSLoader::load "CFPlus", $VERSION;
22} 24}
23 25
24use SDL::OpenGL; 26use utf8;
25 27
26our %GL_EXT; 28use AnyEvent ();
27our $GL_VERSION; 29use Pod::POM ();
30use File::Path ();
31use Storable (); # finally
28 32
29our $GL_NPOT; 33BEGIN {
30 34 use Crossfire::Protocol::Base ();
31sub gl_init { 35 *to_json = \&Crossfire::Protocol::Base::to_json;
32 $GL_VERSION = gl_version * 1; 36 *from_json = \&Crossfire::Protocol::Base::from_json;
33 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
34
35 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
36
37 glEnable GL_TEXTURE_2D;
38 glEnable GL_COLOR_MATERIAL;
39 glShadeModel GL_FLAT;
40 glDisable GL_DEPTH_TEST;
41 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
42
43 glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST;
44
45 CFClient::Texture::restore_state ();
46} 37}
38
39=item guard { BLOCK }
40
41Returns an object that executes the given block as soon as it is destroyed.
42
43=cut
44
45sub guard(&) {
46 bless \(my $cb = $_[0]), "CFPlus::Guard"
47}
48
49sub CFPlus::Guard::DESTROY {
50 ${$_[0]}->()
51}
52
53=item shorten $string[, $maxlength]
54
55=cut
56
57sub shorten($;$) {
58 my ($str, $len) = @_;
59 substr $str, $len, (length $str), "..." if $len + 3 <= length $str;
60 $str
61}
62
63sub asxml($) {
64 local $_ = $_[0];
65
66 s/&/&amp;/g;
67 s/>/&gt;/g;
68 s/</&lt;/g;
69
70 $_
71}
72
73sub socketpipe() {
74 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
75 or die "cannot establish bidirectional pipe: $!\n";
76
77 ($fh1, $fh2)
78}
79
80sub background(&;&) {
81 my ($bg, $cb) = @_;
82
83 my ($fh_r, $fh_w) = CFPlus::socketpipe;
84
85 my $pid = fork;
86
87 if (defined $pid && !$pid) {
88 local $SIG{__DIE__};
89
90 open STDOUT, ">&", $fh_w;
91 open STDERR, ">&", $fh_w;
92 close $fh_r;
93 close $fh_w;
94
95 $| = 1;
96
97 eval { $bg->() };
98
99 if ($@) {
100 my $msg = $@;
101 $msg =~ s/\n+/\n/;
102 warn "FATAL: $msg";
103 CFPlus::_exit 1;
104 }
105
106 # win32 is fucked up, of course. exit will clean stuff up,
107 # which destroys our database etc. _exit will exit ALL
108 # forked processes, because of the dreaded fork emulation.
109 CFPlus::_exit 0;
110 }
111
112 close $fh_w;
113
114 my $buffer;
115
116 my $w; $w = AnyEvent->io (fh => $fh_r, poll => 'r', cb => sub {
117 unless (sysread $fh_r, $buffer, 4096, length $buffer) {
118 undef $w;
119 $cb->();
120 return;
121 }
122
123 while ($buffer =~ s/^(.*)\n//) {
124 my $line = $1;
125 $line =~ s/\s+$//;
126 utf8::decode $line;
127 if ($line =~ /^\x{e877}json_msg (.*)$/s) {
128 $cb->(JSON::XS->new->allow_nonref->decode ($1));
129 } else {
130 ::message ({
131 markup => "background($pid): " . CFPlus::asxml $line,
132 });
133 }
134 }
135 });
136}
137
138sub background_msg {
139 my ($msg) = @_;
140
141 $msg = "\x{e877}json_msg " . JSON::XS->new->allow_nonref->encode ($msg);
142 $msg =~ s/\n//g;
143 utf8::encode $msg;
144 print $msg, "\n";
145}
146
147package CFPlus;
47 148
48sub find_rcfile($) { 149sub find_rcfile($) {
49 my $path; 150 my $path;
50 151
51 for (@INC) { 152 for (grep !ref, @INC) {
52 $path = "$_/CFClient/resources/$_[0]"; 153 $path = "$_/CFPlus/resources/$_[0]";
53 return $path if -r $path; 154 return $path if -r $path;
54 } 155 }
55 156
56 die "FATAL: can't find required file $_[0]\n"; 157 die "FATAL: can't find required file $_[0]\n";
57} 158}
58 159
59sub read_cfg { 160sub read_cfg {
60 my ($file) = @_; 161 my ($file) = @_;
61 162
62 open CFG, $file 163 open my $fh, $file
63 or return; 164 or return;
64 165
65 my $CFG;
66
67 local $/; 166 local $/;
68 $CFG = eval <CFG>; 167 my $CFG = <$fh>;
69 168
70 $::CFG = $CFG; 169 if ($CFG =~ /^---/) { ## TODO compatibility cruft, remove
71 170 require YAML;
72 close CFG; 171 utf8::decode $CFG;
172 $::CFG = YAML::Load ($CFG);
173 } elsif ($CFG =~ /^\{/) {
174 $::CFG = from_json $CFG;
175 } else {
176 $::CFG = eval $CFG; ## todo comaptibility cruft
177 }
73} 178}
74 179
75sub write_cfg { 180sub write_cfg {
76 my ($file) = @_; 181 my ($file) = @_;
77 182
78 open CFG, ">$file" 183 $::CFG->{VERSION} = $::VERSION;
184
185 open my $fh, ">:utf8", $file
79 or return; 186 or return;
80 187 print $fh to_json $::CFG;
81 {
82 require Data::Dumper;
83 local $Data::Dumper::Purity = 1;
84 $::CFG->{VERSION} = $::VERSION;
85 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
86 }
87
88 close CFG;
89} 188}
90 189
91package CFClient::Texture; 190sub http_proxy {
191 my @proxy = win32_proxy_info;
92 192
93use strict; 193 if (@proxy) {
94 194 "http://" . (@proxy < 2 ? "" : @proxy < 3 ? "$proxy[1]\@" : "$proxy[1]:$proxy[2]\@") . $proxy[0]
95use Scalar::Util; 195 } elsif (exists $ENV{http_proxy}) {
96 196 $ENV{http_proxy}
97use SDL::OpenGL;
98
99my @textures;
100
101sub new {
102 my ($class, %data) = @_;
103
104 my $self = bless {
105 internalformat => GL_RGBA,
106 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE,
108 %data,
109 }, $class;
110
111 push @textures, $self;
112 Scalar::Util::weaken $textures[-1];
113
114 $self->upload;
115
116 $self
117}
118
119sub new_from_image {
120 my ($class, $image) = @_;
121
122 $class->new (image => $image)
123}
124
125sub new_from_file {
126 my ($class, $path) = @_;
127
128 open my $fh, "<:raw", $path
129 or die "$path: $!";
130
131 local $/;
132 $class->new_from_image (<$fh>)
133}
134
135#sub new_from_surface {
136# my ($class, $surface) = @_;
137#
138# $surface->rgba;
139#
140# $class->new (
141# data => $surface->pixels,
142# w => $surface->width,
143# h => $surface->height,
144# )
145#}
146
147sub new_from_layout {
148 my ($class, $layout) = @_;
149
150 my ($w, $h, $data) = $layout->render;
151
152 $class->new (
153 w => $w,
154 h => $h,
155 data => $data,
156 internalformat => GL_ALPHA4,
157 format => GL_ALPHA,
158 type => GL_UNSIGNED_BYTE,
159 )
160}
161
162sub new_from_opengl {
163 my ($class, $w, $h, $cb) = @_;
164
165 $class->new (w => $w, h => $h, render_cb => $cb)
166}
167
168sub topot {
169 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
170}
171
172sub upload {
173 my ($self) = @_;
174
175 return unless $GL_VERSION;
176
177 my $data;
178
179 if (exists $self->{data}) {
180 $data = $self->{data};
181
182 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h};
184 glMatrixMode GL_PROJECTION;
185 glLoadIdentity;
186 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity;
189 $self->{render_cb}->($self, $self->{w}, $self->{h});
190
191 } else { 197 } else {
192 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) 198 ()
193 = CFClient::load_image_inline $self->{image};
194 } 199 }
200}
195 201
196 my ($tw, $th) = @$self{qw(w h)}; 202sub set_proxy {
203 my $proxy = http_proxy
204 or return;
197 205
198 unless ($tw && $th) { 206 $ENV{http_proxy} = $proxy;
199 $tw = $th = 1; 207}
200 $data = "\x00" x 64;
201 }
202 208
203 unless ($GL_NPOT) { 209sub lwp_useragent {
204 # TODO: does not work for zero-sized textures 210 require LWP::UserAgent;
205 $tw = topot $tw;
206 $th = topot $th;
207
208 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
209 my $bpp = (length $data) / ($self->{w} * $self->{h});
210 $data = pack "(a" . ($tw * $bpp) . ")*",
211 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
212 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
213 }
214 }
215
216 $self->{s} = $self->{w} / $tw;
217 $self->{t} = $self->{h} / $th;
218
219 $self->{name} ||= (glGenTextures 1)->[0];
220
221 glBindTexture GL_TEXTURE_2D, $self->{name};
222
223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
227 211
228 if (defined $data) { 212 CFPlus::set_proxy;
229 glTexImage2D GL_TEXTURE_2D, 0,
230 $self->{internalformat},
231 $tw, $th, # need to pad texture first
232 0,
233 $self->{format},
234 $self->{type},
235 $data;
236 glGetError and die;
237 } else {
238 glCopyTexImage2D GL_TEXTURE_2D, 0,
239 $self->{internalformat},
240 0, 0,
241 $tw, $th,
242 0;
243 glGetError and die "glCopyTexImage2D $tw,$th";
244 }
245}
246 213
247sub DESTROY { 214 my $ua = LWP::UserAgent->new (
215 agent => "cfplus $VERSION",
216 keep_alive => 1,
217 env_proxy => 1,
218 timeout => 30,
219 );
220}
221
222sub lwp_check($) {
248 my ($self) = @_; 223 my ($res) = @_;
249 224
250 return unless exists $self->{name}; 225 $res->is_error
226 and die $res->status_line;
251 227
252 glDeleteTextures delete $self->{name}; 228 $res
253} 229}
254 230
255sub restore_state{ 231package CFPlus::Layout;
256 $_->upload 232
257 for grep $_, @textures; 233$CFPlus::OpenGL::SHUTDOWN_HOOK{"CFPlus::Layout"} = sub {
234 reset_glyph_cache;
258}; 235};
259 236
2601; 2371;
261 238
262=back 239=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines