ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
Revision: 1.8
Committed: Sat Apr 8 13:36:25 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.7: +26 -7 lines
Log Message:
*** empty log message ***

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     local $Data::Dumper::Purity = 1;
59     $::CFG->{VERSION} = $::VERSION;
60     print CFG Data::Dumper->Dump ([$::CFG], [qw/CFG/]);
61     }
62    
63     close CFG;
64     }
65    
66 root 1.3 package Crossfire::Client::Texture;
67    
68     use Scalar::Util;
69    
70     use SDL::OpenGL;
71    
72     my @textures;
73    
74 root 1.4 sub _new {
75     my ($class, %data) = @_;
76    
77     my $self = bless \%data, $class;
78    
79     push @textures, $self;
80     Scalar::Util::weaken $textures[-1];
81    
82     $self->upload;
83    
84     $self
85     }
86    
87     sub new_from_image {
88     my ($class, $image) = @_;
89    
90     $class->_new (image => $image)
91     }
92    
93 root 1.3 sub new_from_file {
94     my ($class, $path) = @_;
95    
96     open my $fh, "<:raw", $path
97     or die "$path: $!";
98    
99     local $/;
100 root 1.4 $class->new_from_image (<$fh>)
101 root 1.3 }
102    
103 root 1.4 sub new_from_surface {
104     my ($class, $surface) = @_;
105 root 1.3
106 root 1.4 $surface->rgba;
107 root 1.3
108 root 1.4 $class->_new (
109     data => $surface->pixels,
110     width => $surface->width,
111     height => $surface->height,
112     )
113 root 1.3 }
114    
115 root 1.6 sub new_from_ttf {
116     my ($class, $ttf, $text) = @_;
117    
118 root 1.7 utf8::encode $text;
119 root 1.6
120     my $surface = SDL::TTFRenderUTF8Blended $ttf, $text,
121     (new SDL::Color -r => 255, -g => 255, -b => 255);
122    
123 root 1.7 $class->new_from_surface (bless \$surface, SDL::Surface::)
124 root 1.6 }
125    
126 root 1.8 sub new_from_opengl {
127     my ($class, $w, $h, $cb) = @_;
128    
129     $class->_new (width => $w, height => $h, render => $cb)
130     }
131    
132 root 1.3 sub upload {
133     my ($self) = @_;
134    
135     return unless $SDL::App::USING_OPENGL;
136    
137 root 1.6 my $data;
138 root 1.3
139     if (exists $self->{data}) {
140 root 1.6 $data = $self->{data};
141 root 1.8 } elsif (exists $self->{rendercb}) {
142     glViewport 0, 0, $self->{width}, $self->{height};
143     glClear GL_COLOR_BUFFER_BIT;
144    
145     $self->{rendercb}->($self, $self->{width}, $self->{height});
146 root 1.3 } else {
147     my $pb = new Gtk2::Gdk::PixbufLoader;
148 root 1.4 $pb->write ($self->{image});
149 root 1.3 $pb->close;
150    
151     $pb = $pb->get_pixbuf;
152     $pb = $pb->add_alpha (0, 0, 0, 0);
153    
154 root 1.6 $self->{width} = $pb->get_width;
155     $self->{height} = $pb->get_height;
156    
157     $data = $pb->get_pixels;
158 root 1.3 }
159    
160     ($self->{name}) = @{glGenTextures 1};
161    
162     glBindTexture GL_TEXTURE_2D, $self->{name};
163    
164     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
165     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
166     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
167     glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
168    
169 root 1.8 if (defined $data) {
170     glTexImage2D GL_TEXTURE_2D, 0,
171     GL_RGBA8,
172     $self->{width}, $self->{height},
173     0,
174     GL_RGBA,
175     GL_UNSIGNED_BYTE,
176     $data;
177     } else {
178     glCopyTexImage2D GL_TEXTURE_2D, 0,
179     GL_RGBA8,
180     0, 0,
181     $self->{width}, $self->{height},
182     0;
183     }
184 root 1.3 }
185    
186     sub DESTROY {
187     my ($self) = @_;
188    
189     return unless exists $self->{name};
190    
191     glDeleteTextures delete $self->{name};
192     }
193    
194     push @::GLINIT, sub {
195     $_->upload
196     for grep $_, @textures;
197     };
198    
199 root 1.1 1;
200    
201     =back
202    
203     =head1 AUTHOR
204    
205     Marc Lehmann <schmorp@schmorp.de>
206     http://home.schmorp.de/
207    
208     =cut
209