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 | } |
… | |
… | |
53 | |
67 | |
54 | open CFG, ">$file" |
68 | open CFG, ">$file" |
55 | or return; |
69 | or return; |
56 | |
70 | |
57 | { |
71 | { |
|
|
72 | require Data::Dumper; |
58 | local $Data::Dumper::Purity = 1; |
73 | local $Data::Dumper::Purity = 1; |
59 | $::CFG->{VERSION} = $::VERSION; |
74 | $::CFG->{VERSION} = $::VERSION; |
60 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
75 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
61 | } |
76 | } |
62 | |
77 | |
63 | close CFG; |
78 | close CFG; |
64 | } |
79 | } |
65 | |
80 | |
66 | package Crossfire::Client::Texture; |
81 | package CFClient::Texture; |
67 | |
82 | |
68 | use Scalar::Util; |
83 | use Scalar::Util; |
69 | |
84 | |
70 | use SDL::OpenGL; |
85 | use SDL::OpenGL; |
71 | |
86 | |
72 | my @textures; |
87 | my @textures; |
73 | |
88 | |
74 | sub _new { |
89 | sub new { |
75 | my ($class, %data) = @_; |
90 | my ($class, %data) = @_; |
76 | |
91 | |
77 | my $self = bless \%data, $class; |
92 | my $self = bless { |
|
|
93 | internalformat => GL_RGBA, |
|
|
94 | format => GL_RGBA, |
|
|
95 | %data, |
|
|
96 | }, $class; |
78 | |
97 | |
79 | push @textures, $self; |
98 | push @textures, $self; |
80 | Scalar::Util::weaken $textures[-1]; |
99 | Scalar::Util::weaken $textures[-1]; |
81 | |
100 | |
82 | $self->upload; |
101 | $self->upload; |
… | |
… | |
85 | } |
104 | } |
86 | |
105 | |
87 | sub new_from_image { |
106 | sub new_from_image { |
88 | my ($class, $image) = @_; |
107 | my ($class, $image) = @_; |
89 | |
108 | |
90 | $class->_new (image => $image) |
109 | $class->new (image => $image) |
91 | } |
110 | } |
92 | |
111 | |
93 | sub new_from_file { |
112 | sub new_from_file { |
94 | my ($class, $path) = @_; |
113 | my ($class, $path) = @_; |
95 | |
114 | |
… | |
… | |
98 | |
117 | |
99 | local $/; |
118 | local $/; |
100 | $class->new_from_image (<$fh>) |
119 | $class->new_from_image (<$fh>) |
101 | } |
120 | } |
102 | |
121 | |
103 | sub new_from_surface { |
122 | #sub new_from_surface { |
104 | my ($class, $surface) = @_; |
123 | # my ($class, $surface) = @_; |
105 | |
124 | # |
106 | $surface->rgba; |
125 | # $surface->rgba; |
107 | |
126 | # |
108 | $class->_new ( |
127 | # $class->new ( |
109 | data => $surface->pixels, |
128 | # data => $surface->pixels, |
110 | width => $surface->width, |
129 | # width => $surface->width, |
111 | height => $surface->height, |
130 | # height => $surface->height, |
|
|
131 | # ) |
|
|
132 | #} |
|
|
133 | |
|
|
134 | sub new_from_layout { |
|
|
135 | my ($class, $layout) = @_; |
|
|
136 | |
|
|
137 | my ($w, $h, $data) = $layout->render; |
|
|
138 | |
|
|
139 | $class->new ( |
|
|
140 | width => $w, |
|
|
141 | height => $h, |
|
|
142 | data => $data, |
|
|
143 | internalformat => GL_ALPHA4, |
|
|
144 | format => GL_ALPHA, |
112 | ) |
145 | ) |
113 | } |
146 | } |
114 | |
147 | |
115 | sub new_from_ttf { |
148 | sub new_from_opengl { |
116 | my ($class, $ttf, $text) = @_; |
149 | my ($class, $w, $h, $cb) = @_; |
117 | |
150 | |
118 | utf8::upgrade $text; |
151 | $class->new (width => $w, height => $h, rendercb => $cb) |
|
|
152 | } |
119 | |
153 | |
120 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
154 | sub topot { |
121 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
155 | (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0] |
122 | |
|
|
123 | $class->new_from_surface ($surface) |
|
|
124 | } |
156 | } |
125 | |
157 | |
126 | sub upload { |
158 | sub upload { |
127 | my ($self) = @_; |
159 | my ($self) = @_; |
128 | |
160 | |
… | |
… | |
130 | |
162 | |
131 | my $data; |
163 | my $data; |
132 | |
164 | |
133 | if (exists $self->{data}) { |
165 | if (exists $self->{data}) { |
134 | $data = $self->{data}; |
166 | $data = $self->{data}; |
|
|
167 | } elsif (exists $self->{rendercb}) { |
|
|
168 | glViewport 0, 0, $self->{width}, $self->{height}; |
|
|
169 | glMatrixMode GL_PROJECTION; |
|
|
170 | glLoadIdentity; |
|
|
171 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
|
|
172 | glMatrixMode GL_MODELVIEW; |
|
|
173 | glPushmatrix; |
|
|
174 | glLoadIdentity; |
|
|
175 | glClear GL_COLOR_BUFFER_BIT; |
|
|
176 | |
|
|
177 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
135 | } else { |
178 | } else { |
136 | my $pb = new Gtk2::Gdk::PixbufLoader; |
179 | my $pb = new Gtk2::Gdk::PixbufLoader; |
137 | $pb->write ($self->{image}); |
180 | $pb->write ($self->{image}); |
138 | $pb->close; |
181 | $pb->close; |
139 | |
182 | |
… | |
… | |
144 | $self->{height} = $pb->get_height; |
187 | $self->{height} = $pb->get_height; |
145 | |
188 | |
146 | $data = $pb->get_pixels; |
189 | $data = $pb->get_pixels; |
147 | } |
190 | } |
148 | |
191 | |
|
|
192 | my ($tw, $th) = @$self{qw(width height)}; |
|
|
193 | |
|
|
194 | unless ($GL_NPOT && $tw && $th) { |
|
|
195 | $tw = topot $tw; |
|
|
196 | $th = topot $th; |
|
|
197 | |
|
|
198 | if (defined $data) { |
|
|
199 | my $bpp = (length $data) / ($self->{width} * $self->{height}); |
|
|
200 | $data = pack "(a" . ($tw * $bpp) . ")*", |
|
|
201 | unpack "(a" . ($self->{width} * $bpp) . ")*", $data; |
|
|
202 | $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{height}); |
|
|
203 | } |
|
|
204 | } |
|
|
205 | |
|
|
206 | $self->{s} = $self->{width} / $tw; |
|
|
207 | $self->{t} = $self->{height} / $th; |
|
|
208 | |
149 | ($self->{name}) = @{glGenTextures 1}; |
209 | $self->{name} ||= (glGenTextures 1)->[0]; |
150 | |
210 | |
151 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
211 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
152 | |
212 | |
153 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
213 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
154 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
214 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
215 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
156 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
216 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
157 | |
217 | |
|
|
218 | glGetError; |
|
|
219 | if (defined $data) { |
158 | glTexImage2D GL_TEXTURE_2D, 0, |
220 | glTexImage2D GL_TEXTURE_2D, 0, |
159 | GL_RGBA8, |
221 | $self->{internalformat}, |
160 | $self->{width}, $self->{height}, |
222 | $tw, $th, # need to pad texture first |
161 | 0, |
223 | 0, |
162 | GL_RGBA, |
224 | $self->{format}, |
163 | GL_UNSIGNED_BYTE, |
225 | GL_UNSIGNED_BYTE, |
164 | $data; |
226 | $data; |
|
|
227 | glGetError and die; |
|
|
228 | } else { |
|
|
229 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
230 | $self->{internalformat}, |
|
|
231 | 0, 0, |
|
|
232 | $tw, $th, |
|
|
233 | 0; |
|
|
234 | glPopmatrix; |
|
|
235 | } |
165 | } |
236 | } |
166 | |
237 | |
167 | sub DESTROY { |
238 | sub DESTROY { |
168 | my ($self) = @_; |
239 | my ($self) = @_; |
169 | |
240 | |
170 | return unless exists $self->{name}; |
241 | return unless exists $self->{name}; |
171 | |
242 | |
172 | glDeleteTextures delete $self->{name}; |
243 | glDeleteTextures delete $self->{name}; |
173 | } |
244 | } |
174 | |
245 | |
175 | push @::GLINIT, sub { |
246 | sub restore_state{ |
176 | $_->upload |
247 | $_->upload |
177 | for grep $_, @textures; |
248 | for grep $_, @textures; |
178 | }; |
249 | }; |
179 | |
250 | |
180 | 1; |
251 | 1; |