… | |
… | |
53 | |
53 | |
54 | open CFG, ">$file" |
54 | open CFG, ">$file" |
55 | or return; |
55 | or return; |
56 | |
56 | |
57 | { |
57 | { |
|
|
58 | require Data::Dumper; |
58 | local $Data::Dumper::Purity = 1; |
59 | local $Data::Dumper::Purity = 1; |
59 | $::CFG->{VERSION} = $::VERSION; |
60 | $::CFG->{VERSION} = $::VERSION; |
60 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
61 | print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]); |
61 | } |
62 | } |
62 | |
63 | |
… | |
… | |
69 | |
70 | |
70 | use SDL::OpenGL; |
71 | use SDL::OpenGL; |
71 | |
72 | |
72 | my @textures; |
73 | my @textures; |
73 | |
74 | |
74 | sub _new { |
75 | sub new { |
75 | my ($class, %data) = @_; |
76 | my ($class, %data) = @_; |
76 | |
77 | |
77 | my $self = bless \%data, $class; |
78 | my $self = bless { |
|
|
79 | internalformat => GL_RGBA, |
|
|
80 | format => GL_RGBA, |
|
|
81 | %data, |
|
|
82 | }, $class; |
78 | |
83 | |
79 | push @textures, $self; |
84 | push @textures, $self; |
80 | Scalar::Util::weaken $textures[-1]; |
85 | Scalar::Util::weaken $textures[-1]; |
81 | |
86 | |
82 | $self->upload; |
87 | $self->upload; |
… | |
… | |
85 | } |
90 | } |
86 | |
91 | |
87 | sub new_from_image { |
92 | sub new_from_image { |
88 | my ($class, $image) = @_; |
93 | my ($class, $image) = @_; |
89 | |
94 | |
90 | $class->_new (image => $image) |
95 | $class->new (image => $image) |
91 | } |
96 | } |
92 | |
97 | |
93 | sub new_from_file { |
98 | sub new_from_file { |
94 | my ($class, $path) = @_; |
99 | my ($class, $path) = @_; |
95 | |
100 | |
… | |
… | |
98 | |
103 | |
99 | local $/; |
104 | local $/; |
100 | $class->new_from_image (<$fh>) |
105 | $class->new_from_image (<$fh>) |
101 | } |
106 | } |
102 | |
107 | |
103 | sub new_from_surface { |
108 | #sub new_from_surface { |
104 | my ($class, $surface) = @_; |
109 | # my ($class, $surface) = @_; |
105 | |
110 | # |
106 | $surface->rgba; |
111 | # $surface->rgba; |
107 | |
112 | # |
108 | $class->_new ( |
113 | # $class->new ( |
109 | data => $surface->pixels, |
114 | # data => $surface->pixels, |
110 | width => $surface->width, |
115 | # width => $surface->width, |
111 | height => $surface->height, |
116 | # height => $surface->height, |
|
|
117 | # ) |
|
|
118 | #} |
|
|
119 | |
|
|
120 | sub new_from_text { |
|
|
121 | my ($class, $text, $height) = @_; |
|
|
122 | |
|
|
123 | my ($w, $h, $data) = Crossfire::Client::font_render $text, $height; |
|
|
124 | |
|
|
125 | $class->new ( |
|
|
126 | width => $w, |
|
|
127 | height => $h, |
|
|
128 | data => $data, |
|
|
129 | internalformat => GL_ALPHA4, |
|
|
130 | format => GL_ALPHA, |
112 | ) |
131 | ) |
113 | } |
132 | } |
114 | |
133 | |
115 | sub new_from_ttf { |
134 | sub new_from_opengl { |
116 | my ($class, $ttf, $text) = @_; |
135 | my ($class, $w, $h, $cb) = @_; |
117 | |
136 | |
118 | utf8::upgrade $text; |
137 | $class->new (width => $w, height => $h, rendercb => $cb) |
119 | |
|
|
120 | my $surface = SDL::TTFRenderUTF8Blended $ttf, $text, |
|
|
121 | (new SDL::Color -r => 255, -g => 255, -b => 255); |
|
|
122 | |
|
|
123 | $class->new_from_surface ($surface) |
|
|
124 | } |
138 | } |
125 | |
139 | |
126 | sub upload { |
140 | sub upload { |
127 | my ($self) = @_; |
141 | my ($self) = @_; |
128 | |
142 | |
… | |
… | |
130 | |
144 | |
131 | my $data; |
145 | my $data; |
132 | |
146 | |
133 | if (exists $self->{data}) { |
147 | if (exists $self->{data}) { |
134 | $data = $self->{data}; |
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}, -100, 100; |
|
|
154 | glMatrixMode GL_MODELVIEW; |
|
|
155 | glPushmatrix; |
|
|
156 | glLoadIdentity; |
|
|
157 | glClear GL_COLOR_BUFFER_BIT; |
|
|
158 | |
|
|
159 | $self->{rendercb}->($self, $self->{width}, $self->{height}); |
135 | } else { |
160 | } else { |
136 | my $pb = new Gtk2::Gdk::PixbufLoader; |
161 | my $pb = new Gtk2::Gdk::PixbufLoader; |
137 | $pb->write ($self->{image}); |
162 | $pb->write ($self->{image}); |
138 | $pb->close; |
163 | $pb->close; |
139 | |
164 | |
… | |
… | |
148 | |
173 | |
149 | ($self->{name}) = @{glGenTextures 1}; |
174 | ($self->{name}) = @{glGenTextures 1}; |
150 | |
175 | |
151 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
176 | glBindTexture GL_TEXTURE_2D, $self->{name}; |
152 | |
177 | |
153 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
178 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
154 | 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; |
155 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
180 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
156 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
181 | glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
157 | |
182 | |
|
|
183 | glGetError; |
|
|
184 | if (defined $data) { |
158 | glTexImage2D GL_TEXTURE_2D, 0, |
185 | glTexImage2D GL_TEXTURE_2D, 0, |
159 | GL_RGBA8, |
186 | $self->{internalformat}, |
160 | $self->{width}, $self->{height}, |
187 | $self->{width}, $self->{height}, |
161 | 0, |
188 | 0, |
162 | GL_RGBA, |
189 | $self->{format}, |
163 | GL_UNSIGNED_BYTE, |
190 | GL_UNSIGNED_BYTE, |
164 | $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 | } |
165 | } |
201 | } |
166 | |
202 | |
167 | sub DESTROY { |
203 | sub DESTROY { |
168 | my ($self) = @_; |
204 | my ($self) = @_; |
169 | |
205 | |
170 | return unless exists $self->{name}; |
206 | return unless exists $self->{name}; |
171 | |
207 | |
172 | glDeleteTextures delete $self->{name}; |
208 | glDeleteTextures delete $self->{name}; |
173 | } |
209 | } |
174 | |
210 | |
175 | push @::GLINIT, sub { |
211 | push @::GL_INIT, sub { |
176 | $_->upload |
212 | $_->upload |
177 | for grep $_, @textures; |
213 | for grep $_, @textures; |
178 | }; |
214 | }; |
179 | |
215 | |
180 | 1; |
216 | 1; |