ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.18
Committed: Mon Apr 10 11:55:16 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.17: +1 -1 lines
Log Message:
experimentally changed darkness to lightness, works imho much better

File Contents

# User Rev Content
1 root 1.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 root 1.2 use XSLoader;
21 root 1.1 XSLoader::load "Crossfire::Client", $VERSION;
22     }
23    
24 root 1.5 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 elmex 1.9 require Data::Dumper;
59 root 1.5 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 root 1.3 package Crossfire::Client::Texture;
68    
69     use Scalar::Util;
70    
71     use SDL::OpenGL;
72    
73     my @textures;
74    
75 root 1.14 sub new {
76 root 1.4 my ($class, %data) = @_;
77    
78 root 1.14 my $self = bless {
79 root 1.15 internalformat => GL_RGBA,
80     format => GL_RGBA,
81 root 1.14 %data,
82     }, $class;
83 root 1.4
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 root 1.14 $class->new (image => $image)
96 root 1.4 }
97    
98 root 1.3 sub new_from_file {
99     my ($class, $path) = @_;
100    
101     open my $fh, "<:raw", $path
102     or die "$path: $!";
103    
104     local $/;
105 root 1.4 $class->new_from_image (<$fh>)
106 root 1.3 }
107    
108 root 1.14 #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 root 1.16 internalformat => GL_ALPHA4,
130 root 1.14 format => GL_ALPHA,
131 root 1.4 )
132 root 1.3 }
133    
134 root 1.8 sub new_from_opengl {
135     my ($class, $w, $h, $cb) = @_;
136    
137 root 1.14 $class->new (width => $w, height => $h, rendercb => $cb)
138 root 1.8 }
139    
140 root 1.3 sub upload {
141     my ($self) = @_;
142    
143     return unless $SDL::App::USING_OPENGL;
144    
145 root 1.6 my $data;
146 root 1.3
147     if (exists $self->{data}) {
148 root 1.6 $data = $self->{data};
149 root 1.8 } elsif (exists $self->{rendercb}) {
150     glViewport 0, 0, $self->{width}, $self->{height};
151 root 1.12 glMatrixMode GL_PROJECTION;
152     glLoadIdentity;
153 root 1.18 glOrtho 0, $self->{width}, 0, $self->{height}, -10000, 10000;
154 root 1.12 glMatrixMode GL_MODELVIEW;
155     glPushmatrix;
156     glLoadIdentity;
157 root 1.8 glClear GL_COLOR_BUFFER_BIT;
158    
159     $self->{rendercb}->($self, $self->{width}, $self->{height});
160 root 1.3 } else {
161     my $pb = new Gtk2::Gdk::PixbufLoader;
162 root 1.4 $pb->write ($self->{image});
163 root 1.3 $pb->close;
164    
165     $pb = $pb->get_pixbuf;
166     $pb = $pb->add_alpha (0, 0, 0, 0);
167    
168 root 1.6 $self->{width} = $pb->get_width;
169     $self->{height} = $pb->get_height;
170    
171     $data = $pb->get_pixels;
172 root 1.3 }
173    
174     ($self->{name}) = @{glGenTextures 1};
175    
176     glBindTexture GL_TEXTURE_2D, $self->{name};
177    
178 elmex 1.13 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
179     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR;
180 root 1.3 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
181     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
182    
183 root 1.16 glGetError;
184 root 1.8 if (defined $data) {
185     glTexImage2D GL_TEXTURE_2D, 0,
186 root 1.15 $self->{internalformat},
187 root 1.8 $self->{width}, $self->{height},
188     0,
189 root 1.14 $self->{format},
190 root 1.8 GL_UNSIGNED_BYTE,
191     $data;
192 root 1.16 glGetError and die;
193 root 1.8 } else {
194     glCopyTexImage2D GL_TEXTURE_2D, 0,
195 root 1.15 $self->{internalformat},
196 root 1.8 0, 0,
197     $self->{width}, $self->{height},
198     0;
199 root 1.12 glPopmatrix;
200 root 1.8 }
201 root 1.3 }
202    
203     sub DESTROY {
204     my ($self) = @_;
205    
206     return unless exists $self->{name};
207    
208     glDeleteTextures delete $self->{name};
209     }
210    
211 root 1.17 push @::GL_INIT, sub {
212 root 1.3 $_->upload
213     for grep $_, @textures;
214     };
215    
216 root 1.1 1;
217    
218     =back
219    
220     =head1 AUTHOR
221    
222     Marc Lehmann <schmorp@schmorp.de>
223     http://home.schmorp.de/
224    
225     =cut
226