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.6 by root, Fri Apr 7 20:34:44 2006 UTC vs.
Revision 1.30 by root, Wed Apr 12 21:35:10 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} 160}
114 161
115sub new_from_ttf { 162sub new_from_opengl {
116 my ($class, $ttf, $text) = @_; 163 my ($class, $w, $h, $cb) = @_;
117 164
118 utf8::upgrade $text; 165 $class->new (w => $w, h => $h, render_cb => $cb)
166}
119 167
120 my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, 168sub topot {
121 (new SDL::Color -r => 255, -g => 255, -b => 255); 169 (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
122
123 $class->new_from_surface ($surface)
124} 170}
125 171
126sub upload { 172sub upload {
127 my ($self) = @_; 173 my ($self) = @_;
128 174
129 return unless $SDL::App::USING_OPENGL; 175 return unless $GL_VERSION;
130 176
131 my $data; 177 my $data;
132 178
133 if (exists $self->{data}) { 179 if (exists $self->{data}) {
134 $data = $self->{data}; 180 $data = $self->{data};
181
182 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 glMatrixMode GL_PROJECTION;
186 glLoadIdentity;
187 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity;
189 glClear GL_COLOR_BUFFER_BIT;
190 $self->{render_cb}->($self, $self->{w}, $self->{h});
191
135 } else { 192 } else {
136 my $pb = new Gtk2::Gdk::PixbufLoader; 193 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
137 $pb->write ($self->{image}); 194 = CFClient::load_image_inline $self->{image};
138 $pb->close;
139
140 $pb = $pb->get_pixbuf;
141 $pb = $pb->add_alpha (0, 0, 0, 0);
142
143 $self->{width} = $pb->get_width;
144 $self->{height} = $pb->get_height;
145
146 $data = $pb->get_pixels;
147 } 195 }
148 196
197 my ($tw, $th) = @$self{qw(w h)};
198
199 unless ($tw && $th) {
200 $tw = $th = 1;
201 $data = "\x00" x 64;
202 }
203
204 unless ($GL_NPOT) {
205 # TODO: does not work for zero-sized textures
206 $tw = topot $tw;
207 $th = topot $th;
208
209 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
210 my $bpp = (length $data) / ($self->{w} * $self->{h});
211 $data = pack "(a" . ($tw * $bpp) . ")*",
212 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
213 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
214 }
215 }
216
217 $self->{s} = $self->{w} / $tw;
218 $self->{t} = $self->{h} / $th;
219
149 ($self->{name}) = @{glGenTextures 1}; 220 $self->{name} ||= (glGenTextures 1)->[0];
150 221
151 glBindTexture GL_TEXTURE_2D, $self->{name}; 222 glBindTexture GL_TEXTURE_2D, $self->{name};
152 223
153 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 224 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
154 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 225 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
155 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 226 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
156 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
157 228
229 if (defined $data) {
158 glTexImage2D GL_TEXTURE_2D, 0, 230 glTexImage2D GL_TEXTURE_2D, 0,
159 GL_RGBA8, 231 $self->{internalformat},
160 $self->{width}, $self->{height}, 232 $tw, $th, # need to pad texture first
161 0, 233 0,
162 GL_RGBA, 234 $self->{format},
163 GL_UNSIGNED_BYTE, 235 $self->{type},
164 $data; 236 $data;
237 glGetError and die;
238 } else {
239 glCopyTexImage2D GL_TEXTURE_2D, 0,
240 $self->{internalformat},
241 0, 0,
242 $tw, $th,
243 0;
244 glGetError and die;
245 }
165} 246}
166 247
167sub DESTROY { 248sub DESTROY {
168 my ($self) = @_; 249 my ($self) = @_;
169 250
170 return unless exists $self->{name}; 251 return unless exists $self->{name};
171 252
172 glDeleteTextures delete $self->{name}; 253 glDeleteTextures delete $self->{name};
173} 254}
174 255
175push @::GLINIT, sub { 256sub restore_state{
176 $_->upload 257 $_->upload
177 for grep $_, @textures; 258 for grep $_, @textures;
178}; 259};
179 260
1801; 2611;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines