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

# Content
1 =head1 NAME
2
3 CFClient - undocumented utility garbage for our crossfire client
4
5 =head1 SYNOPSIS
6
7 use CFClient;
8
9 =head1 DESCRIPTION
10
11 =over 4
12
13 =cut
14
15 package CFClient;
16
17 BEGIN {
18 $VERSION = '0.1';
19
20 use XSLoader;
21 XSLoader::load "CFClient", $VERSION;
22 }
23
24 use SDL::OpenGL;
25
26 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 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 CFClient::Texture::restore_state ();
46 }
47
48 sub find_rcfile($) {
49 my $path;
50
51 for (@INC) {
52 $path = "$_/CFClient/resources/$_[0]";
53 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 require Data::Dumper;
83 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 package CFClient::Texture;
92
93 use strict;
94
95 use Scalar::Util;
96
97 use SDL::OpenGL;
98
99 my @textures;
100
101 sub new {
102 my ($class, %data) = @_;
103
104 my $self = bless {
105 internalformat => GL_RGBA,
106 format => GL_RGBA,
107 type => GL_UNSIGNED_BYTE,
108 %data,
109 }, $class;
110
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 $class->new (image => $image)
123 }
124
125 sub new_from_file {
126 my ($class, $path) = @_;
127
128 open my $fh, "<:raw", $path
129 or die "$path: $!";
130
131 local $/;
132 $class->new_from_image (<$fh>)
133 }
134
135 #sub new_from_surface {
136 # my ($class, $surface) = @_;
137 #
138 # $surface->rgba;
139 #
140 # $class->new (
141 # data => $surface->pixels,
142 # w => $surface->width,
143 # h => $surface->height,
144 # )
145 #}
146
147 sub new_from_layout {
148 my ($class, $layout) = @_;
149
150 my ($w, $h, $data) = $layout->render;
151
152 $class->new (
153 w => $w,
154 h => $h,
155 data => $data,
156 internalformat => GL_ALPHA4,
157 format => GL_ALPHA,
158 type => GL_UNSIGNED_BYTE,
159 )
160 }
161
162 sub new_from_opengl {
163 my ($class, $w, $h, $cb) = @_;
164
165 $class->new (w => $w, h => $h, render_cb => $cb)
166 }
167
168 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 sub upload {
173 my ($self) = @_;
174
175 return unless $GL_VERSION;
176
177 my $data;
178
179 if (exists $self->{data}) {
180 $data = $self->{data};
181
182 } elsif (exists $self->{render_cb}) {
183 glViewport 0, 0, $self->{w}, $self->{h};
184 glOrtho 0, $self->{w}, 0, $self->{h}, -10000, 10000;
185 glMatrixMode GL_PROJECTION;
186 glLoadIdentity;
187 glMatrixMode GL_MODELVIEW;
188 glLoadIdentity;
189 glClear GL_COLOR_BUFFER_BIT;
190 $self->{render_cb}->($self, $self->{w}, $self->{h});
191
192 } else {
193 ($self->{w}, $self->{h}, $data, $self->{internalformat}, $self->{format}, $self->{type})
194 = CFClient::load_image_inline $self->{image};
195 }
196
197 my ($tw, $th) = @$self{qw(w h)};
198
199 unless ($tw && $th) {
200 $tw = $th = 1;
201 $data = "\x00" x 64;
202 }
203
204 unless ($GL_NPOT) {
205 # TODO: does not work for zero-sized textures
206 $tw = topot $tw;
207 $th = topot $th;
208
209 if ($tw != $self->{w} || $th != $self->{h} && defined $data) {
210 my $bpp = (length $data) / ($self->{w} * $self->{h});
211 $data = pack "(a" . ($tw * $bpp) . ")*",
212 unpack "(a" . ($self->{w} * $bpp) . ")*", $data;
213 $data .= ("\x00" x ($tw * $bpp)) x ($th - $self->{h});
214 }
215 }
216
217 $self->{s} = $self->{w} / $tw;
218 $self->{t} = $self->{h} / $th;
219
220 $self->{name} ||= (glGenTextures 1)->[0];
221
222 glBindTexture GL_TEXTURE_2D, $self->{name};
223
224 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 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
227 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
228
229 if (defined $data) {
230 glTexImage2D GL_TEXTURE_2D, 0,
231 $self->{internalformat},
232 $tw, $th, # need to pad texture first
233 0,
234 $self->{format},
235 $self->{type},
236 $data;
237 glGetError and die;
238 } else {
239 glCopyTexImage2D GL_TEXTURE_2D, 0,
240 $self->{internalformat},
241 0, 0,
242 $tw, $th,
243 0;
244 glGetError and die;
245 }
246 }
247
248 sub DESTROY {
249 my ($self) = @_;
250
251 return unless exists $self->{name};
252
253 glDeleteTextures delete $self->{name};
254 }
255
256 sub restore_state{
257 $_->upload
258 for grep $_, @textures;
259 };
260
261 1;
262
263 =back
264
265 =head1 AUTHOR
266
267 Marc Lehmann <schmorp@schmorp.de>
268 http://home.schmorp.de/
269
270 =cut
271