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