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.8 by root, Sat Apr 8 13:36:25 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}
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 ();
22} 46}
23 47
24sub find_rcfile($) { 48sub find_rcfile($) {
25 my $path; 49 my $path;
26 50
27 for (@INC) { 51 for (@INC) {
28 $path = "$_/Crossfire/resources/$_[0]"; 52 $path = "$_/CFClient/resources/$_[0]";
29 return $path if -r $path; 53 return $path if -r $path;
30 } 54 }
31 55
32 die "FATAL: can't find required file $_[0]\n"; 56 die "FATAL: can't find required file $_[0]\n";
33} 57}
53 77
54 open CFG, ">$file" 78 open CFG, ">$file"
55 or return; 79 or return;
56 80
57 { 81 {
82 require Data::Dumper;
58 local $Data::Dumper::Purity = 1; 83 local $Data::Dumper::Purity = 1;
59 $::CFG->{VERSION} = $::VERSION; 84 $::CFG->{VERSION} = $::VERSION;
60 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); 85 print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
61 } 86 }
62 87
63 close CFG; 88 close CFG;
64} 89}
65 90
66package Crossfire::Client::Texture; 91package CFClient::Texture;
92
93use strict;
67 94
68use Scalar::Util; 95use Scalar::Util;
69 96
70use SDL::OpenGL; 97use SDL::OpenGL;
71 98
72my @textures; 99my @textures;
73 100
74sub _new { 101sub new {
75 my ($class, %data) = @_; 102 my ($class, %data) = @_;
76 103
77 my $self = bless \%data, $class; 104 my $self = bless {
105 internalformat => GL_RGBA,
106 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE,
108 %data,
109 }, $class;
78 110
79 push @textures, $self; 111 push @textures, $self;
80 Scalar::Util::weaken $textures[-1]; 112 Scalar::Util::weaken $textures[-1];
81 113
82 $self->upload; 114 $self->upload;
85} 117}
86 118
87sub new_from_image { 119sub new_from_image {
88 my ($class, $image) = @_; 120 my ($class, $image) = @_;
89 121
90 $class->_new (image => $image) 122 $class->new (image => $image)
91} 123}
92 124
93sub new_from_file { 125sub new_from_file {
94 my ($class, $path) = @_; 126 my ($class, $path) = @_;
95 127
98 130
99 local $/; 131 local $/;
100 $class->new_from_image (<$fh>) 132 $class->new_from_image (<$fh>)
101} 133}
102 134
103sub new_from_surface { 135#sub new_from_surface {
104 my ($class, $surface) = @_; 136# my ($class, $surface) = @_;
105 137#
106 $surface->rgba; 138# $surface->rgba;
107 139#
108 $class->_new ( 140# $class->new (
109 data => $surface->pixels, 141# data => $surface->pixels,
110 width => $surface->width, 142# w => $surface->width,
111 height => $surface->height, 143# h => $surface->height,
144# )
145#}
146
147sub new_from_layout {
148 my ($class, $layout) = @_;
149
150 my ($w, $h, $data) = $layout->render;
151
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,
112 ) 159 )
113}
114
115sub new_from_ttf {
116 my ($class, $ttf, $text) = @_;
117
118 utf8::encode $text;
119
120 my $surface = SDL::TTFRenderUTF8Blended $ttf, $text,
121 (new SDL::Color -r => 255, -g => 255, -b => 255);
122
123 $class->new_from_surface (bless \$surface, SDL::Surface::)
124} 160}
125 161
126sub new_from_opengl { 162sub new_from_opengl {
127 my ($class, $w, $h, $cb) = @_; 163 my ($class, $w, $h, $cb) = @_;
128 164
129 $class->_new (width => $w, height => $h, render => $cb) 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]
130} 170}
131 171
132sub upload { 172sub upload {
133 my ($self) = @_; 173 my ($self) = @_;
134 174
135 return unless $SDL::App::USING_OPENGL; 175 return unless $GL_VERSION;
136 176
137 my $data; 177 my $data;
138 178
139 if (exists $self->{data}) { 179 if (exists $self->{data}) {
140 $data = $self->{data}; 180 $data = $self->{data};
181
141 } elsif (exists $self->{rendercb}) { 182 } elsif (exists $self->{render_cb}) {
142 glViewport 0, 0, $self->{width}, $self->{height}; 183 glViewport 0, 0, $self->{w}, $self->{h};
143 glClear GL_COLOR_BUFFER_BIT; 184 glMatrixMode GL_PROJECTION;
144 185 glLoadIdentity;
186 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity;
145 $self->{rendercb}->($self, $self->{width}, $self->{height}); 189 $self->{render_cb}->($self, $self->{w}, $self->{h});
190
146 } else { 191 } else {
147 my $pb = new Gtk2::Gdk::PixbufLoader; 192 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
148 $pb->write ($self->{image}); 193 = CFClient::load_image_inline $self->{image};
149 $pb->close;
150
151 $pb = $pb->get_pixbuf;
152 $pb = $pb->add_alpha (0, 0, 0, 0);
153
154 $self->{width} = $pb->get_width;
155 $self->{height} = $pb->get_height;
156
157 $data = $pb->get_pixels;
158 } 194 }
159 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
160 ($self->{name}) = @{glGenTextures 1}; 219 $self->{name} ||= (glGenTextures 1)->[0];
161 220
162 glBindTexture GL_TEXTURE_2D, $self->{name}; 221 glBindTexture GL_TEXTURE_2D, $self->{name};
163 222
164 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 223 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
165 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;
166 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
167 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
168 227
169 if (defined $data) { 228 if (defined $data) {
170 glTexImage2D GL_TEXTURE_2D, 0, 229 glTexImage2D GL_TEXTURE_2D, 0,
171 GL_RGBA8, 230 $self->{internalformat},
172 $self->{width}, $self->{height}, 231 $tw, $th, # need to pad texture first
173 0, 232 0,
174 GL_RGBA, 233 $self->{format},
175 GL_UNSIGNED_BYTE, 234 $self->{type},
176 $data; 235 $data;
236 glGetError and die;
177 } else { 237 } else {
178 glCopyTexImage2D GL_TEXTURE_2D, 0, 238 glCopyTexImage2D GL_TEXTURE_2D, 0,
179 GL_RGBA8, 239 $self->{internalformat},
180 0, 0, 240 0, 0,
181 $self->{width}, $self->{height}, 241 $tw, $th,
182 0; 242 0;
243 glGetError and die "glCopyTexImage2D $tw,$th";
183 } 244 }
184} 245}
185 246
186sub DESTROY { 247sub DESTROY {
187 my ($self) = @_; 248 my ($self) = @_;
189 return unless exists $self->{name}; 250 return unless exists $self->{name};
190 251
191 glDeleteTextures delete $self->{name}; 252 glDeleteTextures delete $self->{name};
192} 253}
193 254
194push @::GLINIT, sub { 255sub restore_state{
195 $_->upload 256 $_->upload
196 for grep $_, @textures; 257 for grep $_, @textures;
197}; 258};
198 259
1991; 2601;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines