ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.1 by root, Thu Apr 6 15:30:09 2006 UTC vs.
Revision 1.5 by root, Fri Apr 7 20:13:13 2006 UTC

14 14
15package Crossfire::Client; 15package Crossfire::Client;
16 16
17BEGIN { 17BEGIN {
18 $VERSION = '0.1'; 18 $VERSION = '0.1';
19 @ISA = qw(Exporter);
20 19
21 require XSLoader; 20 use XSLoader;
22 XSLoader::load "Crossfire::Client", $VERSION; 21 XSLoader::load "Crossfire::Client", $VERSION;
23} 22}
23
24sub 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
35sub 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
51sub 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
66package Crossfire::Client::Texture;
67
68use Scalar::Util;
69
70use SDL::OpenGL;
71
72my @textures;
73
74sub _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
87sub new_from_image {
88 my ($class, $image) = @_;
89
90 $class->_new (image => $image)
91}
92
93sub new_from_file {
94 my ($class, $path) = @_;
95
96 open my $fh, "<:raw", $path
97 or die "$path: $!";
98
99 local $/;
100 $class->new_from_image (<$fh>)
101}
102
103sub new_from_surface {
104 my ($class, $surface) = @_;
105
106 $surface->rgba;
107
108 $class->_new (
109 data => $surface->pixels,
110 width => $surface->width,
111 height => $surface->height,
112 )
113}
114
115sub upload {
116 my ($self) = @_;
117
118 return unless $SDL::App::USING_OPENGL;
119
120 my ($data, $width, $height);
121
122 if (exists $self->{data}) {
123 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height});
124 } else {
125 my $pb = new Gtk2::Gdk::PixbufLoader;
126 $pb->write ($self->{image});
127 $pb->close;
128
129 $pb = $pb->get_pixbuf;
130 $pb = $pb->add_alpha (0, 0, 0, 0);
131
132 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height);
133 }
134
135 ($self->{name}) = @{glGenTextures 1};
136
137 glBindTexture GL_TEXTURE_2D, $self->{name};
138
139 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
140 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
141 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
142 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
143
144 glTexImage2D GL_TEXTURE_2D, 0,
145 GL_RGBA8,
146 $width, $height,
147 0,
148 GL_RGBA,
149 GL_UNSIGNED_BYTE,
150 $data;
151}
152
153sub DESTROY {
154 my ($self) = @_;
155
156 return unless exists $self->{name};
157
158 glDeleteTextures delete $self->{name};
159}
160
161push @::GLINIT, sub {
162 $_->upload
163 for grep $_, @textures;
164};
24 165
251; 1661;
26 167
27=back 168=back
28 169

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines