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 | } |
22 | } |
23 | |
23 | |
24 | our %GL_EXT; |
24 | our %GL_EXT; |
25 | our $GL_VERSION; |
25 | our $GL_VERSION; |
26 | |
26 | |
… | |
… | |
30 | $GL_VERSION = gl_version * 1; |
30 | $GL_VERSION = gl_version * 1; |
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
31 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
32 | |
32 | |
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
33 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
34 | |
34 | |
35 | Crossfire::Client::Texture::restore_state (); |
35 | CFClient::Texture::restore_state (); |
36 | } |
36 | } |
37 | |
37 | |
38 | sub find_rcfile($) { |
38 | sub find_rcfile($) { |
39 | my $path; |
39 | my $path; |
40 | |
40 | |
41 | for (@INC) { |
41 | for (@INC) { |
42 | $path = "$_/Crossfire/resources/$_[0]"; |
42 | $path = "$_/CFClient/resources/$_[0]"; |
43 | return $path if -r $path; |
43 | return $path if -r $path; |
44 | } |
44 | } |
45 | |
45 | |
46 | die "FATAL: can't find required file $_[0]\n"; |
46 | die "FATAL: can't find required file $_[0]\n"; |
47 | } |
47 | } |
… | |
… | |
76 | } |
76 | } |
77 | |
77 | |
78 | close CFG; |
78 | close CFG; |
79 | } |
79 | } |
80 | |
80 | |
81 | package Crossfire::Client::Texture; |
81 | package CFClient::Texture; |
|
|
82 | |
|
|
83 | use strict; |
82 | |
84 | |
83 | use Scalar::Util; |
85 | use Scalar::Util; |
84 | |
86 | |
85 | use SDL::OpenGL; |
87 | use SDL::OpenGL; |
86 | |
88 | |
… | |
… | |
124 | # |
126 | # |
125 | # $surface->rgba; |
127 | # $surface->rgba; |
126 | # |
128 | # |
127 | # $class->new ( |
129 | # $class->new ( |
128 | # data => $surface->pixels, |
130 | # data => $surface->pixels, |
129 | # width => $surface->width, |
131 | # w => $surface->width, |
130 | # height => $surface->height, |
132 | # h => $surface->height, |
131 | # ) |
133 | # ) |
132 | #} |
134 | #} |
133 | |
135 | |
134 | sub new_from_text { |
136 | sub new_from_layout { |
135 | my ($class, $text, $height) = @_; |
137 | my ($class, $layout) = @_; |
136 | |
138 | |
137 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
139 | my ($w, $h, $data) = $layout->render; |
138 | |
140 | |
139 | $class->new ( |
141 | $class->new ( |
140 | width => $w, |
142 | w => $w, |
141 | height => $h, |
143 | h => $h, |
142 | data => $data, |
144 | data => $data, |
143 | internalformat => GL_ALPHA4, |
145 | internalformat => GL_ALPHA4, |
144 | format => GL_ALPHA, |
146 | format => GL_ALPHA, |
145 | ) |
147 | ) |
146 | } |
148 | } |
147 | |
149 | |
148 | sub new_from_opengl { |
150 | sub new_from_opengl { |
149 | my ($class, $w, $h, $cb) = @_; |
151 | my ($class, $w, $h, $cb) = @_; |
150 | |
152 | |
151 | $class->new (width => $w, height => $h, rendercb => $cb) |
153 | $class->new (w => $w, h => $h, render_cb => $cb) |
152 | } |
154 | } |
153 | |
155 | |
154 | sub topot { |
156 | sub topot { |
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
157 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
156 | } |
158 | } |
… | |
… | |
162 | |
164 | |
163 | my $data; |
165 | my $data; |
164 | |
166 | |
165 | if (exists $self->{data}) { |
167 | if (exists $self->{data}) { |
166 | $data = $self->{data}; |
168 | $data = $self->{data}; |
|
|
169 | |
167 | } elsif (exists $self->{rendercb}) { |
170 | } elsif (exists $self->{render_cb}) { |
168 | glViewport 0, 0, $self->{width}, $self->{height}; |
171 | glViewport 0, 0, $self->{w}, $self->{h}; |
|
|
172 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
169 | glMatrixMode GL_PROJECTION; |
173 | glMatrixMode GL_PROJECTION; |
170 | glLoadIdentity; |
174 | glLoadIdentity; |
171 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
|
|
172 | glMatrixMode GL_MODELVIEW; |
175 | glMatrixMode GL_MODELVIEW; |
173 | glPushmatrix; |
|
|
174 | glLoadIdentity; |
176 | glLoadIdentity; |
175 | glClear GL_COLOR_BUFFER_BIT; |
177 | glClear GL_COLOR_BUFFER_BIT; |
176 | |
|
|
177 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
178 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
179 | |
178 | } else { |
180 | } else { |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
181 | my $pb = new Gtk2::Gdk::PixbufLoader; |
180 | $pb->write ($self->{image}); |
182 | $pb->write ($self->{image}); |
181 | $pb->close; |
183 | $pb->close; |
182 | |
184 | |
183 | $pb = $pb->get_pixbuf; |
185 | $pb = $pb->get_pixbuf; |
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
186 | $pb = $pb->add_alpha (0, 0, 0, 0); |
185 | |
187 | |
186 | $self->{width} = $pb->get_width; |
188 | $self->{w} = $pb->get_width; |
187 | $self->{height} = $pb->get_height; |
189 | $self->{h} = $pb->get_height; |
188 | |
190 | |
189 | $data = $pb->get_pixels; |
191 | $data = $pb->get_pixels; |
190 | } |
192 | } |
191 | |
193 | |
192 | my ($tw, $th) = @$self{qw(width height)}; |
194 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
195 | |
|
|
196 | unless ($tw && $th) { |
|
|
197 | $tw = $th = 1; |
|
|
198 | $data = "\x00" x 64; |
|
|
199 | } |
193 | |
200 | |
194 | unless ($GL_NPOT) { |
201 | unless ($GL_NPOT) { |
|
|
202 | # TODO: does not work for zero-sized textures |
195 | $tw = topot $tw; |
203 | $tw = topot $tw; |
196 | $th = topot $th; |
204 | $th = topot $th; |
197 | |
205 | |
198 | if (defined $data) { |
206 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
199 | my $bpp = (length $data) / ($self->{width} * $self->{height}); |
207 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
200 | $data = pack "(a" . ($tw * $bpp) . ")*", |
208 | $data = pack "(a" . ($tw * $bpp) . ")*", |
201 | unpack "(a" . ($self->{width} * $bpp) . ")*", $data; |
209 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
202 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); |
210 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
203 | } |
211 | } |
204 | } |
212 | } |
205 | |
213 | |
206 | $self->{s} = $self->{width} / $tw; |
214 | $self->{s} = $self->{w} / $tw; |
207 | $self->{t} = $self->{height} / $th; |
215 | $self->{t} = $self->{h} / $th; |
208 | |
216 | |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
217 | $self->{name} ||= (glGenTextures 1)->[0]; |
210 | |
218 | |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
219 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
212 | |
220 | |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
221 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
223 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
224 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
217 | |
225 | |
218 | glGetError; |
|
|
219 | if (defined $data) { |
226 | if (defined $data) { |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
227 | glTexImage2D GL_TEXTURE_2D, 0, |
221 | $self->{internalformat}, |
228 | $self->{internalformat}, |
222 | $tw, $th, # need to pad texture first |
229 | $tw, $th, # need to pad texture first |
223 | 0, |
230 | 0, |
… | |
… | |
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
236 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
230 | $self->{internalformat}, |
237 | $self->{internalformat}, |
231 | 0, 0, |
238 | 0, 0, |
232 | $tw, $th, |
239 | $tw, $th, |
233 | 0; |
240 | 0; |
234 | glPopmatrix; |
241 | glGetError and die; |
235 | } |
242 | } |
236 | } |
243 | } |
237 | |
244 | |
238 | sub DESTROY { |
245 | sub DESTROY { |
239 | my ($self) = @_; |
246 | my ($self) = @_; |