… | |
… | |
19 | |
19 | |
20 | use XSLoader; |
20 | use XSLoader; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
21 | XSLoader::load "Crossfire::Client", $VERSION; |
22 | } |
22 | } |
23 | |
23 | |
|
|
24 | sub find_rcfile($) { |
|
|
25 | my $path; |
|
|
26 | |
|
|
27 | for (@INC) { |
|
|
28 | $path = "$_/Crossfire/resources/$_[0]"; |
|
|
29 | return $path if -r $path; |
|
|
30 | } |
|
|
31 | |
|
|
32 | die "FATAL: can't find required file $_[0]\n"; |
|
|
33 | } |
|
|
34 | |
|
|
35 | sub read_cfg { |
|
|
36 | my ($file) = @_; |
|
|
37 | |
|
|
38 | open CFG, $file |
|
|
39 | or return; |
|
|
40 | |
|
|
41 | my $CFG; |
|
|
42 | |
|
|
43 | local $/; |
|
|
44 | $CFG = eval <CFG>; |
|
|
45 | |
|
|
46 | $::CFG = $CFG; |
|
|
47 | |
|
|
48 | close CFG; |
|
|
49 | } |
|
|
50 | |
|
|
51 | sub write_cfg { |
|
|
52 | my ($file) = @_; |
|
|
53 | |
|
|
54 | open CFG, ">$file" |
|
|
55 | or return; |
|
|
56 | |
|
|
57 | { |
|
|
58 | require Data::Dumper; |
|
|
59 | local $Data::Dumper::Purity = 1; |
|
|
60 | $::CFG->{VERSION} = $::VERSION; |
|
|
61 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
|
|
62 | } |
|
|
63 | |
|
|
64 | close CFG; |
|
|
65 | } |
|
|
66 | |
24 | package Crossfire::Client::Texture; |
67 | package Crossfire::Client::Texture; |
25 | |
68 | |
26 | use Scalar::Util; |
69 | use Scalar::Util; |
27 | |
70 | |
28 | use SDL::OpenGL; |
71 | use SDL::OpenGL; |
29 | |
72 | |
30 | my @textures; |
73 | my @textures; |
|
|
74 | |
|
|
75 | sub new { |
|
|
76 | my ($class, %data) = @_; |
|
|
77 | |
|
|
78 | my $self = bless { |
|
|
79 | internalformat => GL_RGBA, |
|
|
80 | format => GL_RGBA, |
|
|
81 | %data, |
|
|
82 | }, $class; |
|
|
83 | |
|
|
84 | push @textures, $self; |
|
|
85 | Scalar::Util::weaken $textures[-1]; |
|
|
86 | |
|
|
87 | $self->upload; |
|
|
88 | |
|
|
89 | $self |
|
|
90 | } |
|
|
91 | |
|
|
92 | sub new_from_image { |
|
|
93 | my ($class, $image) = @_; |
|
|
94 | |
|
|
95 | $class->new (image => $image) |
|
|
96 | } |
31 | |
97 | |
32 | sub new_from_file { |
98 | sub new_from_file { |
33 | my ($class, $path) = @_; |
99 | my ($class, $path) = @_; |
34 | |
100 | |
35 | open my $fh, "<:raw", $path |
101 | open my $fh, "<:raw", $path |
36 | or die "$path: $!"; |
102 | or die "$path: $!"; |
37 | |
103 | |
38 | local $/; |
104 | local $/; |
39 | $class->new_from_data (<$fh>) |
105 | $class->new_from_image (<$fh>) |
40 | } |
106 | } |
41 | |
107 | |
42 | sub new_from_image { |
108 | #sub new_from_surface { |
43 | my ($class, $image) = @_; |
109 | # my ($class, $surface) = @_; |
|
|
110 | # |
|
|
111 | # $surface->rgba; |
|
|
112 | # |
|
|
113 | # $class->new ( |
|
|
114 | # data => $surface->pixels, |
|
|
115 | # width => $surface->width, |
|
|
116 | # height => $surface->height, |
|
|
117 | # ) |
|
|
118 | #} |
44 | |
119 | |
45 | my $self = bless { |
120 | sub new_from_text { |
46 | image => $data, |
121 | my ($class, $text, $height) = @_; |
47 | }; |
|
|
48 | |
122 | |
49 | push @textures, $self; |
123 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
50 | Scalar::Util::weaken $textures[-1]; |
|
|
51 | |
124 | |
52 | $self->upload; |
125 | $class->new ( |
|
|
126 | width => $w, |
|
|
127 | height => $h, |
|
|
128 | data => $data, |
|
|
129 | internalformat => GL_ALPHA4, |
|
|
130 | format => GL_ALPHA, |
|
|
131 | ) |
|
|
132 | } |
53 | |
133 | |
54 | $self |
134 | sub new_from_opengl { |
|
|
135 | my ($class, $w, $h, $cb) = @_; |
|
|
136 | |
|
|
137 | $class->new (width => $w, height => $h, rendercb => $cb) |
55 | } |
138 | } |
56 | |
139 | |
57 | sub upload { |
140 | sub upload { |
58 | my ($self) = @_; |
141 | my ($self) = @_; |
59 | |
142 | |
60 | return unless $SDL::App::USING_OPENGL; |
143 | return unless $SDL::App::USING_OPENGL; |
61 | |
144 | |
62 | my ($data, $width, $height); |
145 | my $data; |
63 | |
146 | |
64 | if (exists $self->{data}) { |
147 | if (exists $self->{data}) { |
65 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
148 | $data = $self->{data}; |
|
|
149 | } elsif (exists $self->{rendercb}) { |
|
|
150 | glViewport 0, 0, $self->{width}, $self->{height}; |
|
|
151 | glMatrixMode GL_PROJECTION; |
|
|
152 | glLoadIdentity; |
|
|
153 | glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000; |
|
|
154 | glMatrixMode GL_MODELVIEW; |
|
|
155 | glPushmatrix; |
|
|
156 | glLoadIdentity; |
|
|
157 | glClear GL_COLOR_BUFFER_BIT; |
|
|
158 | |
|
|
159 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
66 | } else { |
160 | } else { |
67 | my $pb = new Gtk2::Gdk::PixbufLoader; |
161 | my $pb = new Gtk2::Gdk::PixbufLoader; |
68 | $pb->write ($self->{data}); |
162 | $pb->write ($self->{image}); |
69 | $pb->close; |
163 | $pb->close; |
70 | |
164 | |
71 | $pb = $pb->get_pixbuf; |
165 | $pb = $pb->get_pixbuf; |
72 | $pb = $pb->add_alpha (0, 0, 0, 0); |
166 | $pb = $pb->add_alpha (0, 0, 0, 0); |
73 | |
167 | |
74 | ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); |
168 | $self->{width} = $pb->get_width; |
|
|
169 | $self->{height} = $pb->get_height; |
|
|
170 | |
|
|
171 | $data = $pb->get_pixels; |
75 | } |
172 | } |
76 | |
173 | |
77 | ($self->{name}) = @{glGenTextures 1}; |
174 | ($self->{name}) = @{glGenTextures 1}; |
78 | |
175 | |
79 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
80 | |
177 | |
81 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
82 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; |
179 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
83 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
84 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
85 | |
182 | |
|
|
183 | glGetError; |
|
|
184 | if (defined $data) { |
86 | glTexImage2D GL_TEXTURE_2D, 0, |
185 | glTexImage2D GL_TEXTURE_2D, 0, |
87 | GL_RGBA8, |
186 | $self->{internalformat}, |
88 | $width, $height, |
187 | $self->{width}, $self->{height}, |
89 | 0, |
188 | 0, |
90 | GL_RGBA, |
189 | $self->{format}, |
91 | GL_UNSIGNED_BYTE, |
190 | GL_UNSIGNED_BYTE, |
92 | $data; |
191 | $data; |
|
|
192 | glGetError and die; |
|
|
193 | } else { |
|
|
194 | glCopyTexImage2D GL_TEXTURE_2D, 0, |
|
|
195 | $self->{internalformat}, |
|
|
196 | 0, 0, |
|
|
197 | $self->{width}, $self->{height}, |
|
|
198 | 0; |
|
|
199 | glPopmatrix; |
|
|
200 | } |
93 | } |
201 | } |
94 | |
202 | |
95 | sub DESTROY { |
203 | sub DESTROY { |
96 | my ($self) = @_; |
204 | my ($self) = @_; |
97 | |
205 | |
98 | return unless exists $self->{name}; |
206 | return unless exists $self->{name}; |
99 | |
207 | |
100 | glDeleteTextures delete $self->{name}; |
208 | glDeleteTextures delete $self->{name}; |
101 | } |
209 | } |
102 | |
210 | |
103 | push @::GLINIT, sub { |
211 | push @::GL_INIT, sub { |
104 | $_->upload |
212 | $_->upload |
105 | for grep $_, @textures; |
213 | for grep $_, @textures; |
106 | }; |
214 | }; |
107 | |
215 | |
108 | 1; |
216 | 1; |