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.3 by root, Fri Apr 7 18:20:13 2006 UTC vs.
Revision 1.24 by root, Tue Apr 11 18:06:52 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} 22}
23 23
24our %GL_EXT;
25our $GL_VERSION;
26
27our $GL_NPOT;
28
29sub gl_init {
30 $GL_VERSION = gl_version * 1;
31 %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions;
32
33 $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2;
34
35 CFClient::Texture::restore_state ();
36}
37
38sub find_rcfile($) {
39 my $path;
40
41 for (@INC) {
42 $path = "$_/CFClient/resources/$_[0]";
43 return $path if -r $path;
44 }
45
46 die "FATAL: can't find required file $_[0]\n";
47}
48
49sub read_cfg {
50 my ($file) = @_;
51
52 open CFG, $file
53 or return;
54
55 my $CFG;
56
57 local $/;
58 $CFG = eval <CFG>;
59
60 $::CFG = $CFG;
61
62 close CFG;
63}
64
65sub write_cfg {
66 my ($file) = @_;
67
68 open CFG, ">$file"
69 or return;
70
71 {
72 require Data::Dumper;
73 local $Data::Dumper::Purity = 1;
74 $::CFG->{VERSION} = $::VERSION;
75 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
76 }
77
78 close CFG;
79}
80
24package Crossfire::Client::Texture; 81package CFClient::Texture;
25 82
26use Scalar::Util; 83use Scalar::Util;
27 84
28use SDL::OpenGL; 85use SDL::OpenGL;
29 86
30my @textures; 87my @textures;
88
89sub new {
90 my ($class, %data) = @_;
91
92 my $self = bless {
93 internalformat => GL_RGBA,
94 format => GL_RGBA,
95 %data,
96 }, $class;
97
98 push @textures, $self;
99 Scalar::Util::weaken $textures[-1];
100
101 $self->upload;
102
103 $self
104}
105
106sub new_from_image {
107 my ($class, $image) = @_;
108
109 $class->new (image => $image)
110}
31 111
32sub new_from_file { 112sub new_from_file {
33 my ($class, $path) = @_; 113 my ($class, $path) = @_;
34 114
35 open my $fh, "<:raw", $path 115 open my $fh, "<:raw", $path
36 or die "$path: $!"; 116 or die "$path: $!";
37 117
38 local $/; 118 local $/;
39 $class->new_from_data (<$fh>) 119 $class->new_from_image (<$fh>)
40} 120}
41 121
42sub new_from_image { 122#sub new_from_surface {
43 my ($class, $image) = @_; 123# my ($class, $surface) = @_;
124#
125# $surface->rgba;
126#
127# $class->new (
128# data => $surface->pixels,
129# w => $surface->width,
130# h => $surface->height,
131# )
132#}
44 133
45 my $self = bless { 134sub new_from_layout {
46 image => $data, 135 my ($class, $layout) = @_;
47 };
48 136
49 push @textures, $self; 137 my ($w, $h, $data) = $layout->render;
50 Scalar::Util::weaken $textures[-1];
51 138
52 $self->upload; 139 $class->new (
140 w => $w,
141 h => $h,
142 data => $data,
143 internalformat => GL_ALPHA4,
144 format => GL_ALPHA,
145 )
146}
53 147
54 $self 148sub new_from_opengl {
149 my ($class, $w, $h, $cb) = @_;
150
151 $class->new (w => $w, h => $h, render_cb => $cb)
152}
153
154sub topot {
155 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
55} 156}
56 157
57sub upload { 158sub upload {
58 my ($self) = @_; 159 my ($self) = @_;
59 160
60 return unless $SDL::App::USING_OPENGL; 161 return unless $SDL::App::USING_OPENGL;
61 162
62 my ($data, $width, $height); 163 my $data;
63 164
64 if (exists $self->{data}) { 165 if (exists $self->{data}) {
65 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); 166 $data = $self->{data};
167 } elsif (exists $self->{render_cb}) {
168 glViewport 0, 0, $self->{w}, $self->{h};
169 glMatrixMode GL_PROJECTION;
170 glLoadIdentity;
171 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
172 glMatrixMode GL_MODELVIEW;
173 glPushmatrix;
174 glLoadIdentity;
175 glClear GL_COLOR_BUFFER_BIT;
176
177 $self->{render_cb}->($self, $self->{w}, $self->{h});
66 } else { 178 } else {
67 my $pb = new Gtk2::Gdk::PixbufLoader; 179 my $pb = new Gtk2::Gdk::PixbufLoader;
68 $pb->write ($self->{data}); 180 $pb->write ($self->{image});
69 $pb->close; 181 $pb->close;
70 182
71 $pb = $pb->get_pixbuf; 183 $pb = $pb->get_pixbuf;
72 $pb = $pb->add_alpha (0, 0, 0, 0); 184 $pb = $pb->add_alpha (0, 0, 0, 0);
73 185
74 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); 186 $self->{w} = $pb->get_width;
75 } 187 $self->{h} = $pb->get_height;
76 188
189 $data = $pb->get_pixels;
190 }
191
192 my ($tw, $th) = @$self{qw(w h)};
193
194 unless ($tw || $th) {
195 $tw = $th = 1;
196 $data = "\x00" x 64;
197 }
198
199 unless ($GL_NPOT) {
200 # TODO: does not work for zero-sized textures
201 $tw = topot $tw;
202 $th = topot $th;
203
204 if (defined $data) {
205 my $bpp = (length $data) / ($self->{w} * $self->{h});
206 $data = pack "(a" . ($tw * $bpp) . ")*",
207 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
208 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
209 }
210 }
211
212 $self->{s} = $self->{w} / $tw;
213 $self->{t} = $self->{h} / $th;
214
77 ($self->{name}) = @{glGenTextures 1}; 215 $self->{name} ||= (glGenTextures 1)->[0];
78 216
79 glBindTexture GL_TEXTURE_2D, $self->{name}; 217 glBindTexture GL_TEXTURE_2D, $self->{name};
80 218
81 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
82 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 220 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
83 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
84 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
85 223
224 glGetError;
225 if (defined $data) {
86 glTexImage2D GL_TEXTURE_2D, 0, 226 glTexImage2D GL_TEXTURE_2D, 0,
87 GL_RGBA8, 227 $self->{internalformat},
88 $width, $height, 228 $tw, $th, # need to pad texture first
89 0, 229 0,
90 GL_RGBA, 230 $self->{format},
91 GL_UNSIGNED_BYTE, 231 GL_UNSIGNED_BYTE,
92 $data; 232 $data;
233 glGetError and die;
234 } else {
235 glCopyTexImage2D GL_TEXTURE_2D, 0,
236 $self->{internalformat},
237 0, 0,
238 $tw, $th,
239 0;
240 glPopmatrix;
241 }
93} 242}
94 243
95sub DESTROY { 244sub DESTROY {
96 my ($self) = @_; 245 my ($self) = @_;
97 246
98 return unless exists $self->{name}; 247 return unless exists $self->{name};
99 248
100 glDeleteTextures delete $self->{name}; 249 glDeleteTextures delete $self->{name};
101} 250}
102 251
103push @::GLINIT, sub { 252sub restore_state{
104 $_->upload 253 $_->upload
105 for grep $_, @textures; 254 for grep $_, @textures;
106}; 255};
107 256
1081; 2571;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines