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.4 by root, Fri Apr 7 18:49:52 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
24package Crossfire::Client::Texture;
25
26use Scalar::Util;
27
28use SDL::OpenGL;
29
30my @textures;
31
32sub _new {
33 my ($class, %data) = @_;
34
35 my $self = bless \%data, $class;
36
37 push @textures, $self;
38 Scalar::Util::weaken $textures[-1];
39
40 $self->upload;
41
42 $self
43}
44
45sub new_from_image {
46 my ($class, $image) = @_;
47
48 $class->_new (image => $image)
49}
50
51sub new_from_file {
52 my ($class, $path) = @_;
53
54 open my $fh, "<:raw", $path
55 or die "$path: $!";
56
57 local $/;
58 $class->new_from_image (<$fh>)
59}
60
61sub new_from_surface {
62 my ($class, $surface) = @_;
63
64 $surface->rgba;
65
66 $class->_new (
67 data => $surface->pixels,
68 width => $surface->width,
69 height => $surface->height,
70 )
71}
72
73sub upload {
74 my ($self) = @_;
75
76 return unless $SDL::App::USING_OPENGL;
77
78 my ($data, $width, $height);
79
80 if (exists $self->{data}) {
81 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height});
82 } else {
83 my $pb = new Gtk2::Gdk::PixbufLoader;
84 $pb->write ($self->{image});
85 $pb->close;
86
87 $pb = $pb->get_pixbuf;
88 $pb = $pb->add_alpha (0, 0, 0, 0);
89
90 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height);
91 }
92
93 ($self->{name}) = @{glGenTextures 1};
94
95 glBindTexture GL_TEXTURE_2D, $self->{name};
96
97 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
98 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
99 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
100 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
101
102 glTexImage2D GL_TEXTURE_2D, 0,
103 GL_RGBA8,
104 $width, $height,
105 0,
106 GL_RGBA,
107 GL_UNSIGNED_BYTE,
108 $data;
109}
110
111sub DESTROY {
112 my ($self) = @_;
113
114 return unless exists $self->{name};
115
116 glDeleteTextures delete $self->{name};
117}
118
119push @::GLINIT, sub {
120 $_->upload
121 for grep $_, @textures;
122};
24 123
251; 1241;
26 125
27=back 126=back
28 127

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines