ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.15
Committed: Sun Apr 9 00:12:58 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.14: +5 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
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_ALPHA8,
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 if (defined $data) {
184 glTexImage2D GL_TEXTURE_2D, 0,
185 $self->{internalformat},
186 $self->{width}, $self->{height},
187 0,
188 $self->{format},
189 GL_UNSIGNED_BYTE,
190 $data;
191 } else {
192 glCopyTexImage2D GL_TEXTURE_2D, 0,
193 $self->{internalformat},
194 0, 0,
195 $self->{width}, $self->{height},
196 0;
197 glPopmatrix;
198 #SDL::GLSwapBuffers;
199 #sleep 1;
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