… | |
… | |
27 | |
27 | |
28 | use SDL::OpenGL; |
28 | use SDL::OpenGL; |
29 | |
29 | |
30 | my @textures; |
30 | my @textures; |
31 | |
31 | |
|
|
32 | sub _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 | |
|
|
45 | sub new_from_image { |
|
|
46 | my ($class, $image) = @_; |
|
|
47 | |
|
|
48 | $class->_new (image => $image) |
|
|
49 | } |
|
|
50 | |
32 | sub new_from_file { |
51 | sub new_from_file { |
33 | my ($class, $path) = @_; |
52 | my ($class, $path) = @_; |
34 | |
53 | |
35 | open my $fh, "<:raw", $path |
54 | open my $fh, "<:raw", $path |
36 | or die "$path: $!"; |
55 | or die "$path: $!"; |
37 | |
56 | |
38 | local $/; |
57 | local $/; |
39 | $class->new_from_data (<$fh>) |
58 | $class->new_from_image (<$fh>) |
40 | } |
59 | } |
41 | |
60 | |
42 | sub new_from_image { |
61 | sub new_from_surface { |
43 | my ($class, $image) = @_; |
62 | my ($class, $surface) = @_; |
44 | |
63 | |
45 | my $self = bless { |
64 | $surface->rgba; |
46 | image => $data, |
|
|
47 | }; |
|
|
48 | |
65 | |
49 | push @textures, $self; |
66 | $class->_new ( |
50 | Scalar::Util::weaken $textures[-1]; |
67 | data => $surface->pixels, |
51 | |
68 | width => $surface->width, |
52 | $self->upload; |
69 | height => $surface->height, |
53 | |
70 | ) |
54 | $self |
|
|
55 | } |
71 | } |
56 | |
72 | |
57 | sub upload { |
73 | sub upload { |
58 | my ($self) = @_; |
74 | my ($self) = @_; |
59 | |
75 | |
… | |
… | |
63 | |
79 | |
64 | if (exists $self->{data}) { |
80 | if (exists $self->{data}) { |
65 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
81 | ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); |
66 | } else { |
82 | } else { |
67 | my $pb = new Gtk2::Gdk::PixbufLoader; |
83 | my $pb = new Gtk2::Gdk::PixbufLoader; |
68 | $pb->write ($self->{data}); |
84 | $pb->write ($self->{image}); |
69 | $pb->close; |
85 | $pb->close; |
70 | |
86 | |
71 | $pb = $pb->get_pixbuf; |
87 | $pb = $pb->get_pixbuf; |
72 | $pb = $pb->add_alpha (0, 0, 0, 0); |
88 | $pb = $pb->add_alpha (0, 0, 0, 0); |
73 | |
89 | |