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.15 by root, Sun Apr 9 00:12:58 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}
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 ();
22} 36}
23 37
24sub find_rcfile($) { 38sub find_rcfile($) {
25 my $path; 39 my $path;
26 40
27 for (@INC) { 41 for (@INC) {
28 $path = "$_/Crossfire/resources/$_[0]"; 42 $path = "$_/CFClient/resources/$_[0]";
29 return $path if -r $path; 43 return $path if -r $path;
30 } 44 }
31 45
32 die "FATAL: can't find required file $_[0]\n"; 46 die "FATAL: can't find required file $_[0]\n";
33} 47}
62 } 76 }
63 77
64 close CFG; 78 close CFG;
65} 79}
66 80
67package Crossfire::Client::Texture; 81package CFClient::Texture;
68 82
69use Scalar::Util; 83use Scalar::Util;
70 84
71use SDL::OpenGL; 85use SDL::OpenGL;
72 86
110# 124#
111# $surface->rgba; 125# $surface->rgba;
112# 126#
113# $class->new ( 127# $class->new (
114# data => $surface->pixels, 128# data => $surface->pixels,
115# width => $surface->width, 129# w => $surface->width,
116# height => $surface->height, 130# h => $surface->height,
117# ) 131# )
118#} 132#}
119 133
120sub new_from_text { 134sub new_from_layout {
121 my ($class, $text, $height) = @_; 135 my ($class, $layout) = @_;
122 136
123 my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; 137 my ($w, $h, $data) = $layout->render;
124 138
125 $class->new ( 139 $class->new (
126 width => $w, 140 w => $w,
127 height => $h, 141 h => $h,
128 data => $data, 142 data => $data,
129 internalformat => GL_ALPHA8, 143 internalformat => GL_ALPHA4,
130 format => GL_ALPHA, 144 format => GL_ALPHA,
131 ) 145 )
132} 146}
133 147
134sub new_from_opengl { 148sub new_from_opengl {
135 my ($class, $w, $h, $cb) = @_; 149 my ($class, $w, $h, $cb) = @_;
136 150
137 $class->new (width => $w, height => $h, rendercb => $cb) 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]
138} 156}
139 157
140sub upload { 158sub upload {
141 my ($self) = @_; 159 my ($self) = @_;
142 160
144 162
145 my $data; 163 my $data;
146 164
147 if (exists $self->{data}) { 165 if (exists $self->{data}) {
148 $data = $self->{data}; 166 $data = $self->{data};
149 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{render_cb}) {
150 glViewport 0, 0, $self->{width}, $self->{height}; 168 glViewport 0, 0, $self->{w}, $self->{h};
151 glMatrixMode GL_PROJECTION; 169 glMatrixMode GL_PROJECTION;
152 glLoadIdentity; 170 glLoadIdentity;
153 glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; 171 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
154 glMatrixMode GL_MODELVIEW; 172 glMatrixMode GL_MODELVIEW;
155 glPushmatrix; 173 glPushmatrix;
156 glLoadIdentity; 174 glLoadIdentity;
157 glClear GL_COLOR_BUFFER_BIT; 175 glClear GL_COLOR_BUFFER_BIT;
158 176
159 $self->{rendercb}->($self, $self->{width}, $self->{height}); 177 $self->{render_cb}->($self, $self->{w}, $self->{h});
160 } else { 178 } else {
161 my $pb = new Gtk2::Gdk::PixbufLoader; 179 my $pb = new Gtk2::Gdk::PixbufLoader;
162 $pb->write ($self->{image}); 180 $pb->write ($self->{image});
163 $pb->close; 181 $pb->close;
164 182
165 $pb = $pb->get_pixbuf; 183 $pb = $pb->get_pixbuf;
166 $pb = $pb->add_alpha (0, 0, 0, 0); 184 $pb = $pb->add_alpha (0, 0, 0, 0);
167 185
168 $self->{width} = $pb->get_width; 186 $self->{w} = $pb->get_width;
169 $self->{height} = $pb->get_height; 187 $self->{h} = $pb->get_height;
170 188
171 $data = $pb->get_pixels; 189 $data = $pb->get_pixels;
172 } 190 }
173 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
174 ($self->{name}) = @{glGenTextures 1}; 215 $self->{name} ||= (glGenTextures 1)->[0];
175 216
176 glBindTexture GL_TEXTURE_2D, $self->{name}; 217 glBindTexture GL_TEXTURE_2D, $self->{name};
177 218
178 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; 220 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
180 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
181 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
182 223
224 glGetError;
183 if (defined $data) { 225 if (defined $data) {
184 glTexImage2D GL_TEXTURE_2D, 0, 226 glTexImage2D GL_TEXTURE_2D, 0,
185 $self->{internalformat}, 227 $self->{internalformat},
186 $self->{width}, $self->{height}, 228 $tw, $th, # need to pad texture first
187 0, 229 0,
188 $self->{format}, 230 $self->{format},
189 GL_UNSIGNED_BYTE, 231 GL_UNSIGNED_BYTE,
190 $data; 232 $data;
233 glGetError and die;
191 } else { 234 } else {
192 glCopyTexImage2D GL_TEXTURE_2D, 0, 235 glCopyTexImage2D GL_TEXTURE_2D, 0,
193 $self->{internalformat}, 236 $self->{internalformat},
194 0, 0, 237 0, 0,
195 $self->{width}, $self->{height}, 238 $tw, $th,
196 0; 239 0;
197 glPopmatrix; 240 glPopmatrix;
198 #SDL::GLSwapBuffers;
199 #sleep 1;
200 } 241 }
201} 242}
202 243
203sub DESTROY { 244sub DESTROY {
204 my ($self) = @_; 245 my ($self) = @_;
206 return unless exists $self->{name}; 247 return unless exists $self->{name};
207 248
208 glDeleteTextures delete $self->{name}; 249 glDeleteTextures delete $self->{name};
209} 250}
210 251
211push @::GLINIT, sub { 252sub restore_state{
212 $_->upload 253 $_->upload
213 for grep $_, @textures; 254 for grep $_, @textures;
214}; 255};
215 256
2161; 2571;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines