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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines