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 | use SDL::OpenGL; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
|
|
27 | our $GL_VERSION; |
|
|
28 | |
|
|
29 | our $GL_NPOT; |
|
|
30 | |
|
|
31 | sub gl_init { |
|
|
32 | $GL_VERSION = gl_version * 1; |
|
|
33 | %GL_EXT = map +($_ => 1), split /\s+/, gl_extensions; |
|
|
34 | |
|
|
35 | $GL_NPOT = $GL_EXT{GL_ARB_texture_non_power_of_two} || $GL_VERSION >= 2; |
|
|
36 | |
|
|
37 | glEnable GL_TEXTURE_2D; |
|
|
38 | glEnable GL_COLOR_MATERIAL; |
|
|
39 | glShadeModel GL_FLAT; |
|
|
40 | glDisable GL_DEPTH_TEST; |
|
|
41 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
42 | |
|
|
43 | glHint GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST; |
|
|
44 | |
|
|
45 | CFClient::Texture::restore_state (); |
22 | } |
46 | } |
23 | |
47 | |
24 | sub find_rcfile($) { |
48 | sub find_rcfile($) { |
25 | my $path; |
49 | my $path; |
26 | |
50 | |
27 | for (@INC) { |
51 | for (@INC) { |
28 | $path = "$_/Crossfire/resources/$_[0]"; |
52 | $path = "$_/CFClient/resources/$_[0]"; |
29 | return $path if -r $path; |
53 | return $path if -r $path; |
30 | } |
54 | } |
31 | |
55 | |
32 | die "FATAL: can't find required file $_[0]\n"; |
56 | die "FATAL: can't find required file $_[0]\n"; |
33 | } |
57 | } |
… | |
… | |
62 | } |
86 | } |
63 | |
87 | |
64 | close CFG; |
88 | close CFG; |
65 | } |
89 | } |
66 | |
90 | |
67 | package Crossfire::Client::Texture; |
91 | package CFClient::Texture; |
|
|
92 | |
|
|
93 | use strict; |
68 | |
94 | |
69 | use Scalar::Util; |
95 | use Scalar::Util; |
70 | |
96 | |
71 | use SDL::OpenGL; |
97 | use SDL::OpenGL; |
72 | |
98 | |
… | |
… | |
76 | my ($class, %data) = @_; |
102 | my ($class, %data) = @_; |
77 | |
103 | |
78 | my $self = bless { |
104 | my $self = bless { |
79 | internalformat => GL_RGBA, |
105 | internalformat => GL_RGBA, |
80 | format => GL_RGBA, |
106 | format => GL_RGBA, |
|
|
107 | type => GL_UNSIGNED_BYTE, |
81 | %data, |
108 | %data, |
82 | }, $class; |
109 | }, $class; |
83 | |
110 | |
84 | push @textures, $self; |
111 | push @textures, $self; |
85 | Scalar::Util::weaken $textures[-1]; |
112 | Scalar::Util::weaken $textures[-1]; |
… | |
… | |
110 | # |
137 | # |
111 | # $surface->rgba; |
138 | # $surface->rgba; |
112 | # |
139 | # |
113 | # $class->new ( |
140 | # $class->new ( |
114 | # data => $surface->pixels, |
141 | # data => $surface->pixels, |
115 | # width => $surface->width, |
142 | # w => $surface->width, |
116 | # height => $surface->height, |
143 | # h => $surface->height, |
117 | # ) |
144 | # ) |
118 | #} |
145 | #} |
119 | |
146 | |
120 | sub new_from_text { |
147 | sub new_from_layout { |
121 | my ($class, $text, $height) = @_; |
148 | my ($class, $layout) = @_; |
122 | |
149 | |
123 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
150 | my ($w, $h, $data) = $layout->render; |
124 | |
151 | |
125 | $class->new ( |
152 | $class->new ( |
126 | width => $w, |
153 | w => $w, |
127 | height => $h, |
154 | h => $h, |
128 | data => $data, |
155 | data => $data, |
129 | internalformat => GL_ALPHA4, |
156 | internalformat => GL_ALPHA4, |
130 | format => GL_ALPHA, |
157 | format => GL_ALPHA, |
|
|
158 | type => GL_UNSIGNED_BYTE, |
131 | ) |
159 | ) |
132 | } |
160 | } |
133 | |
161 | |
134 | sub new_from_opengl { |
162 | sub new_from_opengl { |
135 | my ($class, $w, $h, $cb) = @_; |
163 | my ($class, $w, $h, $cb) = @_; |
136 | |
164 | |
137 | $class->new (width => $w, height => $h, rendercb => $cb) |
165 | $class->new (w => $w, h => $h, render_cb => $cb) |
|
|
166 | } |
|
|
167 | |
|
|
168 | sub topot { |
|
|
169 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
138 | } |
170 | } |
139 | |
171 | |
140 | sub upload { |
172 | sub upload { |
141 | my ($self) = @_; |
173 | my ($self) = @_; |
142 | |
174 | |
143 | return unless $SDL::App::USING_OPENGL; |
175 | return unless $GL_VERSION; |
144 | |
176 | |
145 | my $data; |
177 | my $data; |
146 | |
178 | |
147 | if (exists $self->{data}) { |
179 | if (exists $self->{data}) { |
148 | $data = $self->{data}; |
180 | $data = $self->{data}; |
|
|
181 | |
149 | } elsif (exists $self->{rendercb}) { |
182 | } elsif (exists $self->{render_cb}) { |
150 | glViewport 0, 0, $self->{width}, $self->{height}; |
183 | glViewport 0, 0, $self->{w}, $self->{h}; |
|
|
184 | glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000; |
151 | glMatrixMode GL_PROJECTION; |
185 | glMatrixMode GL_PROJECTION; |
152 | glLoadIdentity; |
186 | glLoadIdentity; |
153 | glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; |
|
|
154 | glMatrixMode GL_MODELVIEW; |
187 | glMatrixMode GL_MODELVIEW; |
155 | glPushmatrix; |
|
|
156 | glLoadIdentity; |
188 | glLoadIdentity; |
157 | glClear GL_COLOR_BUFFER_BIT; |
189 | glClear GL_COLOR_BUFFER_BIT; |
158 | |
|
|
159 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
190 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
191 | |
160 | } else { |
192 | } else { |
161 | my $pb = new Gtk2::Gdk::PixbufLoader; |
193 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
162 | $pb->write ($self->{image}); |
194 | = CFClient::load_image_inline $self->{image}; |
163 | $pb->close; |
|
|
164 | |
|
|
165 | $pb = $pb->get_pixbuf; |
|
|
166 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
167 | |
|
|
168 | $self->{width} = $pb->get_width; |
|
|
169 | $self->{height} = $pb->get_height; |
|
|
170 | |
|
|
171 | $data = $pb->get_pixels; |
|
|
172 | } |
195 | } |
173 | |
196 | |
|
|
197 | my ($tw, $th) = @$self{qw(w h)}; |
|
|
198 | |
|
|
199 | unless ($tw && $th) { |
|
|
200 | $tw = $th = 1; |
|
|
201 | $data = "\x00" x 64; |
|
|
202 | } |
|
|
203 | |
|
|
204 | unless ($GL_NPOT) { |
|
|
205 | # TODO: does not work for zero-sized textures |
|
|
206 | $tw = topot $tw; |
|
|
207 | $th = topot $th; |
|
|
208 | |
|
|
209 | if ($tw != $self->{w} || $th != $self->{h} && defined $data) { |
|
|
210 | my $bpp = (length $data) / ($self->{w} * $self->{h}); |
|
|
211 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
212 | unpack "(a" . ($self->{w} * $bpp) . ")*", $data; |
|
|
213 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h}); |
|
|
214 | } |
|
|
215 | } |
|
|
216 | |
|
|
217 | $self->{s} = $self->{w} / $tw; |
|
|
218 | $self->{t} = $self->{h} / $th; |
|
|
219 | |
174 | ($self->{name}) = @{glGenTextures 1}; |
220 | $self->{name} ||= (glGenTextures 1)->[0]; |
175 | |
221 | |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
222 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
177 | |
223 | |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
224 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
179 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
225 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
226 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
227 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
182 | |
228 | |
183 | glGetError; |
|
|
184 | if (defined $data) { |
229 | if (defined $data) { |
185 | glTexImage2D GL_TEXTURE_2D, 0, |
230 | glTexImage2D GL_TEXTURE_2D, 0, |
186 | $self->{internalformat}, |
231 | $self->{internalformat}, |
187 | $self->{width}, $self->{height}, |
232 | $tw, $th, # need to pad texture first |
188 | 0, |
233 | 0, |
189 | $self->{format}, |
234 | $self->{format}, |
190 | GL_UNSIGNED_BYTE, |
235 | $self->{type}, |
191 | $data; |
236 | $data; |
192 | glGetError and die; |
237 | glGetError and die; |
193 | } else { |
238 | } else { |
194 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
239 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
195 | $self->{internalformat}, |
240 | $self->{internalformat}, |
196 | 0, 0, |
241 | 0, 0, |
197 | $self->{width}, $self->{height}, |
242 | $tw, $th, |
198 | 0; |
243 | 0; |
199 | glPopmatrix; |
244 | glGetError and die; |
200 | } |
245 | } |
201 | } |
246 | } |
202 | |
247 | |
203 | sub DESTROY { |
248 | sub DESTROY { |
204 | my ($self) = @_; |
249 | my ($self) = @_; |
… | |
… | |
206 | return unless exists $self->{name}; |
251 | return unless exists $self->{name}; |
207 | |
252 | |
208 | glDeleteTextures delete $self->{name}; |
253 | glDeleteTextures delete $self->{name}; |
209 | } |
254 | } |
210 | |
255 | |
211 | push @::GLINIT, sub { |
256 | sub restore_state{ |
212 | $_->upload |
257 | $_->upload |
213 | for grep $_, @textures; |
258 | for grep $_, @textures; |
214 | }; |
259 | }; |
215 | |
260 | |
216 | 1; |
261 | 1; |