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.13 by elmex, Sat Apr 8 22:23:57 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
73my @textures; 87my @textures;
74 88
75sub _new { 89sub new {
76 my ($class, %data) = @_; 90 my ($class, %data) = @_;
77 91
78 my $self = bless \%data, $class; 92 my $self = bless {
93 internalformat => GL_RGBA,
94 format => GL_RGBA,
95 %data,
96 }, $class;
79 97
80 push @textures, $self; 98 push @textures, $self;
81 Scalar::Util::weaken $textures[-1]; 99 Scalar::Util::weaken $textures[-1];
82 100
83 $self->upload; 101 $self->upload;
86} 104}
87 105
88sub new_from_image { 106sub new_from_image {
89 my ($class, $image) = @_; 107 my ($class, $image) = @_;
90 108
91 $class->_new (image => $image) 109 $class->new (image => $image)
92} 110}
93 111
94sub new_from_file { 112sub new_from_file {
95 my ($class, $path) = @_; 113 my ($class, $path) = @_;
96 114
99 117
100 local $/; 118 local $/;
101 $class->new_from_image (<$fh>) 119 $class->new_from_image (<$fh>)
102} 120}
103 121
104sub new_from_surface { 122#sub new_from_surface {
105 my ($class, $surface) = @_; 123# my ($class, $surface) = @_;
106 124#
107 $surface->rgba; 125# $surface->rgba;
108 126#
109 $class->_new ( 127# $class->new (
110 data => $surface->pixels, 128# data => $surface->pixels,
111 width => $surface->width, 129# width => $surface->width,
112 height => $surface->height, 130# height => $surface->height,
131# )
132#}
133
134sub new_from_layout {
135 my ($class, $layout) = @_;
136
137 my ($w, $h, $data) = $layout->render;
138
139 $class->new (
140 width => $w,
141 height => $h,
142 data => $data,
143 internalformat => GL_ALPHA4,
144 format => GL_ALPHA,
113 ) 145 )
114}
115
116sub new_from_ttf {
117 my ($class, $ttf, $text) = @_;
118
119 utf8::encode $text;
120
121 my $surface = SDL::TTFRenderUTF8Blended $ttf, $text,
122 (new SDL::Color -r => 0, -g => 0, -b => 0);
123
124 $class->new_from_surface (bless \$surface, SDL::Surface::)
125} 146}
126 147
127sub new_from_opengl { 148sub new_from_opengl {
128 my ($class, $w, $h, $cb) = @_; 149 my ($class, $w, $h, $cb) = @_;
129 150
130 $class->_new (width => $w, height => $h, rendercb => $cb) 151 $class->new (width => $w, height => $h, rendercb => $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]
131} 156}
132 157
133sub upload { 158sub upload {
134 my ($self) = @_; 159 my ($self) = @_;
135 160
141 $data = $self->{data}; 166 $data = $self->{data};
142 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{rendercb}) {
143 glViewport 0, 0, $self->{width}, $self->{height}; 168 glViewport 0, 0, $self->{width}, $self->{height};
144 glMatrixMode GL_PROJECTION; 169 glMatrixMode GL_PROJECTION;
145 glLoadIdentity; 170 glLoadIdentity;
146 glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; 171 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
147 glMatrixMode GL_MODELVIEW; 172 glMatrixMode GL_MODELVIEW;
148 glPushmatrix; 173 glPushmatrix;
149 glLoadIdentity; 174 glLoadIdentity;
150 glClear GL_COLOR_BUFFER_BIT; 175 glClear GL_COLOR_BUFFER_BIT;
151 176
162 $self->{height} = $pb->get_height; 187 $self->{height} = $pb->get_height;
163 188
164 $data = $pb->get_pixels; 189 $data = $pb->get_pixels;
165 } 190 }
166 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
167 ($self->{name}) = @{glGenTextures 1}; 209 $self->{name} ||= (glGenTextures 1)->[0];
168 210
169 glBindTexture GL_TEXTURE_2D, $self->{name}; 211 glBindTexture GL_TEXTURE_2D, $self->{name};
170 212
171 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; 213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
172 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;
173 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
174 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
175 217
218 glGetError;
176 if (defined $data) { 219 if (defined $data) {
177 glTexImage2D GL_TEXTURE_2D, 0, 220 glTexImage2D GL_TEXTURE_2D, 0,
178 GL_RGBA8, 221 $self->{internalformat},
179 $self->{width}, $self->{height}, 222 $tw, $th, # need to pad texture first
180 0, 223 0,
181 GL_RGBA, 224 $self->{format},
182 GL_UNSIGNED_BYTE, 225 GL_UNSIGNED_BYTE,
183 $data; 226 $data;
227 glGetError and die;
184 } else { 228 } else {
185 warn "I{DTEDRAW TEX: $self->{width} $self->{height} \n";
186 warn "ERR: ". (glGetError). "\n";
187 my ($bd, $br) = (glGet (GL_DRAW_BUFFER), glGet (GL_READ_BUFFER));
188 warn "BUF:[$bd $br]\n";
189
190 glCopyTexImage2D GL_TEXTURE_2D, 0, 229 glCopyTexImage2D GL_TEXTURE_2D, 0,
191 GL_RGBA8, 230 $self->{internalformat},
192 0, 0, 231 0, 0,
193 $self->{width}, $self->{height}, 232 $tw, $th,
194 0; 233 0;
195 glPopmatrix; 234 glPopmatrix;
196 warn "ERR: ". (glGetError). "\n";
197 SDL::GLSwapBuffers;
198 sleep 1;
199 } 235 }
200} 236}
201 237
202sub DESTROY { 238sub DESTROY {
203 my ($self) = @_; 239 my ($self) = @_;
205 return unless exists $self->{name}; 241 return unless exists $self->{name};
206 242
207 glDeleteTextures delete $self->{name}; 243 glDeleteTextures delete $self->{name};
208} 244}
209 245
210push @::GLINIT, sub { 246sub restore_state{
211 $_->upload 247 $_->upload
212 for grep $_, @textures; 248 for grep $_, @textures;
213}; 249};
214 250
2151; 2511;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines