ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.28
Committed: Fri Apr 7 22:11:10 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.27: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2 root 1.25
3 root 1.2 use strict;
4 root 1.25 use utf8;
5 root 1.2
6 root 1.13 use Glib;
7     use Gtk2 -init;
8    
9     use SDL;
10     use SDL::App;
11     use SDL::Event;
12     use SDL::Surface;
13     use SDL::OpenGL;
14     use SDL::OpenGL::Constants;
15    
16 elmex 1.11 use Crossfire;
17 root 1.2 use Crossfire::Client;
18     use Crossfire::Protocol;
19    
20 elmex 1.20 use Crossfire::Client::Util;
21     use Crossfire::Client::Widget;
22 elmex 1.10
23 root 1.19 our $FACECACHE;
24    
25 root 1.13 our $VERSION = '0.1';
26 root 1.2
27 elmex 1.10 our $CFG;
28 root 1.13 our $CONN;
29 root 1.2
30 root 1.25 our $UIFONT;
31 root 1.24
32 root 1.13 our $SDL_TIMER;
33     our $SDL_APP;
34     our $SDL_EV = new SDL::Event;
35     our %SDL_CB;
36 root 1.18
37     our @GL_INIT; # hooks called on every gl init
38 root 1.2
39 root 1.13 sub init_screen {
40     $SDL_APP = new SDL::App
41 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
42     -title => "Crossfire+ Client",
43 root 1.13 -width => $CFG->{width},
44     -height => $CFG->{height},
45 root 1.5 -opengl => 1,
46     -red_size => 8,
47     -green_size => 8,
48     -blue_size => 8,
49 root 1.2 -double_buffer => 1,
50 root 1.13 -fullscreen => $CFG->{fullscreen},
51 root 1.5 -resizeable => 0;
52 root 1.2
53 root 1.27 $UIFONT = SDL::TTFOpenFont Crossfire::Client::find_rcfile "uifont.ttf", $CFG->{height} / 40
54     or die "TTFOpenFont: $!";
55    
56 root 1.19 glClearColor 0, 0, 0, 0;
57    
58 root 1.2 glEnable GL_TEXTURE_2D;
59     glShadeModel GL_FLAT;
60     glDisable GL_DEPTH_TEST;
61 root 1.9 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
62 root 1.2
63 root 1.9 glMatrixMode GL_PROJECTION;
64 root 1.4 glLoadIdentity;
65 root 1.24 glOrtho 0, $CFG->{width}, $CFG->{height}, 0, -100 , 100;
66 root 1.19
67     glMatrixMode GL_MODELVIEW;
68 root 1.4
69 root 1.17 $_->() for @GL_INIT;
70 root 1.2 }
71    
72 root 1.24 my $label;#d#
73    
74 root 1.13 sub start_game {
75     $SDL_TIMER = add Glib::Timeout 1000/20, sub {
76     while ($SDL_EV->poll) {
77     ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->();
78     }
79    
80     1
81     };
82    
83     init_screen;
84    
85 root 1.25 $label = new Crossfire::Client::Widget::Label 500, 10, 1, $UIFONT, "Testü[]";
86     $label->activate;
87 root 1.24
88 root 1.13 $CONN = new conn
89     host => $CFG->{host},
90 root 1.28 port => $CFG->{port},
91     user => $CFG->{user},
92     pass => $CFG->{password};
93 root 1.13 }
94    
95     sub stop_game {
96     remove Glib::Source $SDL_TIMER;
97    
98     undef $SDL_APP;
99     SDL::Quit;
100     }
101    
102 root 1.2 sub refresh {
103 root 1.5 glClear GL_COLOR_BUFFER_BIT;
104 root 1.2
105 elmex 1.26 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
106 root 1.1
107 root 1.2 SDL::GLSwapBuffers;
108 root 1.1 }
109    
110 root 1.13 %SDL_CB = (
111     SDL_QUIT() => sub {
112 root 1.19 main_quit Gtk2;
113 root 1.13 },
114     SDL_VIDEORESIZE() => sub {
115     },
116     SDL_VIDEOEXPOSE() => sub {
117     refresh;
118     },
119     SDL_KEYDOWN() => sub {
120 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
121     # alt-enter
122     $CFG->{fullscreen} = !$CFG->{fullscreen};
123     init_screen;
124     } else {
125 root 1.22 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
126 root 1.18 }
127 root 1.13 },
128     SDL_KEYUP() => sub {
129 root 1.22 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
130 root 1.13 },
131     SDL_MOUSEMOTION() => sub {
132     warn "sdl motion\n";#d#
133     },
134     SDL_MOUSEBUTTONDOWN() => sub {
135 root 1.22 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
136 root 1.13 },
137     SDL_MOUSEBUTTONUP() => sub {
138 root 1.22 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
139 root 1.13 },
140     SDL_ACTIVEEVENT() => sub {
141     warn "active\n";#d#
142     },
143     );
144 root 1.1
145 root 1.2 @conn::ISA = Crossfire::Protocol::;
146 root 1.1
147 root 1.2 sub conn::map_update {
148 root 1.1 my ($self, $dirty) = @_;
149    
150 root 1.2 refresh;
151 root 1.1 }
152    
153 root 1.2 sub conn::map_scroll {
154 root 1.1 my ($self, $dx, $dy) = @_;
155    
156 root 1.2 refresh;
157 root 1.1 }
158    
159 root 1.2 sub conn::map_clear {
160 root 1.1 my ($self) = @_;
161    
162 root 1.2 refresh;
163 root 1.1 }
164    
165 root 1.19 sub conn::face_find {
166     my ($self, $face) = @_;
167    
168     $FACECACHE->{"$face->{chksum},$face->{name}"}
169     }
170    
171 root 1.2 sub conn::face_update {
172 root 1.19 my ($self, $face) = @_;
173    
174     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
175 root 1.1
176 root 1.18 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
177 root 1.1 }
178    
179 elmex 1.23 sub gtk_add_cfg_field {
180     my ($tbl, $cfg, $klbl, $key, $value) = @_;
181     my $i = $cfg->{_i}++;
182     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
183     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
184     if ($key eq 'password') {
185     $ent->set_invisible_char ("*");
186     $ent->set (visibility => 0)
187     }
188     $ent->set_text ($value);
189     $ent->signal_connect (changed => sub {
190     my ($ent) = @_;
191     $cfg->{$key} = $ent->get_text;
192     });
193     }
194    
195     sub run_config_dialog {
196     my (%events) = @_;
197    
198     my $w = Gtk2::Window->new;
199    
200     my @cfg = (
201     [qw/Host host/],
202     [qw/Port port/],
203     [qw/Username user/],
204     [qw/Password password/],
205     );
206    
207     my $cfg = {};
208    
209     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
210     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
211    
212     $w->add (my $vb = Gtk2::VBox->new);
213     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
214     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
215     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
216     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
217     my $i = 0;
218     my $act = 0;
219     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
220     if ($_ eq $selmode) { $act = $i }
221     $cb->append_text ($_);
222     $i++;
223     }
224     $cb->set_active ($act);
225     $cb->signal_connect (changed => sub {
226     my ($cb) = @_;
227     my $txt = $cb->get_active_text;
228     if ($txt =~ m/(\d+)x(\d+)/) {
229     $::CFG->{width} = $1;
230     $::CFG->{height} = $2;
231     }
232     });
233    
234     $cfg->{_i} = 1;
235     for (@cfg) {
236     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
237     }
238    
239     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
240     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
241     $cb->signal_connect (clicked => sub {
242     for (keys %$cfg) {
243     $::CFG->{$_} = $cfg->{$_}
244     if $_ ne '_i';
245     }
246     Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
247     });
248     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
249     $cb->signal_connect (clicked => sub {
250     for (keys %$cfg) {
251     $::CFG->{$_} = $cfg->{$_}
252     if $_ ne '_i';
253     }
254     my $cb = $events{login} || sub {};
255     $cb->($::CFG->{user}, $::CFG->{password});
256     });
257     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
258     $cb->signal_connect (clicked => sub {
259     my $cb = $events{login} || sub {};
260     $cb->();
261     });
262     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
263     $cb->signal_connect (clicked => sub { $w->destroy });
264    
265     $w->show_all;
266    
267     $w->signal_connect (destroy => sub { Gtk2->main_quit });
268     }
269    
270    
271 root 1.1 #############################################################################
272    
273 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
274 root 1.27 SDL::TTFInit;
275 elmex 1.20
276     my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
277    
278 elmex 1.16 $mapwidget->activate;
279     $mapwidget->focus_in;
280    
281 root 1.22 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
282 elmex 1.10
283 root 1.13 $CFG ||= {
284     width => 640,
285     height => 480,
286     fullscreen => 0,
287     host => "crossfire.schmorp.de",
288     port => 13327,
289     };
290 elmex 1.12
291 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
292    
293 elmex 1.23 run_config_dialog
294 elmex 1.16 login => sub { start_game },
295     logout => sub { stop_game };
296 root 1.1
297 root 1.13 main Gtk2;
298 root 1.19
299     Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";