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.10 by elmex, Sat Apr 8 17:21:01 2006 UTC vs.
Revision 1.21 by root, Tue Apr 11 13:05:11 2006 UTC

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 "Crossfire::Client", $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 Crossfire::Client::Texture::restore_state ();
22} 36}
23 37
24sub find_rcfile($) { 38sub find_rcfile($) {
25 my $path; 39 my $path;
26 40
70 84
71use SDL::OpenGL; 85use SDL::OpenGL;
72 86
73my @textures; 87my @textures;
74 88
75sub _new { 89sub new {
76 my ($class, %data) = @_; 90 my ($class, %data) = @_;
77 91
78 my $self = bless \%data, $class; 92 my $self = bless {
93 internalformat => GL_RGBA,
94 format => GL_RGBA,
95 %data,
96 }, $class;
79 97
80 push @textures, $self; 98 push @textures, $self;
81 Scalar::Util::weaken $textures[-1]; 99 Scalar::Util::weaken $textures[-1];
82 100
83 $self->upload; 101 $self->upload;
86} 104}
87 105
88sub new_from_image { 106sub new_from_image {
89 my ($class, $image) = @_; 107 my ($class, $image) = @_;
90 108
91 $class->_new (image => $image) 109 $class->new (image => $image)
92} 110}
93 111
94sub new_from_file { 112sub new_from_file {
95 my ($class, $path) = @_; 113 my ($class, $path) = @_;
96 114
99 117
100 local $/; 118 local $/;
101 $class->new_from_image (<$fh>) 119 $class->new_from_image (<$fh>)
102} 120}
103 121
104sub new_from_surface { 122#sub new_from_surface {
105 my ($class, $surface) = @_; 123# my ($class, $surface) = @_;
106 124#
107 $surface->rgba; 125# $surface->rgba;
108 126#
109 $class->_new ( 127# $class->new (
110 data => $surface->pixels, 128# data => $surface->pixels,
111 width => $surface->width, 129# width => $surface->width,
112 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,
113 ) 145 )
114}
115
116sub new_from_ttf {
117 my ($class, $ttf, $text) = @_;
118
119 utf8::encode $text;
120
121 my $surface = SDL::TTFRenderUTF8Blended $ttf, $text,
122 (new SDL::Color -r => 255, -g => 255, -b => 255);
123
124 $class->new_from_surface (bless \$surface, SDL::Surface::)
125} 146}
126 147
127sub new_from_opengl { 148sub new_from_opengl {
128 my ($class, $w, $h, $cb) = @_; 149 my ($class, $w, $h, $cb) = @_;
129 150
130 $class->_new (width => $w, height => $h, rendercb => $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]
131} 156}
132 157
133sub upload { 158sub upload {
134 my ($self) = @_; 159 my ($self) = @_;
135 160
139 164
140 if (exists $self->{data}) { 165 if (exists $self->{data}) {
141 $data = $self->{data}; 166 $data = $self->{data};
142 } elsif (exists $self->{rendercb}) { 167 } elsif (exists $self->{rendercb}) {
143 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;
144 glClear GL_COLOR_BUFFER_BIT; 175 glClear GL_COLOR_BUFFER_BIT;
145 176
146 $self->{rendercb}->($self, $self->{width}, $self->{height}); 177 $self->{rendercb}->($self, $self->{width}, $self->{height});
147 } else { 178 } else {
148 my $pb = new Gtk2::Gdk::PixbufLoader; 179 my $pb = new Gtk2::Gdk::PixbufLoader;
156 $self->{height} = $pb->get_height; 187 $self->{height} = $pb->get_height;
157 188
158 $data = $pb->get_pixels; 189 $data = $pb->get_pixels;
159 } 190 }
160 191
192 my ($tw, $th) = @$self{qw(width height)};
193
194 unless ($GL_NPOT) {
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
161 ($self->{name}) = @{glGenTextures 1}; 209 $self->{name} ||= (glGenTextures 1)->[0];
162 210
163 glBindTexture GL_TEXTURE_2D, $self->{name}; 211 glBindTexture GL_TEXTURE_2D, $self->{name};
164 212
165 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 213 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
166 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;
167 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 215 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
168 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 216 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
169 217
218 glGetError;
170 if (defined $data) { 219 if (defined $data) {
171 glTexImage2D GL_TEXTURE_2D, 0, 220 glTexImage2D GL_TEXTURE_2D, 0,
172 GL_RGBA8, 221 $self->{internalformat},
173 $self->{width}, $self->{height}, 222 $tw, $th, # need to pad texture first
174 0, 223 0,
175 GL_RGBA, 224 $self->{format},
176 GL_UNSIGNED_BYTE, 225 GL_UNSIGNED_BYTE,
177 $data; 226 $data;
227 glGetError and die;
178 } else { 228 } else {
179 glCopyTexImage2D GL_TEXTURE_2D, 0, 229 glCopyTexImage2D GL_TEXTURE_2D, 0,
180 GL_RGBA8, 230 $self->{internalformat},
181 0, 0, 231 0, 0,
182 $self->{width}, $self->{height}, 232 $tw, $th,
183 0; 233 0;
234 glPopmatrix;
184 } 235 }
185} 236}
186 237
187sub DESTROY { 238sub DESTROY {
188 my ($self) = @_; 239 my ($self) = @_;
190 return unless exists $self->{name}; 241 return unless exists $self->{name};
191 242
192 glDeleteTextures delete $self->{name}; 243 glDeleteTextures delete $self->{name};
193} 244}
194 245
195push @::GLINIT, sub { 246sub restore_state{
196 $_->upload 247 $_->upload
197 for grep $_, @textures; 248 for grep $_, @textures;
198}; 249};
199 250
2001; 2511;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines