1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | Crossfire::Client - undocumented utility garbage for our crossfire client |
3 | CFClient - 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 | |
15 | package Crossfire::Client; |
15 | package CFClient; |
16 | |
16 | |
17 | BEGIN { |
17 | BEGIN { |
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 | |
|
|
24 | our %GL_EXT; |
|
|
25 | our $GL_VERSION; |
|
|
26 | |
|
|
27 | our $GL_NPOT; |
|
|
28 | |
|
|
29 | sub 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 | |
24 | sub find_rcfile($) { |
38 | sub 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 | } |
… | |
… | |
62 | } |
76 | } |
63 | |
77 | |
64 | close CFG; |
78 | close CFG; |
65 | } |
79 | } |
66 | |
80 | |
67 | package Crossfire::Client::Texture; |
81 | package CFClient::Texture; |
68 | |
82 | |
69 | use Scalar::Util; |
83 | use Scalar::Util; |
70 | |
84 | |
71 | use SDL::OpenGL; |
85 | use SDL::OpenGL; |
72 | |
86 | |
… | |
… | |
115 | # width => $surface->width, |
129 | # width => $surface->width, |
116 | # height => $surface->height, |
130 | # height => $surface->height, |
117 | # ) |
131 | # ) |
118 | #} |
132 | #} |
119 | |
133 | |
120 | sub new_from_text { |
134 | sub new_from_layout { |
121 | my ($class, $text, $height) = @_; |
135 | my ($class, $layout) = @_; |
122 | |
136 | |
123 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
137 | my ($w, $h, $data) = $layout->render; |
124 | |
138 | |
125 | $class->new ( |
139 | $class->new ( |
126 | width => $w, |
140 | width => $w, |
127 | height => $h, |
141 | height => $h, |
128 | data => $data, |
142 | data => $data, |
… | |
… | |
133 | |
147 | |
134 | sub new_from_opengl { |
148 | sub new_from_opengl { |
135 | my ($class, $w, $h, $cb) = @_; |
149 | my ($class, $w, $h, $cb) = @_; |
136 | |
150 | |
137 | $class->new (width => $w, height => $h, rendercb => $cb) |
151 | $class->new (width => $w, height => $h, rendercb => $cb) |
|
|
152 | } |
|
|
153 | |
|
|
154 | sub topot { |
|
|
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
138 | } |
156 | } |
139 | |
157 | |
140 | sub upload { |
158 | sub upload { |
141 | my ($self) = @_; |
159 | my ($self) = @_; |
142 | |
160 | |
… | |
… | |
169 | $self->{height} = $pb->get_height; |
187 | $self->{height} = $pb->get_height; |
170 | |
188 | |
171 | $data = $pb->get_pixels; |
189 | $data = $pb->get_pixels; |
172 | } |
190 | } |
173 | |
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 | |
174 | ($self->{name}) = @{glGenTextures 1}; |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
175 | |
210 | |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
177 | |
212 | |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
179 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
182 | |
217 | |
183 | glGetError; |
218 | glGetError; |
184 | if (defined $data) { |
219 | if (defined $data) { |
185 | glTexImage2D GL_TEXTURE_2D, 0, |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
186 | $self->{internalformat}, |
221 | $self->{internalformat}, |
187 | $self->{width}, $self->{height}, |
222 | $tw, $th, # need to pad texture first |
188 | 0, |
223 | 0, |
189 | $self->{format}, |
224 | $self->{format}, |
190 | GL_UNSIGNED_BYTE, |
225 | GL_UNSIGNED_BYTE, |
191 | $data; |
226 | $data; |
192 | glGetError and die; |
227 | glGetError and die; |
193 | } else { |
228 | } else { |
194 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
195 | $self->{internalformat}, |
230 | $self->{internalformat}, |
196 | 0, 0, |
231 | 0, 0, |
197 | $self->{width}, $self->{height}, |
232 | $tw, $th, |
198 | 0; |
233 | 0; |
199 | glPopmatrix; |
234 | glPopmatrix; |
200 | } |
235 | } |
201 | } |
236 | } |
202 | |
237 | |
… | |
… | |
206 | return unless exists $self->{name}; |
241 | return unless exists $self->{name}; |
207 | |
242 | |
208 | glDeleteTextures delete $self->{name}; |
243 | glDeleteTextures delete $self->{name}; |
209 | } |
244 | } |
210 | |
245 | |
211 | push @::GL_INIT, sub { |
246 | sub restore_state{ |
212 | $_->upload |
247 | $_->upload |
213 | for grep $_, @textures; |
248 | for grep $_, @textures; |
214 | }; |
249 | }; |
215 | |
250 | |
216 | 1; |
251 | 1; |