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.28 by root, Wed Apr 12 20:06:36 2006 UTC vs.
Revision 1.37 by root, Sun Apr 16 07:25:29 2006 UTC

19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION; 21 XSLoader::load "CFClient", $VERSION;
22} 22}
23 23
24use AnyEvent;
25use BerkeleyDB;
24use SDL::OpenGL; 26use SDL::OpenGL;
25 27
26our %GL_EXT; 28our %GL_EXT;
27our $GL_VERSION; 29our $GL_VERSION;
28 30
31sub gl_init { 33sub gl_init {
32 $GL_VERSION = gl_version * 1; 34 $GL_VERSION = gl_version * 1;
33 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; 35 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
34 36
35 $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;
36
37 glClearColor 0.45, 0.45, 0.45, 1;
38 38
39 glEnable GL_TEXTURE_2D; 39 glEnable GL_TEXTURE_2D;
40 glEnable GL_COLOR_MATERIAL; 40 glEnable GL_COLOR_MATERIAL;
41 glShadeModel GL_FLAT; 41 glShadeModel GL_FLAT;
42 glDisable GL_DEPTH_TEST; 42 glDisable GL_DEPTH_TEST;
88 } 88 }
89 89
90 close CFG; 90 close CFG;
91} 91}
92 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
93package CFClient::Texture; 135package CFClient::Texture;
94 136
95use strict; 137use strict;
96 138
97use Scalar::Util; 139use Scalar::Util;
98 140
99use SDL::OpenGL; 141use SDL::OpenGL;
100 142
101my @textures; 143my %TEXTURES;
102 144
103sub new { 145sub new {
104 my ($class, %data) = @_; 146 my ($class, %data) = @_;
105 147
106 my $self = bless { 148 my $self = bless {
107 internalformat => GL_RGBA, 149 internalformat => GL_RGBA,
108 format => GL_RGBA, 150 format => GL_RGBA,
151 type => GL_UNSIGNED_BYTE,
109 %data, 152 %data,
110 }, $class; 153 }, $class;
111 154
112 push @textures, $self; 155 Scalar::Util::weaken ($TEXTURES{$self+0} = $self);
113 Scalar::Util::weaken $textures[-1];
114 156
115 $self->upload; 157 $self->upload;
116 158
117 $self 159 $self
118} 160}
119 161
120sub new_from_image { 162sub new_from_image {
121 my ($class, $image) = @_; 163 my ($class, $image, %arg) = @_;
122 164
123 $class->new (image => $image) 165 $class->new (image => $image, %arg)
124} 166}
125 167
126sub new_from_file { 168sub new_from_file {
127 my ($class, $path) = @_; 169 my ($class, $path) = @_;
128 170
154 w => $w, 196 w => $w,
155 h => $h, 197 h => $h,
156 data => $data, 198 data => $data,
157 internalformat => GL_ALPHA4, 199 internalformat => GL_ALPHA4,
158 format => GL_ALPHA, 200 format => GL_ALPHA,
201 type => GL_UNSIGNED_BYTE,
159 ) 202 )
160} 203}
161 204
162sub new_from_opengl { 205sub new_from_opengl {
163 my ($class, $w, $h, $cb) = @_; 206 my ($class, $w, $h, $cb) = @_;
179 if (exists $self->{data}) { 222 if (exists $self->{data}) {
180 $data = $self->{data}; 223 $data = $self->{data};
181 224
182 } elsif (exists $self->{render_cb}) { 225 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h}; 226 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 glMatrixMode GL_PROJECTION; 227 glMatrixMode GL_PROJECTION;
186 glLoadIdentity; 228 glLoadIdentity;
229 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_MODELVIEW; 230 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity; 231 glLoadIdentity;
189 glClear GL_COLOR_BUFFER_BIT;
190 $self->{render_cb}->($self, $self->{w}, $self->{h}); 232 $self->{render_cb}->($self, $self->{w}, $self->{h});
191 233
192 } else { 234 } else {
193 use Gtk2;#d# TODO kill 235 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
194 my $pb = new Gtk2::Gdk::PixbufLoader; 236 = CFClient::load_image_inline $self->{image};
195 $pb->write ($self->{image});
196 $pb->close;
197
198 $pb = $pb->get_pixbuf;
199 $pb = $pb->add_alpha (0, 0, 0, 0);
200
201 $self->{w} = $pb->get_width;
202 $self->{h} = $pb->get_height;
203
204 $data = $pb->get_pixels;
205 } 237 }
206 238
207 my ($tw, $th) = @$self{qw(w h)}; 239 my ($tw, $th) = @$self{qw(w h)};
208 240
209 unless ($tw && $th) { 241 unless ($tw && $th) {
210 $tw = $th = 1; 242 $tw = $th = 1;
211 $data = "\x00" x 64; 243 $data = "\x00" x 64;
212 } 244 }
245
246 $self->{minified} = [CFClient::average $tw, $th, $data]
247 if $self->{minify};
213 248
214 unless ($GL_NPOT) { 249 unless ($GL_NPOT) {
215 # TODO: does not work for zero-sized textures 250 # TODO: does not work for zero-sized textures
216 $tw = topot $tw; 251 $tw = topot $tw;
217 $th = topot $th; 252 $th = topot $th;
240 glTexImage2D GL_TEXTURE_2D, 0, 275 glTexImage2D GL_TEXTURE_2D, 0,
241 $self->{internalformat}, 276 $self->{internalformat},
242 $tw, $th, # need to pad texture first 277 $tw, $th, # need to pad texture first
243 0, 278 0,
244 $self->{format}, 279 $self->{format},
245 GL_UNSIGNED_BYTE, 280 $self->{type},
246 $data; 281 $data;
247 glGetError and die; 282 glGetError and die;
248 } else { 283 } else {
249 glCopyTexImage2D GL_TEXTURE_2D, 0, 284 glCopyTexImage2D GL_TEXTURE_2D, 0,
250 $self->{internalformat}, 285 $self->{internalformat},
251 0, 0, 286 0, 0,
252 $tw, $th, 287 $tw, $th,
253 0; 288 0;
254 glGetError and die; 289 glGetError and die "glCopyTexImage2D $tw,$th";
255 } 290 }
256} 291}
257 292
258sub DESTROY { 293sub DESTROY {
259 my ($self) = @_; 294 my ($self) = @_;
260 295
261 return unless exists $self->{name}; 296 delete $TEXTURES{$self+0};
262 297
263 glDeleteTextures delete $self->{name}; 298 glDeleteTextures delete $self->{name}
299 if $self->{name};
264} 300}
265 301
266sub restore_state{ 302sub restore_state{
267 $_->upload 303 $_->upload
268 for grep $_, @textures; 304 for values %TEXTURES;
269}; 305};
270 306
2711; 3071;
272 308
273=back 309=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines