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.3 by root, Fri Apr 7 18:20:13 2006 UTC vs.
Revision 1.15 by root, Sun Apr 9 00:12:58 2006 UTC

19 19
20 use XSLoader; 20 use XSLoader;
21 XSLoader::load "Crossfire::Client", $VERSION; 21 XSLoader::load "Crossfire::Client", $VERSION;
22} 22}
23 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 require Data::Dumper;
59 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
24package Crossfire::Client::Texture; 67package Crossfire::Client::Texture;
25 68
26use Scalar::Util; 69use Scalar::Util;
27 70
28use SDL::OpenGL; 71use SDL::OpenGL;
29 72
30my @textures; 73my @textures;
74
75sub new {
76 my ($class, %data) = @_;
77
78 my $self = bless {
79 internalformat => GL_RGBA,
80 format => GL_RGBA,
81 %data,
82 }, $class;
83
84 push @textures, $self;
85 Scalar::Util::weaken $textures[-1];
86
87 $self->upload;
88
89 $self
90}
91
92sub new_from_image {
93 my ($class, $image) = @_;
94
95 $class->new (image => $image)
96}
31 97
32sub new_from_file { 98sub new_from_file {
33 my ($class, $path) = @_; 99 my ($class, $path) = @_;
34 100
35 open my $fh, "<:raw", $path 101 open my $fh, "<:raw", $path
36 or die "$path: $!"; 102 or die "$path: $!";
37 103
38 local $/; 104 local $/;
39 $class->new_from_data (<$fh>) 105 $class->new_from_image (<$fh>)
40} 106}
41 107
42sub new_from_image { 108#sub new_from_surface {
43 my ($class, $image) = @_; 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#}
44 119
45 my $self = bless { 120sub new_from_text {
46 image => $data, 121 my ($class, $text, $height) = @_;
47 };
48 122
49 push @textures, $self; 123 my ($w, $h, $data) = Crossfire::Client::font_render $text, $height;
50 Scalar::Util::weaken $textures[-1];
51 124
52 $self->upload; 125 $class->new (
126 width => $w,
127 height => $h,
128 data => $data,
129 internalformat => GL_ALPHA8,
130 format => GL_ALPHA,
131 )
132}
53 133
54 $self 134sub new_from_opengl {
135 my ($class, $w, $h, $cb) = @_;
136
137 $class->new (width => $w, height => $h, rendercb => $cb)
55} 138}
56 139
57sub upload { 140sub upload {
58 my ($self) = @_; 141 my ($self) = @_;
59 142
60 return unless $SDL::App::USING_OPENGL; 143 return unless $SDL::App::USING_OPENGL;
61 144
62 my ($data, $width, $height); 145 my $data;
63 146
64 if (exists $self->{data}) { 147 if (exists $self->{data}) {
65 ($data, $width, $height) = ($self->{data}, $self->{width}, $self->{height}); 148 $data = $self->{data};
149 } elsif (exists $self->{rendercb}) {
150 glViewport 0, 0, $self->{width}, $self->{height};
151 glMatrixMode GL_PROJECTION;
152 glLoadIdentity;
153 glOrtho 0, $self->{width}, 0, $self->{height}, -100, 100;
154 glMatrixMode GL_MODELVIEW;
155 glPushmatrix;
156 glLoadIdentity;
157 glClear GL_COLOR_BUFFER_BIT;
158
159 $self->{rendercb}->($self, $self->{width}, $self->{height});
66 } else { 160 } else {
67 my $pb = new Gtk2::Gdk::PixbufLoader; 161 my $pb = new Gtk2::Gdk::PixbufLoader;
68 $pb->write ($self->{data}); 162 $pb->write ($self->{image});
69 $pb->close; 163 $pb->close;
70 164
71 $pb = $pb->get_pixbuf; 165 $pb = $pb->get_pixbuf;
72 $pb = $pb->add_alpha (0, 0, 0, 0); 166 $pb = $pb->add_alpha (0, 0, 0, 0);
73 167
74 ($data, $width, $height) = ($pb->get_pixels, $pb->get_width, $pb->get_height); 168 $self->{width} = $pb->get_width;
169 $self->{height} = $pb->get_height;
170
171 $data = $pb->get_pixels;
75 } 172 }
76 173
77 ($self->{name}) = @{glGenTextures 1}; 174 ($self->{name}) = @{glGenTextures 1};
78 175
79 glBindTexture GL_TEXTURE_2D, $self->{name}; 176 glBindTexture GL_TEXTURE_2D, $self->{name};
80 177
81 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 178 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST;
82 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR; 179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST;#_MIPMAP_LINEAR;
83 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP; 180 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
84 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP; 181 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
85 182
183 if (defined $data) {
86 glTexImage2D GL_TEXTURE_2D, 0, 184 glTexImage2D GL_TEXTURE_2D, 0,
87 GL_RGBA8, 185 $self->{internalformat},
88 $width, $height, 186 $self->{width}, $self->{height},
89 0, 187 0,
90 GL_RGBA, 188 $self->{format},
91 GL_UNSIGNED_BYTE, 189 GL_UNSIGNED_BYTE,
92 $data; 190 $data;
191 } else {
192 glCopyTexImage2D GL_TEXTURE_2D, 0,
193 $self->{internalformat},
194 0, 0,
195 $self->{width}, $self->{height},
196 0;
197 glPopmatrix;
198 #SDL::GLSwapBuffers;
199 #sleep 1;
200 }
93} 201}
94 202
95sub DESTROY { 203sub DESTROY {
96 my ($self) = @_; 204 my ($self) = @_;
97 205

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines