ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.33
Committed: Sat Apr 15 01:13:45 2006 UTC (18 years ago) by root
Branch: MAIN
Changes since 1.32: +3 -3 lines
Log Message:
overview map - just for fun

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.22 CFClient - undocumented utility garbage for our crossfire client
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.22 use CFClient;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15 root 1.22 package CFClient;
16 root 1.1
17     BEGIN {
18     $VERSION = '0.1';
19    
20 root 1.2 use XSLoader;
21 root 1.22 XSLoader::load "CFClient", $VERSION;
22 root 1.1 }
23    
24 root 1.26 use SDL::OpenGL;
25    
26 root 1.19 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 root 1.26 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 root 1.22 CFClient::Texture::restore_state ();
46 root 1.19 }
47    
48 root 1.5 sub find_rcfile($) {
49     my $path;
50    
51     for (@INC) {
52 root 1.22 $path = "$_/CFClient/resources/$_[0]";
53 root 1.5 return $path if -r $path;
54     }
55    
56     die "FATAL: can't find required file $_[0]\n";
57     }
58    
59     sub read_cfg {
60     my ($file) = @_;
61    
62     open CFG, $file
63     or return;
64    
65     my $CFG;
66    
67     local $/;
68     $CFG = eval <CFG>;
69    
70     $::CFG = $CFG;
71    
72     close CFG;
73     }
74    
75     sub write_cfg {
76     my ($file) = @_;
77    
78     open CFG, ">$file"
79     or return;
80    
81     {
82 elmex 1.9 require Data::Dumper;
83 root 1.5 local $Data::Dumper::Purity = 1;
84     $::CFG->{VERSION} = $::VERSION;
85     print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
86     }
87    
88     close CFG;
89     }
90    
91 root 1.22 package CFClient::Texture;
92 root 1.3
93 root 1.25 use strict;
94    
95 root 1.3 use Scalar::Util;
96    
97     use SDL::OpenGL;
98    
99     my @textures;
100    
101 root 1.14 sub new {
102 root 1.4 my ($class, %data) = @_;
103    
104 root 1.14 my $self = bless {
105 root 1.15 internalformat => GL_RGBA,
106     format => GL_RGBA,
107 root 1.29 type => GL_UNSIGNED_BYTE,
108 root 1.14 %data,
109     }, $class;
110 root 1.4
111     push @textures, $self;
112     Scalar::Util::weaken $textures[-1];
113    
114     $self->upload;
115    
116     $self
117     }
118    
119     sub new_from_image {
120 root 1.33 my ($class, $image, %arg) = @_;
121 root 1.4
122 root 1.33 $class->new (image => $image, %arg)
123 root 1.4 }
124    
125 root 1.3 sub new_from_file {
126     my ($class, $path) = @_;
127    
128     open my $fh, "<:raw", $path
129     or die "$path: $!";
130    
131     local $/;
132 root 1.4 $class->new_from_image (<$fh>)
133 root 1.3 }
134    
135 root 1.14 #sub new_from_surface {
136     # my ($class, $surface) = @_;
137     #
138     # $surface->rgba;
139     #
140     # $class->new (
141     # data => $surface->pixels,
142 root 1.24 # w => $surface->width,
143     # h => $surface->height,
144 root 1.14 # )
145     #}
146    
147 root 1.21 sub new_from_layout {
148     my ($class, $layout) = @_;
149 root 1.14
150 root 1.21 my ($w, $h, $data) = $layout->render;
151 root 1.14
152     $class->new (
153 root 1.24 w => $w,
154     h => $h,
155 root 1.14 data => $data,
156 root 1.16 internalformat => GL_ALPHA4,
157 root 1.14 format => GL_ALPHA,
158 root 1.29 type => GL_UNSIGNED_BYTE,
159 root 1.4 )
160 root 1.3 }
161    
162 root 1.8 sub new_from_opengl {
163     my ($class, $w, $h, $cb) = @_;
164    
165 root 1.24 $class->new (w => $w, h => $h, render_cb => $cb)
166 root 1.8 }
167    
168 root 1.19 sub topot {
169     (grep $_ >= $_[0], 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768)[0]
170     }
171    
172 root 1.3 sub upload {
173     my ($self) = @_;
174    
175 root 1.27 return unless $GL_VERSION;
176 root 1.3
177 root 1.6 my $data;
178 root 1.3
179     if (exists $self->{data}) {
180 root 1.6 $data = $self->{data};
181 root 1.25
182 root 1.24 } elsif (exists $self->{render_cb}) {
183     glViewport 0, 0, $self->{w}, $self->{h};
184 root 1.12 glMatrixMode GL_PROJECTION;
185     glLoadIdentity;
186 root 1.31 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
187 root 1.12 glMatrixMode GL_MODELVIEW;
188     glLoadIdentity;
189 root 1.25 $self->{render_cb}->($self, $self->{w}, $self->{h});
190 root 1.8
191 root 1.3 } else {
192 root 1.29 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
193     = CFClient::load_image_inline $self->{image};
194 root 1.3 }
195    
196 root 1.24 my ($tw, $th) = @$self{qw(w h)};
197 root 1.19
198 root 1.25 unless ($tw && $th) {
199 root 1.24 $tw = $th = 1;
200     $data = "\x00" x 64;
201     }
202    
203 root 1.32 $self->{minified} = [CFClient::average $tw, $th, $data]
204 root 1.33 if $self->{minify};
205 root 1.32
206 root 1.24 unless ($GL_NPOT) {
207     # TODO: does not work for zero-sized textures
208 root 1.19 $tw = topot $tw;
209     $th = topot $th;
210    
211 root 1.25 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
212 root 1.24 my $bpp = (length $data) / ($self->{w} * $self->{h});
213 root 1.20 $data = pack "(a" . ($tw * $bpp) . ")*",
214 root 1.24 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
215     $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
216 root 1.19 }
217     }
218    
219 root 1.24 $self->{s} = $self->{w} / $tw;
220     $self->{t} = $self->{h} / $th;
221 root 1.19
222     $self->{name} ||= (glGenTextures 1)->[0];
223 root 1.3
224     glBindTexture GL_TEXTURE_2D, $self->{name};
225    
226 root 1.26 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
227     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
228 root 1.3 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
229     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
230    
231 root 1.8 if (defined $data) {
232     glTexImage2D GL_TEXTURE_2D, 0,
233 root 1.15 $self->{internalformat},
234 root 1.19 $tw, $th, # need to pad texture first
235 root 1.8 0,
236 root 1.14 $self->{format},
237 root 1.29 $self->{type},
238 root 1.8 $data;
239 root 1.16 glGetError and die;
240 root 1.8 } else {
241     glCopyTexImage2D GL_TEXTURE_2D, 0,
242 root 1.15 $self->{internalformat},
243 root 1.8 0, 0,
244 root 1.19 $tw, $th,
245 root 1.8 0;
246 root 1.31 glGetError and die "glCopyTexImage2D $tw,$th";
247 root 1.8 }
248 root 1.3 }
249    
250     sub DESTROY {
251     my ($self) = @_;
252    
253     return unless exists $self->{name};
254    
255     glDeleteTextures delete $self->{name};
256     }
257    
258 root 1.19 sub restore_state{
259 root 1.3 $_->upload
260     for grep $_, @textures;
261     };
262    
263 root 1.1 1;
264    
265     =back
266    
267     =head1 AUTHOR
268    
269     Marc Lehmann <schmorp@schmorp.de>
270     http://home.schmorp.de/
271    
272     =cut
273