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.5 by root, Fri Apr 7 20:13: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}
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}
53 67
54 open CFG, ">$file" 68 open CFG, ">$file"
55 or return; 69 or return;
56 70
57 { 71 {
72 require Data::Dumper;
58 local $Data::Dumper::Purity = 1; 73 local $Data::Dumper::Purity = 1;
59 $::CFG->{VERSION} = $::VERSION; 74 $::CFG->{VERSION} = $::VERSION;
60 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); 75 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
61 } 76 }
62 77
63 close CFG; 78 close CFG;
64} 79}
65 80
66package Crossfire::Client::Texture; 81package CFClient::Texture;
67 82
68use Scalar::Util; 83use Scalar::Util;
69 84
70use SDL::OpenGL; 85use SDL::OpenGL;
71 86
72my @textures; 87my @textures;
73 88
74sub _new { 89sub new {
75 my ($class, %data) = @_; 90 my ($class, %data) = @_;
76 91
77 my $self = bless \%data, $class; 92 my $self = bless {
93 internalformat => GL_RGBA,
94 format => GL_RGBA,
95 %data,
96 }, $class;
78 97
79 push @textures, $self; 98 push @textures, $self;
80 Scalar::Util::weaken $textures[-1]; 99 Scalar::Util::weaken $textures[-1];
81 100
82 $self->upload; 101 $self->upload;
85} 104}
86 105
87sub new_from_image { 106sub new_from_image {
88 my ($class, $image) = @_; 107 my ($class, $image) = @_;
89 108
90 $class->_new (image => $image) 109 $class->new (image => $image)
91} 110}
92 111
93sub new_from_file { 112sub new_from_file {
94 my ($class, $path) = @_; 113 my ($class, $path) = @_;
95 114
98 117
99 local $/; 118 local $/;
100 $class->new_from_image (<$fh>) 119 $class->new_from_image (<$fh>)
101} 120}
102 121
103sub new_from_surface { 122#sub new_from_surface {
104 my ($class, $surface) = @_; 123# my ($class, $surface) = @_;
105 124#
106 $surface->rgba; 125# $surface->rgba;
107 126#
108 $class->_new ( 127# $class->new (
109 data => $surface->pixels, 128# data => $surface->pixels,
110 width => $surface->width, 129# w => $surface->width,
111 height => $surface->height, 130# h => $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 w => $w,
141 h => $h,
142 data => $data,
143 internalformat => GL_ALPHA4,
144 format => GL_ALPHA,
112 ) 145 )
146}
147
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]
113} 156}
114 157
115sub upload { 158sub upload {
116 my ($self) = @_; 159 my ($self) = @_;
117 160
118 return unless $SDL::App::USING_OPENGL; 161 return unless $SDL::App::USING_OPENGL;
119 162
120 my ($data, $width, $height); 163 my $data;
121 164
122 if (exists $self->{data}) { 165 if (exists $self->{data}) {
123 ($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});
124 } else { 178 } else {
125 my $pb = new Gtk2::Gdk::PixbufLoader; 179 my $pb = new Gtk2::Gdk::PixbufLoader;
126 $pb->write ($self->{image}); 180 $pb->write ($self->{image});
127 $pb->close; 181 $pb->close;
128 182
129 $pb = $pb->get_pixbuf; 183 $pb = $pb->get_pixbuf;
130 $pb = $pb->add_alpha (0, 0, 0, 0); 184 $pb = $pb->add_alpha (0, 0, 0, 0);
131 185
132 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); 186 $self->{w} = $pb->get_width;
133 } 187 $self->{h} = $pb->get_height;
134 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
135 ($self->{name}) = @{glGenTextures 1}; 215 $self->{name} ||= (glGenTextures 1)->[0];
136 216
137 glBindTexture GL_TEXTURE_2D, $self->{name}; 217 glBindTexture GL_TEXTURE_2D, $self->{name};
138 218
139 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 219 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
140 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;
141 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 221 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
142 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 222 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
143 223
224 glGetError;
225 if (defined $data) {
144 glTexImage2D GL_TEXTURE_2D, 0, 226 glTexImage2D GL_TEXTURE_2D, 0,
145 GL_RGBA8, 227 $self->{internalformat},
146 $width, $height, 228 $tw, $th, # need to pad texture first
147 0, 229 0,
148 GL_RGBA, 230 $self->{format},
149 GL_UNSIGNED_BYTE, 231 GL_UNSIGNED_BYTE,
150 $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 }
151} 242}
152 243
153sub DESTROY { 244sub DESTROY {
154 my ($self) = @_; 245 my ($self) = @_;
155 246
156 return unless exists $self->{name}; 247 return unless exists $self->{name};
157 248
158 glDeleteTextures delete $self->{name}; 249 glDeleteTextures delete $self->{name};
159} 250}
160 251
161push @::GLINIT, sub { 252sub restore_state{
162 $_->upload 253 $_->upload
163 for grep $_, @textures; 254 for grep $_, @textures;
164}; 255};
165 256
1661; 2571;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines