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 | |
73 | my @textures; |
99 | my @textures; |
74 | |
100 | |
75 | sub _new { |
101 | sub new { |
76 | my ($class, %data) = @_; |
102 | my ($class, %data) = @_; |
77 | |
103 | |
78 | my $self = bless \%data, $class; |
104 | my $self = bless { |
|
|
105 | internalformat => GL_RGBA, |
|
|
106 | format => GL_RGBA, |
|
|
107 | type => GL_UNSIGNED_BYTE, |
|
|
108 | %data, |
|
|
109 | }, $class; |
79 | |
110 | |
80 | push @textures, $self; |
111 | push @textures, $self; |
81 | Scalar::Util::weaken $textures[-1]; |
112 | Scalar::Util::weaken $textures[-1]; |
82 | |
113 | |
83 | $self->upload; |
114 | $self->upload; |
… | |
… | |
86 | } |
117 | } |
87 | |
118 | |
88 | sub new_from_image { |
119 | sub new_from_image { |
89 | my ($class, $image) = @_; |
120 | my ($class, $image) = @_; |
90 | |
121 | |
91 | $class->_new (image => $image) |
122 | $class->new (image => $image) |
92 | } |
123 | } |
93 | |
124 | |
94 | sub new_from_file { |
125 | sub new_from_file { |
95 | my ($class, $path) = @_; |
126 | my ($class, $path) = @_; |
96 | |
127 | |
… | |
… | |
99 | |
130 | |
100 | local $/; |
131 | local $/; |
101 | $class->new_from_image (<$fh>) |
132 | $class->new_from_image (<$fh>) |
102 | } |
133 | } |
103 | |
134 | |
104 | sub new_from_surface { |
135 | #sub new_from_surface { |
105 | my ($class, $surface) = @_; |
136 | # my ($class, $surface) = @_; |
106 | |
137 | # |
107 | $surface->rgba; |
138 | # $surface->rgba; |
108 | |
139 | # |
109 | $class->_new ( |
140 | # $class->new ( |
110 | data => $surface->pixels, |
141 | # data => $surface->pixels, |
111 | width => $surface->width, |
142 | # w => $surface->width, |
112 | height => $surface->height, |
143 | # h => $surface->height, |
|
|
144 | # ) |
|
|
145 | #} |
|
|
146 | |
|
|
147 | sub new_from_layout { |
|
|
148 | my ($class, $layout) = @_; |
|
|
149 | |
|
|
150 | my ($w, $h, $data) = $layout->render; |
|
|
151 | |
|
|
152 | $class->new ( |
|
|
153 | w => $w, |
|
|
154 | h => $h, |
|
|
155 | data => $data, |
|
|
156 | internalformat => GL_ALPHA4, |
|
|
157 | format => GL_ALPHA, |
|
|
158 | type => GL_UNSIGNED_BYTE, |
113 | ) |
159 | ) |
114 | } |
|
|
115 | |
|
|
116 | sub new_from_ttf { |
|
|
117 | my ($class, $ttf, $text) = @_; |
|
|
118 | |
|
|
119 | utf8::encode $text; |
|
|
120 | |
|
|
121 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
|
|
122 | (new SDL::Color -r => 0, -g => 0, -b => 0); |
|
|
123 | |
|
|
124 | $class->new_from_surface (bless \$surface, SDL::Surface::) |
|
|
125 | } |
160 | } |
126 | |
161 | |
127 | sub new_from_opengl { |
162 | sub new_from_opengl { |
128 | my ($class, $w, $h, $cb) = @_; |
163 | my ($class, $w, $h, $cb) = @_; |
129 | |
164 | |
130 | $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] |
131 | } |
170 | } |
132 | |
171 | |
133 | sub upload { |
172 | sub upload { |
134 | my ($self) = @_; |
173 | my ($self) = @_; |
135 | |
174 | |
136 | return unless $SDL::App::USING_OPENGL; |
175 | return unless $GL_VERSION; |
137 | |
176 | |
138 | my $data; |
177 | my $data; |
139 | |
178 | |
140 | if (exists $self->{data}) { |
179 | if (exists $self->{data}) { |
141 | $data = $self->{data}; |
180 | $data = $self->{data}; |
|
|
181 | |
142 | } elsif (exists $self->{rendercb}) { |
182 | } elsif (exists $self->{render_cb}) { |
143 | 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; |
144 | glMatrixMode GL_PROJECTION; |
185 | glMatrixMode GL_PROJECTION; |
145 | glLoadIdentity; |
186 | glLoadIdentity; |
146 | glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100; |
|
|
147 | glMatrixMode GL_MODELVIEW; |
187 | glMatrixMode GL_MODELVIEW; |
148 | glPushmatrix; |
|
|
149 | glLoadIdentity; |
188 | glLoadIdentity; |
150 | glClear GL_COLOR_BUFFER_BIT; |
189 | glClear GL_COLOR_BUFFER_BIT; |
151 | |
|
|
152 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
190 | $self->{render_cb}->($self, $self->{w}, $self->{h}); |
|
|
191 | |
153 | } else { |
192 | } else { |
154 | my $pb = new Gtk2::Gdk::PixbufLoader; |
193 | ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type}) |
155 | $pb->write ($self->{image}); |
194 | = CFClient::load_image_inline $self->{image}; |
156 | $pb->close; |
|
|
157 | |
|
|
158 | $pb = $pb->get_pixbuf; |
|
|
159 | $pb = $pb->add_alpha (0, 0, 0, 0); |
|
|
160 | |
|
|
161 | $self->{width} = $pb->get_width; |
|
|
162 | $self->{height} = $pb->get_height; |
|
|
163 | |
|
|
164 | $data = $pb->get_pixels; |
|
|
165 | } |
195 | } |
166 | |
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 | |
167 | ($self->{name}) = @{glGenTextures 1}; |
220 | $self->{name} ||= (glGenTextures 1)->[0]; |
168 | |
221 | |
169 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
222 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
170 | |
223 | |
171 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
224 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR; |
172 | 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; |
173 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
226 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
174 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
227 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
175 | |
228 | |
176 | if (defined $data) { |
229 | if (defined $data) { |
177 | glTexImage2D GL_TEXTURE_2D, 0, |
230 | glTexImage2D GL_TEXTURE_2D, 0, |
178 | GL_RGBA8, |
231 | $self->{internalformat}, |
179 | $self->{width}, $self->{height}, |
232 | $tw, $th, # need to pad texture first |
180 | 0, |
233 | 0, |
181 | GL_RGBA, |
234 | $self->{format}, |
182 | GL_UNSIGNED_BYTE, |
235 | $self->{type}, |
183 | $data; |
236 | $data; |
|
|
237 | glGetError and die; |
184 | } else { |
238 | } else { |
185 | warn "I{DTEDRAW TEX: $self->{width} $self->{height} \n"; |
|
|
186 | warn "ERR: ". (glGetError). "\n"; |
|
|
187 | my ($bd, $br) = (glGet (GL_DRAW_BUFFER), glGet (GL_READ_BUFFER)); |
|
|
188 | warn "BUF:[$bd $br]\n"; |
|
|
189 | |
|
|
190 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
239 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
191 | GL_RGBA8, |
240 | $self->{internalformat}, |
192 | 0, 0, |
241 | 0, 0, |
193 | $self->{width}, $self->{height}, |
242 | $tw, $th, |
194 | 0; |
243 | 0; |
195 | glPopmatrix; |
244 | glGetError and die; |
196 | warn "ERR: ". (glGetError). "\n"; |
|
|
197 | SDL::GLSwapBuffers; |
|
|
198 | sleep 1; |
|
|
199 | } |
245 | } |
200 | } |
246 | } |
201 | |
247 | |
202 | sub DESTROY { |
248 | sub DESTROY { |
203 | my ($self) = @_; |
249 | my ($self) = @_; |
… | |
… | |
205 | return unless exists $self->{name}; |
251 | return unless exists $self->{name}; |
206 | |
252 | |
207 | glDeleteTextures delete $self->{name}; |
253 | glDeleteTextures delete $self->{name}; |
208 | } |
254 | } |
209 | |
255 | |
210 | push @::GLINIT, sub { |
256 | sub restore_state{ |
211 | $_->upload |
257 | $_->upload |
212 | for grep $_, @textures; |
258 | for grep $_, @textures; |
213 | }; |
259 | }; |
214 | |
260 | |
215 | 1; |
261 | 1; |