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.14 by root, Sun Apr 9 00:06:09 2006 UTC vs.
Revision 1.36 by root, Sat Apr 15 23:24:59 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines