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 | |
… | |
… | |
110 | # |
124 | # |
111 | # $surface->rgba; |
125 | # $surface->rgba; |
112 | # |
126 | # |
113 | # $class->new ( |
127 | # $class->new ( |
114 | # data => $surface->pixels, |
128 | # data => $surface->pixels, |
115 | # width => $surface->width, |
129 | # w => $surface->width, |
116 | # height => $surface->height, |
130 | # h => $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 | w => $w, |
127 | height => $h, |
141 | h => $h, |
128 | data => $data, |
142 | data => $data, |
129 | internalformat => GL_ALPHA4, |
143 | internalformat => GL_ALPHA4, |
130 | format => GL_ALPHA, |
144 | format => GL_ALPHA, |
131 | ) |
145 | ) |
132 | } |
146 | } |
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 (w => $w, h => $h, render_cb => $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 | |
… | |
… | |
144 | |
162 | |
145 | my $data; |
163 | my $data; |
146 | |
164 | |
147 | if (exists $self->{data}) { |
165 | if (exists $self->{data}) { |
148 | $data = $self->{data}; |
166 | $data = $self->{data}; |
149 | } elsif (exists $self->{rendercb}) { |
167 | } elsif (exists $self->{render_cb}) { |
150 | glViewport 0, 0, $self->{width}, $self->{height}; |
168 | glViewport 0, 0, $self->{w}, $self->{h}; |
151 | glMatrixMode GL_PROJECTION; |
169 | glMatrixMode GL_PROJECTION; |
152 | glLoadIdentity; |
170 | glLoadIdentity; |
153 | glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; |
171 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
154 | glMatrixMode GL_MODELVIEW; |
172 | glMatrixMode GL_MODELVIEW; |
155 | glPushmatrix; |
173 | glPushmatrix; |
156 | glLoadIdentity; |
174 | glLoadIdentity; |
157 | glClear GL_COLOR_BUFFER_BIT; |
175 | glClear GL_COLOR_BUFFER_BIT; |
158 | |
176 | |
159 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
177 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
160 | } else { |
178 | } else { |
161 | my $pb = new Gtk2::Gdk::PixbufLoader; |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
162 | $pb->write ($self->{image}); |
180 | $pb->write ($self->{image}); |
163 | $pb->close; |
181 | $pb->close; |
164 | |
182 | |
165 | $pb = $pb->get_pixbuf; |
183 | $pb = $pb->get_pixbuf; |
166 | $pb = $pb->add_alpha (0, 0, 0, 0); |
184 | $pb = $pb->add_alpha (0, 0, 0, 0); |
167 | |
185 | |
168 | $self->{width} = $pb->get_width; |
186 | $self->{w} = $pb->get_width; |
169 | $self->{height} = $pb->get_height; |
187 | $self->{h} = $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(w h)}; |
|
|
193 | |
|
|
194 | unless ($tw || $th) { |
|
|
195 | $tw = $th = 1; |
|
|
196 | $data = "\x00" x 64; |
|
|
197 | } |
|
|
198 | |
|
|
199 | unless ($GL_NPOT) { |
|
|
200 | # TODO: does not work for zero-sized textures |
|
|
201 | $tw = topot $tw; |
|
|
202 | $th = topot $th; |
|
|
203 | |
|
|
204 | if (defined $data) { |
|
|
205 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
206 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
207 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
208 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | $self->{s} = $self->{w} / $tw; |
|
|
213 | $self->{t} = $self->{h} / $th; |
|
|
214 | |
174 | ($self->{name}) = @{glGenTextures 1}; |
215 | $self->{name} ||= (glGenTextures 1)->[0]; |
175 | |
216 | |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
217 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
177 | |
218 | |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
219 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
179 | 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; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
221 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
222 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
182 | |
223 | |
183 | glGetError; |
224 | glGetError; |
184 | if (defined $data) { |
225 | if (defined $data) { |
185 | glTexImage2D GL_TEXTURE_2D, 0, |
226 | glTexImage2D GL_TEXTURE_2D, 0, |
186 | $self->{internalformat}, |
227 | $self->{internalformat}, |
187 | $self->{width}, $self->{height}, |
228 | $tw, $th, # need to pad texture first |
188 | 0, |
229 | 0, |
189 | $self->{format}, |
230 | $self->{format}, |
190 | GL_UNSIGNED_BYTE, |
231 | GL_UNSIGNED_BYTE, |
191 | $data; |
232 | $data; |
192 | glGetError and die; |
233 | glGetError and die; |
193 | } else { |
234 | } else { |
194 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
235 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
195 | $self->{internalformat}, |
236 | $self->{internalformat}, |
196 | 0, 0, |
237 | 0, 0, |
197 | $self->{width}, $self->{height}, |
238 | $tw, $th, |
198 | 0; |
239 | 0; |
199 | glPopmatrix; |
240 | glPopmatrix; |
200 | } |
241 | } |
201 | } |
242 | } |
202 | |
243 | |
… | |
… | |
206 | return unless exists $self->{name}; |
247 | return unless exists $self->{name}; |
207 | |
248 | |
208 | glDeleteTextures delete $self->{name}; |
249 | glDeleteTextures delete $self->{name}; |
209 | } |
250 | } |
210 | |
251 | |
211 | push @::GLINIT, sub { |
252 | sub restore_state{ |
212 | $_->upload |
253 | $_->upload |
213 | for grep $_, @textures; |
254 | for grep $_, @textures; |
214 | }; |
255 | }; |
215 | |
256 | |
216 | 1; |
257 | 1; |