ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.30
Committed: Wed Apr 12 21:35:10 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.29: +0 -2 lines
Log Message:
fow configurable

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     my ($class, $image) = @_;
121    
122 root 1.14 $class->new (image => $image)
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.25 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 root 1.12 glMatrixMode GL_PROJECTION;
186     glLoadIdentity;
187     glMatrixMode GL_MODELVIEW;
188     glLoadIdentity;
189 root 1.8 glClear GL_COLOR_BUFFER_BIT;
190 root 1.25 $self->{render_cb}->($self, $self->{w}, $self->{h});
191 root 1.8
192 root 1.3 } else {
193 root 1.29 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
194     = CFClient::load_image_inline $self->{image};
195 root 1.3 }
196    
197 root 1.24 my ($tw, $th) = @$self{qw(w h)};
198 root 1.19
199 root 1.25 unless ($tw && $th) {
200 root 1.24 $tw = $th = 1;
201     $data = "\x00" x 64;
202     }
203    
204     unless ($GL_NPOT) {
205     # TODO: does not work for zero-sized textures
206 root 1.19 $tw = topot $tw;
207     $th = topot $th;
208    
209 root 1.25 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
210 root 1.24 my $bpp = (length $data) / ($self->{w} * $self->{h});
211 root 1.20 $data = pack "(a" . ($tw * $bpp) . ")*",
212 root 1.24 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
213     $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
214 root 1.19 }
215     }
216    
217 root 1.24 $self->{s} = $self->{w} / $tw;
218     $self->{t} = $self->{h} / $th;
219 root 1.19
220     $self->{name} ||= (glGenTextures 1)->[0];
221 root 1.3
222     glBindTexture GL_TEXTURE_2D, $self->{name};
223    
224 root 1.26 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
225     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, $::FAST ? GL_NEAREST : GL_LINEAR;
226 root 1.3 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
227     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
228    
229 root 1.8 if (defined $data) {
230     glTexImage2D GL_TEXTURE_2D, 0,
231 root 1.15 $self->{internalformat},
232 root 1.19 $tw, $th, # need to pad texture first
233 root 1.8 0,
234 root 1.14 $self->{format},
235 root 1.29 $self->{type},
236 root 1.8 $data;
237 root 1.16 glGetError and die;
238 root 1.8 } else {
239     glCopyTexImage2D GL_TEXTURE_2D, 0,
240 root 1.15 $self->{internalformat},
241 root 1.8 0, 0,
242 root 1.19 $tw, $th,
243 root 1.8 0;
244 root 1.25 glGetError and die;
245 root 1.8 }
246 root 1.3 }
247    
248     sub DESTROY {
249     my ($self) = @_;
250    
251     return unless exists $self->{name};
252    
253     glDeleteTextures delete $self->{name};
254     }
255    
256 root 1.19 sub restore_state{
257 root 1.3 $_->upload
258     for grep $_, @textures;
259     };
260    
261 root 1.1 1;
262    
263     =back
264    
265     =head1 AUTHOR
266    
267     Marc Lehmann <schmorp@schmorp.de>
268     http://home.schmorp.de/
269    
270     =cut
271