ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.32
Committed: Sat Apr 8 17:21:01 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.31: +9 -0 lines
Log Message:
added Window widget (doesn't work correctly yet)

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