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.17 by root, Sun Apr 9 21:50:21 2006 UTC vs.
Revision 1.22 by root, Tue Apr 11 13:14:36 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
115# width => $surface->width, 129# width => $surface->width,
116# height => $surface->height, 130# height => $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 width => $w,
127 height => $h, 141 height => $h,
128 data => $data, 142 data => $data,
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 (width => $w, height => $h, rendercb => $cb)
138} 152}
139 153
154sub topot {
155 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
156}
157
140sub upload { 158sub upload {
141 my ($self) = @_; 159 my ($self) = @_;
142 160
143 return unless $SDL::App::USING_OPENGL; 161 return unless $SDL::App::USING_OPENGL;
144 162
148 $data = $self->{data}; 166 $data = $self->{data};
149 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{rendercb}) {
150 glViewport 0, 0, $self->{width}, $self->{height}; 168 glViewport 0, 0, $self->{width}, $self->{height};
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->{width}, 0, $self->{height}, -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
169 $self->{height} = $pb->get_height; 187 $self->{height} = $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(width height)};
193
194 unless ($GL_NPOT) {
195 $tw = topot $tw;
196 $th = topot $th;
197
198 if (defined $data) {
199 my $bpp = (length $data) / ($self->{width} * $self->{height});
200 $data = pack "(a" . ($tw * $bpp) . ")*",
201 unpack "(a" . ($self->{width} * $bpp) . ")*", $data;
202 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height});
203 }
204 }
205
206 $self->{s} = $self->{width} / $tw;
207 $self->{t} = $self->{height} / $th;
208
174 ($self->{name}) = @{glGenTextures 1}; 209 $self->{name} ||= (glGenTextures 1)->[0];
175 210
176 glBindTexture GL_TEXTURE_2D, $self->{name}; 211 glBindTexture GL_TEXTURE_2D, $self->{name};
177 212
178 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; 214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
180 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
181 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
182 217
183 glGetError; 218 glGetError;
184 if (defined $data) { 219 if (defined $data) {
185 glTexImage2D GL_TEXTURE_2D, 0, 220 glTexImage2D GL_TEXTURE_2D, 0,
186 $self->{internalformat}, 221 $self->{internalformat},
187 $self->{width}, $self->{height}, 222 $tw, $th, # need to pad texture first
188 0, 223 0,
189 $self->{format}, 224 $self->{format},
190 GL_UNSIGNED_BYTE, 225 GL_UNSIGNED_BYTE,
191 $data; 226 $data;
192 glGetError and die; 227 glGetError and die;
193 } else { 228 } else {
194 glCopyTexImage2D GL_TEXTURE_2D, 0, 229 glCopyTexImage2D GL_TEXTURE_2D, 0,
195 $self->{internalformat}, 230 $self->{internalformat},
196 0, 0, 231 0, 0,
197 $self->{width}, $self->{height}, 232 $tw, $th,
198 0; 233 0;
199 glPopmatrix; 234 glPopmatrix;
200 } 235 }
201} 236}
202 237
206 return unless exists $self->{name}; 241 return unless exists $self->{name};
207 242
208 glDeleteTextures delete $self->{name}; 243 glDeleteTextures delete $self->{name};
209} 244}
210 245
211push @::GL_INIT, sub { 246sub restore_state{
212 $_->upload 247 $_->upload
213 for grep $_, @textures; 248 for grep $_, @textures;
214}; 249};
215 250
2161; 2511;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines