1 |
=head1 NAME |
2 |
|
3 |
Crossfire::Client - undocumented utility garbage for our crossfire client |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Crossfire::Client; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
=over 4 |
12 |
|
13 |
=cut |
14 |
|
15 |
package Crossfire::Client; |
16 |
|
17 |
BEGIN { |
18 |
$VERSION = '0.1'; |
19 |
|
20 |
use XSLoader; |
21 |
XSLoader::load "Crossfire::Client", $VERSION; |
22 |
} |
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 |
|
67 |
package Crossfire::Client::Texture; |
68 |
|
69 |
use Scalar::Util; |
70 |
|
71 |
use SDL::OpenGL; |
72 |
|
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 |
} |
97 |
|
98 |
sub new_from_file { |
99 |
my ($class, $path) = @_; |
100 |
|
101 |
open my $fh, "<:raw", $path |
102 |
or die "$path: $!"; |
103 |
|
104 |
local $/; |
105 |
$class->new_from_image (<$fh>) |
106 |
} |
107 |
|
108 |
#sub new_from_surface { |
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 |
#} |
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, |
131 |
) |
132 |
} |
133 |
|
134 |
sub new_from_opengl { |
135 |
my ($class, $w, $h, $cb) = @_; |
136 |
|
137 |
$class->new (width => $w, height => $h, rendercb => $cb) |
138 |
} |
139 |
|
140 |
sub upload { |
141 |
my ($self) = @_; |
142 |
|
143 |
return unless $SDL::App::USING_OPENGL; |
144 |
|
145 |
my $data; |
146 |
|
147 |
if (exists $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}); |
160 |
} else { |
161 |
my $pb = new Gtk2::Gdk::PixbufLoader; |
162 |
$pb->write ($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 |
} |
173 |
|
174 |
($self->{name}) = @{glGenTextures 1}; |
175 |
|
176 |
glBindTexture GL_TEXTURE_2D, $self->{name}; |
177 |
|
178 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST; |
179 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR; |
180 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; |
181 |
glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; |
182 |
|
183 |
glGetError; |
184 |
if (defined $data) { |
185 |
glTexImage2D GL_TEXTURE_2D, 0, |
186 |
$self->{internalformat}, |
187 |
$self->{width}, $self->{height}, |
188 |
0, |
189 |
$self->{format}, |
190 |
GL_UNSIGNED_BYTE, |
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 |
} |
201 |
} |
202 |
|
203 |
sub DESTROY { |
204 |
my ($self) = @_; |
205 |
|
206 |
return unless exists $self->{name}; |
207 |
|
208 |
glDeleteTextures delete $self->{name}; |
209 |
} |
210 |
|
211 |
push @::GLINIT, sub { |
212 |
$_->upload |
213 |
for grep $_, @textures; |
214 |
}; |
215 |
|
216 |
1; |
217 |
|
218 |
=back |
219 |
|
220 |
=head1 AUTHOR |
221 |
|
222 |
Marc Lehmann <schmorp@schmorp.de> |
223 |
http://home.schmorp.de/ |
224 |
|
225 |
=cut |
226 |
|