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.23 by root, Tue Apr 11 17:02:35 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# width => $surface->width,
111 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,
112 ) 145 )
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} 146}
125 147
126sub new_from_opengl { 148sub new_from_opengl {
127 my ($class, $w, $h, $cb) = @_; 149 my ($class, $w, $h, $cb) = @_;
128 150
129 $class->_new (width => $w, height => $h, render => $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]
130} 156}
131 157
132sub upload { 158sub upload {
133 my ($self) = @_; 159 my ($self) = @_;
134 160
138 164
139 if (exists $self->{data}) { 165 if (exists $self->{data}) {
140 $data = $self->{data}; 166 $data = $self->{data};
141 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{rendercb}) {
142 glViewport 0, 0, $self->{width}, $self->{height}; 168 glViewport 0, 0, $self->{width}, $self->{height};
169 glMatrixMode GL_PROJECTION;
170 glLoadIdentity;
171 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
172 glMatrixMode GL_MODELVIEW;
173 glPushmatrix;
174 glLoadIdentity;
143 glClear GL_COLOR_BUFFER_BIT; 175 glClear GL_COLOR_BUFFER_BIT;
144 176
145 $self->{rendercb}->($self, $self->{width}, $self->{height}); 177 $self->{rendercb}->($self, $self->{width}, $self->{height});
146 } else { 178 } else {
147 my $pb = new Gtk2::Gdk::PixbufLoader; 179 my $pb = new Gtk2::Gdk::PixbufLoader;
155 $self->{height} = $pb->get_height; 187 $self->{height} = $pb->get_height;
156 188
157 $data = $pb->get_pixels; 189 $data = $pb->get_pixels;
158 } 190 }
159 191
192 my ($tw, $th) = @$self{qw(width height)};
193
194 unless ($GL_NPOT && $tw && $th) {
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
160 ($self->{name}) = @{glGenTextures 1}; 209 $self->{name} ||= (glGenTextures 1)->[0];
161 210
162 glBindTexture GL_TEXTURE_2D, $self->{name}; 211 glBindTexture GL_TEXTURE_2D, $self->{name};
163 212
164 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
165 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 214 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
166 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
167 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
168 217
218 glGetError;
169 if (defined $data) { 219 if (defined $data) {
170 glTexImage2D GL_TEXTURE_2D, 0, 220 glTexImage2D GL_TEXTURE_2D, 0,
171 GL_RGBA8, 221 $self->{internalformat},
172 $self->{width}, $self->{height}, 222 $tw, $th, # need to pad texture first
173 0, 223 0,
174 GL_RGBA, 224 $self->{format},
175 GL_UNSIGNED_BYTE, 225 GL_UNSIGNED_BYTE,
176 $data; 226 $data;
227 glGetError and die;
177 } else { 228 } else {
178 glCopyTexImage2D GL_TEXTURE_2D, 0, 229 glCopyTexImage2D GL_TEXTURE_2D, 0,
179 GL_RGBA8, 230 $self->{internalformat},
180 0, 0, 231 0, 0,
181 $self->{width}, $self->{height}, 232 $tw, $th,
182 0; 233 0;
234 glPopmatrix;
183 } 235 }
184} 236}
185 237
186sub DESTROY { 238sub DESTROY {
187 my ($self) = @_; 239 my ($self) = @_;
189 return unless exists $self->{name}; 241 return unless exists $self->{name};
190 242
191 glDeleteTextures delete $self->{name}; 243 glDeleteTextures delete $self->{name};
192} 244}
193 245
194push @::GLINIT, sub { 246sub restore_state{
195 $_->upload 247 $_->upload
196 for grep $_, @textures; 248 for grep $_, @textures;
197}; 249};
198 250
1991; 2511;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines