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.25 by root, Tue Apr 11 19:31:17 2006 UTC vs.
Revision 1.37 by root, Sun Apr 16 07:25:29 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 AnyEvent;
25use BerkeleyDB;
26use SDL::OpenGL;
23 27
24our %GL_EXT; 28our %GL_EXT;
25our $GL_VERSION; 29our $GL_VERSION;
26 30
27our $GL_NPOT; 31our $GL_NPOT;
29sub gl_init { 33sub gl_init {
30 $GL_VERSION = gl_version * 1; 34 $GL_VERSION = gl_version * 1;
31 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; 35 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
32 36
33 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; 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;
34 46
35 CFClient::Texture::restore_state (); 47 CFClient::Texture::restore_state ();
36} 48}
37 49
38sub find_rcfile($) { 50sub find_rcfile($) {
76 } 88 }
77 89
78 close CFG; 90 close CFG;
79} 91}
80 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_SYNC{$db} = AnyEvent->timer (after => 5, cb => sub { $db->db_sync });
131
132 $db->db_put ($key => $data)
133}
134
81package CFClient::Texture; 135package CFClient::Texture;
82 136
83use strict; 137use strict;
84 138
85use Scalar::Util; 139use Scalar::Util;
86 140
87use SDL::OpenGL; 141use SDL::OpenGL;
88 142
89my @textures; 143my %TEXTURES;
90 144
91sub new { 145sub new {
92 my ($class, %data) = @_; 146 my ($class, %data) = @_;
93 147
94 my $self = bless { 148 my $self = bless {
95 internalformat => GL_RGBA, 149 internalformat => GL_RGBA,
96 format => GL_RGBA, 150 format => GL_RGBA,
151 type => GL_UNSIGNED_BYTE,
97 %data, 152 %data,
98 }, $class; 153 }, $class;
99 154
100 push @textures, $self; 155 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
101 Scalar::Util::weaken $textures[-1];
102 156
103 $self->upload; 157 $self->upload;
104 158
105 $self 159 $self
106} 160}
107 161
108sub new_from_image { 162sub new_from_image {
109 my ($class, $image) = @_; 163 my ($class, $image, %arg) = @_;
110 164
111 $class->new (image => $image) 165 $class->new (image => $image, %arg)
112} 166}
113 167
114sub new_from_file { 168sub new_from_file {
115 my ($class, $path) = @_; 169 my ($class, $path) = @_;
116 170
142 w => $w, 196 w => $w,
143 h => $h, 197 h => $h,
144 data => $data, 198 data => $data,
145 internalformat => GL_ALPHA4, 199 internalformat => GL_ALPHA4,
146 format => GL_ALPHA, 200 format => GL_ALPHA,
201 type => GL_UNSIGNED_BYTE,
147 ) 202 )
148} 203}
149 204
150sub new_from_opengl { 205sub new_from_opengl {
151 my ($class, $w, $h, $cb) = @_; 206 my ($class, $w, $h, $cb) = @_;
158} 213}
159 214
160sub upload { 215sub upload {
161 my ($self) = @_; 216 my ($self) = @_;
162 217
163 return unless $SDL::App::USING_OPENGL; 218 return unless $GL_VERSION;
164 219
165 my $data; 220 my $data;
166 221
167 if (exists $self->{data}) { 222 if (exists $self->{data}) {
168 $data = $self->{data}; 223 $data = $self->{data};
169 224
170 } elsif (exists $self->{render_cb}) { 225 } elsif (exists $self->{render_cb}) {
171 glViewport 0, 0, $self->{w}, $self->{h}; 226 glViewport 0, 0, $self->{w}, $self->{h};
172 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
173 glMatrixMode GL_PROJECTION; 227 glMatrixMode GL_PROJECTION;
174 glLoadIdentity; 228 glLoadIdentity;
229 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
175 glMatrixMode GL_MODELVIEW; 230 glMatrixMode GL_MODELVIEW;
176 glLoadIdentity; 231 glLoadIdentity;
177 glClear GL_COLOR_BUFFER_BIT;
178 $self->{render_cb}->($self, $self->{w}, $self->{h}); 232 $self->{render_cb}->($self, $self->{w}, $self->{h});
179 233
180 } else { 234 } else {
181 my $pb = new Gtk2::Gdk::PixbufLoader; 235 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
182 $pb->write ($self->{image}); 236 = 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->{w} = $pb->get_width;
189 $self->{h} = $pb->get_height;
190
191 $data = $pb->get_pixels;
192 } 237 }
193 238
194 my ($tw, $th) = @$self{qw(w h)}; 239 my ($tw, $th) = @$self{qw(w h)};
195 240
196 unless ($tw && $th) { 241 unless ($tw && $th) {
197 $tw = $th = 1; 242 $tw = $th = 1;
198 $data = "\x00" x 64; 243 $data = "\x00" x 64;
199 } 244 }
245
246 $self->{minified} = [CFClient::average $tw, $th, $data]
247 if $self->{minify};
200 248
201 unless ($GL_NPOT) { 249 unless ($GL_NPOT) {
202 # TODO: does not work for zero-sized textures 250 # TODO: does not work for zero-sized textures
203 $tw = topot $tw; 251 $tw = topot $tw;
204 $th = topot $th; 252 $th = topot $th;
216 264
217 $self->{name} ||= (glGenTextures 1)->[0]; 265 $self->{name} ||= (glGenTextures 1)->[0];
218 266
219 glBindTexture GL_TEXTURE_2D, $self->{name}; 267 glBindTexture GL_TEXTURE_2D, $self->{name};
220 268
221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 269 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 270 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 271 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 272 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
225 273
226 if (defined $data) { 274 if (defined $data) {
227 glTexImage2D GL_TEXTURE_2D, 0, 275 glTexImage2D GL_TEXTURE_2D, 0,
228 $self->{internalformat}, 276 $self->{internalformat},
229 $tw, $th, # need to pad texture first 277 $tw, $th, # need to pad texture first
230 0, 278 0,
231 $self->{format}, 279 $self->{format},
232 GL_UNSIGNED_BYTE, 280 $self->{type},
233 $data; 281 $data;
234 glGetError and die; 282 glGetError and die;
235 } else { 283 } else {
236 glCopyTexImage2D GL_TEXTURE_2D, 0, 284 glCopyTexImage2D GL_TEXTURE_2D, 0,
237 $self->{internalformat}, 285 $self->{internalformat},
238 0, 0, 286 0, 0,
239 $tw, $th, 287 $tw, $th,
240 0; 288 0;
241 glGetError and die; 289 glGetError and die "glCopyTexImage2D $tw,$th";
242 } 290 }
243} 291}
244 292
245sub DESTROY { 293sub DESTROY {
246 my ($self) = @_; 294 my ($self) = @_;
247 295
248 return unless exists $self->{name}; 296 delete $TEXTURES{$self+0};
249 297
250 glDeleteTextures delete $self->{name}; 298 glDeleteTextures delete $self->{name}
299 if $self->{name};
251} 300}
252 301
253sub restore_state{ 302sub restore_state{
254 $_->upload 303 $_->upload
255 for grep $_, @textures; 304 for values %TEXTURES;
256}; 305};
257 306
2581; 3071;
259 308
260=back 309=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines