ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.35
Committed: Sat Apr 8 18:48:35 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.34: +8 -8 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::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.34 our $tw; # Test widget #d#
47 elmex 1.32
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.34 # Test code #d#
75 elmex 1.32 $tw = new Crossfire::Client::Widget::Window;
76 root 1.35 $tw->add (my $lbl = new Crossfire::Client::Widget::Label 0, 0, 10, $UIFONT, "Foo in the garden!");
77 elmex 1.32 # $tw = new Crossfire::Client::Widget::Label 0, $HEIGHT - $HEIGHT / 40, 10, $UIFONT, "Foo in the garden!";
78    
79 root 1.35 $tw->move (0, 0);
80 elmex 1.32 $tw->activate;
81 elmex 1.34 # Test code end #d#
82 elmex 1.32
83 root 1.19 glClearColor 0, 0, 0, 0;
84    
85 root 1.2 glEnable GL_TEXTURE_2D;
86     glShadeModel GL_FLAT;
87     glDisable GL_DEPTH_TEST;
88 root 1.9 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
89 root 1.2
90 root 1.17 $_->() for @GL_INIT;
91 root 1.2 }
92    
93 root 1.13 sub start_game {
94 root 1.30 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
95     ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
96     while $SDL_EV->poll;
97 root 1.13
98     1
99     };
100    
101 root 1.30 $WIDTH = $CFG->{width};
102     $HEIGHT = $CFG->{height};
103     $FULLSCREEN = 0;
104    
105 root 1.13 init_screen;
106    
107     $CONN = new conn
108     host => $CFG->{host},
109 root 1.28 port => $CFG->{port},
110     user => $CFG->{user},
111     pass => $CFG->{password};
112 root 1.13 }
113    
114     sub stop_game {
115     remove Glib::Source $SDL_TIMER;
116    
117     undef $SDL_APP;
118     SDL::Quit;
119     }
120    
121 root 1.30
122     sub force_refresh {
123     glViewport 0, 0, $WIDTH, $HEIGHT;
124 root 1.35
125     glMatrixMode GL_PROJECTION;
126     glLoadIdentity;
127     glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100;
128     glMatrixMode GL_MODELVIEW;
129    
130 root 1.5 glClear GL_COLOR_BUFFER_BIT;
131 root 1.2
132 elmex 1.26 $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS;
133 root 1.1
134 root 1.2 SDL::GLSwapBuffers;
135 root 1.1 }
136    
137 root 1.30 my $refresh_handler;
138    
139     sub refresh {
140     $refresh_handler ||= add Glib::Idle sub {
141     force_refresh;
142     undef $refresh_handler;
143     0
144     };
145     }
146    
147 root 1.13 %SDL_CB = (
148     SDL_QUIT() => sub {
149 root 1.19 main_quit Gtk2;
150 root 1.13 },
151     SDL_VIDEORESIZE() => sub {
152     },
153     SDL_VIDEOEXPOSE() => sub {
154     refresh;
155     },
156     SDL_KEYDOWN() => sub {
157 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
158     # alt-enter
159 root 1.30 $FULLSCREEN = !$FULLSCREEN;
160 root 1.18 init_screen;
161     } else {
162 root 1.22 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
163 root 1.18 }
164 root 1.13 },
165     SDL_KEYUP() => sub {
166 root 1.22 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
167 root 1.13 },
168     SDL_MOUSEMOTION() => sub {
169     warn "sdl motion\n";#d#
170     },
171     SDL_MOUSEBUTTONDOWN() => sub {
172 root 1.22 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
173 root 1.13 },
174     SDL_MOUSEBUTTONUP() => sub {
175 root 1.22 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
176 root 1.13 },
177     SDL_ACTIVEEVENT() => sub {
178     warn "active\n";#d#
179     },
180     );
181 root 1.1
182 root 1.2 @conn::ISA = Crossfire::Protocol::;
183 root 1.1
184 root 1.2 sub conn::map_update {
185 root 1.1 my ($self, $dirty) = @_;
186    
187 root 1.2 refresh;
188 root 1.1 }
189    
190 root 1.2 sub conn::map_scroll {
191 root 1.1 my ($self, $dx, $dy) = @_;
192    
193 root 1.2 refresh;
194 root 1.1 }
195    
196 root 1.2 sub conn::map_clear {
197 root 1.1 my ($self) = @_;
198    
199 root 1.2 refresh;
200 root 1.1 }
201    
202 root 1.19 sub conn::face_find {
203     my ($self, $face) = @_;
204    
205     $FACECACHE->{"$face->{chksum},$face->{name}"}
206     }
207    
208 root 1.2 sub conn::face_update {
209 root 1.19 my ($self, $face) = @_;
210    
211     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
212 root 1.1
213 root 1.18 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
214 root 1.1 }
215    
216 root 1.33 sub conn::query {
217     my ($self, $flags, $prompt) = @_;
218    
219     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
220     }
221    
222 elmex 1.23 sub gtk_add_cfg_field {
223     my ($tbl, $cfg, $klbl, $key, $value) = @_;
224     my $i = $cfg->{_i}++;
225     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
226     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
227     if ($key eq 'password') {
228     $ent->set_invisible_char ("*");
229     $ent->set (visibility => 0)
230     }
231     $ent->set_text ($value);
232     $ent->signal_connect (changed => sub {
233     my ($ent) = @_;
234     $cfg->{$key} = $ent->get_text;
235     });
236     }
237    
238     sub run_config_dialog {
239     my (%events) = @_;
240    
241     my $w = Gtk2::Window->new;
242    
243     my @cfg = (
244     [qw/Host host/],
245     [qw/Port port/],
246     [qw/Username user/],
247     [qw/Password password/],
248     );
249    
250     my $cfg = {};
251    
252     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
253     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
254    
255     $w->add (my $vb = Gtk2::VBox->new);
256     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
257     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
258     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
259     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
260     my $i = 0;
261     my $act = 0;
262     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
263     if ($_ eq $selmode) { $act = $i }
264     $cb->append_text ($_);
265     $i++;
266     }
267     $cb->set_active ($act);
268     $cb->signal_connect (changed => sub {
269     my ($cb) = @_;
270     my $txt = $cb->get_active_text;
271     if ($txt =~ m/(\d+)x(\d+)/) {
272     $::CFG->{width} = $1;
273     $::CFG->{height} = $2;
274     }
275     });
276    
277     $cfg->{_i} = 1;
278     for (@cfg) {
279     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
280     }
281    
282     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
283     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
284     $cb->signal_connect (clicked => sub {
285     for (keys %$cfg) {
286     $::CFG->{$_} = $cfg->{$_}
287     if $_ ne '_i';
288     }
289     Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
290     });
291     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
292     $cb->signal_connect (clicked => sub {
293     for (keys %$cfg) {
294     $::CFG->{$_} = $cfg->{$_}
295     if $_ ne '_i';
296     }
297     my $cb = $events{login} || sub {};
298     $cb->($::CFG->{user}, $::CFG->{password});
299     });
300     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
301     $cb->signal_connect (clicked => sub {
302     my $cb = $events{login} || sub {};
303     $cb->();
304     });
305     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
306     $cb->signal_connect (clicked => sub { $w->destroy });
307    
308     $w->show_all;
309    
310     $w->signal_connect (destroy => sub { Gtk2->main_quit });
311     }
312    
313    
314 root 1.1 #############################################################################
315    
316 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
317 root 1.27 SDL::TTFInit;
318 elmex 1.20
319     my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
320    
321 elmex 1.16 $mapwidget->activate;
322     $mapwidget->focus_in;
323    
324 root 1.22 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
325 elmex 1.10
326 root 1.13 $CFG ||= {
327     width => 640,
328     height => 480,
329     fullscreen => 0,
330     host => "crossfire.schmorp.de",
331     port => 13327,
332     };
333 elmex 1.12
334 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
335    
336 elmex 1.23 run_config_dialog
337 elmex 1.16 login => sub { start_game },
338     logout => sub { stop_game };
339 root 1.1
340 root 1.13 main Gtk2;
341 root 1.19
342     Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";